本指南引導您完成使用Spring Data Redis發佈和訂閱經過Redis發送的消息的過程。Messaging with Redishtml
您將構建一個使用StringRedisTemplate發佈字符串消息的應用程序,並使用MessageListenerAdapter爲其提供POJO訂閱。java
使用Spring Data Redis做爲發佈消息的手段可能聽起來很奇怪,但正如您將發現的那樣,Redis不只提供了NoSQL數據存儲,還提供了消息傳遞系統。git
大約十五分鐘github
一個喜歡的文本編輯器或者IDEredis
JDK 1.8 或者更高spring
Gradle 4+ 或者 Maven 3.2+apache
你也能夠導入代碼到你的IDE中windows
Redis server
像大多數Spring入門指南同樣,您能夠從頭開始並完成每一個步驟,也能夠繞過已熟悉的基本設置步驟。 不管哪一種方式,你最終獲得工做代碼。
爲了兼顧沒法使用Intellij Idea 的讀者,我這裏依然採用STS來完成這個指南。
1. 打開咱們的STS ,New ————> Import Spring Getting Started Content
2. 輸入message, 搜索找到Message Redis
Tips: Code Sets 咱們仍然所有勾選,這樣系統默認會生成一個已經寫好的complete 項目,這樣方便咱們在Initial項目中模仿學習。
3. 搭建Redis 服務器
在構建消息傳遞應用程序以前,您須要設置將處理接收和發送消息的服務器。
Redis是一個開源的BSD許可的鍵值數據存儲器,它還附帶了一個消息傳遞系統。
3.1 在Windows 操做系統安裝Redis
關於Redis 在windows 上的安裝詳情可參考個人另一篇博文: 揭開Redis的神祕面紗
3.2 啓動Redis 服務器
下載安裝好後,你若是沒有配置環境變量須要先進入Redis 安裝文件夾
好比咱們的安裝在C:\app\Redis\Redis-x64-3.2.100 那麼咱們打開命令行窗口後,執行如下命令進入安裝文件夾:
C:\app\Redis\Redis-x64-3.2.100
Tips: 若是已經配置了環境變量能夠跳過上述步驟
而後咱們執行啓動Redis 服務器命令
redis-serve
執行成功後你會看到下面圖示的內容:
Tips: 若是看到上述內容,說明服務器啓動成功,端口監聽在6379端口
4. 建立一個Redis消息接收器
在任何基於消息傳遞的應用程序中,都有消息發佈者和消息接收者。 要建立消息接收方,請使用一種方法來實現接收方以響應消息:
src/main/java/hello/Receiver.java
package hello; import java.util.concurrent.CountDownLatch; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; public class Receiver { private static final Logger LOGGER=LoggerFactory.getLogger(Receiver.class); private CountDownLatch latch; @Autowired public Receiver(CountDownLatch latch) { this.latch=latch; } public void receiveMessage(String message) { LOGGER.info("Received <" +message+">"); latch.countDown(); } }
Receiver是一個簡單的POJO,它定義了一種接收消息的方法。 正如您將Receiver註冊爲消息偵聽器時所看到的,您能夠根據須要命名消息處理方法。
出於演示的目的,它的構造函數使用倒計數鎖存器進行自動裝配。 這樣,它能夠在收到消息時發出信號。
5. 註冊偵聽器併發送消息
Spring Data Redis提供了使用Redis發送和接收消息所需的全部組件。 具體來講,你須要配置:
您將使用Redis模板發送消息,而且您將向Receiver註冊消息偵聽器容器,以便它能夠接收消息。 鏈接工廠驅動模板和消息偵聽器容器,使它們可以鏈接到Redis服務器。
本示例使用Spring Boot的默認RedisConnectionFactory,它是基於Jedis Redis庫的JedisConnectionFactory的一個實例。 鏈接工廠被注入到消息監聽器容器和Redis模板中。
src/main/java/hello/Application.java
package hello; import java.util.concurrent.CountDownLatch; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.listener.PatternTopic; import org.springframework.data.redis.listener.RedisMessageListenerContainer; import org.springframework.data.redis.listener.adapter.MessageListenerAdapter; @SpringBootApplication public class Application { private static final Logger LOGGER=LoggerFactory.getLogger(Application.class); @Bean RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,MessageListenerAdapter listenerAdapter) { RedisMessageListenerContainer container=new RedisMessageListenerContainer(); container.setConnectionFactory(connectionFactory); container.addMessageListener(listenerAdapter,new PatternTopic("chat")); return container; } @Bean MessageListenerAdapter listenerAdapter(Receiver receiver) { return new MessageListenerAdapter(receiver,"receiveMessage"); } @Bean Receiver receiver(CountDownLatch latch) { return new Receiver(latch); } @Bean CountDownLatch latch() { return new CountDownLatch(1); } @Bean StringRedisTemplate template(RedisConnectionFactory connectionFactory) { return new StringRedisTemplate(connectionFactory); } public static void main(String[] args)throws InterruptedException{ ApplicationContext ctx=SpringApplication.run(Application.class,args); StringRedisTemplate template=ctx.getBean(StringRedisTemplate.class); CountDownLatch latch = ctx.getBean(CountDownLatch.class); LOGGER.info("Sending message..."); template.convertAndSend("chat", "Hello from Redis!"); latch.await(); System.exit(0); } }
在listenerAdapter方法中定義的bean在容器中定義的消息偵聽器容器中註冊爲消息偵聽器,並將偵聽「chat」主題上的消息。
因爲Receiver類是POJO,所以須要將其包裝在實現AddMessageListener()所需的MessageListener接口的消息偵聽器適配器中。
消息偵聽器適配器還配置爲在消息到達時調用Receiver上的receiveMessage()方法。
鏈接工廠和消息監聽器容器bean都是您須要偵聽消息的。
要發送消息,您還須要一個Redis模板。在這裏,它是一個配置爲StringRedisTemplate的bean,它是RedisTemplate的一個實現,它着重於Redis的經常使用用法,其中鍵和值都是`String`s。
main()方法經過建立Spring應用程序上下文來解決全部問題。應用程序上下文而後啓動消息監聽器容器,而且消息監聽器容器bean開始監聽消息。而後,main()方法從應用程序上下文中檢索StringRedisTemplate bean,並使用它發送「來自Redis的Hello!」消息在「chat」主題上。最後,它關閉Spring應用程序上下文並結束應用程序。
6. 編譯成可執行Jar
您能夠使用Gradle或Maven從命令行運行應用程序。 或者您能夠構建一個包含全部必需的依賴項,類和資源的可執行JAR文件,並運行該文件。 這使得在整個開發生命週期內跨越不一樣環境等,將服務做爲應用程序發佈,版本化和部署變得很是容易。
生成Jar
若是您正在使用Gradle,則能夠使用./gradlew bootRun運行該應用程序。 或者您能夠使用./gradlew構建構建JAR文件。 而後你能夠運行JAR文件:
java -jar build/libs/gs-messaging-redis-0.1.0.jar
若是您使用的是Maven,則能夠使用./mvn spring-boot:run來運行該應用程序。 或者您能夠使用./mvn clean包構建JAR文件。 而後你能夠運行JAR文件:
java -jar target/gs-messaging-redis-0.1.0.jar
上述過程將建立一個可運行的JAR。 您也能夠選擇構建經典的WAR文件。
7. 執行成功後你將看到下面的信息:
恭喜! 您剛剛使用Spring和Redis開發了一個簡單的發佈和訂閱應用程序。
源碼:點擊查看