• 7_spring-cloud-config-center



    配置中心

    • spirng-cloud-config + github 搭建;
    • 服务比较多, 下发配置文件比较麻烦;
    • 服务启动时, 从公共服务拉取配置信息; 这个公共服务就是配置中心
    • 什么样的数据需要放配置中心:
      • 个性化的配置, 这种情况可以放本地, 或者命令行传入;
    • 3个角色:配置存储方-github/gitlab、配置读取方-configServer、配置使用方-普通的微服务节点,从configServer获取配置;

    使用

    • 也需要一个独立的服务;

    • 依赖:

    <dependency>
        <groupId>org.springframework.cloudgroupId>
        <artifactId>spring-cloud-config-serverartifactId>
    dependency>
    <dependency>
        <groupId>org.springframework.cloudgroupId>
        <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 完整pom:
    
    <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">
        <modelVersion>4.0.0modelVersion>
        <parent>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-parentartifactId>
            <version>2.2.6.RELEASEversion>
            <relativePath/> 
        parent>
    
        <groupId>com.go.cngroupId>
        <artifactId>config-centerartifactId>
        <version>0.0.1version>
    
        <properties>
            <java.version>1.8java.version>
            <spring-cloud.version>Hoxton.SR3spring-cloud.version>
        properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-config-serverartifactId>
            dependency>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
            dependency>
        dependencies>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloudgroupId>
                    <artifactId>spring-cloud-dependenciesartifactId>
                    <version>${spring-cloud.version}version>
                    <type>pomtype>
                    <scope>importscope>
                dependency>
            dependencies>
        dependencyManagement>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-maven-pluginartifactId>
                plugin>
            plugins>
        build>
    
    project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • @EnableConfigServer
    • 改配置文件:其实是新项目的完整配置:
    server:
      port: 9000
    
    eureka:
      client:
        service-url:
          defaultZone: http://euk1.com:7000/eureka/
    spring:
      application:
        name: config-server
      cloud:
        config:
          label: master
          server:
            git:
              uri: https://github.com/xxx/config-center.git
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 测试地址:http://localhost:9000/master/config-client-dev.properties

    匹配规则

    获取配置规则:根据前缀匹配
    /{name}-{profiles}.properties
    /{name}-{profiles}.yml
    /{name}-{profiles}.json
    /{label}/{name}-{profiles}.yml
    
    ---
    lable:仓库分支、默认master分支
    name:服务名称
    profile:环境名称,开发、测试、生产:dev qa prd
    
    ---
    匹配原则:从前缀开始。
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    具体节点从配置中心读取配置

    • 此处使用consumer 节点;配置文件名称要从 application.properties/application.yml 修改为 bootstrap.properties/bootstrap.yml
    • 需要注意 consumer 要在 ConfigCenter 启动之后再启动。bootstrap.yml 和 application.yml 是可以共存的。
    • 引入依赖:
    <dependency>
        <groupId>org.springframework.cloudgroupId>
        <artifactId>spring-cloud-config-clientartifactId>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • bootstrap.yml 配置文件:
    spring:
      cloud:
        config:
          label: dev
          profile: dev
          uri: http://localhost:9000/
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • controller:
     @Value("${config.info}")
        String info;
    
    @GetMapping("/configInfo")
    public Object configInfo() {
        return info;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • curl -Lv http://localhost:90/configInfo

    刷新配置

    单个刷新

    • 修改配置文件后热生效/加载:
      • Controller 上加: @RefreshScope
      • 加 actuator,开启actuator中的refresh端点
      • 开放/暴露端点;
    • 手动更新一个服务: post 请求 http://ip:port/actuator/refresh;

    批量刷新

    • 批量更新: 使用消息总线 bus, 与注册中心的区别在于, bus 是主动推消息的, 而注册中心的数据需要拉取;

    bus 使用

    • 需要一个mq: spring-cloud默认支持 rabbitmq, kafka;
    • 安装 rabbitmq:
      • rabbitmq 注意 rabbitmq-plugins enable rabbitmq_management;
      • 默认用户: guest/guest;
      • 具体安装步骤参考:https://blog.csdn.net/wwq921220/article/details/126923410
    • 添加 amqp 依赖,config-center 和 consumer 都要进行添加:
    <dependency>
        <groupId>org.springframework.cloudgroupId>
        <artifactId>spring-cloud-starter-bus-amqpartifactId>
    dependency>
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-actuatorartifactId>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 配置文件调整:consumer 配置在 bootstrap.yml 中:
    # 这一段consumer 和 config-center 都需要配置
    spring:
      rabbitmq:
        host: 192.168.1.9
        username: admin
        password: 123456
        port: 5672
    
      # 这一段只有 consumer 进行配置
      cloud:
        config:
          discovery:
            enabled: true
            service-id: config-server
          label: dev
          profile: dev
    
    #  config-center 没有配置中心的配置时,需要这一段
    eureka:
      client:
        service-url:
          defaultZone: http://euk1.com:7000/eureka/
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 某个服务集群批量更新,向服务任一端点发送post请求: curl -X POST http://ip:port/actuator/bus-refresh
    • 所有服务集群批量更新: 将mq/amqp/actuator打开端点 那一套配置到 config-center 上, curl -X POST http://config_center_ip:port/actuator/bus-refresh
    • config-center 暴露端点:
    management:
      endpoints:
        web:
          exposure:
            include: '*'
    
    • 1
    • 2
    • 3
    • 4
    • 5

  • 相关阅读:
    07 目标检测-YOLO的基本原理详解
    第2-3-4章 上传附件的接口开发-文件存储服务系统-nginx/fastDFS/minio/阿里云oss/七牛云oss
    数据同步工具—sqoop 2.x
    QT5.15使用VISA接口连接GPIB设备和USB设备
    第一章 行列式
    电子统计台账:中文标记月度流水账格式数据的转换,以及过滤模板的普遍适配性
    netsh int ipv4 show dynamicport tcp动态端口port设置
    HQS-C++构造函数
    【系统分析师之路】第六章 复盘需求工程(案例论文)
    弱监督学习
  • 原文地址:https://blog.csdn.net/wwq921220/article/details/127146256