Search code examples
amazon-web-servicesamazon-ec2spring-cloudamazon-ecs

IP Address specification in deployment of Spring Cloud microservice


I am trying to develop spring cloud microservice. I developed a sample demo of Spring Cloud project by using Zuul proxy, Eureka server and Hystrix. I added my developed service as a client of Eureka server and applied the routing. All are working well. Now I need to deploy in my AWS Ec2 machine. In my local I added the default zone URL in application.properties file like the following,

eureka.client.serviceUrl.defaultZone=http://localhost:8071/eureka/

When I am moving to my Ec2 machine or by sing AWS ECS, how I can modify this IP address belongs to cloud for proper configuration? I also using localhost:8090 and 8091 like these ports for Zuul and Turbine dashboard project etc. So how I need to change this URL when I am deploying to cloud?


Solution

  • We use domains. So you would point an A-record of api.yourdomain.com at the IP address or load balancer alias that is supporting your services.

    Why? When we decided to change infrastructure we are able to change a DNS entry rather than modify all of our microservices' configurations. We recently moved from Eureka/Zuul to AWS's ALB. Using domains allowed us to run both environments in parallel and cutover with no down time. In the event there was a failure in the new environment, the old one was still running and we could cut back with a simple A-record change.

    In your application.yml file you can configure different profiles so that you can test locally and then in ECS you can define the profile to use when creating the task definition.

    First here is an example of how you can configure your application.yml file to be able to run on different profiles:

    ############# for running locally ################
        server:
      port: 1234
    
    logging:
      file: logs/example.log
      level:
        com.example: INFO
    
    endpoints:
       health:
         sensitive: true
    
    spring:
      datasource:
        url: jdbc:mysql://example.us-east-1.rds.amazonaws.com/example_db?noAccessToProcedureBodies=true
        username: example
        password: example
        driver-class-name: com.mysql.jdbc.Driver
    
    
    
    security:
      oauth2:
        client:
          clientId: example
          clientSecret: examplesecret
          scope: webapp
          accessTokenUri: http://localhost:9999/uaa/oauth/token
          userAuthorizationUri: http://localhost:9999/uaa/oauth/authorize
        resource:
          userInfoUri: http://localhost:9999/uaa/user
    
    ########## For deployment in Docker containers/ECS ########
    spring:
      profiles: prod
      datasource:
        url: jdbc:mysql://example.rds.amazonaws.com/example_db?noAccessToProcedureBodies=true
        username: example
        password: example
        driver-class-name: com.mysql.jdbc.Driver
    
    
    prodnetwork:
      ipAddress: api.yourdomain.com
    
    security:
      oauth2:
        client:
          clientId: exampleid
          clientSecret: examplesecret
          scope: webapp
          accessTokenUri: https://${prodnetwork.ipAddress}/v1/uaa/oauth/token
          userAuthorizationUri: https://${prodnetwork.ipAddress}/v1/uaa/oauth/authorize
        resource:
          userInfoUri: https://${prodnetwork.ipAddress}/v1/uaa/user
    

    Second: Setting up ECS to use your Prod profile:

    When you build your docker container, tag it with your new profile's name, in this case "prod" ECR

    Third: Create a task definition and define your Docker tag in the repo URL and your new profile in your container run command:

    enter image description here

    Now when you work on your application on your local machine, you can run it with "localhost" and when you deploy it to ECS you can define your new domain/ip to be used in the run command in your container definition.