由于websocket session不能序列化,所以不能存储在redis中。故在分布式系统中,可以通过把websocket 的session存储在服务器本地map,然后把消息发布到redis指定的频道上,每个服务器节点都订阅该频道,这样的话,消息一发布,每个节点都能接受到该消息,然后再从map中获取session,来完成消息推送。
两种方式都能实现redis的发布/订阅功能
第一种:spring-data-redis实现
1、spring配置
<beans xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.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.xsd">
<!-- 引入配置文件 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<!-- 从JVM中读取配置 -->
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
<!-- 忽略没找到的配置 -->
<property name="ignoreResourceNotFound" value="true"/>
<property name="locations">
<list>
<value>classpath*:jdbc.properties</value>
<value>classpath*:redis.properties</value>
<value>classpath*:ftpPoolConfig.properties</value>
</list>
</property>
</bean>
<!-- 数据源配置DruidDataSource -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<!-- 可选项,druid会自动根据URL判断 -->
<property name="driverClassName" value="${jdbc_driverClassName}"/>
<!-- 基本属性 url、user、password -->
<property name="url" value="${jdbc_url}"/>
<property name="username" value="${jdbc_user}"/>
<property name="password" value="${jdbc_password}"/>
<!-- 配置监控统计拦截的filters -->
<property name="filters" value="${jdbc_filters}"/>
<!-- 数据库密码加密 -->
<!-- <property name="connectionProperties" value="${jdbc_connectionProperties}" /> -->
<!-- 配置初始化大小、最小、最大 -->
<property name="initialSize" value="${jdbc_initialSize}"/>
<property name="minIdle" value="${jdbc_minIdle}"/>
<property name="maxActive" value="${jdbc_maxActive}"/>
<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="${jdbc_maxWait}"/>
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="${jdbc_timeBetweenEvictionRunsMillis}"/>
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="${jdbc_minEvictableIdleTimeMillis}"/>
<property name="validationQuery" value="${jdbc_validationQuery}"/>
<property name="testWhileIdle" value="${jdbc_testWhileIdle}"/>
<property name="testOnBorrow" value="${jdbc_testOnBorrow}"/>
<property name="testOnReturn" value="${jdbc_testOnReturn}"/>
<!-- 打开PSCache,并且指定每个连接上PSCache的大小 --><!-- oracle true|mysql false -->
<property name="poolPreparedStatements" value="${jdbc_poolPreparedStatements}"/>
<property name="maxOpenPreparedStatements" value="${jdbc_maxOpenPreparedStatements}"/>
<!-- 打开removeAbandoned功能 --><!-- 确认泄露的时候再打开,上线不打开。影响性能 -->
<property name="removeAbandoned" value="${jdbc_re