前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring Cloud Bus与Spring Cloud Stream的关系示例

Spring Cloud Bus与Spring Cloud Stream的关系示例

原创
作者头像
堕落飞鸟
发布2023-04-15 12:52:35
5480
发布2023-04-15 12:52:35
举报
文章被收录于专栏:飞鸟的专栏

准备工作

首先,我们需要创建一个 Spring Boot 应用程序,并添加以下依赖:

代码语言:javascript
复制
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>

这些依赖将启用 Spring Cloud Bus 和 Spring Cloud Stream,并将其配置为使用 RabbitMQ 作为消息代理。

创建事件

接下来,我们需要创建一个事件类,以便在不同的服务之间传递事件信息。在本例中,我们将创建一个 GreetingEvent 类,用于表示向其他服务发送问候语的事件:

代码语言:javascript
复制
public class GreetingEvent {
    private String message;

    public GreetingEvent() {}

    public GreetingEvent(String message) {
        this.message = message;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

发布事件

现在,我们将创建一个服务,用于发布 GreetingEvent 事件。我们将使用 Spring Cloud Stream 来实现消息传递,使用 Spring Cloud Bus 来实现事件通知。在本例中,我们将创建一个名为 greeting-service 的服务,用于向其他服务发布 GreetingEvent 事件:

代码语言:javascript
复制
@SpringBootApplication
@EnableBinding(Source.class)
public class GreetingServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(GreetingServiceApplication.class, args);
    }

    @Bean
    @InboundChannelAdapter(channel = Source.OUTPUT, poller = @Poller(fixedDelay = "5000"))
    public MessageSource<GreetingEvent> greetingEventSource() {
        return () -> new GenericMessage<>(new GreetingEvent("Hello from Greeting Service"));
    }

    @RestController
    public class GreetingController {

        @Autowired
        private MessageChannel output;

        @GetMapping("/greeting")
        public void sendGreeting() {
            GreetingEvent event = new GreetingEvent("Hello from Greeting Service");
            output.send(MessageBuilder.withPayload(event).build());
        }
    }
}

这段代码中,我们使用了 @EnableBinding(Source.class) 注解来启用 Spring Cloud Stream,并创建了一个 MessageSource bean,用于向输出通道发送 GreetingEvent 事件。

此外,我们还创建了一个 GreetingController 类,用于处理 HTTP GET 请求,并使用 MessageChannel 将 GreetingEvent 事件发送到输出通道。

订阅事件

现在,我们将创建另一个服务,用于订阅 GreetingEvent 事件。我们将使用 Spring Cloud Stream 来实现消息传递,使用 Spring Cloud Bus 来实现事件订阅。在本例中,我们将创建一个名为 greeting-client 的服务,用于接收来自 greeting-service 的 GreetingEvent 事件:

代码语言:javascript
复制
@SpringBootApplication
@EnableBinding(Sink.class)
public class GreetingClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(GreetingClientApplication.class, args);
    }

    @StreamListener(Sink.INPUT)
    public void receiveGreeting(GreetingEvent event) {
          System.out.println("Received greeting: " + event.getMessage());
    }
}

这段代码中,我们使用了 @EnableBinding(Sink.class) 注解来启用 Spring Cloud Stream,并创建了一个 @StreamListener 注解的方法,用于接收从输入通道发送的 GreetingEvent 事件。

运行应用程序

现在,我们已经准备好运行我们的应用程序了。首先,我们需要启动 RabbitMQ 作为消息代理。然后,我们可以启动 greeting-service 和 greeting-client 应用程序。

当我们访问 greeting-service 的 /greeting 路径时,将向输出通道发送一个 GreetingEvent 事件。由于 greeting-client 订阅了输入通道,它将接收到这个事件,并输出消息。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 准备工作
  • 创建事件
  • 发布事件
  • 订阅事件
  • 运行应用程序
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档