redis 哨兵模式配置 和 spring data redis 哨兵配置 总结

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
简介: redis 哨兵模式配置 和 spring data redis 哨兵配置 总结

主要是关于bind配置,密码认证,ip的问题。然后再次总结一下。学习了哨兵配置,但是在实践的过程中遇到了问http://blog.csdn.net/men_wen/article/details/72724406从帖子

redis版本3.2.100

分别有3个Sentinel节点,1个主节点,2个从节点组成一个Redis Sentinel。

角色

ip

端口

master

192.168.30.249

6379

slave1

192.168.30.123

6379

slave2

192.168.30.254

6379

Sentinel1

192.168.30.249

26379

Sentinel2

192.168.30.123

26379

Sentinel3

192.168.30.254

26379

192.168.30.249 配置

redis.conf配置

主要修改的配置如下,其他配置默认。

##这里绑定真实ip。让客户端可以远程访问
bind 192.168.30.249
port 6379
##开启日志级别是debug,方便看日志调试。
loglevel debug
##日志位置
logfile "D:/myStuepss/redis-x64-3.2.100/redisserver.log"
##配置了master的认证密码,虽然当前角色是master,但是一旦挂掉恢复后,会变为salve,这个时候再连接新master的时候,如果master需要密码认证,就需要配置。
masterauth "redistest@123"
##客户端连接的时候需要密码认证。
requirepass "redistest@123"

sentinel配置

#这里配置成真实ip。为了客户端可以远程访问,例如:我用了spring data redis. 这里如果不配置ip,会报错的。
bind 192.168.30.249
port 26379
dir "D:\\myStuepss\\Redis-x64-3.2.100\\temp"
#监控master
sentinel monitor mymaster 192.168.30.249 6379 2
#要配置上master的认证密码,否则监控不到
sentinel auth-pass mymaster redistest@123
#配置上日志,方便排除错误,查看状态。
logfile "D:/myStuepss/redis-x64-3.2.100/sentinellog.log"
loglevel debug


192.168.30.254配置

redis.conf配置

主要修改的配置如下,其他配置默认。


##这里绑定真实ip。让客户端可以远程访问
bind 192.168.30.254
port 6379
slaveof 192.168.30.249 6379
##配置了master的认证密码,作为salve要同步master的数据,不配置就连不上master
masterauth "redistest@123"
##客户端连接的时候需要密码认证。
requirepass "redistest@123"

sentinel配置

#这里配置成真实ip。为了客户端可以远程访问,例如:我用了spring data redis. 这里如果不配置ip,会报错的。
bind 192.168.30.254
port 26379
dir "D:\\myStuepss\\Redis-x64-3.2.100\\temp"
#监控master
sentinel monitor mymaster 192.168.30.249 6379 2
#要配置上master的认证密码,否则监控不到
sentinel auth-pass mymaster redistest@123
#配置上日志,方便排除错误,查看状态。
logfile "D:/myStuepss/redis-x64-3.2.100/sentinellog.log"
loglevel debug


192.168.30.123配置

redis.conf配置

主要修改的配置如下,其他配置默认。

##这里绑定真实ip。让客户端可以远程访问
bind 192.168.30.123
port 6379
slaveof 192.168.30.249 6379
##配置了master的认证密码,作为salve要同步master的数据,不配置就连不上master
masterauth "redistest@123"
##客户端连接的时候需要密码认证。
requirepass "redistest@123"


sentinel配置



#这里配置成真实ip。为了客户端可以远程访问,例如:我用了spring data redis. 这里如果不配置ip,会报错的。
bind 192.168.30.123
port 26379
dir "D:\\myStuepss\\Redis-x64-3.2.100\\temp"
#监控master
sentinel monitor mymaster 192.168.30.249 6379 2
#要配置上master的认证密码,否则监控不到
sentinel auth-pass mymaster redistest@123
#配置上日志,方便排除错误,查看状态。
logfile "D:/myStuepss/redis-x64-3.2.100/sentinellog.log"
loglevel debug


spring data redis 哨兵配置

  • spring版本4.3.5.RELEASE
  • spring-data-redis版本1.8.9.RELEASE
  • jedis版本2.9.0

