spring boot整合RabbitMQ(Direct模式)


springboot集成RabbitMQ非常簡單,如果只是簡單的使用配置非常少,springboot提供了spring-boot-starter-amqp項目對消息各種支持。

Direct Exchange介紹

Direct Exchange是RabbitMQ默認的交換機模式,也是最簡單的模式,根據key全文匹配去尋找隊列。

 


第一個 X - Q1 就有一個 binding key,名字為 orange; X - Q2 就有 2 個 binding key,名字為 black 和 green。當消息中的 路由鍵 和 這個 binding key 對應上的時候,那么就知道了該消息去到哪一個隊列中。
Ps:為什么 X 到 Q2 要有 black,green,2個 binding key呢,一個不就行了嗎? - 這個主要是因為可能又有 Q3,而Q3只接受 black 的信息,而Q2不僅接受black 的信息,還接受 green 的信息。


1.新建一個Spring Boot工程,命名為:“rabbitmq-hello”。
在pom.xml中引入如下依賴內容,其中spring-boot-starter-amqp用於支持RabbitMQ。
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-amqp</artifactId>
    </dependency>
    
2.在application.properties中配置關於RabbitMQ的連接和用戶信息,用戶可以回到上面的安裝內容,在管理頁面中創建用戶。
spring.application.name=rabbitmq-hello
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=123456

3.創建消息生產者Sender。通過注入AmqpTemplate接口的實例來實現消息的發送,AmqpTemplate接口定義了一套針對AMQP協議的基礎操作。在Spring Boot中會根據配置來注入其具體實現。在該生產者,我們會產生一個字符串,並發送到名為hello的隊列中。
@Component
public class Sender {
    @Autowired
    private AmqpTemplate rabbitTemplate;
    public void send() {
        String context = "hello " + new Date();
        System.out.println("Sender : " + context);
        this.rabbitTemplate.convertAndSend("hello", context);
    }
}

4.創建消息消費者Receiver。通過@RabbitListener注解定義該類對hello隊列的監聽,並用@RabbitHandler注解來指定對消息的處理方法。所以,該消費者實現了對hello隊列的消費,消費操作為輸出消息的字符串內容。
@Component
@RabbitListener(queues = "hello")
public class Receiver {
    @RabbitHandler
    public void process(String hello) {
        System.out.println("Receiver : " + hello);
    }
}

5.創建RabbitMQ的配置類RabbitConfig,用來配置隊列、交換器、路由等高級信息。這里我們以入門為主,先以最小化的配置來定義,以完成一個基本的生產和消費過程。
@Configuration
public class RabbitConfig {
    @Bean
    public Queue helloQueue() {
        return new Queue("hello");
    }
}

6.創建應用主類:
@SpringBootApplication
public class HelloApplication {
    public static void main(String[] args) {
        SpringApplication.run(HelloApplication.class, args);
    }
}

7.創建單元測試類,用來調用消息生產:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = HelloApplication.class)
public class HelloApplicationTests {
    @Autowired
    private Sender sender;
    @Test
    public void hello() throws Exception {
        sender.send();
    }
}

8.啟動應用主類,從控制台中,我們看到如下內容,程序創建了一個訪問127.0.0.1:5672中admin的連接。
o.s.a.r.c.CachingConnectionFactory       : Created new connection: SimpleConnection@29836d32 [delegate=amqp://admin@127.0.0.1:5672/]
同時,我們通過RabbitMQ的控制面板,可以看到Connection和Channels中包含當前連接的條目。

9.運行單元測試類,我們可以看到控制台中輸出下面的內容,消息被發送到了RabbitMQ Server的hello隊列中。
Sender : hello Sun Sep 25 11:06:11 CST 2016

10.切換到應用主類的控制台,我們可以看到類似如下輸出,消費者對hello隊列的監聽程序執行了,並輸出了接受到的消息信息。
Receiver : hello Sun Sep 25 11:06:11 CST 2016

通過上面的示例,我們在Spring Boot應用中引入spring-boot-starter-amqp模塊,進行簡單配置就完成了對RabbitMQ的消息生產和消費的開發內容。

實際上RabbitMQ還可以支持發送對象:當然由於涉及到序列化和反序列化,該對象要實現Serilizable接口.HelloSender做出如下改寫:
public void send() {
    User user=new User();    //實現Serializable接口
    user.setUsername("hlhdidi");
    user.setPassword("123");
    template.convertAndSend("queue",user);
    
}
HelloReceiver做出如下改寫:
@RabbitListener(queues="queue")    //監聽器監聽指定的Queue

public void process1(User user) {    //用User作為參數
    System.out.println("Receive1:"+user);
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM