【问题标题】:IP Address specification in deployment of Spring Cloud microserviceSpring Cloud微服务部署中的IP地址规范
【发布时间】:2018-06-17 07:55:51
【问题描述】:
我正在尝试开发 Spring Cloud 微服务。我使用 Zuul 代理、Eureka 服务器和 Hystrix 开发了 Spring Cloud 项目的示例演示。我将我开发的服务添加为 Eureka 服务器的客户端并应用了路由。一切都运作良好。现在我需要在我的 AWS Ec2 机器上进行部署。在我的本地,我在 application.properties 文件中添加了默认区域 URL,如下所示,
eureka.client.serviceUrl.defaultZone=http://localhost:8071/eureka/
当我移动到我的 Ec2 机器或通过运行 AWS ECS 时,如何修改这个属于云的 IP 地址以进行正确配置?我还使用 localhost:8090 和 8091 之类的端口用于 Zuul 和 Turbine 仪表板项目等。那么当我部署到云时,我需要如何更改此 URL?
【问题讨论】:
标签:
amazon-web-services
amazon-ec2
spring-cloud
amazon-ecs
【解决方案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 配置文件:
在构建 docker 容器时,使用新配置文件的名称对其进行标记,在本例中为“prod”
第三:创建任务定义并在 repo URL 中定义您的 Docker 标记,并在容器运行命令中定义您的新配置文件:
现在,当您在本地计算机上处理您的应用程序时,您可以使用“localhost”运行它,并且当您将其部署到 ECS 时,您可以定义您的新域/IP,以便在容器定义中的运行命令中使用。