Chapter 13 Detailed Explanation of Spring Cloud Config Unified Configuration Center-Client Dynamic Refresh

Table of contents

1. Problem analysis and solution

1. Problem analysis:

2. Solutions

2. Manual refresh

1. Add service monitoring

2. Expose the service endpoint so that the client can feel the configuration update.

3. Refresh the business class controller

4. Manual refresh

 3. Automatic refresh


1. Problem analysis and solution

1. Problem analysis:

       In the previous chapter, we talked about the unified management and configuration information of remote storage. The client can obtain the configuration information through the config server server of the unified configuration service center. Now let's make a change and analyze it.
       First start the registration center, the unified configuration center configserver server, and the order service. Browser access address: http://localhost:9000/order/getConfig to view the effect.


       Then change the information of the remote storage order service dev environment, and add version=01 to the info.
Browser access address: http://localhost:9000/order/getConfig to view the effect


       Restart the order service, and visit the address http://localhost:9000/order/getConfig in the browser to view the effect

       We have seen that the configuration information of the remote storage has changed. If the order service is not restarted, the configuration information of the remote storage cannot be refreshed.

2. Solutions

       Use dynamic refresh, dynamic refresh is divided into two forms, one is manual refresh, one is automatic refresh.

2. Manual refresh

1. Add service monitoring

       Add the service monitoring dependency spring-boot-starter-actuator to the pom file, and modify the pom as follows:

<?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>springcloudbase</artifactId>
        <groupId>com.hwadee.springcloud2022</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.hwadee.springcloud</groupId>
    <artifactId>orderServer9000</artifactId>

    <dependencies>
        <!-- 统一配置服务中心客户端依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <!--动态健康监控 可以用于动态感知配置变化-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        

        <!--web依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>

        </dependency>
        <!-- 管理公共api -->
        <dependency>
            <groupId>com.hwadee.springcloud</groupId>
            <artifactId>springcloud-api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <!--Eureka Client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!-- 方便创建类的gettter setter -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

</project>

2. Expose the service endpoint so that the client can feel the configuration update.

       Expose the service endpoint so that the client can feel the configuration update, and modify it in the bootstrap configuration file as follows:

spring:
  cloud:
    config:
      label: master # 指定分支
      name: order # 指定应用名称
      profile: dev # 指定激活环境
      uri: http://localhost:7009 #硬编码 指定访问 config server 远程仓储的地址的ip和端口
      
#暴露服务端点,让客户端能感受到配置的更新
management:
  endpoints:
    web:
      exposure:
        include: "*"

3. Refresh the business class controller

       Refresh the business class controller, use the annotation @RefreshScope on the controller of the order service, and have the ability to refresh after use. @Refreshscope is used to refresh the information in the current scope domain to the latest configuration information without restarting the emblem service. Order service The controller code is modified as follows:

import com.hwadee.springcloud.entity.Product;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/order")
@RefreshScope
public class OrderController {

    @Value("${env}")
    private String env;
    @Value("${port}")
    private String port;
    @Value("${info}")
    private String info;

    @RequestMapping(value = "/getConfig")
    public Product getConfigInfo() {
        Product product = new Product();
        product.setName(env +"环境 端口:"+ port +" "+ info);
        return product;
    }

}

4. Manual refresh

       Manual refresh in the cmd window must be a post request with a fixed address: curl -X POST "http://localhost:9000/actuator/refresh".

      Visit again: http://localhost:9000/order/getConfig

 3. Automatic refresh

       Problem: If each microservice needs to load the latest configuration information, it must manually send a post request to each microservice to load the latest configuration information. Because microservices are generally clustered, this method is inconvenient. We can use a component Bus to achieve automatic refresh. The Bus bus supports RubbitMQ and kafak message brokers.

What is a bus
       In a microservice architecture system, a lightweight message broker is usually used to build a common message topic and connect all microservice instances in the system. Since the messages generated in this topic will be monitored and consumed by all instances, it is called a message bus. Each instance on the bus can easily broadcast some messages that need to be known to other instances connected to the topic.

Basic principle
       ConfigClient instances all listen to the same topic in MQ (default is springCloudBus). When a service refreshes data, it will put this information into the Topic, so that other services listening to the same Topic can be notified, and then update their own configuration.

To be updated later.

Chapter 12: Detailed Explanation of Spring Cloud Config Unified Configuration Center

Chapter Fourteen: Detailed Explanation of Spring Cloud Bus Message Bus

Guess you like

Origin blog.csdn.net/qq_41946216/article/details/127461068