SpringBoot中使用redis實現訊息佇列

2021-09-02 18:33:13 字數 1541 閱讀 1627

org.springframework.boot

spring-boot-starter-data-redis

# redis資料庫索引(預設為0)

spring.redis.database=0

# redis伺服器位址

spring.redis.host=192.168.4.100

# redis伺服器連線埠

spring.redis.port=6379

# redis伺服器連線密碼(預設為空)

spring.redis.password=

# 連線池最大連線數(使用負值表示沒有限制)

spring.redis.pool.max-active=20

# 連線池最大阻塞等待時間(使用負值表示沒有限制)

spring.redis.pool.max-wait=-1

# 連線池中的最大空閒連線

spring.redis.pool.max-idle=20

# 連線池中的最小空閒連線

spring.redis.pool.min-idle=0

# 連線超時時間(毫秒)

spring.redis.timeout=1000

public class receiver 

public void receivemessage(string message)

}

//注入訊息接收者

@bean

receiver receiver(countdownlatch latch)

@bean

countdownlatch latch()

//使用spring-data-reids提供的stringredistemplate傳送訊息

@bean

stringredistemplate template(redisconnectionfactory connectionfactory)

//注入訊息監聽容器

@bean

redismessagelistenercontainer container(redisconnectionfactory connectionfactory, messagelisteneradapter listeneradapter)

@bean

messagelisteneradapter listeneradapter(receiver receiver)

@restcontroller

public class redisqueuecontroller

}

通過postman訪問此介面:

控制台輸出資訊:

要對接收到的資訊進行入庫等後續操作,即可在receiver類中進行處理。

Spring Boot中使用MongoDB資料庫

前段時間分享了關於spring boot中使用redis的文章,除了redis之後,我們在網際網路產品中還經常會用到另外一款著名的nosql資料庫mongodb。下面就來簡單介紹一下mongodb,並且通過乙個例子來介紹spring boot中對mongodb訪問的配置和使用。mongodb是乙個基...

Spring Boot中使用MongoDB資料庫

前段時間分享了關於spring boot中使用redis的文章,除了redis之後,我們在網際網路產品中還經常會用到另外一款著名的nosql資料庫mongodb。下面就來簡單介紹一下mongodb,並且通過乙個例子來介紹spring boot中對mongodb訪問的配置和使用。mongodb是乙個基...

SpringBoot中使用日誌

結果 所有配置檔案都會被載入,高優先順序的配置檔案會覆蓋低優先順序的配置檔案 springboot 底層是spring框架,spring框架預設是用jcl springboot選用slf4j和logback作為日誌框架 如何使用slf4j import org.slf4j.logger import...