spring cloud bus - 自定义事件
基于 cloud bus 3.x
引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-bus</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-rabbit</artifactId>
</dependency>
自定义事件
@Data
public abstract class AbstractAresEvent<T> extends RemoteApplicationEvent {
private T data;
public AbstractAresEvent() {
// for serialization
}
public AbstractAresEvent(Object source, String originService, Destination destination, T data) {
super(source, originService, destination);
this.data = data;
}
}
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonIgnoreProperties({"source"})
public class AresDataChangedEvent extends AbstractAresEvent<UserDto> {
public AresDataChangedEvent() {
}
public AresDataChangedEvent(Object source, String originService,
Destination destination, UserDto data) {
super(source, originService, destination, data);
}
}
自定义事件监听
@Component
public class AesDataChangedListener implements ApplicationListener<AresDataChangedEvent> {
@SneakyThrows
@Override
public void onApplicationEvent(AresDataChangedEvent event) {
ObjectMapper objectMapper = new ObjectMapper();
System.out.println(objectMapper.writeValueAsString(event));
}
}
扫描自定义事件
使用RemoteApplicationEventScan进行扫描,不然通过cloud bus 发布自定义事件会变成 UnknownRemoteApplicationEvent
@SpringBootApplication
@RemoteApplicationEventScan(basePackages = "com.x.z.cloudbus.event")
public class CloudBusSampleApplication {
public static void main(String[] args) {
SpringApplication.run(CloudBusSampleApplication.class, args);
}
}
application.properties配置文件
spring.application.name=cloud-bus
spring.rabbitmq.virtual-host=/
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.password=guest
spring.rabbitmq.username=guest
测试用例
@RestController
public class PublishController {
@Autowired
private BusBridge busBridge;
@Autowired
private PathDestinationFactory pathDestinationFactory;
@GetMapping("/publish/{type}")
public String publishEvent(@PathVariable int type) {
switch (type) {
case 1:
UserDto userDto = new UserDto();
userDto.setUserName("test");
userDto.setAge(12);
AresDataChangedEvent aresDataChangedEvent = new AresDataChangedEvent(new Object(), "",
pathDestinationFactory.getDestination("cloud-bus"), userDto);
busBridge.send(aresDataChangedEvent);
break;
case 2:
RefreshRemoteApplicationEvent refreshEvent = new RefreshRemoteApplicationEvent(
new Object(), "",pathDestinationFactory.getDestination("cloud-bus"));
busBridge.send(refreshEvent);
break;
default:
return "error type";
}
return "success";
}
}
直接访问: http://localhost:8080/publish/1
,结果:
{
"type":"AresDataChangedEvent",
"timestamp":1628295736625,
"originService":"",
"destinationService":"cloud-bus:**",
"id":"708299e8-da33-4024-beee-d26d2d0dd252",
"data":{
"userName":"test",
"age":12
}
}
可以发现自定义事件已经生效!
官方文档地址: spring cloud bus 自定义事件
good luck!!