Spring Cloud Config远程配置中心 清疚 2022-05-11 14:34 227阅读 0赞 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在Spring Cloud中,有分布式配置中心组件spring cloud config 配置管理工具包,让你可以把配置放到远程服务器,集中化管理集群配置,目前支持本地存储、Git以及Subversion。支持远程更改,服务在不重启的情况下,使配置生效。 spring cloud config分server和client端。 ![70][] ## ## ### Config Server ### * 引入依赖 <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-test</artifactId> <scope>test</scope> </dependency> </dependencies> * 启动入口加注解@EnableConfigServer @SpringBootApplication @EnableDiscoveryClient @EnableConfigServer public class SpringcloudConfigApplication { public static void main(String[] args) { SpringApplication.run(SpringcloudConfigApplication.class, args); } } * 配置远程仓库地址 spring: application: name: config cloud: config: server: git: uri: 远程仓库地址 username: 远程仓库账号用户名 password: 远程仓库账号密码 client: service-url: defaultZone: http://localhost:8761/eureka/ register-with-eureka: false instance: prefer-ip-address: true server: #自我保护模式,测试环境关闭,生产环境一定要打开 enable-self-preservation: false 先看一下我的git仓库的内容: ![70 1][] 启动,访问。访问方式:/\{label\}/\{name\}-\{profile\}.想要展示的文件类型 ![70 2][] 访问成功,我们也可以指定json格式返回。 ![70 3][] 此时,查看控制台日志,发现本地会创建Git仓库。 ![70 4][]由于我们生产环境一般是linux,文件夹一般指定了访问权限,为避免默认缓存的文件位置没有写的权限,需要指定一下config-repo的位置。 git: basedir: I:/Code/EclipseEEWorkspace/springcloud-config/config-repo-cache 重启,观察控制台,配置生效了: ![70 5][] ## ## ### Config Client ### * 加依赖 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-client</artifactId> </dependency> * 修改application.yml => bootstrap.yml ![70 6][] bootstrap.yml: spring: application: name: product cloud: config: discovery: enabled: true service-id: CONFIG profile: dev server: port: 8081 * 远程仓库配置文件 ![70 7][] ![70 8][] * 写一个测试接口,去获取远程配置文件中env的值。 @RestController @RequestMapping("/env") public class ServerController { @Value("${env}") private String env; @GetMapping("/print") public String print() { return env; } } 访问接口,成功获取的配置。 ![70 9][] 此时,在远程Git仓库修改该配置env => prod,访问发现env打印的依赖时dev,修改未生效。但我们访问config-server,却是生效的。 ![70 10][] 接下来是重点,必须怀疑,此时config-server获取的最新配置,但没有及时通知到service刷新,所以需要引入 Spring Cloud Bus自动刷新配置。 ### Spring Cloud Bus自动刷新配置 ### ![70 11][] 原理:当远程仓库的配置文件更新,config-server获取到最新的配置文件,需要通过Mq消息中间件给各Services发生更新消息。SpringCloud为我们提供了一个刷新配置的接口 Bus-Refrsh,我们可以采用git的WebHooks实现动态更新,即当git远程仓库配置文件发生变更时,Git自动刷新Bus-Refrsh接口。 需要修改config的yml文件,将bus-refresh暴露出来。 #需要暴露bus-refresh接口 [/actuator/bus-refresh/{destination}],methods=[POST] #include: "*" 表示暴露所有 endpoints 出去,默认是“health”,“info” management: endpoints: web: exposure: include: "*" 此时,采用手动刷新试试看是否生效: ![70 12][] 发现mq有收到消息,控制台出现重新拉取配置的日志 ![70 13][] 但此时通过get请求获取配置还是未生效,还需要在加@RefreshScope @RestController @RequestMapping("/env") @RefreshScope public class ServerController { @Value("${env}") private String env; @GetMapping("/print") public String print() { return env; } } 此时就能做到自动刷新配置了。 但我们总不能每次发生修改了,手动去刷新一次bus-refresh接口,此时我们可以借助git的webhooks。Spring Cloud Config专门提供了用于Webhooks的路由 monitor. ![70 14][] ![70 15][] 报错,开源中国的git格式不兼容,可以采用github的git试一下,是ok的。 ![70 16][] [70]: /images/20220505/280389fbd7b84e369c7e809d620f698f.png [70 1]: /images/20220505/8a3620103ee34765a193ca017d156398.png [70 2]: /images/20220505/def01dea8d7b4642a478d71c9664e346.png [70 3]: /images/20220505/26d3a0fae33f43138cfd2ee99ae62b48.png [70 4]: /images/20220505/d6f7b30dc76741ea95f6f63d8367d341.png [70 5]: /images/20220505/76b4295b25ee4207b2c1e54f552c24fa.png [70 6]: /images/20220505/bb78ae8698c5466bb0e43acf67362421.png [70 7]: /images/20220505/a44ba01bb1524fa59600d3ef9cb786ac.png [70 8]: /images/20220505/ae41921f859b436981815d744d7acdc8.png [70 9]: /images/20220505/89290205cedf40a29eb23a3e0bd17464.png [70 10]: /images/20220505/70efbbe63c604317abe1215e4ac68d0a.png [70 11]: /images/20220505/396b793ab7b5494c83ede0ce1ff42567.png [70 12]: /images/20220505/c2a15991980542c088d9ea746020f6ba.png [70 13]: /images/20220505/5dc8b394c373494cbff4773049313409.png [70 14]: /images/20220505/c9c99c0a9d144647b315b629ac3d09aa.png [70 15]: /images/20220505/f78fc1e707394361b6056b45ab48e081.png [70 16]: /images/20220505/9567c6cd13674674a8862d8a9c156857.png
还没有评论,来说两句吧...