前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring整合Redis哨兵模式(Sentinel)

Spring整合Redis哨兵模式(Sentinel)

作者头像
itze
发布2022-10-31 16:14:35
8780
发布2022-10-31 16:14:35
举报
文章被收录于专栏:IT者

前提

准备Jar包依赖,注意版本不要随意更换,经测试,有的版本会报错

代码语言:javascript
复制
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>1.8.6.RELEASE</version>
        </dependency>
        <!-- redis 依赖 -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>

配置文件

redis.properties

代码语言:javascript
复制
#最大空闲数(默认:8)
redis.maxIdle=300
#当连接池资源耗尽时,调用者最大阻塞时间,超时将抛出异常.单位:毫秒,默认:-1,表示永不超时.
redis.maxWait=1000
#最大连接数(默认:8)
redis.maxTotal=500
#指明是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个 (默认:false)
redis.testOnBorrow=true
redis.testOnReturn=true
redis.testWhileIdle=true
redis.blockWhenExhausted=false
redis.numTestsPerEvictionRun=1024
redis.timeBetweenEvictionRunsMillis=30000
redis.minEvictableIdleTimeMillis=1800000
redis.maxActive = 1024
redis.timeOut = 10000
#Sentinel
redis.sentinel.master = mymaster #这里是配置Redis哨兵sentinel.conf中的名字!一定要一样!
redis.sentinel.auth = 123456789  #同样是sentinel.conf中配置的密码,如果没设置可以不写
#Redis哨兵配置的所有服务器和端口号(sentinel.conf中的端口号,不是redis.conf中的!)
redis.sentinel.addr_1 = 192.168.100.1
redis.sentinel.port_1 = 26379
redis.sentinel.addr_2 = 192.168.100.2
redis.sentinel.port_2 = 26379
redis.sentinel.addr_3 = 192.168.100.3
redis.sentinel.port_3 = 26379

redis.xml

代码语言:javascript
复制
<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd">
    <!--扫描redis配置文件-->
    <context:property-placeholder location="classpath:redis.properties"/>

    <!--设置连接池-->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <!-- 最大空闲连接数 -->
        <property name="maxIdle" value="{redis.maxIdle}"/>
        <!-- 最大连接数 -->
        <property name="maxTotal" value="{redis.maxTotal}"/>
        <!-- 每次释放连接的最大数目 -->
        <property name="numTestsPerEvictionRun" value="{redis.numTestsPerEvictionRun}"/>
        <!-- 释放连接的扫描间隔(毫秒) -->
        <property name="timeBetweenEvictionRunsMillis" value="{redis.timeBetweenEvictionRunsMillis}"/>
        <!-- 连接最小空闲时间 -->
        <property name="minEvictableIdleTimeMillis" value="{redis.minEvictableIdleTimeMillis}"/>
        <!-- 获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1 -->
        <property name="maxWaitMillis" value="{redis.maxWait}"/>
        <!-- 在获取连接的时候检查有效性, 默认false -->
        <property name="testOnBorrow" value="{redis.testOnBorrow}"/>
        <property name="testOnReturn" value="{redis.testOnReturn}"/>
        <!-- 在空闲时检查有效性, 默认false -->
        <property name="testWhileIdle" value="{redis.testWhileIdle}"/>
        <!-- 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true -->
        <property name="blockWhenExhausted" value="{redis.blockWhenExhausted}"/>
    </bean>
    <!-- Sentinel模式 -->
    <bean id="sentinelConfiguration" class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
        <property name="master">
            <bean class="org.springframework.data.redis.connection.RedisNode">
                <!--这个值要和Sentinel中指定的master的值一致,不然启动时找不到Sentinel会报错的-->
                <property name="name" value="{redis.sentinel.master}"/>
            </bean>
        </property>
        <!-- 指定Sentinel的IP和端口 -->
        <property name="sentinels">
            <set>
                <bean class="org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg name="host" value="{redis.sentinel.addr_1}"/>
                    <constructor-arg name="port" value="{redis.sentinel.port_1}"/>
                </bean>
                <bean class="org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg name="host" value="{redis.sentinel.addr_2}"/>
                    <constructor-arg name="port" value="{redis.sentinel.port_2}"/>
                </bean>
                <bean class="org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg name="host" value="{redis.sentinel.addr_3}"/>
                    <constructor-arg name="port" value="{redis.sentinel.port_3}"/>
                </bean>
            </set>
        </property>
    </bean>
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <constructor-arg name="sentinelConfig" ref="sentinelConfiguration"/>
        <constructor-arg name="poolConfig" ref="jedisPoolConfig"/>
        <!-- 如果没有设置密码,下面这行可以注释掉 -->
        <property name="password" value="{redis.sentinel.auth}"/>
    </bean>
    <bean id="redisTemplate"
          class="org.springframework.data.redis.core.StringRedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory"/>
    </bean>
</beans>

Spring上下文配置文件

在SpringContext的上下文配置文件中导入以上两个配置文件,一般这个上下文配置文件名字都是类似:applicationContext.xml、springContext.xml等等等…这里就不放全部的内容,每个人的项目可能都不太一样,只放需要配置的

导入redis.properties

这个一般放在配置文件的较上面位置

代码语言:javascript
复制
    <!-- 引入配置文件 -->
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <!-- 系统配置参数 -->
        <property name="locations">
            <list>
                <!-- redis配置参数 -->
                <value>classpath:redis.properties</value>
            </list>
        </property>
    </bean>

导入redis.xml

这个一般放在配置文件的最下面

代码语言:javascript
复制
    <!-- 引入redis配置 -->
    <import resource="classpath:redis.xml"/>

之后启动项目验证就行了,至于JedisConnectionFactory是怎么实现通过Sentinel帮我们发现并且切换master主节点的,博主有时间再研究一下

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021年11月18日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前提
  • 配置文件
    • redis.properties
      • redis.xml
      • Spring上下文配置文件
        • 导入redis.properties
          • 导入redis.xml
          相关产品与服务
          云数据库 Redis
          腾讯云数据库 Redis(TencentDB for Redis)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档