栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring cloud——config

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring cloud——config

目录

配置中心

pom

yml

主启动

客户端

pom

yml

主启动

 测试


配置中心

pom

        
            org.springframework.cloud
            spring-cloud-config-server
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
            org.projectlombok
            lombok
        
        
            org.springframework.boot
            spring-boot-starter-test
        
    

yml

server:
  port: 3344
spring:
  application:
    name: cloud-config-center
  cloud:
    config:
      server:
        git:
          uri: https://github.com/用户名/springcloud-config.git #github仓库名字,对应哪个地址
          search-paths: #搜索目录
            - springcloud-config  #哪个仓库
          force-pull: true
          username: 用户名
          password: 密码
      label: mian   #哪个分支
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);
    }
}

客户端 pom
 
        
            org.springframework.cloud
            spring-cloud-starter-config
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
            org.projectlombok
            lombok
        
        
            org.springframework.boot
            spring-boot-starter-test
        
    
yml

注意,取名为bootstrap.yml优先级高于application.yml

server:
  port: 3355
spring:
  application:
    name: config-client
  cloud:
    config: #客户端配置
      label: master #分支名称
      name: config  #配置文件名称
      profile: dev  #读取的后缀的名称 结合起来就是读取 master/config-dev.yml的配置文件
      uri: http://localhost:3344  #配置中心地址
eureka:
  client:
    service-url:
      defaultZone:  http://localhost:7001/eureka

主启动
@SpringBootApplication
@EnableEurekaClient
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("/getConfigInfo")
    public String getConfigInfo(){
        return configInfo;
    }
}

存在问题,当GitHub仓库里面的配置文件修改以后,配置中心会随之变化,但是客户端并不会改变,必须要重启客户端的服务才能刷新出来,针对此问题,现阶段可以使用config动态刷新手动版,想要实现自动更新需要使用Bus

1、客户端的pom引入actuator

2、客户端修改yml,添加:

management:         #暴露监控端点

     endpoints:

          web:

               exposure:

                   include: "*"

3、控制器上添加注解

@RefreshScope   //刷新

4、发送post请求

curl -X POST "http://localhost:3355/actuator/refresh"

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/927084.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号