SpringCloudStream整合RabbitMQ和Kafka

SpringCloudStream 就是使用了基于消息系统的微服务处理架构。对于消息系统而言一共分为两类:基于应用标准的 JMS、基于协议标准的 AMQP,在整个 SpringCloud 之中支持有 RabbitMQ、Kafka 组件的消息系统。利用 SpringCloudStream 可以实现更加方便的消息系统的整合处理。

1、生产者和消费者引入依赖

<dependency>
   <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-stream-kafka</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>

2、创建生产者

(1)定义配置文件
spring:
  rabbitmq:
    host: 192.168.0.33
    username: user
    password: 123456
    virtual-host: /test
  cloud:
    stream:
      bindings:
        goods_output:
          #指定要连接binders中kafka或rabbitmq
          binder: rabbit1
          consumer:
            headerMode: raw
          producer:
            headerMode: raw
          destination: goods-topic
          content-type: text/plain
      binders:
        kafka1:
          type: kafka
          environment:
            spring:
              cloud:
                stream:
                  kafka:
                    binder:
                      brokers: http://192.168.0.33:9092
                      auto-add-partitions: true
                      auto-create-topics: true
                      min-partition-count: 1
        rabbit1:
          type: rabbit
          environment:
            spring:
              rabbitmq:
                addresses: 192.168.0.33
                port: 5672
                username: user
                password: 123456
                virtual-host: /test
(2)创建通道
import org.springframework.cloud.stream.annotation.Output;
import org.springframework.messaging.MessageChannel;

/**
 * 发送消息通道
 *
 * @author yanglei
 */
public interface GoodsSource {

    String GOODS_OUTPUT = "goods_output";

    @Output(GoodsSource.GOODS_OUTPUT)
	MessageChannel output();

}
(3)发送消息
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.messaging.support.MessageBuilder;

/**
 * 消息发送
 *
 * @author yanglei
 */
@EnableBinding(GoodsSource.class)
public class GoodsProducer {

    @Autowired
    private GoodsSource source;

    public void sendMessage(String msg) {
        try {
            source.output().send(MessageBuilder.withPayload(msg).build());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
(4)创建测试接口
@RestController
@RequestMapping("test")
@Api(tags="sku表,表示具体的商品实体")
public class TestController {

	@Autowired
	private GoodsProducer goodsProducer;

    @GetMapping("send")
    @ApiOperation("信息")
    public void get(){
		goodsProducer.sendMessage("我发送了一条消息");
    }

}

3、创建消费者

(1)定义配置文件
spring:
  rabbitmq:
    host: 192.168.0.33
    username: user
    password: 123456
    virtual-host: /test
  cloud:
    stream:
      bindings:
        goods_input:
          binder: rabbit1
          consumer:
            headerMode: raw
          producer:
            headerMode: raw
          #绑定的kafka topic名称
          destination: goods-topic
          content-type: text/plain
      binders:
        kafka1:
          type: kafka
          environment:
            spring:
              cloud:
                stream:
                  kafka:
                    binder:
                      brokers: http://192.168.0.33:9092
                      auto-add-partitions: true
                      auto-create-topics: true
                      min-partition-count: 1
        rabbit1:
          type: rabbit
          environment:
            spring:
              rabbitmq:
                addresses: 192.168.0.33
                port: 5672
                username: user
                password: 123456
                virtual-host: /test
(2)创建通道
import org.springframework.cloud.stream.annotation.Input;
import org.springframework.messaging.SubscribableChannel;

/**
 * 接收消息通道
 *
 * @author yanglei
 */
public interface GoodsSink {

    String GOODS_INPUT = "goods_input";

    @Input(GoodsSink.GOODS_INPUT)
	SubscribableChannel input();

}
(3)接受消息
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;

/**
 * 消息监听消费
 *
 * @author yanglei
 */
@EnableBinding(GoodsSink.class)
@Slf4j
public class GoodsConsumer {

    @StreamListener(GoodsSink.GOODS_INPUT)
    public void onReceive(String shopJson) {
        log.info(shopJson);
    }
}
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东黧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值