一、Redis集群:
集群解决并发的问题;
哨兵解决高可用的问题;
二、集群搭建:
//基于redis5.0;
1)启动三台Linux系统,且用docker启动三台redis系统;
2)配置文件: 不要配置密码;
6379/redis.conf配置:
protected-mode no #非保护模式
port 6379 #端口
pidfile /var/run/redis_6379.pid #需要修改为 reids_{port}.pid 的形式
appendonly yes #开启AOF日志 指定持久化方式
cluster-enabled yes #开启集群
cluster-config-file nodes-6379.conf #集群的配置文件 nodes_{port}.conf的形式
cluster-node-timeout 5000 #超时时间
6380/redis.conf配置:
protected-mode no #非保护模式
port 6380 #端口
pidfile /var/run/redis_6380.pid #需要修改为 reids_{port}.pid 的形式
appendonly yes #开启AOF日志 指定持久化方式
cluster-enabled yes #开启集群
cluster-config-file nodes-6380.conf #集群的配置文件 nodes_{port}.conf的形式
cluster-node-timeout 5000 #超时时间
3)创建容器:
主容器:
docker run -p 6379:6379 -p 16379:16379 -v /usr/local/docker/redis-cluster/6379/conf/redis.conf:/usr/local/etc/redis/redis.conf -v /usr/local/docker/redis-cluster/6379/data:/data --privileged=true --name redis1 -d redis redis-server /usr/local/etc/redis/redis.conf
从容器:
docker run -p 6380:6380 -p 16380:16380 -v /usr/local/docker/redis-cluster/6380/conf/redis.conf:/usr/local/etc/redis/redis.conf -v /usr/local/docker/redis-cluster/6380/data:/data --privileged=true --name redis2 -d redis redis-server /usr/local/etc/redis/redis.conf
4)配置集群:
//配置集群失败,重写配置前记得删除每个容器数据卷中data的数据;
docker exec -it redis1 bash
cd /usr/local/bin
./redis-cli --cluster create 192.168.80.135:6379 192.168.80.135:6380 192.168.80.136:6379 192.168.80.136:6380 192.168.80.137:6379 192.168.80.137:6380 --cluster-replicas 1
输入yes:
5)连接:
./redis-cli -c -h 192.168.80.135 -p 6379
6)相关命令:
cluster info
cluster nodes
7)docker连接:
docker exec -it redis1 redis-cli -p 6379
8)关闭开启redis集群:
docker stop redis1 redis2
docker start redis1 redis2
三、spring-data-redis中配置redis集群:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 配置redis连接池对象 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- 最大空闲数 -->
<property name="maxIdle" value="50" />
<!-- 最大连接数 -->
<property name="maxTotal" value="100" />
<!-- 最大等待时间 -->
<property name="maxWaitMillis" value="20000" />
</bean>
<!-- stringRedisSerializer序列化器 -->
<bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
<!-- Redis集群配置 -->
<bean id="redisClusterConfiguration"
class="org.springframework.data.redis.connection.RedisClusterConfiguration">
<property name="clusterNodes">
<set>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="192.168.80.135"/>
<constructor-arg name="port" value="6379"/>
</bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="192.168.80.135"/>
<constructor-arg name="port" value="6380"/>
</bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="192.168.80.136"/>
<constructor-arg name="port" value="6379"/>
</bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="192.168.80.136"/>
<constructor-arg name="port" value="6380"/>
</bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="192.168.80.137"/>
<constructor-arg name="port" value="6379"/>
</bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="192.168.80.137"/>
<constructor-arg name="port" value="6380"/>
</bean>
</set>
</property>
</bean>
<!-- 配置redis连接工厂 -->
<bean id="connectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<!-- 连接池配置 -->
<property name="poolConfig" ref="poolConfig" />
<!--集群配置-->
<constructor-arg name="clusterConfig" ref="redisClusterConfiguration"/>
</bean>
<!-- 配置redis模板对象 -->
<bean class="org.springframework.data.redis.core.RedisTemplate">
<!-- 配置连接工厂 -->
<property name="connectionFactory" ref="connectionFactory" />
<!--配置序列化器-->
<property name="keySerializer" ref="stringRedisSerializer"/>
<!--<property name="valueSerializer" ref="stringRedisSerializer"/>-->
</bean>
<!--配置监听器的实现类-->
<bean id="messageListener" class="com.qf.listener.MyMessageListener"/>
<!-- 配置消息监听容器 -->
<bean id="messageListenerContainer" class="org.springframework.data.redis.listener.RedisMessageListenerContainer"
destroy-method="destroy">
<!-- redis连接工厂 -->
<property name="connectionFactory" ref="connectionFactory"/>
<!-- 配置监听配置 -->
<property name="messageListeners">
<map>
<!-- 配置监听器 -->
<entry key-ref="messageListener">
<!-- 配置监听的渠道 -->
<bean class="org.springframework.data.redis.listener.ChannelTopic">
<!-- -->
<constructor-arg name="name" value="channel1"/>
</bean>
</entry>
</map>
</property>
</bean>
</beans>
四、springboot整合redis
spring:
redis:
cluster:
nodes: 192.168.80.135:6379,192.168.80.135:6380,192.168.80.136:6379,192.168.80.136:6380,192.168.80.137:6379,192.168.80.137:6380
jedis:
pool:
max-active: 100