使用RabbitMQ實現商品數據同步

【生產者端】

一,添加依賴

<依賴性>

 <的groupId> org.springframework.amqp </的groupId>  

  <artifactId的>彈簧兔</ artifactId的>

</依賴性>


二,配置的applicationContext-rabbitmq.xml文件

<?xml version =“1.0” encoding =“UTF-8” ?>
 < beans xmlns =“http://www.springframework.org/schema/beans”
 xmlns:xsi =“http://www.w3.org/2001 / XMLSchema-instance“ 
 xmlns:rabbit =”http://www.springframework.org/schema/rabbit“
 xsi :schemaLocation =”http://www.springframework.org/schema/beans 
        http://www.springframework。 org / schema / beans / spring-beans.xsd 
        http://www.springframework.org/schema/rabbit 
        http://www.springframework.org/schema/rabbit/spring-rabbit.xsd“ >
 <! - 連接工廠 - >
 <                     
        :connection-factory id =“connectionFactory” host =“$ {rabbitmq.host}”
 port =“$ {rabbitmq.port}” username =“$ {rabbitmq.username}”
 password =“$ {rabbitmq.password}” virtual -host =“$ {rabbitmq.vhost}” />
 <! -  Rabbit的管理,自動管理隊列和交換機 - >
 < rabbit :admin connection-factory =“connectionFactory” />
 <! - 交換機這裏使用topic類型 - >
 < rabbit :topic-exchange name =“JESSES_ITEM_EXCHANGE” />
 <! -  Rabbit的模板,提供了操作消息的API,需要指定連接工廠和交換機 - >
 <兔子:模板ID                                                                      
        
        =“rabbitTemplate”
 connection-factory =“connectionFactory” exchange =“JESSES_ITEM_EXCHANGE” />
 </ beans >                     

三,寫方法實現發送消息。

private void sendMessage Item item String type ){
 try {
 Map < String Object > msg = new HashMap <>();
味精“數據” 項目的getId ());
味精put “type” type );
味精“時間戳” 系統的currentTimeMillis ());
                                    
        這個rabbitTemplate convertAndSend “項目” + JsonUtils 的toString MSG ));
    } 捕獲JsonProcessingException ë ){
 Ë printStackTrace ();
/ *服務層的異常一般是拋,如果嘗試會無法事務回滾。
         但這裏用試試。就算消息發送失敗,也不應該回滾事務讓修改失敗。
         應該在這裏進行處理。寫代碼重新發送。* /
 }
 }                    



【消費者端】

一,導入彈簧兔的罐包

二,配置的applicationContext-rabbitmq.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:rabbit="http://www.springframework.org/schema/rabbit"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans.xsd 
       http://www.springframework.org/schema/rabbit 
       http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">

    <!--連接工廠-->
    <rabbit:connection-factory id="connectionFactory" host="${rabbitmq.host}"
                               port="${rabbitmq.port}" username="${rabbitmq.username}"
                               password="${rabbitmq.password}" virtual-host="${rabbitmq.vhost}" />
    <!--Rabbit的管理,自動管理隊列和交換機-->
    <rabbit:admin connection-factory="connectionFactory" />
    <!--定義隊列-->
    <rabbit:queue name="JESSES_WEB_ITEM_QUEUE" />
    <!--消息監聽者-->
    <bean id="itemMessageListener" class="com.jesses.web.mq.listener.ItemMessageListener" />
    <rabbit:listener-container connection-factory="connectionFactory">
        <rabbit:listener ref="itemMessageListener" method="consume" 
                         queue-names="JESSES_WEB_ITEM_QUEUE"/>
    </rabbit:listener-container>
</beans>

三、寫消費者實現消費消息。

package com.jesses.web.mq.listener;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jesses.web.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;

/**
 * 監聽商品信息的一個消費者
 */
public class ItemMessageListener {

    @Autowired
    private ItemService itemService;

    private static final ObjectMapper MAPPER=new ObjectMapper();

    //消費方法
    public void consume(String msg){
        Long itemId=0L;
        try {
            JsonNode jsonNode = MAPPER.readTree(msg);//讀取傳過來的字符串讀成樹節點
            itemId = jsonNode.get("data").asLong();//用保存時的key獲取指定節點。
        } catch (IOException e) {
            e.printStackTrace();
            返回;
        }
 //刪除緩存
 this itemService deleteCache itemId );
    }
 }                

PS:隊列和交換機的綁定可以在配置中定義,也可以在控制面板手動去做。

手動去做可以避免日後再改代碼,利於解耦合。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章