SpringBoot2.0x+SpringCloud Bus+RabbitMQ实现实时刷新配置

实现环境:springboot2.2.5,springcloud Finchley.SR1,RabbitMQ 3.7.5,Erlang20.3,码云Gitee

此例已经搭建好Eureka服务端

一、搭建服务端config-server

pom.xml如下:

        
            org.springframework.cloud
            spring-cloud-config-server
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
            org.springframework.cloud
            spring-cloud-starter-bus-amqp
        

        
            org.springframework.cloud
            spring-cloud-bus
        

        
            org.springframework.cloud
            spring-cloud-starter-stream-rabbit
        
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        

    

实现需要的是:spring-cloud-config-server,spring-cloud-starter-bus-amqp,spring-cloud-bus,spring-cloud-starter-stream-rabbit,spring-boot-starter-actuator 5个依赖

application.properties配置:

#访问端口
server.port=8086

#配置文件地址
spring.cloud.config.server.git.uri= 你的配置文件url
spring.cloud.config.server.git.username=******
spring.cloud.config.server.git.password=******
spring.cloud.config.server.git.search-paths= /**

#指定分支
spring.cloud.config.label=master

#spring cloud bus 刷新配置
spring.cloud.bus.trace.enabled=true
#eureka
eureka.client.service-url.defaultZone=http://192.168.13.118:8081/eureka
eureka.client.fetch-registry=true
eureka.client.register-with-eureka=true
# Rabbitmq配置
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=admin
#暴露刷新接口
management.endpoints.web.exposure.include=bus-refresh
启动类:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

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

}

先启动Eureka服务,以及RabbitMQ服务端然后启动项目,访问http://localhost:8086/+你的文件名,如:http://localhost:8086/order-dev.properties可的到内容

二、客户端

pom.xml

        
                org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
        
        
            org.springframework.cloud
            spring-cloud-starter-bus-amqp
        
        
            org.springframework.cloud
            spring-cloud-bus
        

        
            org.springframework.cloud
            spring-cloud-starter-stream-rabbit
        
        
            org.springframework.cloud
            spring-cloud-starter-config
        

实现需要的是:spring-cloud-starter-bus-amqp,spring-cloud-bus,spring-cloud-starter-stream-rabbit,spring-boot-starter-actuator 4个依赖

bootstrap.properties配置:

#分支名
spring.cloud.config.label=master
#环境
spring.cloud.config.profile=dev
#config-server的路径
spring.cloud.config.uri=http://localhost:8086
#暴露刷新接口
management.endpoints.web.exposure.include= bus-refresh
启动类:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient//客户端
public class EoApplication {

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

}
定义一个Controller 如下:
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
@RefreshScope//刷新配置注解
public class OrderController {
    @Value("${env}")//码云上定义的
    private String env;
    @RequestMapping("/getEvn")
    public String getEvn(){
        return env;
    }
}
Gitee

SpringBoot2.0x+SpringCloud Bus+RabbitMQ实现实时刷新配置_第1张图片

在git上添加webHooks,域名可以上https://natapp.cn自己配置一个

当你以为能成功时git报出了错误

SpringBoot2.0x+SpringCloud Bus+RabbitMQ实现实时刷新配置_第2张图片

spring boot因为无法正常反序列化这串载荷而报了400错误

在服务端config-server中

1、自定义的wrapper类

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;

public class CustometRequestWrapper extends HttpServletRequestWrapper {

    public CustometRequestWrapper(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();
            }


        };
    }
}

2、FilterConfig

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class FilterConfig {
    @Bean
    public FilterRegistrationBean filterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new BusRefreshFilter());
        List urlList = new ArrayList();
        urlList.add("/*");
        registration.setUrlPatterns(urlList);
        registration.setName("BusRefreshFilter");
        registration.setOrder(1);
        return registration;
    }
}

3、BusRefreshFilter

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

public class BusRefreshFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest)request;
        String url = new String(httpServletRequest.getRequestURI());
        //只过滤/actuator/bus-refresh请求
        if (!url.endsWith("/bus-refresh")) {
            chain.doFilter(request, response);
            return;
        }
        //使用HttpServletRequest包装原始请求达到修改post请求中body内容的目的
        CustometRequestWrapper requestWrapper = new CustometRequestWrapper(httpServletRequest);
        chain.doFilter(requestWrapper, response);
    }
}

此时重新启动服务端。然后再修改配置文件就可以刷新配置了

修改完配置信息后RabbitMQ会出现消息对列

SpringBoot2.0x+SpringCloud Bus+RabbitMQ实现实时刷新配置_第3张图片

你可能感兴趣的:(git,spring,boot)