微服务SpringCloud开弓之SpringCloud config分布式配置中心「十」

1、概述

地址

  • 分布式系统面临的问题–配置问题

在这里插入图片描述

  • 是什么

​ Springcloud config为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供一个中心化的外部配置。

  • 能干嘛

​ 1、集中管理配置文件

​ 2、不同环境不同配置,动态化的配置更新,分环境部署比如dev/test/prod/beta/release

​ 3、运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一一拉取配置自己的信息

​ 4、当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置

​ 5、将配置信息以REST接口的形式暴露

  • 与GitHub整合配置

在这里插入图片描述

2、Config服务端配置与测试

  • 新建一个名为springcloud-config的新Respository

  • 新建Module模块cloud-config-center-3344它即为Cloud的配置中心模块cloudCofing Center

  • POM文件

    <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-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.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    </dependencies>
    
  • YML

    server:
      port: 3344
    
    eureka:
      client:
        service-url:
          defaultZone: http://eureka7001.com:7001/eureka
    spring:
      application:
        name: cloud-config-center
      cloud:
        config:
          server:
            git:
              uri: https://github.com/ClqStart/SpringCloud-config.git
              force-pull: true
              search-paths:
                - SpringCloud-config
    
          label: master
    
  • 主启动类

    @SpringBootApplication
    @EnableConfigServer
    public class ConfigCenterMain3344 {
        public static void main(String[] args) {
            SpringApplication.run(ConfigCenterMain3344.class,args);
        }
    }
    
  • 测试

在这里插入图片描述

http://config-3344.com:3344/master/config-dev.yml

在这里插入图片描述

  • 总结

在这里插入图片描述

当启动我们的3344项目时,可以通过三种方法找到配置文件并读取

在这里插入图片描述

3、Config客户端配置与测试

  • 新建cloud-config-center-3355

  • POM

        <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>
    
  • bootstrap.yml

    • 是什么

1、applicaiton. yml是用户级的资源配置项

2、bootstrap. yml是系统级的,优先级更加高

3、Spring Clould会创建一个“Bootstrap Context” , 作为Spring应用的Application Context的父上下文。初始化的时候,BootstrapContext’负责从外部源加载配置属性并解析配置。这两个上下文共享一个从外部获取的Environment。

4、Bootstrap’属性有高优先级,默认情况下,它们不会被本地配置覆盖。 Bootstrap context’和Application Context有着不同的约定,所以新增了一个bootstrap.ymI文件, 保证Bootstrap Context’和Application Context配置的分离。

5、要将Client模块下的application.yml文件改为bootstrap.yml,这是很关键的,因为bootstrap.ym是比application.yml先加载的。bootstrap.yml优先级高于application.yml

配置

server:
  port: 3355

spring:
  application:
    name: config-client
  cloud:
    config:
      label: master  #分支名称
      name: config  #配置文件名称
      profile: dev  #读取后缀名称   上述三个综合http://localhost:3344/master/config-dev.yml
      uri: http://localhost:3344  #配置中心的地址

eureka:
  client:
    register-with-eureka: true
    service-url:
      defaultZone: http://localhost:7001/eureka
  • 主启动层

    @RefreshScope
    @EnableEurekaClient
    @SpringBootApplication
    public class ConfigCenterMain3355 {
        public static void main(String[] args) {
            SpringApplication.run(ConfigCenterMain3355.class,args);
        }
    }
    
  • 业务层

    @RestController
    public class ConfigClientController {
        @Value("${config.info}")
        private String configInfo;  //要访问的3344上的信息
    
        @GetMapping("/configInfo")	//请求地址
        public String getConfigInfo(){
            return configInfo;
        }
    }
    
  • 测试

    修改config-dev.yml配置文件提交到GitHub中,比如加个age或者版本号version
    在这里插入图片描述

结果

这里我们返回从3344主配置中心获取的config.info配置值
在这里插入图片描述

4、Config客户端手动动态刷新

避免每次更新配置都要重启客户端微服务3355

  • 修改3355模块

  • POM引入actuator监控

  • 修改YML,暴露监控接口

    management:
      endpoints:
        web:
          exposure:
            include: "*"
    
  • @RefreshScope业务类Controller修改

    @RefreshScope
    @RestController
    public class ConfigClientController {
        @Value("${config.info}")
        private String configInfo;  //要访问的3344上的信息
        @GetMapping("/configInfo")	//请求地址
        public String getConfigInfo(){
            return configInfo;
        }
    }
    
  • 此时修改GitHub–>3344–>3355

    http://localhost:3355/configInfo

    3355没有改变

  • 解决

    必须是POST请求

    curl -X POST “http://localhost:3355/actuator/refresh”

在这里插入图片描述
获取最新结果:

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值