• Spring Cloud Config(分布式配置中心)


    一、简介

    Spring Cloud Config为分布式系统中的配置提供服务器端和客户端支持。可以集中管理所有环境中应用程序的配置文件。其服务器端存储的默认实现使用GIT。  

    优势

    • 提供服务端和客户端支持(spring cloud config server和spring cloud config client)

    • 集中式管理分布式环境中的配置信息(所有配置文件统一放在了GIT仓库中)

    • 基于Spring环境提供配置管理,与Spring系列框架无缝结合

    • 可用于任何语言开发环境,基于Http协议

    • 默认基于GIT仓库实现版本控制。

     二、使用

    1.搭建配置文件仓库

    2.搭建Eureka-Server注册中心服务器

    3.搭建Config-Server分布式配置中心服务器

    (1)导入依赖


        org.springframework.boot
        spring-boot-starter-parent
        2.3.12.RELEASE


       
           
                org.springframework.cloud
                spring-cloud-dependencies
                Hoxton.SR12
                pom
                import
           

       


       
       
            org.springframework.cloud
            spring-cloud-config-server
       

       
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
       

    (2)编写配置文件

    server:
      port: 8888

    # 增加分布式配置中心服务端配置。连接什么GIT仓库
    spring:
      application:
        name: config-server
      cloud: # spring cloud常用配置前置
        config: # 分布式配置中心配置前置
          server: # 服务端配置
            git: # git文件仓库配置
              uri: https://gitee.com/bjsxt_test/config.git # git仓库具体地址
              #username: bjsxt_test # 私有仓库必须配置用户名和密码。
              #password: 123456789 # 公开仓库可以省略用户名和密码配置。

    eureka:
      client:
        service-url:
          defaultZone: http://localhost:8761/eureka/

    (3)编写启动类

    1. /**
    2. * EnableConfigServer - 开启Spring Cloud Config Server的注解。
    3. * 提供分布式配置中心服务端功能。
    4. */
    5. @SpringBootApplication
    6. @EnableConfigServer
    7. public class ConfigServerApp {
    8. public static void main(String[] args) {
    9. SpringApplication.run(ConfigServerApp.class, args);
    10. }
    11. }

    4.搭建Config Client

    Config Client对于Spring Cloud Config是客户端,对于Eureka来说可以是Application Server 也可以是Application Client。

    (1)导入依赖


       
            org.springframework.boot
            spring-boot-starter-web
       

       
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
       

       
       
            org.springframework.cloud
            spring-cloud-starter-config
        

    (2)编写配置文件

    # 新配置文件 bootstrap.yml | properties。是spring cloud config技术支持的新配置文件。
    # 配置文件由config分布式配置中心客户端读取,并请求分布式配置中心服务端,查询获取配置文件之后,Spring Boot根据配置文件,初始化环境

    spring:

      application:

        name: Config-Client
      cloud:
        config: # spring cloud config 客户端配置
          uri: http://localhost:8888 # 分布式配置中心服务端地址。 默认http://localhost:8888
          name: bjsxt # 要读取的配置文件名,默认是spring.application.name的配置值,如果没有配置,默认application
          profile: default # 要读取的配置文件环境是什么,默认default
          label: master # 要读取的配置文件所在分支名称。默认null。从主干分支获取文件。

    (3)服务接口

    1. public interface ConfigClientService {
    2. String test();
    3. }

    (4)服务实现

    1. @Service
    2. public class ConfigClientServiceImpl implements ConfigClientService {
    3. @Value("${my.content}")
    4. private String content;
    5. @Override
    6. public String test() {
    7. System.out.println("content = " + content);
    8. return content;
    9. }
    10. }

    (5)编写控制器

    1. @RestController
    2. public class ConfigClientController {
    3. @Autowired
    4. private ConfigClientService configClientService;
    5. @RequestMapping("/test")
    6. public String test(){
    7. return configClientService.test();
    8. }
    9. }

    (6)编写启动类

    1. @SpringBootApplication
    2. public class ConfigClientApplication {
    3. public static void main(String[] args) {
    4. SpringApplication.run(ConfigClientApplication.class,args);
    5. }
    6. }

    三、热刷新

    (1)导入依赖(和优雅关闭的依赖一样)


        org.springframework.boot
        spring-boot-starter-actuator

    (2)修改配置文件

    management:
      endpoints:
        web:
          exposure:
            include: refresh,info,health

    (3)修改服务实现(在类上添加@RefreshScope注解)

    1. @Service
    2. @RefreshScope
    3. public class ConfigClientServiceImpl implements ConfigClientService {
    4. @Value("${my.content}")
    5. private String content;
    6. @Override
    7. public String test() {
    8. System.out.println("content = " + content);
    9. return content;
    10. }
    11. }

    (4)测试热刷新

     http://localhost:8080/actuator/refresh

    四、Spring Cloud Bus(消息总线)

    (1)导入依赖


        org.springframework.boot
        spring-boot-starter-actuator



        org.springframework.cloud
        spring-cloud-starter-bus-amqp

    (2)修改配置文件

    spring:
      rabbitmq:
        host: 192.168.91.128
        username: bjsxt
        password: bjsxt
    management:
      endpoints:
        enabled-by-default: true
        web:
          exposure:
            include: bus-refresh,info,health

    (3)测试

    http://localhost:8080/actuator/bus-refresh

  • 相关阅读:
    ​力扣解法汇总1796. 字符串中第二大的数字
    数商云:东方甄选品控翻车,如何通过供应链建设打造可持续商业模式
    SQLAlchemy删除所有重复的用户|Counter类运用
    Linux操作系统(四):内存管理组件
    python中应对各种机制
    mysql各个版本修改root用户密码
    备战秋招--mybatis篇
    NLP对话系统面试总结,从阿里 百度 滴滴一轮游到最终拿下40万offer
    【数据结构】二叉树的·深度优先遍历(前中后序遍历)and·广度优先(层序遍历)
    flink之Sink to MySQL和Redis
  • 原文地址:https://blog.csdn.net/weixin_53455615/article/details/128175078