日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

Redis整合springboot实现消息队列

發布時間:2023/12/13 数据库 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Redis整合springboot实现消息队列 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

publisher消息的發出

代碼整體的結構

publisherConfig

package com.cc.springbootredispublisher.config;import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.StringRedisTemplate;@Configuration public class PublisherConfig {@Beanpublic StringRedisTemplate template(RedisConnectionFactory connectionFactory) {return new StringRedisTemplate(connectionFactory);} }

publisherController

package com.cc.springbootredispublisher.controller;import com.cc.springbootredispublisher.service.PublishService; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;@RestController @RequestMapping public class PublisherController {@Resourceprivate PublishService publishService;@GetMapping("/publish/{msg}")public String publih(@PathVariable String msg) {// 發送消息publishService.publish(msg);return "ok";} }

PublishServiceImpl?

package com.cc.springbootredispublisher.service.impl;import com.cc.springbootredispublisher.service.PublishService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Service;import javax.annotation.Resource;@Service public class PublishServiceImpl implements PublishService{static Logger logger = LoggerFactory.getLogger(PublishServiceImpl.class);@Resourceprivate StringRedisTemplate stringRedisTemplate;@Value("${redis.msg.topic}")private String redisTopic;@Overridepublic void publish(String msg) {stringRedisTemplate.convertAndSend(redisTopic, msg);logger.info("send msg: {}", msg);} }

PublishService

package com.cc.springbootredispublisher.service;public interface PublishService {void publish(String msg); }

application.properties

server.port=8098 ######################################################## ###REDIS (RedisProperties) redis基本配置; ######################################################## # database name spring.redis.database=0 # server host1 單機使用,對應服務器ip spring.redis.host=192.168.133.130 # server password 密碼,如果沒有設置可不配 #spring.redis.password= #connection port 單機使用,對應端口號 spring.redis.port=10190 # pool settings ...池配置 spring.redis.pool.max-idle=8 spring.redis.pool.min-idle=0 spring.redis.pool.max-active=8 spring.redis.pool.max-wait=-1redis.msg.topic=test-topic

配置文件

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.cc</groupId><artifactId>springboot-redis-publisher</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>springboot-redis-publisher</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.13.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId><version>2.4.2</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.8.0</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

?

消息的接收

代碼的整體結構

SubscriberConfig

package com.cc.springbootredissubscriber.config;import com.cc.springbootredissubscriber.Receiver; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.listener.PatternTopic; import org.springframework.data.redis.listener.RedisMessageListenerContainer; import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;import java.util.concurrent.CountDownLatch;@Configuration public class SubscriberConfig {@Value("${redis.msg.topic}")private String redisTopic;@Beanpublic RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,MessageListenerAdapter listenerAdapter){RedisMessageListenerContainer container = new RedisMessageListenerContainer();container.setConnectionFactory(connectionFactory);container.addMessageListener(listenerAdapter,new PatternTopic(redisTopic));return container;}@Beanpublic MessageListenerAdapter listenerAdapter(Receiver receiver){return new MessageListenerAdapter(receiver,"receiveMessage");}@Beanpublic Receiver receiver(CountDownLatch latch){return new Receiver(latch);}@Beanpublic CountDownLatch latch(){return new CountDownLatch(1);} }

?Receiver

package com.cc.springbootredissubscriber;import org.slf4j.Logger; import org.slf4j.LoggerFactory;import java.util.concurrent.CountDownLatch;public class Receiver {private static final Logger logger = LoggerFactory.getLogger(Receiver.class);private CountDownLatch latch;public Receiver(CountDownLatch latch) {this.latch = latch;}public void receiveMessage(String msg) {logger.info("receive msg: {} ", msg);latch.countDown();} }

application.properties

server.port=8092######################################################## ###REDIS (RedisProperties) redis基本配置; ######################################################## # database name spring.redis.database=0 # server host1 單機使用,對應服務器ip spring.redis.host=192.168.133.130 # server password 密碼,如果沒有設置可不配 #spring.redis.password= #connection port 單機使用,對應端口號 spring.redis.port=10190 # pool settings ...池配置 spring.redis.pool.max-idle=8 spring.redis.pool.min-idle=0 spring.redis.pool.max-active=8 spring.redis.pool.max-wait=-1redis.msg.topic=test-topic

配置文件

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.cc</groupId><artifactId>springboot-redis-subscriber</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>springboot-redis-subscriber</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.13.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId><version>2.4.2</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.8.0</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

?

總結

以上是生活随笔為你收集整理的Redis整合springboot实现消息队列的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。