代码:config-parent_IJ_soft_backup

Spring CloudConfig 解决了在分布式场景下多环境配置文件的管理和维护
好处:
流程
config server:
目的: 编写config server功能,使其可以访问到外部配置文件,之后编写config client通过configserver访问外部文件

学这个单分支简单

期望把远程仓库克隆到本地,做一些文件添加,再添加到远程仓库
这里使用到git小乌龟使用 汉化及安装方法
使用参考https://www.cnblogs.com/xuanwotianming153/p/8504762.html
在gitrepository右键选择clone,

添加config-dev.yml配置文件


consumer provider参考下面文章的consumer provider
修改maven仓库和编码UTF-8
sever搭建:

pom.xml
- <dependencies>
-
- <dependency>
- <groupId>org.springframework.cloudgroupId>
- <artifactId>spring-cloud-config-serverartifactId>
- dependency>
- dependencies>
configServerApp:
- package com.gao.config;
-
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.cloud.config.server.EnableConfigServer;
-
-
- @SpringBootApplication
- //启用 EurekaServer
- @EnableConfigServer
- public class ConfigServerApp {
- public static void main(String[] args){
- SpringApplication.run(ConfigServerApp.class,args);
- }
- }
application.yml
- server:
- port: 9527
-
- spring:
- application:
- name: config-provider
- cloud:
- config:
- server:
- git:
- uri: https://gitee.com/GXQ205153964/springcloud-configs.git #远程仓库的地址
- label: master
localhost:9527/master/config-dev.yml

config client
目的: 完成config client 并测试client是否可以通过server访问到gitee上的外部文件

pom.xml
- <dependencies>
-
- <dependency>
- <groupId>org.springframework.cloudgroupId>
- <artifactId>spring-cloud-config-serverartifactId>
- dependency>
- dependencies>
ConfigServerApp;
- @SpringBootApplication
- //启用 EurekaServer
- @EnableConfigServer
- public class ConfigServerApp {
- public static void main(String[] args){
- SpringApplication.run(ConfigServerApp.class,args);
- }
- }
application.yml
- server:
- port: 9527
-
- spring:
- application:
- name: config-provider
- cloud:
- config:
- server:
- git:
- uri: https://gitee.com/GXQ205153964/springcloud-configs.git #远程仓库的地址
- label: master

pom.xml
- <dependencies>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-webartifactId>
- dependency>
-
- <dependency>
- <groupId>org.springframework.cloudgroupId>
- <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
- dependency>
- <dependency>
- <groupId>org.springframework.cloudgroupId>
- <artifactId>spring-cloud-starter-netflix-hystrixartifactId>
- dependency>
- <dependency>
- <groupId>org.springframework.cloudgroupId>
- <artifactId>spring-cloud-starter-configartifactId>
- dependency>
- dependencies>
GoodsController
- //注入 从gitee上的配置文件config-dev.yml获取键值对
- @Value("${gao}")
- private String key1;
-
-
- //结果显示到web
- goods.setTitle(goods.getTitle()+":" + key1);
application.yml
- server:
- port: 8000
-
- eureka:
- client:
- service-url:
- defaultZone: http://localhost:8761/eureka # 注册
-
- spring:
- application:
- name: config-provider #设置当前应用的名称。将来会在eureka中application显示,将来需要使用该名称来获取路径
bootstrap.yml
- #bootstrap优先级更高
-
- #配置config-server地址
- #配置获取配置文件的名称等信息
-
- spring:
- cloud:
- config:
- #配置config-server地址
- uri: http://localhost:9527
- name: config #文件名
- profile: dev #开发环境
- label: master #分支
开启config-provider和config-server服务

当外部配置文件修改后, config-server是可以刷新更新数据,但config-server不能
目的: client可以通过server访问到外部文件后,当外部文件修改后client需要重启才能获取到新的外部文件,非常不方便,这里配置后可以通过刷新获取到新的外部文件
流程:
在bootstrap.yml中
- <dependencies>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-webartifactId>
- dependency>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-actuatorartifactId>
- dependency>
-
- <dependency>
- <groupId>org.springframework.cloudgroupId>
- <artifactId>spring-cloud-starter-netflix-hystrixartifactId>
- dependency>
-
- <dependency>
- <groupId>org.springframework.cloudgroupId>
- <artifactId>spring-cloud-starter-configartifactId>
- dependency>
- dependencies>

在哪个地方获取外部配置文件就在哪个地方加

bootstrap.yml
- #暴露端点
- management:
- endpoints:
- web:
- exposure:
- include: 'refresh' # 或者 * 也可以代替,表示全部暴露不安全
在启动config-server和config-provider后,cmd发送这个post请求
![]()

目的: 当config server地址发生变化后ABC所存的固定地址都需要修改,非常麻烦,这里加入Eureka服务注册中心,让ABC客户端通过Eureka去获取config server的地址,当server地址发生变化后只需要修改Eureka的注册地址即可非常方便。
原来:

现在:
当config server地址发生改变时ABC内的uri地址都需要修改,加入Eureka后,动态获取Config地址,ConfigServer地址发生改变,只需要在注册中心修改一下即可。eureka会自动分发给ABC最新地址
-
-
org.springframework.cloud -
spring-cloud-starter-netflix-eureka-client -
- eureka:
- client:
- service-url:
- defaultZone: http://localhost:8761/eureka # 注册
@EnableEurekaClient
- #bootstrap优先级更高
-
- #配置config-server地址
- #配置获取配置文件的名称等信息
-
- spring:
- cloud:
- config:
- #配置config-server地址
- #uri: http://localhost:9527
- name: config #文件名
- profile: dev #开发环境
- label: master #分支
- #从注册中心去寻找config-server地址
- discovery:
- enabled: true
- serice-id: CONFIG-SERVER
-
- #暴露端点
- management:
- endpoints:
- web:
- exposure:
- include: refresh # 或者 * 也可以代替,表示全部暴露不安全

- //启动类
- @SpringBootApplication
-
- //启用 EurekaServer
- @EnableEurekaServer
- public class EurekaApp {
- public static void main(String[] args){
- SpringApplication.run(EurekaApp.class,args);
- }
- }
-
-
- //application.yml
- server:
- port: 8761
- # 默认8761
-
- eureka:
- instance:
- hostname: localhost
-
- client:
- service-url:
- defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka #eureka服务端地址,将客户端使用该地址和eureka
- register-with-eureka: false #是否将自己的路径注册到eureka上,eureka server不需要,eureak client需要
- fetch-registry: false #是否需要从eureka中抓取路径 eureka server不需要,eureak consumer client需要
-
-
- //pom.xml
-
- <dependencies>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-webartifactId>
- dependency>
-
-
- <dependency>
- <groupId>org.springframework.cloudgroupId>
- <artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
- dependency>
- dependencies>



当客户端多的时候需要一个一个的发送post请求才能让各个客户端收到最先的外部配置文件。不方便,需要引入bus消息总线。一次post请求所有客户端更新。