模拟实际应用生产端和消费端消息队列:
一.创建springbootredispublisher生产者(发送消息端)
1.创建springboot项目加入maven管理
2.在pom.xml中添加依赖:
<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> <version>2.0.4.RELEASE</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <version>2.6.0</version> </dependency>
3.配置application.properties:
server.port=8098 # database name spring.redis.database=0 # server host1 单机使用,对应服务器ip spring.redis.host=192.168.1.11 # server password 密码,如果没有设置可不配 #spring.redis.password= #connection port 单机使用,对应端口号 spring.redis.port=10179 # 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=-1 redis.msg.topic=test-topic
4.创建config包并创建PublisherConfig类作为项目配置和各种工厂管理入口:
@Configuration public class PublisherConfig { //获取redis工厂 @Bean public StringRedisTemplate template(RedisConnectionFactory connectionFactory){ return new StringRedisTemplate(connectionFactory); } }
5.创建service包并建立PublishService接口:
package com.cc.springbootredispublisher.service; public interface PublishService { void publish(String msg); }
6.创建impl包并创建PublishService接口的实现类PublishServiceImpl:
package com.cc.springbootredispublisher.impl; import com.cc.springbootredispublisher.service.PublishService; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Service; import org.slf4j.Logger; import javax.annotation.Resource; @Service public class PublishServiceImpl implements PublishService { static Logger logger = LoggerFactory.getLogger(PublishServiceImpl.class); @Resource private StringRedisTemplate stringRedisTemplate; @Value("${redis.msg.topic}") private String redisTopic; @Override public void publish(String msg){ stringRedisTemplate.convertAndSend(redisTopic,msg); logger.info("send msg:{}",msg); } }
7.创建controller包并创建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 { @Resource private PublishService publishService; @GetMapping("/publish/{msg}") public String publish(@PathVariable String msg){ publishService.publish(msg); return "ok"; } }
生产者完成;
二.创建springbootredissubscriber消费者(消息接收方)
1.消费者的前三步与生产者相同,调整一下第三步配置文件中server.port=8099即可;
2.创建config包并创建SubscriberConfig类:
package com.cc.springbootredisspsubscriber.config; import com.cc.springbootredisspsubscriber.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 msgTopic; //建立监听容器,来监听队列 @Bean public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,MessageListenerAdapter listenerAdapter) { RedisMessageListenerContainer container = new RedisMessageListenerContainer(); container.setConnectionFactory(connectionFactory); container.addMessageListener(listenerAdapter, new PatternTopic(msgTopic)); return container; } @Bean public MessageListenerAdapter listenerAdapter(Receiver receiver) { return new MessageListenerAdapter(receiver,"receiveMessage"); } //做一个线程的控制器,否则主线程一旦结束,方法就停止 @Bean public Receiver receiver(CountDownLatch latch) { return new Receiver(latch); } @Bean public CountDownLatch latch() { return new CountDownLatch(1); } }
3.创建Receiver类:接收消息
package com.cc.springbootredisspsubscriber; import java.util.concurrent.CountDownLatch; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class Receiver { public static final Logger logger = LoggerFactory.getLogger(Receiver.class); private CountDownLatch latch; public Receiver(CountDownLatch latch) { // TODO Auto-generated constructor stub this.latch=latch; } public void receiveMessage(String msg) { logger.info("received msg:"+msg); } }
三、分别启动生产者和消费者并模拟消息队列传输:
1.终端模拟消息发送:
2.生产者:
3.消费者:
-----------------------------------------------------------模拟完成----------------------------------------------------------------------------------------------