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

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

服务端配置 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"

posted on   你就学个JVAV?  阅读(28)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· 因为Apifox不支持离线,我果断选择了Apipost!
点击右上角即可分享
微信分享提示