弹簧云微服务部署中的IP地址规范

时间:2018-01-08 10:39:45

标签: amazon-web-services amazon-ec2 spring-cloud amazon-ecs

我正在尝试开发spring cloud微服务。我使用zuul代理,Eureka服务器和hystrix开发了Spring云项目的示例演示。我将我开发的服务添加为eureka服务器的客户端并应用了路由。一切都运作良好。现在我需要部署在我的AWS Ec2机器上。在My local中,我在application.properties文件中添加了默认区域URL,如下所示,

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

当我转移到我的Ec2机器或唱AWS ECS时,如何修改此IP地址属于云以进行正确配置? 。我也使用localhost:8090和8091这些端口用于zuul和涡轮机仪表板项目等。那么当我部署到云时我需要更改此URL吗?

有人可以帮我解决部署问题吗?

1 个答案:

答案 0 :(得分:1)

我们使用域名。因此,您可以将api.yourdomain.com的A记录指向支持您的服务的IP地址或负载均衡器别名。

为什么呢?当我们决定更改基础架构时,我们可以更改DNS条目,而不是修改我们所有的微服务配置。我们最近从Eureka / Zuul迁移到AWS的ALB。使用域允许我们并行运行两个环境并且没有停机时间进行切换。如果新环境出现故障,旧环境仍在运行,我们可以通过简单的A记录更改来减少。

在application.yml文件中,您可以配置不同的配置文件,以便可以在本地进行测试,然后在ECS中,您可以定义在创建任务定义时要使用的配置文件。

首先,这里是一个如何配置application.yml文件以便能够在不同的配置文件上运行的示例:

############# 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

第二步:设置ECS以使用您的Prod配置文件:

构建泊坞窗容器时,请使用新配置文件的名称对其进行标记,在本例中为“prod” ECR

第三步:创建任务定义并在repo URL中定义Docker标记,并在容器运行命令中定义新的配置文件:

enter image description here

现在,当您在本地计算机上处​​理应用程序时,可以使用“localhost”运行它,当您将其部署到ECS时,您可以定义要在容器定义中的run命令中使用的新域/ ip。

相关问题