配置中心的搭建和配置分为服务端和客户端,整个过程比较简单。服务端的的数据源依赖于Git或SVN,本次使用的是git:https://gitee.com,国内的访问速度还快些,所以选用国内的。
配置中心的服务端搭建
1.去gitee新建一个仓库,然后把项目克隆到本地,然后在克隆下来的目录里,新建两个文件并提交
文件命名规则参考:https://blog.csdn.net/u010240125/article/details/106727380
2.新建配置中心的项目
3.设置项目,两步:
- 添加配置文件
- 启动类添加注解:@EnableConfigServer
添加配置文件:bootstrap.yml (不是application.yml了)
server:
port: 8206
spring:
application:
name: config-single-server # 应用名称
cloud:
config:
server:
git:
uri: git@gitee.com:xxxxxx/config-test.git #配置文件所在仓库
#username: github 登录账号
#password: github 登录密码
default-label: master #配置文件分支
#search-paths: config #配置文件所在根目录
启动类添加注解:@EnableConfigServer
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class Config8206Application {
public static void main(String[] args) {
SpringApplication.run(Config8206Application.class, args);
}
}
然后启动,访问:http://localhost:8206/config-client-dev.yml。config-client-dev.yml 是事先在git的master分支下建好的文件。
访问 config-client-prod.yml 文件:
到此,配置中心搭建完毕。修改下文件内容,提交到git后可以刷新后查看更新后的内容。
搭建配置中心的客户端(即微服务端)
新建项目:
编辑项目,两步:
- 新建配置文件:bootstrap.yml
- 编写控制器,并添加注解:@RefreshScope
1.新建配置文件:bootstrap.yml
# bootstrap 项目启动会优先加载
server:
port: 8207
spring:
profiles:
active: dev # 指定使用哪个配置文件
application:
name: config-client
cloud:
config:
uri: http://localhost:8206 # 指定配置中心访问地址
label: master # 指定分支
# 暴露端点
management:
endpoints:
web:
exposure:
include: '*' # 端点可能包含敏感信息,生产上选择下要开哪些,如:refersh,health,info
2.编写控制器,并添加注解:@RefreshScope
要注意下注解是写在控制器上的,也就是哪里需要动态刷新配置就写到哪里,可以是entity类,也可以是service类。
package com.example.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RefreshScope
@RestController
public class IndexController {
@Value("${info}") // 注入配置中的数据
String info;
@GetMapping("/index")
public String index(){
return info; // 将配置内容输出
}
}
启动,访问:http://localhost:8207/index
注意:当在git提交了更新之后,客户端不会刷新配置,刷新客户端的配置可以有两种操作:
- 重启项目。
- 使用POST方式,使用JSON的头格式,请求客户端的refresh地址。
重启项目会中断服务,所以一般会使用第二种方式刷新配置。
POST : http://localhost:8207/actuator/refresh
请求结果:
把微服务端打成jar包,执行命令:
java -jar client-8207-0.0.1-SNAPSHOT.jar --spring.profiles.active=prod
再次访问可以看到配置内容变成了生产环境的
使用消息总线刷新配置
要解决配置修改后,需要手动去更新每一个微服务的问题,使用消息总线的功能来进行统一通知。
需要使用到 RabbitMQ 来传递消息。开始前要先安装好RabbitMQ。
spring-cloud.version = Hoxton.SR6 的版本需要配置中心项目和微服务项目分别引入两个包:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-bus</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-rabbit</artifactId>
</dependency>
为配置中心项目的配置文件增加配置:
server:
port: 8206
spring:
application:
name: config-single-server # 应用名称
cloud:
config:
server:
git:
uri: git@gitee.com:xxxxxxxxxxxx/config-test.git #配置文件所在仓库
#username: github 登录账号
#password: github 登录密码
default-label: master #配置文件分支
#search-paths: config #配置文件所在根目录
# 以下是为消息总线添加的配置
# rabbitmq相关配置
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
# 暴露bus刷新配置的端点
management:
endpoints:
web:
exposure:
include: 'bus-refresh'
为微服务端增加配置:
# bootstrap 项目启动会优先加载
server:
port: 8207
spring:
profiles:
active: dev # 指定使用哪个配置文件
application:
name: config-client
cloud:
config:
uri: http://localhost:8206 # 指定配置中心访问地址
label: master # 指定分支
# rabbitmq配置
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
# 有消息总线以后,配置刷新的端点可以不要了,只留有用的
# 暴露端点
#management:
# endpoints:
# web:
# exposure:
# include: '*' # 端点可能包含敏感信息,生产上选择下要开哪些,如:refersh,health,info
重启两个项目,修改配置后使用POST方式访问:http://localhost:8206/actuator/bus-refresh,刷新看微服务端的效果。
完毕。