redis.properties配置

#主机和端口号
redis.host1.cloudq=192.168.30.254
redis.port1.cloudq=26379
redis.host2.cloudq=192.168.30.249
redis.port2.cloudq=26379
redis.host3.cloudq=192.168.30.123
redis.port3.cloudq=26379
#JedisPoolConfig的参数
#最大连接数
redis.pool.maxTotal=30
#最大空闲时间
redis.pool.maxIdle=10
#每次最大连接数
redis.pool.numTestsPerEvictionRun=1024
#释放扫描的扫描间隔
redis.pool.timeBetweenEvictionRunsMillis=30000
#连接的最小空闲时间
redis.pool.minEvictableIdleTimeMillis=1800000
#连接控歘按时间多久后释放,当空闲时间>该值且空闲连接>最大空闲连接数时直接释放
redis.pool.softMinEvictableIdleTimeMillis=10000
#获得链接时的最大等待毫秒数,小于0:阻塞不确定时间,默认-1
redis.pool.maxWaitMillis=1500
#在获得链接的时候检查有效性,默认false
redis.pool.testOnBorrow=true
#在空闲时检查有效性,默认false
redis.pool.testWhileIdle=true
#连接耗尽时是否阻塞,false报异常,true阻塞超时,默认true
redis.pool.blockWhenExhausted=false
#JedisConnectionFactory的参数
#超时时间,默认:2000
redis.timeout.cloudq=3000
#密码
redis.password.cloudq=redistest@123
#是否使用连接池,默认true
redis.usePool=true
#使用数据库的索引,0-15之间的数字,默认:0
redis.dbIndex=0
#是否使用数据类型的转换,默认:true
#redis.convertPipelineAndTxResults
#哨兵配置
#redis.sentinelConfig
redis.expiration=3000

spring-redis.xml配置

<?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:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
">
    <!--引入配置文件-->
    <bean id="placeholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="order" value="1"/>
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
        <property name="locations">
            <list>
                <value>classpath:redis.properties</value>
            </list>
        </property>
    </bean>
    <!--配置 jedis pool-->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <!-- 最大连接数 -->
        <property name="maxTotal" value="${redis.pool.maxTotal}"/>
        <!-- 最大空闲时间 -->
        <property name="maxIdle" value="${redis.pool.maxIdle}"/>
        <!-- 每次最大连接数 -->
        <property name="numTestsPerEvictionRun" value="${redis.pool.numTestsPerEvictionRun}"/>
        <!-- 释放扫描的扫描间隔 -->
        <property name="timeBetweenEvictionRunsMillis" value="${redis.pool.timeBetweenEvictionRunsMillis}"/>
        <!-- 连接的最小空闲时间 -->
        <property name="minEvictableIdleTimeMillis" value="${redis.pool.minEvictableIdleTimeMillis}"/>
        <!-- 连接控歘按时间多久后释放,当空闲时间>该值且空闲连接>最大空闲连接数时直接释放 -->
        <property name="softMinEvictableIdleTimeMillis" value="${redis.pool.softMinEvictableIdleTimeMillis}"/>
        <!-- 获得链接时的最大等待毫秒数,小于0:阻塞不确定时间,默认-1 -->
        <property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}"/>
        <!-- 在获得链接的时候检查有效性,默认false -->
        <property name="testOnBorrow" value="${redis.pool.testOnBorrow}"/>
        <!-- 在空闲时检查有效性,默认false -->
        <property name="testWhileIdle" value="${redis.pool.testWhileIdle}"/>
        <!-- 连接耗尽时是否阻塞,false报异常,true阻塞超时 默认:true-->
        <property name="blockWhenExhausted" value="${redis.pool.blockWhenExhausted}"/>
    </bean>
    <!--配置redisSentinelConfiguration 哨兵模式-->
    <bean id="redisSentinelConfiguration"
          class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
        <property name="master">
            <bean class="org.springframework.data.redis.connection.RedisNode">
                <property name="name" value="mymaster">
                </property>
            </bean>
        </property>
        <property name="sentinels">
            <set>
                <bean class="org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg name="host" value="${redis.host1.cloudq}"/>
                    <constructor-arg name="port" value="${redis.port1.cloudq}"/>
                </bean>
                <bean class="org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg name="host" value="${redis.host2.cloudq}"/>
                    <constructor-arg name="port" value="${redis.port2.cloudq}"/>
                </bean>
                <bean class="org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg name="host" value="${redis.host3.cloudq}"/>
                    <constructor-arg name="port" value="${redis.port3.cloudq}"/>
                </bean>
            </set>
        </property>
    </bean>
    <bean id="redisConnectionFactorySentinel"
          class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
          p:password="${redis.password.cloudq}">
        <constructor-arg name="sentinelConfig" ref="redisSentinelConfiguration"></constructor-arg>
        <constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>
    </bean>
    <!-- redis template definition -->
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
          p:connection-factory-ref="redisConnectionFactorySentinel" p:keySerializer-ref="genericJackson2JsonRedisSerializer"
          p:valueSerializer-ref="genericJackson2JsonRedisSerializer"
          p:hashKeySerializer-ref="genericJackson2JsonRedisSerializer"
          p:hashValueSerializer-ref="genericJackson2JsonRedisSerializer"
          p:enableTransactionSupport="false"/>
    <bean id="genericJackson2JsonRedisSerializer"
          class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>
    <!-- 配置RedisCacheManager 实现spring框架提供的缓存 -->
   <!-- <bean id="redisCacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
        <constructor-arg name="redisOperations" ref="redisTemplate"/>
        <property name="defaultExpiration" value="${redis.expiration}"/>
    </bean>-->
