1、消息發送方(發佈者)
1)添加maven依賴
<dependency>
<groupid>org.springframework.boot/<groupid>
<artifactid>spring-boot-starter-amqp/<artifactid>
/<dependency>
2)配置RabbitMQ配置(application.properties)
#RabbitMQ 服務配置,不寫默認走本地ip
spring.rabbitmq.host=192.168.0.3
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
3)創建發送方法
package com.example.provide.rabbitmq;
import com.alibaba.fastjson.JSON;
import com.example.provide.dto.UserDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
import static org.springframework.integration.jmx.JmxHeaders.OPERATION_NAME;
/*
* @auth yuesf
* @data 2019/11/4
*/
@Component
public class Sender {
private static final Logger logger = LoggerFactory.getLogger(Sender.class);
@Autowired
private RabbitTemplate rabbitTemplate;
public void storeInfoWindQSend(Object object) {
String message = JSON.toJSONString(object);
logger.info("RabbitMQ: 發送消息={}", message);
rabbitTemplate.convertAndSend("demo.direct.exchange", "demo.direct", message);
logger.info("發送消息完成 message={}", message);
}
}
2、消息接收方(訂閱者)
1)添加maven依賴
<dependency>
<groupid>org.springframework.boot/<groupid>
<artifactid>spring-boot-starter-amqp/<artifactid>
/<dependency>
2)配置RabbitMQ配置(application.properties)
#RabbitMQ 服務配置,不寫默認走本地ip
spring.rabbitmq.host=192.168.0.3
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
示例中使用的直連交換機,聲明一個交換機,一個隊列。交換機與隊列綁定關係
package com.example.consume.listener;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/*
* Rabbitmq的配置示例
* @auth yuesf
* @data 2019/11/4
*/
@Configuration
public class RabbitConfigDemo {
/**
* 示例交換機
*
* @return
*/
@Bean
public DirectExchange demoExchange() {
return new DirectExchange("demo.direct.exchange", true, false);
}
/**
* 示例隊列
*
* @return
*/
@Bean
public Queue demoQueue() {
return new Queue("demo.queue", true, false, false);
}
/**
* 交換機與隊列的綁定關係
*
* @param demoQueue
* @param demoExchange
* @return
*/
@Bean
public Binding bindingDemoQueue(@Qualifier("demoQueue") Queue demoQueue,
@Qualifier("demoExchange") DirectExchange demoExchange) {
return BindingBuilder.bind(demoQueue).to(demoExchange).with("demo.direct");
}
}
4)監聽方法
package com.example.consume.listener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/*
* @auth yuesf
* @data 2019/11/4
*/
@Component
public class RabbitDemoListener {
private static final Logger logger = LoggerFactory.getLogger(RabbitDemoListener.class);
@RabbitListener(queues = "demo.queue")
public void goodsListenerProcess(Object message) {
logger.info("接收消息 message={}", message);
}
}
本文由博客一文多發平臺 https://openwrite.cn?from=article_bottom 發佈!
再次感謝!!! 您已看完全文,歡迎關注微信公眾號猿碼 ,你的支持是我持續更新文章的動力!
閱讀更多 零售雲技術內參 的文章