消息驱动

SpringCloud Stream消息驱动可以简化开发人员对消息中间件的使用复杂度,让系统开发人员更多尽力专注与核心业务逻辑的开发

批注 2020-03-17 133449

使用

  • 依赖
<deendency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-stream</artifactId>
</dependency>
<!-- 整合rabbitmq -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>
  • 配置
spring.rabbitmq.host=192.168.182.130
spring.rabbitmq.username=my
spring.rabbitmq.password=123
@EnableBinding(MessageService.class)
  • 生产者
public interface MessageService {

    @Output("msg1")
    SubscribableChannel send();
}

发送消息

messageService.send().send(MessageBuilder.withPayload("cxk"+ UUID.randomUUID()).build());
  • 消费者
public interface MessageService {

    @Input("msg1")
    SubscribableChannel receive();
}

接收消息

@StreamListener("msg1")
public void listen(String msg){
    System.out.println("消费者接收到消息:"+msg);
}
  • 消费者组
# 配置相同组的消费者会对消息进行轮询消费
spring.cloud.stream.bindings.msg1.group=group1

results matching " "

No results matching " "

results matching " "

No results matching " "