Spring Cloud message bus (BUS) to achieve automatic refresh

Attachment: natapp intranet penetration detailed document

Attachment: github code (updating)

ps: springboot version: 2.1.7.RELEASE, springcloud version: Greenwich.SR2

一.config-server:

 1. Introduce the corresponding jar package:

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    

2. Modify the configuration file: application.yml

server:
  port: 8765

spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/LuciferZK/config-repo
          username: [email protected]
          password: ---------*******
          #basedir: D:\project\morning-star\config-server\config\basedir
  rabbitmq:
    host: 192.168.47.131
    port: 5672
    username: guest
    password: guest

management:
  endpoints:
    web:
      exposure:
        include: bus-refresh

3. Add @EnableConfigServer annotation to the main configuration class and open config

@EnableConfigServer
@SpringBootApplication
public class MorningConfigApplication {

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

}

二:config-client

1. Introduce the corresponding jar package

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

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
         <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

2: Add RefreshScope annotation to the control layer; otherwise, you cannot refresh to see the effect

3. Configuration file: bootstrap.yml

spring:
  application:
    name: morning-service-order
  cloud:
    config:
      uri: http://localhost:8765
      profile: dev
    bus:
      enabled: true
      trace:
        enabled: true
  rabbitmq:
    host: 192.168.47.131
    port: 5672
    username: guest
    password: guest

3. Test with postman: http: // localhost: 8765 / actuator / bus-refresh

There is no problem. If you want to refresh automatically, it is not possible to request a postman every time, so just follow these steps:

Four: operation on github:

(1) Setting

(2) Add

ps: Payload URL This address needs to be an address that can be accessed by the external network, not just the local http: // localhost: 8765 / actuator / bus-refresh , there is no domain name, so I use the intranet penetration tool here .

I only use the free version here. The disadvantage of free is that the mapped external network URL will not be fixed and random ;

For specific operations, see the attached document; double-click the tool to open:

The forwarding address here is the address of the Payload URL filled on github; again, the free version, the mapping address will change unless you do not close the cmd.

After the above operation, use postman test, no problem, but you modify the configuration file on github, when github automatically requests your http: // **** / actuator / bus-refresh , your config-server will An error is reported, the content of the error is that the json parsing failed .

Five: increase the filter filter, and rewrite the getInputStream method

package com.lucifer.config.filter;

import org.springframework.stereotype.Component;

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

/**
 * @author: lucifer
 * @date: 2019/8/27
 * @description:
 */
@Component
//@WebFilter(urlPatterns = "/**", filterName = "BusFilter")
public class BusFilter implements Filter {

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) req;
        String requestURI = httpServletRequest.getRequestURI();
        //过滤掉非/actuator/bus-refresh请求
        if (!requestURI.endsWith("/actuator/bus-refresh")) {
            filterChain.doFilter(req, res);
            return;
        }
        RequestWrapper requestWrapper = new RequestWrapper(httpServletRequest);
        filterChain.doFilter(requestWrapper, res);
    }

    @Override
    public void destroy() {

    }

    @Override
    public void init(FilterConfig arg0) {

    }


}

package com.lucifer.config.filter;

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;

/**
 * @author: lucifer
 * @date: 2019/8/27
 * @description:
 */
public class RequestWrapper extends HttpServletRequestWrapper {

    public RequestWrapper(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 int read() throws IOException {
                return byteArrayInputStream.read();
            }

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

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

            @Override
            public void setReadListener(ReadListener readListener) {
            }
        };
    }
}

 

 
Published 187 original articles · Like 146 · Visit 490,000+

Guess you like

Origin blog.csdn.net/qq_37495786/article/details/100103449