安装:
wget http://download.redis.io/releases/redis-5.0.9.tar.gz
tar xzf redis-5.0.9.tar.gz
mv redis-5.0.9 /usr/local/redis
cd /usr/local/redis
make
make install
配置(一主二从三哨兵):
主服务器用于数据的读写,当发生写入操作时,主服务器会自动将数据同步给从服务器。从服务器只负责读取,不能写入。如果主服务器宕机,哨兵(sentinel)会在从服务器中选举产生一个主服务器。此时该服务器具有读写权限。
master
创建文件夹
mkdir -p /usr/local/redis-master-slave/master
cp -rf /usr/local/redis/* /usr/local/redis-master-slave/master
修改配置文件
redis.conf
bind 192.168.9.128
port 6379
requirepass Occ2018
masterauth Occ2018
daemonize yes
pidfile /var/run/redis_6379.pid
logfile "/usr/local/redis-master-slave/master/redis.log"
sentinel.conf
port 26379
daemonize yes
pidfile "/var/run/redis-sentinel.26379.pid"
sentinel monitor mymaster 192.168.9.128 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 900000
salve1
创建文件夹
mkdir -p /usr/local/redis-master-slave/slave1
cp -rf /usr/local/redis/* /usr/local/redis-master-slave/slave1
修改配置文件
redis.conf
bind 192.168.9.128
port 6380
requirepass Occ2018
masterauth Occ2018
daemonize yes
pidfile /var/run/redis_6380.pid
logfile "/usr/local/redis-master-slave/slave1/redis.log"
slaveof 192.168.9.128 6379
sentinel.conf
port 26380
daemonize yes
pidfile "/var/run/redis-sentinel.26380.pid"
sentinel monitor mymaster 192.168.9.128 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 900000
salve2
创建文件夹
mkdir -p /usr/local/redis-master-slave/slave2
cp -rf /usr/local/redis/* /usr/local/redis-master-slave/slave2
修改配置文件
redis.conf
bind 192.168.9.128
port 6381
requirepass Occ2018
masterauth Occ2018
daemonize yes
pidfile /var/run/redis_6381.pid
logfile "/usr/local/redis-master-slave/slave2/redis.log"
slaveof 192.168.9.128 6379
sentinel.conf
port 26381
daemonize yes
pidfile "/var/run/redis-sentinel.26381.pid"
sentinel monitor mymaster 192.168.9.128 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 900000
启动运行所有节点:
cd /usr/local/redis-master-slave/master
./redis-server redis.conf
./redis-server sentinel.conf --sentinel
微服务配置
应用配置(application.yml)
spring:
redis:
# host: 192.168.9.128
# port: 6379
password: Occ2018
database: 0
pool:
max-idle: 50
min-idle: 0
max-active: 50
max-wait: -1
max-total: 50
sentinel:
master: mymaster
nodes: 192.168.9.128:26379,192.168.9.128:26380,192.168.9.128:26381
应用配置(application.properties)
#spring.redis.host=192.168.9.128
#spring.redis.port=6379
spring.redis.password=Occ2018
spring.redis.database=0
spring.redis.pool.max-idle=50
spring.redis.pool.min-idle=0
spring.redis.pool.max-active=50
spring.redis.pool.max-wait=-1
spring.redis.pool.max-total=50
spring.redis.sentinel.master=mymaster
spring.redis.sentinel.nodes=192.168.9.128:26379,192.168.9.128:26380,192.168.9.128:26381
查询缓存配置(applicationContext-cache.xml)
<!-- Redis查询缓存 - start -->
<!-- 开启缓存注解 -->
<cache:annotation-driven cache-manager="searchCacheManager"/>
<!-- Redis缓存管理器 -->
<bean id="searchCacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
<constructor-arg name="redisOperations" ref="redisTemplate"/>
</bean>
<!-- Redis模板 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory"/>
<property name="keySerializer" ref="stringSerializer"/>
<property name="valueSerializer" ref="jackson2JsonSerializer"/>
</bean>
<bean name="stringSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
<bean name="jackson2JsonSerializer"
class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>
<!-- Jedis客户端连接工厂 -->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:database="${spring.redis.database}" p:password="${spring.redis.password}">
<constructor-arg name="sentinelConfig" ref="sentinelConfig"/>
<constructor-arg name="poolConfig" ref="jedisPoolConfig"/>
</bean>
<!-- Redis哨兵 -->
<bean id="sentinelConfig" class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
<constructor-arg name="master" value="${spring.redis.sentinel.master}"/>
<constructor-arg name="sentinelHostAndPorts" value="${spring.redis.sentinel.nodes}"/>
</bean>
<!-- Jedis连接池 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${spring.redis.pool.max-total}"/>
<property name="maxIdle" value="${spring.redis.pool.max-idle}"/>
<property name="maxWaitMillis" value="${spring.redis.pool.max-wait}"/>
<property name="testOnBorrow" value="true"/>
</bean>
<!-- Redis查询缓存 - end -->
参考资料:
- http://blog.mayongfa.cn/258.html
- https://www.cnblogs.com/mafly/p/redis_cluster.html
- https://tecadmin.net/install-ruby-latest-stable-centos/
- https://blog.csdn.net/liuchuanhong1/article/details/54601037
- https://www.cnblogs.com/HendSame-JMZ/p/7722104.html
- https://blog.csdn.net/u011521890/article/details/78088799
网友评论