SpringCloud(Hoxton) Config--客户端配置与测试、客户端动态刷新

SpringCloud Config客户端配置

服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,加密/解密信息等访问接口。

客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息服务器默认采用git来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过git客户端工具来方便的管理和访问配置内容。

新建cloud-config-client-3355客户端
依赖:

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <artifactId>cloud2020</artifactId>
        <groupId>pers.zhang.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-config-client-3355</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </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>

</project>

配置:bootstrap.yml

server:
  port: 3355

spring:
  application:
    name: config-client

  cloud:
    config: #Config客户端配置
      label: master #分支名称
      name: config #配置文件名称
      profile: dev #读取后缀
      uri: http://localhost:3344 #配置中心地址



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

Controller

@RestController
public class ConfigClientController {

    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/configInfo")
    public String getConfigInfo() {
        return configInfo;
    }
}

启动类:

@SpringBootApplication
@EnableEurekaClient
public class ConfigClientMain3355 {
    public static void main(String[] args) {
        SpringApplication.run(ConfigClientMain3355.class, args);
    }
}

测试:
启动eureka,3344服务端,3355客户端:

在这里插入图片描述

访问:localhost:3355/configInfo,成功读取到配置信息
在这里插入图片描述

此时,修改config-dev.yml的version=2:
访问3344配置中心:http://localhost:3344/master/config-dev.yml,立即加载到最新的版本
在这里插入图片描述

访问3355客户端:http://localhost:3355/configInfo,还是老版本:
在这里插入图片描述
3355客户端没有变化,除非自己重启或者重新加载。
重启后加载到最新的:
在这里插入图片描述

客户端的动态刷新

修改3355模块,引入actuator图形监控:

		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

暴露监控端点,在bootstrap.yml中增加如下配置:

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

在Controller上添加@RefreshScope注解:
在这里插入图片描述

测试:

启动eureka,3344服务端,3355客户端,修改version=3

访问:http://localhost:3344/master/config-dev.yml

在这里插入图片描述

访问:http://localhost:3355/configInfo
在这里插入图片描述

为什么会失败?因为@RefreshScope注解需要使用一个POST请求刷新3355,才可以加载到最新数据:

在这里插入图片描述

再次访问:成功获取最新配置

在这里插入图片描述

发布了842 篇原创文章 · 获赞 2256 · 访问量 29万+

猜你喜欢

转载自blog.csdn.net/cold___play/article/details/104975330