Springboot統合rabbitMQ
18855 ワード
『ここではrabbitMQのtopicモードを例として使用する』
一:メッセージ生産側
(1):依存の導入
(2):構成アプリケーション.ymlここではデフォルト構成です.変更しなければ書かなくてもいいです.virtual-host:/rabbitMQデフォルトの仮想マシンは/です.
(3):rabbieMQの構成クラスを記述する
(4):RabbitTemplateテンプレートを使用してメッセージを送信
二:情報消費側
(1):依存の導入
(2):アプリケーション.ymlの構成
(3):リスニングクラスリスニングメッセージキューの定義
一:メッセージ生産側
(1):依存の導入
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
</dependencies>
(2):構成アプリケーション.ymlここではデフォルト構成です.変更しなければ書かなくてもいいです.virtual-host:/rabbitMQデフォルトの仮想マシンは/です.
spring:
rabbitmq:
host: localhost
port: 5672
virtual-host: /
username: guest
password: guest
(3):rabbieMQの構成クラスを記述する
package com.test;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitMQConf {
//
public static final String TOPIC_EXCHANGE = "topic_exchange";
//
public static final String TOPIC_QUEUE1 = "topic_queue1";
public static final String TOPIC_QUEUE2 = "topic_queue2";
//
@Bean("TopicExchange")
public Exchange topicExchange(){
return ExchangeBuilder.topicExchange(TOPIC_EXCHANGE).durable(true).build();
}
//
@Bean("Queue1")
public Queue Queue1(){
return QueueBuilder.durable(TOPIC_QUEUE1).build();
}
@Bean("Queue2")
public Queue Queue2(){
return QueueBuilder.durable(TOPIC_QUEUE2).build();
}
// 。 ,
@Bean
public Binding Queue1Exchange(@Qualifier("Queue1") Queue queue, @Qualifier("TopicExchange") Exchange exchange){
//with("it.#) Queue1 ,# 。*
return BindingBuilder.bind(queue).to(exchange).with("it.#").noargs();
}
@Bean
public Binding Queue2Exchange(@Qualifier("Queue2") Queue queue, @Qualifier("TopicExchange") Exchange exchange){
return BindingBuilder.bind(queue).to(exchange).with("it.*").noargs();
}
}
(4):RabbitTemplateテンプレートを使用してメッセージを送信
package com.itheima.rabbitmq;
import com.itheima.rabbitmq.config.RabbitMQConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class RabbitMQTest {
@Autowired
private RabbitTemplate rabbitTemplate;
@Test
public void test(){
//convertAndSend( , ey, )
// Key it.#。 。
rabbitTemplate.convertAndSend(RabbitMQConfig.TOPIC_EXCHANGE, "it.insert.insert", " ,routing key it.insert.insert");
// Key it.*。 。
rabbitTemplate.convertAndSend(RabbitMQConfig.TOPIC_EXCHANGE, "it.delete", " ,routing key it.delete");
}
}
二:情報消費側
(1):依存の導入
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
</dependencies>
(2):アプリケーション.ymlの構成
spring:
rabbitmq:
host: localhost
port: 5672
virtual-host: /
username: guest
password: guest
(3):リスニングクラスリスニングメッセージキューの定義
package com.itheima.rabbitmq.listener;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class MyListener {
/**
*
* @param message
*/
@RabbitListener(queues = "topic_queue1")
public void myListener1(String message){
System.out.println(" topic_queue1 :" + message);
}
@RabbitListener(queues = "topic_queue2")
public void myListener1(String message){
System.out.println(" topic_queue2 :" + message);
}
}