Springboot 集成 RabbitMq(注解版)

1:pom中引入相关的依赖

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

2:配置文件中加入对应的配置

# mq配置
#添加类型
spring.cloud.stream.binders.rabbit.type = rabbit
#IP地址
spring.cloud.stream.binders.environment.spring.rabbitmq.host = 
#端口
spring.cloud.stream.binders.environment.spring.rabbitmq.port = 
#用户名
spring.cloud.stream.binders.environment.spring.rabbitmq.username = 
#密码
spring.cloud.stream.binders.environment.spring.rabbitmq.password = 

#生产者对应的配置
spring.cloud.stream.bindings.my-test-mq-producer.destination = my.test.topic
spring.cloud.stream.bindings.my-test-mq-producer.content-type = application/json
spring.cloud.stream.bindings.my-test-mq-producer.binder = rabbit
spring.cloud.stream.rabbit.bindings.my-test-mq-producer.producer.routing-key-expression = my.test.key

#消费者对应的配置
spring.cloud.stream.bindings.my-test-mq-customer.destination = my.test.topic
spring.cloud.stream.bindings.my-test-mq-customer.content-type = application/json
spring.cloud.stream.bindings.my-test-mq-customer.binder = rabbit
spring.cloud.stream.bindings.my-test-mq-customer.group = my.test
spring.cloud.stream.rabbit.bindings.my-test-mq-customer.consumer.binding-routing-key = my.test.key
spring.cloud.stream.rabbit.bindings.my-test-mq-customer.consumer.queueNameGroupOnly = true

3:生产者

  创建生产者需要的配置类

import org.springframework.cloud.stream.annotation.Output;
import org.springframework.messaging.MessageChannel;

/**
 * @Description TODO
 * @author:一尘
 * @Date:2020/6/8 17:51
 */
public interface MySource {
    /**
     * 发送邮件
     */
   //这个要和配置文件中的生产者设置的一直
    String MY_TEST_MQ_PRODUCER = "my-test-mq-producer";

    @Output(MY_TEST_MQ_PRODUCER)
    MessageChannel confirmReceipt();
}

生产者服务

import com.qishucai.sc.quote.mq.pp.PPSource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Classname MyProducer 
 * @Description TODO
 * @author:yangmeng
 * @Date:2020/6/8 17:58
 */
@Slf4j
//绑定
@EnableBinding(MySource.class)
@Component
public class MyProducer {

    @Resource
    private MySource mySource;


  //QuoteEmailMsgDTO 为你要往消息队列里面要放的实体 
    public void sendMessage(QuoteEmailMsgDTO msgDTO){
        mySource.confirmReceipt().send(MessageBuilder.withPayload(msgDTO).build());
    }


    @Async 
   //异步注解 需要在启动类上加上@EnableAsync 才能生效
    public void sendDeliveryMsg(QuoteEmailMsgDTO quoteEmailMsgDTO){

        sendMessage(quoteEmailMsgDTO);
    }


}

使用生产者生产消息

    //注入
    @Resource
    private MyProducer myProducer;

  //调用生产者生产消息
    public void test(QuoteEmailMsgDTO quoteEmailMsgDTO ) {
    

        myProducer.sendDeliveryMsg(quoteEmailMsgDTO);
    }

4:消费值

 消费者所需的配置类

import org.springframework.cloud.stream.annotation.Input;
import org.springframework.messaging.SubscribableChannel;

/**
 * 
 * @author:yicheng
 * @Date:2020/6/2 20:47
 */
public interface MySink {
    /**
     * 确认订单
     */
    String SC_QUOTE_EMAIL_CREATE_INPUT = "my-test-mq-customer";


    @Input(SC_QUOTE_EMAIL_CREATE_INPUT)
    SubscribableChannel scEmailCreate();

}

消费者

import com.alibaba.fastjson.JSONObject;
import com.qishucai.pp.common.service.EmailInfoService;
import com.qishucai.pp.quote.listener.ScSink;
import com.qishucai.pp.quote.listener.model.QuoteEmailMsgDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Slf4j
@Component
#绑定消费者配置类
@EnableBinding(MySink.class)
public class MyCustomer {

 

    @Transactional
    @StreamListener(value = ScSink.MY_TEST_MQ_CUSTOMER)
    public void onMessage(String message) throws Exception {
        try {
            log.info("receive   message :{}",message);
            QuoteEmailMsgDTO quoteEmailMsgDTO = JSONObject.parseObject(message, QuoteEmailMsgDTO.class);

        
        } catch (Exception ex) {
            log.error("receive  message :{} exception:{}", message, ex);
        }
    }


}

 

©️2020 CSDN 皮肤主题: 书香水墨 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值