springcloud -config配置中心 整合github 或者gitee 单个刷新配置

发布时间 2023-07-08 21:42:51作者: 你就学个JVAV?

配置中心,通过从开源仓库上拉去配置,而不是在本地修改

服务端配置 cloud-config-center-3344

         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-config-server</artifactId>
         </dependency>
         <dependency>

application.yml配置

 server:
   port: 3344
 spring:
   application:
     name:  cloud-config-center #注册进Eureka服务器的微服务名
   cloud:
     config:
       server:
         git:
           uri: git@github.com:bighuo/springcloud-config.git #GitHub上面的git仓库名字
         ####搜索目录
           search-paths:
             - springcloud-config
       ####读取分支
       label: master
 #服务注册到eureka地址
 eureka:
   client:
     service-url:
       defaultZone: http://localhost:7001/eureka
  
 ​

主启动类

 @SpringBootApplication
 @EnableConfigServer  // 主要注解
 public class ConfigCenterMain3344
 {
     public static void main(String[] args) {
             SpringApplication.run(ConfigCenterMain3344.class, args);
     }
 }

访问localhost:3344/master/config-client.yml 可以获取到内容 对应格式/{application}-{profile}.yml config-client.yml

             /{name}-{profiles}.yml
  
 /{label}-{name}-{profiles}.yml
  
 label:分支(branch)
 name :服务名
 profiles:环境(dev/test/prod)

客户端配置 cloud-config-client-3355 导入依赖

 <?xml version="1.0" encoding="UTF-8"?>
 <project xmlns="http://maven.apache.org/POM/4.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     <parent>
         <artifactId>mscloud</artifactId>
         <groupId>com.atguigu.springcloud</groupId>
         <version>1.0-SNAPSHOT</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
 ​
     <artifactId>cloud-config-client-3355</artifactId>
 ​
     <dependencies>
         <dependency>
             // 核心依赖
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-starter-config</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-web</artifactId>
         </dependency>
         
         // 想要被发现和监控一定要配置这个依赖
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-actuator</artifactId>
         </dependency>
 ​
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-devtools</artifactId>
             <scope>runtime</scope>
             <optional>true</optional>
         </dependency>
         <dependency>
             <groupId>org.projectlombok</groupId>
             <artifactId>lombok</artifactId>
             <optional>true</optional>
         </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-test</artifactId>
             <scope>test</scope>
         </dependency>
     </dependencies>
 </project>
 ​

applicaiton.yml是用户级的资源配置项 bootstrap.yml是系统级的,优先级更加高

配置bootstrap.yml 优先级是最高的这个文件

 server:
   port: 3355
 ​
 spring:
   application:
     name: config-client
   cloud:
     #Config客户端配置
     config:
       label: master #分支名称
       name: config #配置文件名称
       profile: dev #读取后缀名称   上述3个综合:master分支上config-dev.yml的配置文件被读取http://config-3344.com:3344/master/config-dev.yml
       uri: http://localhost:3344 #配置中心地址k
 ​
 #服务注册到eureka地址
 eureka:
   client:
     service-url:
       defaultZone: http://localhost:7001/eureka
 ​

主要启动类

 /**
  * @auther zzyy
  * @create 2020-02-08 11:09
  */
 @EnableEurekaClient
 @SpringBootApplication
 public class ConfigClientMain3355
 {
     public static void main(String[] args)
     {
         SpringApplication.run(ConfigClientMain3355.class,args);
     }
 }

 

业务测试类

 @RestController
 public class ConfigClientController
 {
     @Value("${config.info}")
     private String configInfo;
 ​
     @GetMapping("/configInfo")
     public String getConfigInfo() 
     {
         return configInfo;
     }
 }

手动刷新

修改3355客户端服务,加上最新配置 yaml文件

 # 暴露监控端点
 management:
   endpoints:
     web:
       exposure:
         include: "*"

通过发送post请求通知刷新 curl -X POST "http://localhost:3355/actuator/refresh"