• Config 分布式配置中心


     代码:config-parent_IJ_soft_backup 

     Config 简介:

     

     Spring CloudConfig 解决了在分布式场景下多环境配置文件的管理和维护

    好处:

    1. 集中管理配置文件
    2. 不同环境不同配置,动态化的配置更新
    3. 配置信息改变时不需要重启客户端 即可更新配置信息到服务

    流程

    config server:

    1. 使用创建远程仓库,上传配置文件
    2. 搭建config server模块
    3. 导入config-server依赖
    4. 编写配置,设置gitee远程仓库地址
    5. 测试访问远程配置文件

    config server

    目的: 编写config server功能,使其可以访问到外部配置文件,之后编写config client通过configserver访问外部文件

    使用创建远程仓库,上传配置文件

    在gitee上创建仓库

    学这个单分支简单 

     

     创建文件夹git-repository克隆远程仓库

    期望把远程仓库克隆到本地,做一些文件添加,再添加到远程仓库

    这里使用到git小乌龟使用 汉化及安装方法

    git小乌龟下载及汉化

     使用参考https://www.cnblogs.com/xuanwotianming153/p/8504762.html

     在gitrepository右键选择clone,

     在本地添加配置文件上传

     添加config-dev.yml配置文件

     

     

     搭建consumer provider和server

    consumer provider参考下面文章的consumer provider

    spirngcloud Eureka服务治理_参考一二章节

     修改maven仓库和编码UTF-8

    sever搭建:

    pom.xml 

    1. <dependencies>
    2. <dependency>
    3. <groupId>org.springframework.cloudgroupId>
    4. <artifactId>spring-cloud-config-serverartifactId>
    5. dependency>
    6. dependencies>

     configServerApp:

    1. package com.gao.config;
    2. import org.springframework.boot.SpringApplication;
    3. import org.springframework.boot.autoconfigure.SpringBootApplication;
    4. import org.springframework.cloud.config.server.EnableConfigServer;
    5. @SpringBootApplication
    6. //启用 EurekaServer
    7. @EnableConfigServer
    8. public class ConfigServerApp {
    9. public static void main(String[] args){
    10. SpringApplication.run(ConfigServerApp.class,args);
    11. }
    12. }

    application.yml

    1. server:
    2. port: 9527
    3. spring:
    4. application:
    5. name: config-provider
    6. cloud:
    7. config:
    8. server:
    9. git:
    10. uri: https://gitee.com/GXQ205153964/springcloud-configs.git #远程仓库的地址
    11. label: master

    测试访问远程配置文件:

    localhost:9527/master/config-dev.yml

    config client

    1. 导入starter-config依赖
    2. 配置config server 地址,读取配置文件名称信息 
    3. 获取配置值
    4. 启动测试 

     config client:

    目的:   完成config client 并测试client是否可以通过server访问到gitee上的外部文件

     

    pom.xml

    1. <dependencies>
    2. <dependency>
    3. <groupId>org.springframework.cloudgroupId>
    4. <artifactId>spring-cloud-config-serverartifactId>
    5. dependency>
    6. dependencies>

     ConfigServerApp;

    1. @SpringBootApplication
    2. //启用 EurekaServer
    3. @EnableConfigServer
    4. public class ConfigServerApp {
    5. public static void main(String[] args){
    6. SpringApplication.run(ConfigServerApp.class,args);
    7. }
    8. }

     application.yml

    1. server:
    2. port: 9527
    3. spring:
    4. application:
    5. name: config-provider
    6. cloud:
    7. config:
    8. server:
    9. git:
    10. uri: https://gitee.com/GXQ205153964/springcloud-configs.git #远程仓库的地址
    11. label: master

     

     pom.xml

    1. <dependencies>
    2. <dependency>
    3. <groupId>org.springframework.bootgroupId>
    4. <artifactId>spring-boot-starter-webartifactId>
    5. dependency>
    6. <dependency>
    7. <groupId>org.springframework.cloudgroupId>
    8. <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
    9. dependency>
    10. <dependency>
    11. <groupId>org.springframework.cloudgroupId>
    12. <artifactId>spring-cloud-starter-netflix-hystrixartifactId>
    13. dependency>
    14. <dependency>
    15. <groupId>org.springframework.cloudgroupId>
    16. <artifactId>spring-cloud-starter-configartifactId>
    17. dependency>
    18. dependencies>

     GoodsController

    1. //注入 从gitee上的配置文件config-dev.yml获取键值对
    2. @Value("${gao}")
    3. private String key1;
    4. //结果显示到web
    5. goods.setTitle(goods.getTitle()+":" + key1);

    application.yml

    1. server:
    2. port: 8000
    3. eureka:
    4. client:
    5. service-url:
    6. defaultZone: http://localhost:8761/eureka # 注册
    7. spring:
    8. application:
    9. name: config-provider #设置当前应用的名称。将来会在eureka中application显示,将来需要使用该名称来获取路径

     bootstrap.yml

    1. #bootstrap优先级更高
    2. #配置config-server地址
    3. #配置获取配置文件的名称等信息
    4. spring:
    5. cloud:
    6. config:
    7. #配置config-server地址
    8. uri: http://localhost:9527
    9. name: config #文件名
    10. profile: dev #开发环境
    11. label: master #分支

    开启config-provider和config-server服务 

    当外部配置文件修改后, config-server是可以刷新更新数据,但config-server不能

    Config 客户端刷新

    目的:   client可以通过server访问到外部文件后,当外部文件修改后client需要重启才能获取到新的外部文件,非常不方便,这里配置后可以通过刷新获取到新的外部文件

    流程:

    1. 在config客户端引入actuator依赖
    2. 获取配置信息类上,添加@RefreshScope注解
    3. 添加配置  management.endpoints.web.exposure.include:refresh
    4. 使用curl工具发送post请求curl -X POST http://localhost:8001/actuator/refresh

     在config客户端引入actuator依赖

    在bootstrap.yml中

    1. <dependencies>
    2. <dependency>
    3. <groupId>org.springframework.bootgroupId>
    4. <artifactId>spring-boot-starter-webartifactId>
    5. dependency>
    6. <dependency>
    7. <groupId>org.springframework.bootgroupId>
    8. <artifactId>spring-boot-starter-actuatorartifactId>
    9. dependency>
    10. <dependency>
    11. <groupId>org.springframework.cloudgroupId>
    12. <artifactId>spring-cloud-starter-netflix-hystrixartifactId>
    13. dependency>
    14. <dependency>
    15. <groupId>org.springframework.cloudgroupId>
    16. <artifactId>spring-cloud-starter-configartifactId>
    17. dependency>
    18. dependencies>

     

     获取配置信息类上,添加@RefreshScope注解

    在哪个地方获取外部配置文件就在哪个地方加

     

     添加配置

    bootstrap.yml

    1. #暴露端点
    2. management:
    3. endpoints:
    4. web:
    5. exposure:
    6. include: 'refresh' # 或者 * 也可以代替,表示全部暴露不安全

     使用curl工具发送post请求

    在启动config-server和config-provider后,cmd发送这个post请求

     

     测试:

    1.  启动config-server和config-provider
    2. 修改外置文件
    3. 发现config-provider无法通过刷新获取到更改后的配置文件
    4. 发送post请求  
    5.   刷新config-provider,获取到最新配置文件 

     Config 集成 Eureka

    目的:  当config server地址发生变化后ABC所存的固定地址都需要修改,非常麻烦,这里加入Eureka服务注册中心,让ABC客户端通过Eureka去获取config server的地址,当server地址发生变化后只需要修改Eureka的注册地址即可非常方便。

    原来:

    现在:

    当config server地址发生改变时ABC内的uri地址都需要修改,加入Eureka后,动态获取Config地址,ConfigServer地址发生改变,只需要在注册中心修改一下即可。eureka会自动分发给ABC最新地址

     修改config-Server

    pom.xml添加坐标

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

    application.xml添加eureka配置  

    1. eureka:
    2. client:
    3. service-url:
    4. defaultZone: http://localhost:8761/eureka # 注册

    启动类上添加注解,标注自己 是Eureka的客户端

    @EnableEurekaClient

    修改config-provider

    bootstrap.yml

    1. #bootstrap优先级更高
    2. #配置config-server地址
    3. #配置获取配置文件的名称等信息
    4. spring:
    5. cloud:
    6. config:
    7. #配置config-server地址
    8. #uri: http://localhost:9527
    9. name: config #文件名
    10. profile: dev #开发环境
    11. label: master #分支
    12. #从注册中心去寻找config-server地址
    13. discovery:
    14. enabled: true
    15. serice-id: CONFIG-SERVER
    16. #暴露端点
    17. management:
    18. endpoints:
    19. web:
    20. exposure:
    21. include: refresh # 或者 * 也可以代替,表示全部暴露不安全

    新建Eureka服务模块eureka-server-config

     代码 配置和依赖:

    1. //启动类
    2. @SpringBootApplication
    3. //启用 EurekaServer
    4. @EnableEurekaServer
    5. public class EurekaApp {
    6. public static void main(String[] args){
    7. SpringApplication.run(EurekaApp.class,args);
    8. }
    9. }
    10. //application.yml
    11. server:
    12. port: 8761
    13. # 默认8761
    14. eureka:
    15. instance:
    16. hostname: localhost
    17. client:
    18. service-url:
    19. defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka #eureka服务端地址,将客户端使用该地址和eureka
    20. register-with-eureka: false #是否将自己的路径注册到eureka上,eureka server不需要,eureak client需要
    21. fetch-registry: false #是否需要从eureka中抓取路径 eureka server不需要,eureak consumer client需要
    22. //pom.xml
    23. <dependencies>
    24. <dependency>
    25. <groupId>org.springframework.bootgroupId>
    26. <artifactId>spring-boot-starter-webartifactId>
    27. dependency>
    28. <dependency>
    29. <groupId>org.springframework.cloudgroupId>
    30. <artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
    31. dependency>
    32. dependencies>

    结果测试:

    1.  启动后Eureka web注册到config-server和config-provider
    2.  修改外部配置文件
    3.  cmd发送post请求
    4.  config-provider得到修改后的数据

    当客户端多的时候需要一个一个的发送post请求才能让各个客户端收到最先的外部配置文件。不方便,需要引入bus消息总线。一次post请求所有客户端更新。

  • 相关阅读:
    双飞翼布局
    解决尚医通com.aliyun.oss 和com.aliyun 爆红
    年产200万件的超级工厂投产!巨头「闭环」汽车电子全产业链
    vue2源码分析-响应式原理
    stm32cubemx:systick系统定时器中断与TIM定时器中断的配置及使用方法
    golang单元测试:testing包的基本使用
    为什么程序员都喜欢节后跳槽?内行人告诉你原因
    极客日报:罗永浩吐槽苹果新品:更丑更贵更胡来;苹果售卖145元擦屏布;MySQL 8.0.27 GA版本发布
    Thinking for Doing:让LLMs能推断他人心理状态来做出适当的行动。
    【数据加密、解密】前后端数据传输的过程中,如何进行数据加密传输,保证数据的传输安全,防止被他人窃取
  • 原文地址:https://blog.csdn.net/qq_51497041/article/details/126205872