本文将示例一个Spring Cloud Server的配置,以及Client的连接配置,Server端的配置文件从本地文件以及Git仓库读取,当配置发生更改时将通过Spring Cloud Bus推送事件给Client进行配置环境的刷新,消息队列使用的是Kafka,构建工具使用Gradle,官方文档:spring-cloud-config、spring-cloud。先说明下Spring的版本:
- Spring Boot: 2.1.5.RELEASE
- Spring Cloud: Greenwich.SR1
Config Server
Gradle配置
spring-cloud-starter-bus-kafka
是kafka的实现,如果用RabbitMq则换成org.springframework.cloud:spring-cloud-starter-bus-amqp
,spring-cloud-config-monitor
的项目很简单,提供了新的改动提交到Github、Gitlab等时webhook回调的rest。
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
repositories {
maven { url 'https://maven.aliyun.com/repository/public' }
maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
maven { url 'https://maven.aliyun.com/repository/spring' }
maven { url 'https://maven.aliyun.com/repository/spring-plugin' }
mavenCentral()
}
ext {
set('springCloudVersion', "Greenwich.SR1")
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.cloud:spring-cloud-config-server'
implementation 'org.springframework.cloud:spring-cloud-starter-bus-kafka'
implementation 'org.springframework.cloud:spring-cloud-config-monitor'
testImplementation 'junit:junit:4.12'
testCompile('org.springframework.boot:spring-boot-starter-test')
annotationProcessor "org.springframework.boot:spring-boot-configuration-processor"
annotationProcessor("org.projectlombok:lombok")
compileOnly("org.projectlombok:lombok")
}
项目配置
git仓库等信息放到bootstrap.yml配置文件中,但是放到application.yml中也可以正常启动的,等有空调式这个项目的源码的时候再探究区别,这里先按官方建议的写。需要注意的是,本地配置中,windows系统需要有三个///,linux系统需要两个//。
bootstrap.yml
spring:
application:
name: 'Spring Cloud Server'
cloud:
config:
server:
native:
search-locations: file:///D:/IDEA/localconfig
order: 1
git:
uri: https://github.com/你的github配置项目地址
username: 用户名
password: 密码
skip-ssl-validation: true
order: 2
#refresh-rate: 300
application.yml
server:
port: 8888
address: 127.0.0.1
spring:
kafka:
bootstrap-servers: kafka连接地址
consumer:
group-id: SpringCloud-Bus
profiles:
active:
- native
- git
management:
endpoints:
web:
exposure:
include: '*'
这里Server端的配置就完成了,只需要在启动类加上注解@SpringBootApplication
、@EnableConfigServer
就可以启动配置中心的服务端。稍微说下配置文件名。可以使用浏览器获取配置中心当前的配置,也就是模拟client端获取配置的结果。浏览器上GET /host:8888/{applicationName}/{profile}/{label},就能获取到配置文件,相应的配置文件命名也是{applicationName}-{profile}-{label}.yml,lable用于标记版本(没用过)。默认不带profile用default代替。
Config Client
Gradle配置
和Server端的类似,去掉monitor和config server,添加client的依赖包。
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
repositories {
maven { url 'https://maven.aliyun.com/repository/public' }
maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
maven { url 'https://maven.aliyun.com/repository/spring' }
maven { url 'https://maven.aliyun.com/repository/spring-plugin' }
mavenCentral()
}
ext {
set('springCloudVersion', "Greenwich.SR1")
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.cloud:spring-cloud-starter-config'
implementation 'org.springframework.cloud:spring-cloud-starter-bus-kafka'
testImplementation 'junit:junit:4.12'
testCompile('org.springframework.boot:spring-boot-starter-test')
annotationProcessor "org.springframework.boot:spring-boot-configuration-processor"
annotationProcessor("org.projectlombok:lombok")
compileOnly("org.projectlombok:lombok")
}
项目配置
以下配置会去读取applicationName是common,prifile为prod的配置文件,例如配置目录中存在三个文件:common.yml, common-prod.yml, common-test.yml,则会返回common-prod.yml,common.yml的内容,前者覆盖后者。
bootstrap.yml
spring:
cloud:
config:
name: common
profile: prod
fail-fast: true
uri: http://127.0.0.1:8888
application.yml
spring:
kafka:
bootstrap-servers: kafka连接地址
server:
address: 127.0.0.1
port: 8080
management:
endpoints:
web:
exposure:
include: '*'
启动Spring Boot入口程序,Spring就会从Config Server获取配置文件。
自动刷新
依赖了两个项目Spring Cloud Bus,以及Monitor项目。Spring Cloud Bus允许调用rest POST config_server_host/actuator/bus-refresh 发送一个RefreshRemoteApplicationEvent事件,其他项目接受到事件后会重新获取Config Server的配置并刷新需要刷新的组件,不需要我们一个个子项目去调用/refresh的方法,这在微服务的系统中非常的方便也避免了出纰漏。Spring Cloud Bus解决了项目内部自动通知的问题,而Monitor则解决了外部系统自动通知的问题,下面以Github为例说明:
path=*意思是当该仓库有push时,对所有的服务发送RefreshRemoteApplicationEvent事件,如果需要可以规定某几个service,通过逗号分隔。
Webhook配置完成后,当有配置提交到仓库时,GitHub会调用rest,Config Server会发送RefreshRemoteApplicationEvent事件,Client会从配置中心重新获取配置并刷新相关组件,一切都是全自动的,不需要人为的操作或者重启程序。