欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

springcloudconfig和springcloudBus消息总线

时间:2023-04-27

首先下载rabbitMQ
config是配置中心,多个配置中心用bus全局广播(通知其它的配置中心该配置文件已更新)



这里使用第二种,利用消息总线触发一个服务端
3344是服务端,
yml配置文件:

server: port: 3344spring: application: name: cloud-config-center #注册进Eureka服务器的微服务名 cloud: config: server: git: uri: https://gitee.com/lixiaogou/sprincloud-config.git #GitHub上面的git仓库名字 search-paths: #搜索目录 - springcloud-config label: master #读取分支 #启动成功后访问的路径 http://ip:3344/{label}/{application}-{profile}.yml 能访问的配置文件 就表示成功了eureka: client: service-url: defaultZone: http://localhost:7001/eureka##rabbitmq相关配置,暴露bus刷新配置的端点 SpringCloud Bus动态刷新全局广播management: endpoints: #暴露bus刷新配置的端点 web: exposure: include: 'bus-refresh'#rabbitmq相关配置rabbitmq: host: localhost port: 15672 username: guest password: guest

pom.xml

org.springframework.cloud spring-cloud-starter-bus-amqp org.springframework.cloud spring-cloud-config-server

过滤器:

package com.atguigu.springcloud.filter;import javax.servlet.*;import javax.servlet.annotation.WebFilter;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletRequestWrapper;import java.io.ByteArrayInputStream;import java.io.IOException;// Created by Zjt on 2021/1/15 13:53@WebFilter("/*")public class UrlFilter implements Filter { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest; String url = httpServletRequest.getRequestURI(); System.out.println(url); if (!url.endsWith("/actuator/bus-refresh")) { filterChain.doFilter(servletRequest, servletResponse); return; } String body = (httpServletRequest).toString(); System.out.println("original body: " + body); RequestWrapper requestWrapper = new RequestWrapper(httpServletRequest); filterChain.doFilter(requestWrapper, servletResponse); } private 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); ServletInputStream servletInputStream = 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 listener) { } }; return servletInputStream; } }}

启动类:(注解@EnableConfigServer)

@SpringBootApplication@EnableConfigServer@ServletComponentScan("com.atguigu.springcloud.filter")public class CloudConfigCenter3344Application { public static void main(String[] args) { SpringApplication.run(CloudConfigCenter3344Application.class, args); System.out.println("启动成功"); }}

3355端:
yml:

server: port: 3355spring: application: name: config-client cloud: config: label: master #分支名称 name: config #配置文件名称 profile: dev #读取后缀名称 上述三个综合http://localhost:3344/master/config-dev.yml uri: http://localhost:3344 #配置中心的地址#服务注册到eureka地址eureka: client: service-url: #设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址 defaultZone: http://localhost:7001/eureka #单机版# 暴露监控端点 否则 curl -X POST "http://localhost:3355/actuator/refresh" 不可使用management: endpoints: web: exposure: include: "*"#SpringCloud Bus动态刷新定点通知 公式:http://localhost:配置中心的端口号/actuator/bus-refresh/{destination}#例如 只通知3355,curl -X POST "http://localhost:3344/actuator/bus-refresh/config-client:3355"##rabbitmq相关配置,暴露bus刷新配置的端点rabbitmq: host: localhost port: 15672 username: guest password: guest

pom.xml:

org.springframework.cloud spring-cloud-starter-bus-amqp org.springframework.cloud spring-cloud-starter-config org.springframework.cloud spring-cloud-starter-netflix-eureka-client com.atguigu.springcloud cloud-api-commons ${project.version} org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-actuator

启动类:

@SpringBootApplicationpublic class CloudConfigClient3355Application { public static void main(String[] args) { SpringApplication.run(CloudConfigClient3355Application.class, args); System.out.println("启动成功"); }}

controller类:

@RestController@RefreshScopepublic class ConfigController { @Value("${config.info}") private String configInfo; @GetMapping("/configInfo") public String getConfigInfo() { return configInfo; }}

3366和3355一样

执行命令如下图,通知配置文件已更新,全部配置文件都会更新成最新的



Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。