redis的下载安装
参考地址:
http://www.runoob.com/redis/redis-install.html
如上面无法下载可通过下面地址下载:
链接:https://pan.baidu.com/s/1aO8TJr4al2ZDeLul-xwWQA
提取码:brym
整合redis
仅仅简单整合redis的订阅发布功能,不使用缓存功能
首先java操作Redis的方式有两种(据我所知)
1. 使用Spring Data Redis操作Redis
2. 使用Jedis 操作redis(参考地址:https://blog.csdn.net/lovelichao12/article/details/75333035/)
我们这里采用第一种,第二种给个参考地址感兴趣的可以看看
个人理解
既然是实现订阅和发布,那就有消息的发送方和接收方。
发送方 发送消息
1.通过JedisConnection的Pub/Sub相关的方法来向Redis服务发布消息
2.RedisTemplate的convertAndSend方法(使用的)
接收方 包括监听消息并输出,需要实现消息监听类并在xml中注册这个类
两种方式实现监听类
- 一种是实现org.springframework.data.redis.connection.MessageListener接口,实现onMessage方法(使用的)
- 使用自己定义的类,方法名称自己定义
两种方式来实现消息监听容器的声明
- 一种是通过Redis的命名空间
- 一种是定义Bean (使用的)
参考博客 http://blog.51cto.com/aiilive/1627478 来理解这两种方式
maven引入Redis
pom.xml
<!--redis 订阅发布 -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.6.4.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
在resource资源包下建立redis文件下建立redis.properties
# Redis settings
redis.host=127.0.0.1
redis.port=6379
redis.timeOut=10000
redis.usePool=true
# redis.pass=
redis.maxIdle=300
redis.maxTotal=1024
redis.maxWaitMillis=10000
redis.testOnBorrow=true
redis.maxActive=3000
redis.maxWait=10000
# channel
redis.test.channel1=channel1
redis.test.channel2=channel2
并在spring文件夹下建立redis.xml,其中com.sys.service.impl.RedisTestMessageListener在后面会创建
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd">
<!-- 加载redis文件 -->
<context:property-placeholder location="classpath:redis/redis.properties" />
<!-- redis 整合 -->
<!-- jedis pool配置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.maxActive}"/>
<property name="maxIdle" value="${redis.maxIdle}"/>
<property name="maxWaitMillis" value="${redis.maxWait}"/>
<property name="testOnBorrow" value="${redis.testOnBorrow}"/>
</bean>
<!-- spring data redis -->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="poolConfig" ref="jedisPoolConfig"></property>
<property name="hostName" value="${redis.host}"></property>
<property name="port" value="${redis.port}"></property>
<property name="timeout" value="${redis.timeOut}"></property>
<property name="usePool" value="${redis.usePool}"></property>
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory"/>
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="hashKeySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
</property>
</bean>
<!-- 消息接受类 -->
<bean id="redisTestMessageListener" class="com.sys.service.impl.RedisTestMessageListener"></bean>
<bean id="redisContainer" class="org.springframework.data.redis.listener.RedisMessageListenerContainer">
<property name="connectionFactory" ref="jedisConnectionFactory" />
<property name="taskExecutor">
<bean class="org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler">
<property name="poolSize" value="2"></property>
</bean>
</property>
<property name="messageListeners">
<map>
<entry key-ref="redisTestMessageListener">
<list>
<!-- 普通订阅,订阅具体的频道 -->
<bean class="org.springframework.data.redis.listener.ChannelTopic">
<constructor-arg value="${redis.test.channel1}" />
</bean>
<bean class="org.springframework.data.redis.listener.ChannelTopic">
<constructor-arg value="${redis.test.channel2}" />
</bean>
</list>
</entry>
</map>
</property>
</bean>
<bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</beans>
web.xml引入redis.xml的配置
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/redis.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
到此配置已经完成
参考
Redis消息的发布/订阅 https://blog.csdn.net/m15732622413/article/details/78414891
java+redis+spring mvc实现发布订阅 https://blog.csdn.net/m0_37064092/article/details/54617297
redis 消息发布订阅与消息队列 https://blog.csdn.net/jslcylcy/article/details/78201812
代码测试
创建相应的controller service 发送消息
@Service("sendRedisService")
public class SendRedisServiceimpl implements SendRedisService {
@Autowired
public RedisTemplate<String, Object> redisTemplate;
@Override
public void sendMessage(String channel, String message) {
System.out.println("开始向"+channel+"发布消息"+ message);
redisTemplate.convertAndSend(channel, message);
System.out.println("发布成功!");
}
创建 redis.xml中定义的 监听类
@Component
public class RedisTestMessageListener implements MessageListener {
@Resource
public RedisTemplate<String, Object> redisTemplate;
//redisTemplate 注入失败 使用这种方法 会空指针
/* @Override
public void onMessage(Message message, byte[] pattern) {
String topic = (String) redisTemplate.getStringSerializer().deserialize(message.getChannel());
Object body = redisTemplate.getValueSerializer().deserialize(message.getBody());
String msg = String.valueOf(body);
System.out.println(topic);
System.out.println("接受消息:" + msg);
}
*/
//使用这种方法 会出现中文乱码
@Override
public void onMessage(Message message, byte[] pattern) {
System.out.println("channel:" + new String(message.getChannel())
+ ",message:" + new String(message.getBody()));
}
}
此时请求发送消息,会输出信息(当然你的redis是启动的)
输入 redis-cli.exe
订阅channel1 subscribe channel1
同样可以监听到消息
发布消息