#|spring-cloud-config使用

实现功能 可通过git仓库或本地文件进行配置 支持手动更新配置
本文代码 https://github.com/tothis/spring-cloud-learn/tree/master/config
查看客户端配置
http://localhost:8082/config-client/dev http://localhost:8082/config-client-dev.json http://localhost:8082/config-client-dev.yaml http://localhost:8082/config-client-dev.yml http://localhost:8082/config-client-dev.properties

刷新配置
curl -X POST http://localhost:8082/actuator/bus-refresh

properties yml 相同的键显示properties的值
配置中心服务端
  1. pom.xml
org.springframework.cloud spring-cloud-config-server org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.cloud spring-cloud-starter-bus-amqp

  1. application.yml
server: port: 8002 spring: application: name: config-server profiles: # 使用本地文件时需写入native active: native rabbitmq: host: 192.168.92.134 port: 5672 username: guest password: guest cloud: # https://docs.spring.io/spring-cloud-config/docs/2.2.4.RELEASE/reference/html config: label: master server: # git: #uri: https://github.com/tothis/spring-cloud-learn #search-paths: config/profile native: # search-locations: classpath:profile search-locations: file:D:/IDEAProjects/project/spring-cloud-learn/config/profile eureka: client: service-url: defaultZone: http://192.168.92.134:8001/eureka management: endpoints: web: # 默认前缀为'/actuator' # base-path: / exposure: # include: bus-refresh include: '*'

  1. ConfigServerApplication.java
package com.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @EnableConfigServer @SpringBootApplication public class ConfigServerApplication {public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } }

【#|spring-cloud-config使用】配置中心客户端
  1. pom.xml
org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-config org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.cloud spring-cloud-starter-bus-amqp

  1. bootstrap.yml
# bootstrap配置优先于application配置文件 配置不会被覆盖 spring: cloud: config: label: master profile: dev # 配置中心服务地址 使用eureka后此配置失效 # uri: http://localhost:8002 discovery: enabled: true serviceId: config-server eureka: client: service-url: defaultZone: http://192.168.92.134:8001/eureka

  1. application.yml
server: port: 9001 spring: application: name: config-client rabbitmq: host: 192.168.92.134 port: 5672 username: guest password: guest

  1. ConfigClientApplication.java
package com.example; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController /** * 当前controller支持热刷新配置中心配置 */ @RefreshScope @SpringBootApplication public class ConfigClientApplication {@Value("${version}") private String test; public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); }/** * a.properties a.yml 相同的键显示properties的值 */ @GetMapping public String test() { return test; } }

    推荐阅读