Spring 云微服务部署中的 IP 地址规范
IP Address specification in deployment of Spring Cloud microservice
我正在尝试开发 spring 云微服务。我使用 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?
我们使用域名。因此,您可以将 api.yourdomain.com 的 A 记录指向支持您的服务的 IP 地址或负载均衡器别名。
为什么?当我们决定更改基础架构时,我们能够更改 DNS 条目而不是修改我们所有微服务的配置。我们最近从 Eureka/Zuul 转移到了 AWS 的 ALB。使用域使我们能够 运行 并行和切换两种环境,而无需停机。如果新环境出现故障,旧环境仍然 运行ning,我们可以通过简单的 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"
第三步:创建任务定义并在存储库 URL 中定义您的 Docker 标签,并在您的容器 运行 命令中定义您的新配置文件:
现在,当您在本地机器上处理您的应用程序时,您可以 运行 使用 "localhost" 并且当您将它部署到 ECS 时,您可以将新的 domain/ip 定义为在容器定义中的 运行 命令中使用。
我正在尝试开发 spring 云微服务。我使用 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?
我们使用域名。因此,您可以将 api.yourdomain.com 的 A 记录指向支持您的服务的 IP 地址或负载均衡器别名。
为什么?当我们决定更改基础架构时,我们能够更改 DNS 条目而不是修改我们所有微服务的配置。我们最近从 Eureka/Zuul 转移到了 AWS 的 ALB。使用域使我们能够 运行 并行和切换两种环境,而无需停机。如果新环境出现故障,旧环境仍然 运行ning,我们可以通过简单的 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"