梦想还是要有的,万一忘了咋办?

0%

spring-cloud-bus

目录:

  • 介绍
  • 使用
  • 主意

介绍

  • 将轻量级消息代理程序链接到分布式系统的节点
  • 基于消息中间件(mq、kafka等)

使用

添加依赖

1
2
3
implementation 'org.springframework.cloud:spring-cloud-starter-bus-kafka'
//或者
implementation 'org.springframework.cloud:spring-cloud-starter-bus-amqp'

设置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
spring:
rabbitmq:
host: localhost
port: 9092
username:
password:
bus:
enabled: true
refresh:
enabled: true
trace:
enabled: true
management:
endpoints:
web:
exposure:
include: "*"

EndPoint

  • /actuator/busrefresh
    清除RefreshScope缓存和重新绑定 @ConfigurationProperties;
    1
    2
    3
    - /actuator/busenv  
     接收POST的{"name": "key1","value": "value1"},来更新属性; 
    ```management.endpoints.web.exposure.include=busenv

自定义事件

  • 继承 RemoteApplicationEvent
  • 包路径 org.springframework.cloud.bus.event
    或者,通过@RemoteApplicationEventScan指定扫描包路径

属性

名称 默认值 描述
spring.cloud.bus.ack.destination-service - 监听哪些服务、默认(null)代表全部服务
spring.cloud.bus.ack.enabled true ack开关、默认打开
spring.cloud.bus.content-type bus总线类型
spring.cloud.bus.destination 消息的Spring Cloud Stream目标名称
spring.cloud.bus.enabled true 指示总线是否启用的标志
spring.cloud.bus.env.enabled true 标记以关闭环境更改事件(默认为打开)
spring.cloud.bus.id application 该应用程序实例的标识符
spring.cloud.bus.refresh.enabled true 标记以关闭刷新事件(默认为打开)
spring.cloud.bus.trace.enabled false 标记以打开跟踪(默认关闭)

注意

添加security后,/actuator/busrefresh 返回403、401等

解决:
版本问题,不支持httpBasic了,通过自定义WebSecurityConfig

1
2
3
4
5
6
7
8
9
10
11
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
// Configure HttpSecurity as needed (e.g. enable http basic). http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER);
http.csrf().disable();
//注意:为了可以使用 http://${user}:${password}@${host}:${port}/eureka/ 这种方式登录,所以必须是httpBasic,
// 如果是form方式,不能使用url格式登录 http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
}
}

EndPoint路径历史

  • /busrefresh
  • /actuator/bus-refresh
  • /actuator/busrefresh 当前路径