一、简介
springcloud config为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环节提供了一个中心化的外部配置
-
集中管理配置文件
-
不同环境不同配置,动态化的配置更新,分环境部署比如dev/test/prod
-
允许期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务回想配置中心同意拉去配置自己的信息,当配置发生变动时,服务不需要重启即可感知到配置文件的变化并应用新的配置
-
将配置信息以rest接口的形式暴漏
二、服务端配置
-
用棍自己的账号再github新建一个springcloud-config的新repository
-
新建Module模块Cloud-config-center-3344作为cloud的配置中心模块cloudconfig center
-
修改pom文件
<dependency> <groupId>org.springframework.cloud</groupId> <actifactId>spring-cloud-config-server</actifactId> </dependency>
-
修改yml文件
server: port: 3344 spring: application: name: spring-cloud-config cloud: server: git: uri: git@github.com:zzyybs/springcloud-config.git #搜索目录 search-paths: - springcloud-config #读取分值 label: master erkeua: instance: hostname: cloud-gateway-service client: service-url: registry-with-eureka: true fetch-registry: true defaultZone: http://eureka7001.com:7001/eureka
-
添加启动类注解
@EnableConfigServer
-
测试访问配置文件
localhost:3344/master/config-dev.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.yml
/{application}/{profile}[/{label}]
分支、服务名、环境
-
三、客户端配置与测试
1、添加pom文件
<dependency>
<groupId>org.springframework.cloud</groupId>
<actifactId>spring-cloud-starter-config</actifactId>
</dependency>
2、添加yml配置
application.yml时用户及的资源配置项,bootstrap.yml是系统级,优先级更高
config配置需要写在bootstrap.yml里面
spring:
application:
name: spring-cloud-config-client
cloud:
config:
label: master #分支名称
name: config #配置文件名称
profile: dev #读取后缀名称 master分支上的config-dev.yml文件
#localhost:3344/master/config-dev.yml
uri: http://localhost:3344
erkeua:
instance:
hostname: cloud-gateway-service
client:
service-url:
registry-with-eureka: true
fetch-registry: true
defaultZone: http://eureka7001.com:7001/eureka
3、启动类
不需要添加其他的注解
四、客户端之动态刷新配置
另一种方法使用bus总线实现
-
git修改了配置文件,config服务刷新获取配置可以拿到最新配置,但是客户端不行,必须重启服务使其生效,可以使用动态刷新配置
-
客户端添加以下配置(除了网关不加,其他客户端都要加)
<dependency> <gtoupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
-
修改yml暴漏监控端口
#暴漏监控端点 management: endpoints: web: exposure: include: "*"
-
再业务类上controller添加@RefreshScope, 发现3355获取配置配置没有生效
-
现在需要运维人员发送post撒核心3355
curl -X POST “http://localhost:3355/actuator/refreah”,即可刷新最新配置
如果服务过多需要在每个端点去刷新,这时候我们用下一章的bus消息总线来通过rabbitmq来再config-server端统一刷新