步骤一:导入maven依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
步骤二:引入RabbitMQ基本的配置信息 application-rabbitmq.properties
spring.application.name=springboot-rabbitmq
spring.rabbitmq.host=47.95.202.91
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
步骤三:启动云服务器rabbitmq 并注入 template (需要服务器rabbitmq安装成功)
RabbitConfig.java
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.Scope;
@Configuration
@PropertySource("classpath:application-rabbitmq.properties")
public class RabbitConfig {
@Value("${spring.rabbitmq.host}")
private String host;
@Value("${spring.rabbitmq.port}")
private int port;
@Value("${spring.rabbitmq.username}")
private String username;
@Value("${spring.rabbitmq.password}")
private String password;
@Bean
public ConnectionFactory connectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory(host,port);
connectionFactory.setUsername(username);
connectionFactory.setPassword(password);
connectionFactory.setVirtualHost("/");
connectionFactory.setPublisherConfirms(true);
return connectionFactory;
}
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public RabbitTemplate rabbitTemplate() {
RabbitTemplate template = new RabbitTemplate(connectionFactory());
return template;
}
//Queue:消息的载体,每个消息都会被投到一个或多个队列。
@Bean
public Queue helloQueue() {
return new Queue("helloQueue");
}
//Exchange:消息交换机,它指定消息按什么规则,路由到哪个队列。
@Bean
TopicExchange exchange() {
return new TopicExchange("exchange");
}
}
步骤四:编写消息生产者和消费者
@Component
public class HelloSender1 {
@Autowired
private AmqpTemplate rabbitTemplate;
public void send() {
String sendMsg = "hello1 " + new Date();
System.out.println("Sender1 : " + sendMsg);
this.rabbitTemplate.convertAndSend("helloQueue", sendMsg);
}
}
@Component
@RabbitListener(queues = "helloQueue")
public class HelloReceiver {
@RabbitHandler
public void process(String hello) {
System.out.println("Receiver 1 : " + hello);
}
}
测试:
@RestController
@RequestMapping("/rabbit")
public class RabbitTest {
@Autowired
private HelloSender1 helloSender1;
@PostMapping("/hello")
public void hello() {
helloSender1.send();
}
}
访问:http://localhost:8091/rabbit/hello
结果:
Sender1 : hello1 Tue Mar 19 14:41:51 CST 2019
Receiver1 : hello1 Tue Mar 19 14:41:51 CST 2019
步骤五:广播模式生产者消费者demo
添加been
@Bean
public Queue AMessage() {
return new Queue("fanout.A");
}
@Bean
public Queue BMessage() {
return new Queue("fanout.B");
}
@Bean
public Queue CMessage() {
return new Queue("fanout.C");
}
@Bean
FanoutExchange fanoutExchange() {
return new FanoutExchange("fanoutExchange");
}
@Bean
Binding bindingExchangeA(Queue AMessage,FanoutExchange fanoutExchange) {
return BindingBuilder.bind(AMessage).to(fanoutExchange);
}
@Bean
Binding bindingExchangeB(Queue BMessage, FanoutExchange fanoutExchange) {
return BindingBuilder.bind(BMessage).to(fanoutExchange);
}
@Bean
Binding bindingExchangeC(Queue CMessage, FanoutExchange fanoutExchange) {
return BindingBuilder.bind(CMessage).to(fanoutExchange);
}
生产者:
@Component
public class FanoutSender {
@Autowired
private AmqpTemplate rabbittemplate;
public void send(){
String msgString="fanoutSender :hello i am zhangsan";
System.out.println(msgString);
this.rabbittemplate.convertAndSend("fanoutExchange","aaa.bbb",msgString);
}
}
消费者1;
@Component
@RabbitListener(queues = "fanout.A")
public class FanoutReceiverA {
@RabbitHandler
public void process(String msg){
System.out.println("FanoutReceiverA : " + msg);
}
}
消费者2:
@Component
@RabbitListener(queues = "fanout.B")
public class FanoutReceiverB {
@RabbitHandler
public void process(String msg){
System.out.println("FanoutReceiverB : " + msg);
}
}
...
测试:
@Autowired private FanoutSender fanoutSender;
@PostMapping("/fanoutTest") public void fanoutTest() { fanoutSender.send(); }
访问:http://localhost:8091/rabbit/fanoutTest
结果:
fanoutSender :hello i am zhangsan
FanoutReceiverA : fanoutSender :hello i am zhangsan
FanoutReceiverB : fanoutSender :hello i am zhangsan
FanoutReceiverC : fanoutSender :hello i am zhangsan
由以上结果可知:就算fanoutSender发送消息的时候,指定了routing_key为"aaa.bbb",但是所有接收者都接受到了消息