SpringCloud Greenwich 版本集成 Config Client(注册到Eureka Serever)
添加相关依赖
- 加依赖
1 | <!-- 注册到 Eureka Server 时需要添加的依赖 start --> |
- 新建
bootstrap.yml
1 | spring: |
yml
spring.application.name: 对应的config server所获取的配置文件的{application}spring.cloud.uri: 指定Config Server的地址,默认是http://localhost:8888spring.cloud.config.profile:profile对应Config Server所获取配置文件的{profile}spring.cloud.config.label:指定Git仓库的分支,对应Config Server所获取配置文件的{label}
[KEY POINT]:
注意属性配置文件,以上属性为
bootstrap.yml文件内容而不是application.yml文件.如果配置在application.yml中,改部分配置就不能获取配置的正确值.例如:applicaiton.yml默认spring.cloud.config.uri的默认值是http://localhost:8888,而并非是http://localhost:3011SpringCloud有一个 “引导上下文” 的概念,这是主应用程序上下文(Application Context) 的父上下文。引导上下文负责从配置服务器加载配置属性,一级解密外部配置文件中的属性。和主应用程序加载application.*(yml或properties)中的属性不同,引导上下文加载bootstrap.*中的属性。配置在bootstrap.*中的属性优先级更高,因此默认情况下他们不能被而本地配置覆盖
从API接口验证是否能够正确获取自定义配置
1 |
|
注: 如果需要注册到Eureka,需要在启动类SpringcloudConfigClientApplication上添加@EnableEurekaClient注解(注册到Eureka Server)
测试
访问地址: http://localhost:3011/profile