微服务意味着要将单体应用中的业务拆分成一个个子业务,每个子业务的粒度较小,因此系统中会出现大量的服务,由于每个服务都需要必要的配置信息才能运行,所以一套集中式的,动态的配置管理必不可少。
springcloud config为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置。
springcloud默认使用git来存储配置文件,与github整合。
首先在自己的github上创建一个名为springcloud-config的新Repository,然后获取git地址
下载一个git,然后再工作目录下创建一个git本地仓库git clone 地址
ps:下载到本地可能不成功,需要创建一个ssh密钥。
新建module cloud-config-center-3344
org.springframework.cloud
spring-cloud-config-server
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
com.atguigu.springcloud
cloud-api-commons
${project.version}
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-devtools
runtime
true
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
server:
port: 3344
spring:
application:
name: cloud-config-center
cloud:
config:
server:
git:
uri: https://github.com/luokong12138/sprincloud-config.git
search-paths: sprincloud-config
label: main
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
management:
endpoints:
web:
exposure:
include: 'bus-refresh'
在连接github时候可能会出现问题,首先是github的分组从master变成了main,然后最好使用http的网址,git网址可能需要用户名密码。代码仓库最好是公开的。如果配置都是对的访问不了可能只是因为网络原因。
主启动需要@EnableConfigServer
修改windows下host文件增加映射,最下面加上127.0.0.1 config-3344.com
新建cloud-config-client-3355
org.springframework.cloud
spring-cloud-starter-config
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
com.atguigu.springcloud
cloud-api-commons
${project.version}
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-devtools
runtime
true
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
创建bootsrap.yml和application.yml文件
(bootstrap文件是系统级的优先级更高,优先加载)
server:
port: 3355
spring:
application:
name: config-client
cloud:
config:
label: main
name: config
#配置文件名
profile: dev
#配置文件后缀,自动配置-符号
uri: http://localhost:3344
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka
management:
endpoints:
web:
exposure:
include: "*"
@RestController
public class ConfigClientController {
@Value("${config.info}")
private String configInfo;
@GetMapping("/configInfo")
public String getConfigInfo(){
return configInfo;
}
}
分布式配置的动态刷新:更改github上的配置就会发信,3344能立刻响应,但是3355却不能刷新,必须重启服务。
在3355引入监控,暴露接口yml文件
org.springframework.boot
spring-boot-starter-actuator
management:
endpoints:
web:
exposure:
include: "*"
controller层加上@RefreshScope注解
然后需要用post请求发送刷新请求http://localhost:3355/actuator/refresh
这样就避免了服务重启,但是还是非常不方便
springcloud Bus配合springcloud config可以实现配置的自动刷新
bus支持两种消息代理RabbitMQ和Kafka
springcloud Bus能管理和传播分布式系统间的消息,就像一个分布式执行器,可用于广播状态更改,事件推送等,也可以当做微服务间的通信通道
总线:在微服务架构的系统中,通常使用轻量级的消息代理来构建一个共同的消息主题,并让系统中的所有微服务都连接。该主题中所有消息都会被所有实例监听和消费,所以称为消息总线。
基本原理:configClient实例都能监听mq中同一个topic,当一个服务刷新数据的时候,他会把这个消息放入到topic中,这样其他监听同一个topic就能得到通知,更新自身配置。
6.RabbiMQ环境配置
安装Erlang,安装RabbitQM。需要版本匹配
进去RabbitMQ安装目录下的sbin
rabbitmq-plugins enable rabbitmq_management
访问地址http://localhost:15672/账号密码均为guest
按照3355增加一个3366
设计思想:利用总线触发一个服务端从而刷新所有客户端的配置
给3344,3355,3366添加消息总线支持
org.springframework.cloud
spring-cloud-starter-bus-amqp
server:
port: 3344
spring:
application:
name: cloud-config-center
cloud:
config:
server:
git:
uri: https://github.com/hhf19906/springcloud-config.git #[email protected]:hhf19906/springcloud-config.git
search-paths:
- springcloud-config
label: master
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
management:
endpoints:
web:
exposure:
include: 'bus-refresh'
然后发送post请求curl -X POST "http://localhost:3344/actuator/bus-refresh"就可以刷新全部
只通知3355
http://localhost:配置中心的端口号/actuator/bus-refresh/{destination}
curl -X POST "http://localhost:3344/actuator/bus-refresh/config-client:3355"