SpringcCloud Config分布式配置中心
首先
创建config_server的Module
添加如下依赖
< dependency >< groupId > org.springframework.cloud </ groupId >< artifactId > spring-cloud-config-server </ artifactId ></ dependency ><!-- SpringBoot 整合 eureka 客户端 -->< dependency >< groupId > org.springframework.cloud </ groupId >< artifactId > spring-cloud-starter-netflix-eureka-client </ artifactId ></ dependency >
配置中心的配置文件
#服务注册到eureka地址 eureka: client: service-url: defaultZone: http://127.0.0.1:8081/eureka spring: application: #注册中心应用名称 name: config-server cloud: config: server: git: #git环境地址 uri: https://gitee.com/zero_piece/config_server.git #所在目录 search-paths: - config #读取分支 label: master #端口号 server: port: 8888
在启动类上加入@EnableConfigServer注解
其次
创建config_client服务,我们这里使用actuator来手动刷新配置文件
添加依赖
<!-- actuator监控中心 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><!-- SpringBoot整合Web组件 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-client</artifactId></dependency><!-- SpringBoot整合eureka客户端 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency>
配置客户端的配置文件
spring: application: ####注册中心应用名称 name: config-client cloud: config: ####读取后缀 profile: dev ####读取config-server注册地址 discovery: service-id: config-server enabled: true ##### eureka服务注册地址 eureka: client: service-url: defaultZone: http://127.0.0.1:8081/eureka server: port: 8882 #开启监控 management: endpoint: shutdown: enabled: false endpoints: web: exposure: include: "*"
然后在码云路径里添加文件(文件名=应用名+环境后缀.yaml)
启动服务
调用getName
@RestController@RefreshScopepublic class UserController {@Value("${userBoy}")private String name;@RequestMapping(value = "/getName")public String getName(){return name;}}
返回
修改码云配置文件
使用
http://localhost:8882/actuator/refresh来刷新配置文件
可能会报415的错误,请求加上头部(
Content-Type:application/json)
结果