SpringCloud学习笔记10——初级篇之分布式配置中心

    技术2025-07-15  9

    八.Config分布式配置中心

    1.知识点

    2.在GitHub创建新的repository

    使用GitHub桌面版创建 ,具体可以参考我的另一篇文章点击这里或者访问链接

    https://blog.csdn.net/qq_41015977/article/details/101213960

    3.构建项目(服务端3344)

    ①创建module

    ②编写pom文件

    <dependencies> <!-- config --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <!--eureka-client--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!--引入自定义的api通用包 可以使用公用的entities--> <dependency> <groupId>com.hry.springcloud</groupId> <artifactId>cloud-api-commons</artifactId> <version>${project.version}</version> </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>

    ③修改yml文件

    server: port: 3344 spring: application: name: cloud-config-center cloud: config: server: git: #GitHub xxx为github用户名 uri: https://github.com/xxx/springcloud-config #搜索目录 search-paths: - springcloud-config #读取分支 default-label: master username: xxx写自己的github用户名 password: xxx写自己的github密码 eureka: client: service-url: defaultZone: http://localhost:7001/eureka

    ④创建主启动类

    package com.hry.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication @EnableConfigServer public class ConfigCenterMain3344 { public static void main(String[] args) { SpringApplication.run(ConfigCenterMain3344.class, args); } }

    ⑤修改hosts文件

    添加

    127.0.0.1 config-3344.com

    然后保存 之前演示过,在初级篇–学习笔记3–5.搭建集群环境–③编写yml文件中

    ⑥Push一个yml

    config: info: master branch,springcloud-config/config-dev.yml version=1

    Commit to master 然后Push 刷新

    ⑦测试

    访问后可以看到我们在yml中写的配置 这里读取的是master分支的config-dev.yml,还可以有多个yml配置读取 此外还可以读取其他分支的yml 只需要修改default-label即可切换要访问的分支。

    4.构建项目(客户端3355)

    ①创建module

    ②编写pom文件

    <dependencies> <!-- config --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <!--eureka-client--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!--引入自定义的api通用包 可以使用公用的entities--> <dependency> <groupId>com.hry.springcloud</groupId> <artifactId>cloud-api-commons</artifactId> <version>${project.version}</version> </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>

    ③修改yml文件

    server: port: 3355 spring: application: name: config-client cloud: config: label: master #分支名称 name: config #配置文件名称 profile: dev #读取后缀名称 三者合一master分支上config-dev.yml的配置文件被读取 uri: http://localhost:3344 #注册到Eureka eureka: client: service-url: defaultZone: http://localhost:7001/eureka

    ④创建主启动类

    package com.hry.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient public class ConfigClientMain3355 { public static void main(String[] args) { SpringApplication.run(ConfigClientMain3355.class, args); } }

    ⑤业务代码

    controller

    package com.hry.springcloud.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class ConfigClientController { /** * 读取github上的yml文件的config.info */ @Value("config.info") private String configInfo; @GetMapping("/configInfo") public String getConfigInfo(){ return configInfo; } }

    ⑥测试

    启动3355 成功实现了客户端3355访问SpringCloud Config3344通过GitHub获取配置信息

    ⑦存在的问题

    现在修改GitHub上的版本号为2,然后提交当前分支 然后刷新3344,3344立即更新 刷新3355,3355还是1没变 虽然可以通过重启3355来解决此问题,但明显不是明智的选择。如果使用集群有几十几百个3355这样的客户端都重启那不就都闲着没事干摸鱼了。

    5.动态刷新

    ①确定3355的pom文件有actuator坐标

    ②修改3355yml文件

    在bootstrap.xml添加配置

    #暴露监控端点 management: endpoints: web: exposure: include: "*"

    ③修改controller

    在controller类上添加注解

    @RefreshScope

    ④测试

    GitHub修改版本号为3并提交 刷新3344正常 而刷新3355没反应啊还是2 此时是因为这个得手动刷新(掀桌(╯‵□′)╯︵┻━┻) 打开cmd使用curl发送个POST

    curl -X POST "http://localhost:3355/actuator/refresh"

    然后刷新浏览器就可以了 难道不能自动刷新吗?当然可以,后面等你。

    Processed: 0.010, SQL: 9