Spring Cloud Bus在服务之间传递自定义事件(一)
Spring Cloud Bus是Spring Cloud生态系统中的一个组件,用于在分布式系统中传递消息和事件。除了提供消息总线的基本功能之外,它还可以用于在服务之间传递自定义事件。
自定义事件
在Spring Framework中,我们可以使用ApplicationEvent来创建自定义事件。为了演示如何在Spring Cloud Bus中传递自定义事件,我们创建了一个名为MyCustomEvent的自定义事件。代码如下所示:
public class MyCustomEvent extends ApplicationEvent {
private final String message;
public MyCustomEvent(Object source, String message) {
super(source);
this.message = message;
}
public String getMessage() {
return message;
}
}
在这个自定义事件中,我们简单地将消息作为一个String类型的属性添加到事件中。
生产者
为了在Spring Cloud Bus中传递自定义事件,我们需要一个生产者应用程序,该应用程序将触发自定义事件并将其发送到Spring Cloud Bus上。在这个示例中,我们将使用Spring Cloud Bus来连接两个应用程序,这两个应用程序都是使用Spring Cloud Config从同一个配置服务器获取它们的配置。我们使用Spring Cloud Config作为我们的配置服务器,因为它已经为我们集成了Spring Cloud Bus。
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class ProducerApplication {
private static final Logger LOGGER = LoggerFactory.getLogger(ProducerApplication.class);
@Autowired
private ApplicationEventPublisher publisher;
@Value("${spring.application.name}")
private String appName;
@PostMapping("/sendMessage")
public void sendMessage(@RequestParam String message) {
LOGGER.info("Sending message: {}", message);
publisher.publishEvent(new MyCustomEvent(this, message));
}
public static void main(String[] args) {
SpringApplication.run(ProducerApplication.class, args);
}
}
在这个生产者应用程序中,我们使用ApplicationEventPublisher来发布MyCustomEvent事件。我们定义了一个名为sendMessage的REST API来触发这个事件,该API接收一个名为message的字符串参数。
消费者
现在,我们需要一个消费者应用程序来接收这些自定义事件。在这个示例中,我们将使用Spring Cloud Stream来创建一个名为ConsumerApplication的消费者应用程序。
@SpringBootApplication
@EnableDiscoveryClient
public class ConsumerApplication {
private static final Logger LOGGER = LoggerFactory.getLogger(ConsumerApplication.class);
@Value("${spring.application.name}")
private String appName;
@StreamListener(target = Sink.INPUT, condition = "headers['type']=='myCustomEvent'")
public void handleMyCustomEvent(MyCustomEvent event) {
LOGGER.info("Received MyCustomEvent: {}", event.getMessage());
}
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
在这个消费者应用程序中,我们使用@StreamListener注释来监听Spring Cloud Stream通道上的消息。我们定义了一个名为handleMyCustomEvent的方法来处理接收到的MyCustomEvent事件。
相关文章
- Spring 全家桶之 Spring Boot 2.6.4(六)- Web Develop(Part A)
- Spring学习笔记(三十)——SpringBoot对象拷贝总结&Mapstruct
- Spring Cloud Gateway内存马注入
- 一文入门 Spring Cloud Tencent
- spring cloud gateway 路由转发原理_微服务网关的作用是什么
- 正式抛弃 Feign!Spring 6 推出新特性:HTTP Interface,这波太秀了!
- 一文讲透Spring三种集成方式
- Spring Cloud 最新版发布,Spring Security + OAuth2 终于安排上了!
- 为什么要推荐大家使用Spring Cloud Alibaba
- 通过这家二进制奶茶店,带你走近Spring的世界!
- Spring Cloud Gateway环境搭建和配置(一)
- 集成Spring Cloud Sleuth和Zipkin
- 在生产环境中使用Spring Cloud Sleuth的注意事项(二)
- Spring Cloud Stream核心组件Channel(一)
- Spring Cloud LoadBalancer 的高级特性-服务实例过滤器(一)
- Spring Cloud Bus使用自定义的消息转换器(一)
- Spring Cloud组件
- Spring Boot中使用MongoDB数据库的方法
- spring数据连接池配置参数详解数据库
- 微服务架构下使用Spring Cloud Zuul作为网关将多个微服务整合到一个Swagger服务上详解架构师
- Redis(八):spring data redis 理解详解大数据
- Spring与多线程详解编程语言
- Spring Cloud(五):熔断监控Hystrix Dashboard和Turbine详解编程语言
- spring boot应用启动原理分析详解编程语言
- struts2+spring+hibernate分页代码[比较多]