你需要什么
- 大约 15 + 15 分钟
- IntelliJ IDEA或其他编辑器
- JDK 1.8或更高版本
- Maven 3.2+
- Redis server
你会建立什么
您将构建一个使用StringRedisTemplate
发布字符串消息的应用程序,并使用MessageListenerAdapter
为其提供POJO
订阅。
使用
Spring Data Redis
作为发布消息的手段可能听起来很奇怪,但正如您将发现的那样,Redis不仅提供了NoSQL
数据存储,还提供了消息传递系统。
构建步骤
1、安装redis
2、添加maven
依赖。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
3、新建 Redis
消息接收器
Receiver是一个简单的POJO,它定义了一种接收消息的方法。正如您将Receiver注册为消息侦听器时所看到的,您可以根据需要命名消息处理方法。
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();
}
}
4、注册侦听器(listener )并发送消息(message)
Spring Data Redis
提供了使用Redis
发送和接收消息所需的所有组件。具体来说,你需要配置:
- 连接工厂
- 消息侦听器容器
- Redis模板
您将使用 Redis模板
发送消息,并且您将向 Receiver 注册 消息侦听器容器
,以便它可以接收消息。
连接工厂
驱动 模板 和 消息侦听器容器,使它们能够连接到 Redis服务器。
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);
/** RedisConnectionFactory 一个基于Jedis Redis库的JedisConnectionFactory实例。连接工厂被注入到消息监听器容器和Redis模板中。 */
@Bean
RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
MessageListenerAdapter listenerAdapter) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
/** 通过MessageListenerAdapter包装Receiver后,添加到消息侦听器容器中,并制定监听"chat" topic */
container.addMessageListener(listenerAdapter, new PatternTopic("chat"));
return container;
}
/** 消息侦听器适配器还配置为在消息到达时调用Receiver上的receiveMessage()方法 */
@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);
}
}
5、配置 redis
服务。
# Redis 数据库索引(默认为0)
spring.redis.database=0
# Redis 服务器地址
spring.redis.host=192.168.56.101
# Redis 服务器连接端口
spring.redis.port=6379
# Redis 服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=10000
测试结果
启动 redis
,然后确定能够远程连接了。执行结果如下: