Automatically refresh the client configuration (Consul, Spring Cloud Config, Spring Cloud Bus) mechanism by bus

Realized through the bus mechanism automatically refresh client configuration

Program Map

Alt text

Git all customers using the services webhook notification function after each configuration update, Git server calls / actuator / bus-refresh interface configuration center distribution center bus service will broadcast this event with the POST method to join the bus terminal the client after receiving the event to read the contents of the new configuration center.

POM-dependent increase

Server configuration center (spring-cloud-config-server) and client (spring-cloud-config-client) have joined the Spring Cloud Bus reference package:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

Start Rabbitmq

docker pull rabbitmq:3-management

docker run -d --hostname my-rabbit --name rabbit -p 5672:5672 -p 15672:15672 rabbitmq:3-management

Access to 127.0.0.1:15672/ login rabbitmq management monitor backstage, username and password are guest / guest.

Modify the configuration information

Server configuration center (spring-cloud-config-server ) and client (spring-cloud-config-client ) needs to modify the configuration file content:
application.properties the Spring-Cloud-config-Server project increases:

# 开启消息跟踪
spring.cloud.bus.trace.enabled=true
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

#显示的暴露接入点
management.endpoints.web.exposure.include=*

application.properties spring-cloud-config-client projects increased:

# 开启消息跟踪
spring.cloud.bus.trace.enabled=true
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

#显示的暴露接入点
management.endpoints.web.exposure.include=*

bootstrap.properties spring-cloud-config-client projects increase (otherwise it will error: A component required a bean named 'configServerRetryInterceptor' that could):

spring.cloud.config.fail-fast=true

Configuration of Webhook Git

Alt text
Alt text
Alt text

192.168.0.21:9004/actuator/bus-refresh I address a distribution center, and if there are multiple configurations centers can write multiple webhook, on page 204 tests if the return that success.

POST request body in the empty Webhook

Git webhood post while making the request so default string plus load (payload), Spring Boot not parallelized body, so the central server configuration (spring-cloud-config-server ) Create the following two classes:
This the reference code: spring_cloud config distribution center and using automated thermal loading configuration Github

import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.ByteArrayInputStream;
import java.io.IOException;

//清空请求中的Body
public class EmptyRequestWrapper extends HttpServletRequestWrapper{

    public EmptyRequestWrapper(HttpServletRequest request) {
        super(request);
    }

    @Override
    public ServletInputStream getInputStream() throws IOException {
        byte[] bytes = new byte[0];
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);

        return new ServletInputStream() {
            @Override
            public boolean isFinished() {
                return byteArrayInputStream.read() == -1 ? true:false;
            }

            @Override
            public boolean isReady() {
                return false;
            }

            @Override
            public void setReadListener(ReadListener readListener) {

            }

            @Override
            public int read() throws IOException {
                return byteArrayInputStream.read();
            }
        };
    }
}
import org.springframework.core.annotation.Order;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@WebFilter(filterName = "bodyFilter", urlPatterns = "/*")
@Order(1)
//Git在进行webhood post请求的同时默认会在body加上这么一串载荷(payload),Spring Boot 无法并行化。
public class BusRefreshFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest)servletRequest;

        String url = new String(httpServletRequest.getRequestURI());

        //只过滤/actuator/bus-refresh请求
        if (!url.endsWith("/bus-refresh")) {
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }

        //使用HttpServletRequest包装原始请求达到修改post请求中body内容的目的
        EmptyRequestWrapper requestWrapper = new EmptyRequestWrapper(httpServletRequest);

        filterChain.doFilter(requestWrapper, servletResponse);
    }

    @Override
    public void destroy() {

    }
}

Finally add @ServletComponentScan notes on startup class

@SpringBootApplication
//启动配置中心
@EnableConfigServer
//启动服务发现
@EnableDiscoveryClient
@ServletComponentScan
public class SpringCloudConfigServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringCloudConfigServerApplication.class, args);
    }

}

Automatic refresh Automatic Test

Access client program 127.0.0.1:9006/ConfigTest, get the current results of Test-8, the center also access the configuration Test-8:
Alt text
Alt text
We will update Git reference configuration would read Test-9:
Alt text
Alt text
view the configuration center 127.0.0.1:9004/ConfigDepot / Test, the content has been changed to Test-9, then refresh the client program 127.0.0.1:9006/ConfigTest, then configure the content has been successfully changed Test-9, bus event notification client refresh configuration is successful.
Alt text
Alt text
From the configuration center server and client logs can also be seen refresh process configuration information:
Alt text
Alt text

Source

Github repository: https: //github.com/sunweisheng/spring-cloud-example

Guess you like

Origin www.cnblogs.com/bluersw/p/11610722.html