• 服务的动态配置-Config


    1 引言
    • 配置文件分散在不同的项目中,不方便维护。
    • 配置文件的安全问题。
    • 修改完配置文件,无法立即生效。

    2 搭建Config-Server

    创建Maven工程,修改为SpringBoot
    导入依赖

    >
        >org.springframework.cloud>
        >spring-cloud-config-server>
    >
    
    >
        >org.springframework.cloud>
        >spring-cloud-starter-netflix-eureka-client>
    >
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    添加注解 在启动类上

    @EnableConfigServer
    
    • 1

    编写配置文件(Git的操作)

    spring:
      cloud:
        config:
          server:
            git:
              basedir: D:\basedir    # 本地仓库的地址
              username: zjw_2301211@126.com    #  远程仓库用户名
              password: z123123   #   远程仓库密码
              uri: https://gitee.com/zhengdaxian/config-resp.git       # 远程仓库地址
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    http://localhost:8099/master/bootstrap-xxs.yml

    测试(http://localhost:port/{label}/{application}-{profile}.yml)

    label 就是你远程仓库分支名称
    application 文件名称
    “-” 中横线 必须有
    profile 必须有:如果没有profile后缀 也必须随便写一个,如果有就拉取你自己的profile(dev、test、pre、pro)

    bootstrap.yml 优先级别最高,如果要采用我们的config,配置文件必须采用bootstrap.yml

    3 搭建Config-Client
    >
        >org.springframework.cloud>
        >spring-cloud-config-client>
    >
    
    • 1
    • 2
    • 3
    • 4

    编写配置文件

    # 指定Eureka服务地址
    eureka:
      client:
        service-url:
          defaultZone: http://root:root@localhost:8761/eureka,http://root:root@localhost:8762/eureka
    
    #指定服务的名称
    spring:
      application:
        name: CUSTOMER-${version}
      cloud:
        config:  
          discovery:
            enabled: true  #开启远程配置
            service-id: CONFIG  #访问config服务器
          profile: dev  #远程配置文件的后缀
    
    version: v1
    
    
    # CONFIG -> CUSTOMER-v1-dev.yml
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    4 实现动态配置

    4.1 实现原理

    4.2 服务连接RabbitMQ

    导入依赖

    >
        >org.springframework.cloud>
        >spring-cloud-starter-bus-amqp>
    >
    
    • 1
    • 2
    • 3
    • 4

    编写配置文件连接RabbitMQ信息

    spring:
      rabbitmq:
        host: 192.168.199.109
        port: 5672
        username: test
        password: test
        virtual-host: /test
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    4.3 实现手动刷新

    导入依赖

    >
        >org.springframework.boot>
        >spring-boot-starter-actuator>
    >
    
    • 1
    • 2
    • 3
    • 4
    management:
      endpoints:
        web:
          exposure:
            include: "*"
    
    • 1
    • 2
    • 3
    • 4
    • 5

    为customer添加一个controller

    @RestController
    @RefreshScope    //刷新
    public class CustomerController {
    
        @Value("${env}")
        private String env;
    
        @GetMapping("/env")
        public String env(){
            return env;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    测试

    1. CONFIG在Gitee修改之后,自动拉取最新的配置信息。
      2. 其他模块需要更新的话,手动发送一个请求http://ip:port/actuator/bus-refresh,不重启项目,即可获取最新的配置信息
    4.4 内网穿透

    内网穿透的官网https://natapp.cn/
    注册登录
    购买一个免费的隧道。

    下载客户端,并复制config.ini文件,在文件中指定authtoken

    启动exe文件,并测试使用域名访问config接口

    4.5 实现自动刷新

    配置Gitee中的WebHooks

    给Config添加一个过滤器
    直接去代码中找到Filter

  • 相关阅读:
    k8s核心组件
    android studio项目实例-基于Uniapp+Springboot实现的患者服药提醒APP
    原码 反码 补码 移码
    OSS功能详解
    K8S云计算系列-(3)
    linux网络协议栈源码分析 - 传输层(TCP连接的终止)
    Python:实现bitonic sort双调排序算法(附完整源码)
    【Java】泛型 之 泛型和反射
    记一次因为C#官方扩展导致自动补全出错的情况 (C# & Godot)
    某车联网App 通讯协议加密分析
  • 原文地址:https://blog.csdn.net/yc_Cabbage/article/details/126395017