spring cloud bus - 自定义事件

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!!

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值