转载请表明出处 https://blog.csdn.net/Amor_Leo/article/details/87884267 谢谢
Spring Cloud Config Bus
Spring Cloud Config概述
Spring Cloud Config 是Sping Cloud下用于分布式配置管理的组件,分成了两个角色Config Server和Config Client;Config Server端分布式配置中心;并对外提供接口方便Config Client访问,接口使用HTTP的方式对外提供访问;Config Client通过接口获取配置文件,然后可以在应用中使用;Config Server存储/管理的配置文件可以来自本地文件,远程Git仓库以及远程Svn仓库,默认Git。
Spring Cloud Config搭建
Config Server:
-
pom
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
-
yml
- 默认
server: port: 8888 spring: application: name: config-server cloud: config: server: git: uri: https://github.com/amor412/spring-cloud-config-repo # 配置Git仓库的地址 username: # Git仓库的账号 password: # Git仓库的密码 health: # 健康状况指示器 repositories: a-foo: label: config-label-v2.0 name: provide-foo profiles: dev eureka: client: service‐url: defaultZone: http://localhost:8761/eureka instance: prefer‐ip‐address: true #访问路径可以显示ip地址 logging: level: org.springframework.cloud: DEBUG org.springframework.boot: DEBUG management: endpoints: web: exposure: include: "*" security: enabled: false
- 占位符支持
server: port: 8888 spring: application: name: config-server cloud: config: server: git: uri: https://github.com/amor412/{application} username: # Git仓库的账号 password: # Git仓库的密码 logging: level: org.springframework.cloud: DEBUG org.springframework.boot: DEBUG eureka: client: service‐url: defaultZone: http://localhost:8761/eureka instance: prefer‐ip‐address: true #访问路径可以显示ip地址 management: endpoints: web: exposure: include: "*" security: enabled: false
- 模式匹配和多仓库
server: port: 8888 spring: application: name: config-server cloud: config: server: git: uri: https://github.com/spring-cloud-samples/config-repo repos: simple: https://github.com/simple/config-repo special: pattern: special*/dev*,*special*/dev* uri: https://github.com/special/config-repo local: pattern: local* uri: file:/home/configsvc/config-repo logging: level: org.springframework.cloud: DEBUG org.springframework.boot: DEBUG eureka: client: service‐url: defaultZone: http://localhost:8761/eureka instance: prefer‐ip‐address: true #访问路径可以显示ip地址 management: endpoints: web: exposure: include: "*" security: enabled: false
- 子目录查询(搜索目录)
search-paths 支持占位符;
server: port: 8888 spring: application: name: config-server cloud: config: server: git: uri: http://github.com/amor412/spring-cloud-config-repo search-paths: foo,bar* logging: level: org.springframework.cloud: DEBUG org.springframework.boot: DEBUG eureka: client: service‐url: defaultZone: http://localhost:8761/eureka instance: prefer‐ip‐address: true #访问路径可以显示ip地址 management: endpoints: web: exposure: include: "*" security: enabled: false
- 启动时clone
clone-on-start: 设置Config Server启动时是否克隆git仓库;
server: port: 8888 spring: application: name: config-server cloud: config: server: git: uri: https://github.com/spring-cloud-samples/config-repo repos: team-a: pattern: team-a-* cloneOnStart: true uri: http://github.com/team-a/spring-cloud-config-repo team-b: pattern: team-b-* cloneOnStart: false uri: http://github.com/team-b/spring-cloud-config-repo team-c: pattern: team-c-* uri: http://github.com/team-c/spring-cloud-config-repo logging: level: org.springframework.cloud: DEBUG org.springframework.boot: DEBUG eureka: client: service‐url: defaultZone: http://localhost:8761/eureka instance: prefer‐ip‐address: true #访问路径可以显示ip地址 management: endpoints: web: exposure: include: "*" security: enabled: false
-
svn
- pom
<dependency> <groupId>org.tmatesoft.svnkit</groupId> <artifactId>svnkit</artifactId> <version>1.8.10</version> </dependency>
- yml
server: port: 8888 spring: application: name: config-server cloud: config: enabled: true server: svn: uri: http://ip:port/svn/Code/Config/app-config username: username password: password default-label: config-repo #指定svn目录下的某个文件夹作为配置仓库 默认为trunk profiles: active: subversion #指定配置中心使用svn管理 eureka: client: service‐url: defaultZone: http://localhost:8761/eureka instance: prefer‐ip‐address: true #访问路径可以显示ip地址 management: endpoints: web: exposure: include: "*" security: enabled: false
-
Application类上
@EnableEurekaClient @EnableConfigServer
ConfigClient:
可以使ConfigServer的端点获取配置文件的内容,端点与配置文件的映射规则如下:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
application对应微服务名称;profile表示使用哪种环境的配置文件,这里可以是dev,test,prod;label可选的标签,git仓库默认值master,svn仓库默认值是trunk。
provide-foo-dev.properties profile=dev
provide-foo-test.properties profile=test
provide-foo-prod.properties profile=prod
- pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
- Application类上
@EnableEurekaClient
- yml
application.yml
server:
port: 8081
bootstrap.yml
spring:
application:
name: provide-foo # 对应config server所获取的配置文件的{application}
cloud:
config:
discovery:
enabled: true # 表示使用服务发现的configserver配置,而不是自己配置的Config Server的uri,默认false
service-id: config-server # 指定Config Server在服务发现中的serviceId,默认是configserver
profile: dev # profile对应config server所获取的配置文件中的{profile}
label: master # 指定Git仓库的分支,对应config server所获取的配置文件的{label}
eureka:
client:
service‐url:
defaultZone: http://localhost:8761/eureka
instance:
prefer‐ip‐address: true #访问路径可以显示ip地址
management:
endpoints:
web:
exposure:
include: "*"
security:
enabled: false
- Controller
@RestController
public class ConfigClientController {
@Value("${profile}")
private String profile;
@GetMapping("/profile")
public String hello() {
return this.profile;
}
}
ConfigClient配置/refresh手动刷新
- ConfigServer
- pom
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
- application.yml
server: port: 8888 spring: application: name: config-server cloud: bus: trace: enabled: true config: label: master server: git: uri: https://github.com/SophiaLeo/spring-cloud-config-repo username: password: eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka instance: prefer‐ip‐address: true logging: level: org.springframework.cloud: DEBUG org.springframework.boot: DEBUG management: endpoints: web: exposure: include: "*" security: enabled: false
- ConfigClient
- pom
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud--starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
- Application类上
@EnableEurekaClient
- yml
application.yml
server:
port: 8081
bootstrap.yml:
spring:
application:
name: provide-foo # 对应config server所获取的配置文件的{application}
cloud:
config:
discovery:
enabled: true # 表示使用服务发现的configserver配置,而不是自己配置的Config Server的uri,默认false
service-id: config-server # 指定Config Server在服务发现中的serviceId,默认是configserver
profile: dev # profile对应config server所获取的配置文件中的{profile}
label: master # 指定git仓库的分支,对应config server所获取的配置文件的{label}
eureka:
client:
service‐url:
defaultZone: http://localhost:8761/eureka
instance:
prefer‐ip‐address: true #访问路径可以显示ip地址
management:
endpoints:
web:
exposure:
include: "*" #因为springboot2.1.必须加上,支持访问/actuator/hystrix.stream
security:
enabled: false
- Controller
@RestController
@RefreshScope
public class ConfigClientController {
@Value("${profile}")
private String profile;
@GetMapping("/profile")
public String hello() {
return this.profile;
}
}
修改 provide-foo-dev.properties 内容为profile=dev-change
访问http://localhost:8081/profile
post请求访问http://localhost:8888/actuator/bus-refresh 接口
再次访问http://localhost:8081/profile
ConfigClient使用SpringCloud Bus配置自动刷新:
- ConfigServer
- pom
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
- application.yml
server: port: 8888 spring: application: name: config-server cloud: bus: trace: enabled: true config: label: master server: git: uri: https://github.com/SophiaLeo/spring-cloud-config-repo username: password: rabbitmq: host: 192.168.0.111 port: 5672 username: admin password: admin eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka instance: prefer‐ip‐address: true logging: level: org.springframework.cloud: DEBUG org.springframework.boot: DEBUG management: endpoints: web: exposure: include: "*" security: enabled: false
- ConfigClient
- pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
- Application类上
@EnableEurekaClient
application.yml
server:
port: 8081
bootstrap.yml:
spring:
application:
name: provide-foo # 对应config server所获取的配置文件的{application}
cloud:
config:
discovery:
enabled: true # 表示使用服务发现的configserver配置,而不是自己配置的Config Server的uri,默认false
service-id: config-server # 指定Config Server在服务发现中的serviceId,默认是configserver
profile: dev # profile对应config server所获取的配置文件中的{profile}
label: master # 指定Git仓库的分支,对应config server所获取的配置文件的{label}
rabbitmq:
host: 192.168.0.111
port: 5672
username: admin
password: admin
eureka:
client:
service‐url:
defaultZone: http://localhost:8761/eureka
instance:
prefer‐ip‐address: true #访问路径可以显示ip地址
management:
endpoints:
web:
exposure:
include: "*" #因为springboot2.1.必须加上,支持访问/actuator/hystrix.stream
security:
enabled: false
- Application类上
@EnableEurekaClient
- Controller
@RestController
@RefreshScope
public class ConfigClientController {
@Value("${profile}")
private String profile;
@GetMapping("/profile")
public String hello() {
return this.profile;
}
}
- 如果是Zuul作为Config Client 则Application类添加:
@RefreshScope
@ConfigurationProperties("zuul")
public ZuulProperties zuulProperties() {
returnnew ZuulProperties();
}
修改 provide-foo-dev.properties 内容为profile=dev-change-bus
访问http://localhost:8081/profile
post请求访问http://localhost:8888/actuator/bus-refresh 接口
再次访问http://localhost:8081/profile
可以借助Git仓库的WebHooks
SpringCloud Config 用户认证与Eureka一起
- ConfigServer:
- pom
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> </dependencies>
- yml
server: port: 8888 spring: security: user: name: user # 配置登录的账号是user password: password123 # 配置登录的密码是password123 application: name: config-server cloud: config: server: git: uri: https://github.com/amor412/spring-cloud-config-repo # 配置Git仓库的地址 username: # Git仓库的账号 password: # Git仓库的密码 eureka: client: service‐url: defaultZone: http://localhost:8761/eureka instance: prefer‐ip‐address: true #访问路径可以显示ip地址
- Application类上
@EnableConfigServer @EnableEurekaClient
- ConfigClient:
- pom
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies>
- yml
- application.yml
server: port: 8080
- bootstrap.yml
spring: application: name: provide-foo # 对应config server所获取的配置文件的{application} cloud: config: username: user password: password123 profile: dev label: master discovery: enabled: true # 表示使用服务发现的configserver配置,而不是自己配置的Config Server的uri,默认false service-id: config-server # 指定Config Server在服务发现中的serviceId,默认是configserver eureka: client: service‐url: defaultZone: http://localhost:8761/eureka instance: prefer‐ip‐address: true #访问路径可以显示ip地址
- application.yml
- Application类上
@EnableEurekaClient
- Controller
@RestController public class ConfigClientController { @Value("${profile}") private String profile; @GetMapping("/profile") public String hello() { return this.profile; } }
- yml