</beans>

junit测试

import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/spring-redis.xml"}
)
public class LockTest {
    @Autowired
    private RedisTemplate redisTemplate;
    @org.junit.Test
    public void test() throws Exception{
        ValueOperations valueOperations =   redisTemplate.opsForValue();
        for(int i =0;i<10000;i++){
            try{
                valueOperations.set("test:"+i,i);
            }catch (Exception e){
                e.printStackTrace();
            }
            Thread.sleep(1000);
        }
    }
}
相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
13天前
|
SQL Java 数据库连接
(自用)Spring常用配置
(自用)Spring常用配置
16 0
|
1月前
|
SQL Java 数据库连接
挺详细的spring+springmvc+mybatis配置整合|含源代码
挺详细的spring+springmvc+mybatis配置整合|含源代码
42 1
|
5天前
|
存储 安全 Java
第2章 Spring Security 的环境设置与基础配置(2024 最新版)(下)
第2章 Spring Security 的环境设置与基础配置(2024 最新版)(下)
15 0
|
5天前
|
安全 Java 数据库
第2章 Spring Security 的环境设置与基础配置(2024 最新版)(上)
第2章 Spring Security 的环境设置与基础配置(2024 最新版)
28 0
|
7天前
|
安全 Java Spring
Spring Security 5.7 最新配置细节(直接就能用),WebSecurityConfigurerAdapter 已废弃
Spring Security 5.7 最新配置细节(直接就能用),WebSecurityConfigurerAdapter 已废弃
19 0
|
7天前
|
安全 Java 应用服务中间件
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
24 0
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
|
12天前
|
JSON Java 数据库连接
属性注入掌握:Spring Boot配置属性的高级技巧与最佳实践
属性注入掌握:Spring Boot配置属性的高级技巧与最佳实践
22 1
|
12天前
|
Java 数据库连接 Spring
简化配置,提高灵活性:Spring中的参数化配置技巧
简化配置,提高灵活性:Spring中的参数化配置技巧
19 0
|
12天前
|
Java Shell 测试技术
一次配置,多场景适用:Spring Boot多套配置文件的深度剖析
一次配置,多场景适用:Spring Boot多套配置文件的深度剖析
27 0
一次配置,多场景适用:Spring Boot多套配置文件的深度剖析
|
16天前
|
Java 容器
SpringBoot使用配置注解开启自动配置功能&整合spring-boot-configuration-processor
SpringBoot使用配置注解开启自动配置功能&整合spring-boot-configuration-processor
14 0

热门文章

最新文章