Redis-Sentinel 数据源配置

Redis-Sentinel 数据源配置

1.redis配置文件 : redis.properties

# Redis settings
#sentinel_node_1
redis.sentinel1.host=192.168.0.1
redis.sentinel1.port=8001
#sentinel_node_2
redis.sentinel2.host=192.168.0.1
redis.sentinel2.port=8002
#sentinel_node_3
redis.sentinel3.host=192.168.0.1
redis.sentinel3.port=8003
#sentinel_auth
redis.sentinel.masterName=TestMaster
redis.sentinel.password=testmaster123
 
redis.maxIdle=500
redis.maxTotal=5000
redis.maxWaitTime=1000
redis.minIdle=300
redis.testOnBorrow=true

2.redis数据源的配置文件:redis-datasource.xml

  这里给大家介绍两种配置模式  spring-redis配置模式  非spring-redis配置模式

  2.1spring-redis配置模式:

  maven依赖:

    <dependency>
        <groupId>redis.clientsgroupId>
        <artifactId>jedisartifactId>
        <version>2.8.0version>
    dependency>
    
    <dependency>
        <groupId>org.springframework.datagroupId>
        <artifactId>spring-data-redisartifactId>
        <version>1.6.4.RELEASEversion>
    dependency>

 


    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxTotal" value="${redis.maxTotal}" />
        <property name="minIdle" value="${redis.minIdle}" />
        <property name="maxWaitMillis" value="${redis.maxWaitTime}" />
        <property name="maxIdle" value="${redis.maxIdle}" />
        <property name="testOnBorrow" value="${redis.testOnBorrow}" />
        <property name="testOnReturn" value="true" />
        <property name="testWhileIdle" value="true" />
    bean>
 
    <bean id="sentinelConfiguration"
        class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
        <property name="master">
            <bean class="org.springframework.data.redis.connection.RedisNode">
                <property name="name" value="${redis.sentinel.masterName}">property>
            bean>
        property>
        <property name="sentinels">
            <set>
                <bean class="org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg name="host"
                        value="${redis.sentinel1.host}">constructor-arg>
                    <constructor-arg name="port"
                        value="${redis.sentinel1.port}">constructor-arg>
                bean>
                <bean class="org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg name="host"
                        value="${redis.sentinel2.host}">constructor-arg>
                    <constructor-arg name="port"
                        value="${redis.sentinel2.port}">constructor-arg>
                bean>
                <bean class="org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg name="host"
                        value="${redis.sentinel3.host}">constructor-arg>
                    <constructor-arg name="port"
                        value="${redis.sentinel3.port}">constructor-arg>
                bean>
            set>
        property>
    bean>
 
    <bean id="connectionFactory"
        class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:password="${redis.sentinel.password}">
        <constructor-arg name="sentinelConfig" ref="sentinelConfiguration">constructor-arg>
        <constructor-arg name="poolConfig" ref="poolConfig">constructor-arg>
    bean>
 
    <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
        <property name="connectionFactory" ref="connectionFactory" />
    bean>
    
    <bean id="redisService" class="com.usi.hlqb.service.redis.RedisService">
        <property name="redisTemplate" ref="redisTemplate">property>
    bean>
    

  2.2 非spring-redis的配置模式:

  maven依赖:

 <dependency>
   <groupId>com.imwinstongroupId>
    <artifactId>JedisBaseartifactId>
    <version>2.1.17-sentinel-4version>
 dependency>
 <dependency>
   <groupId>redis.clientsgroupId>
    <artifactId>jedisartifactId>
    <version>2.8.0version>
 dependency>
 <dependency>
   <groupId>com.google.guavagroupId>
    <artifactId>guavaartifactId>
    <version>18.0version>
dependency>
<dependency>
  <groupId>com.esotericsoftwaregroupId>
   <artifactId>kryoartifactId>
   <version>3.0.1version>
dependency>

 

    
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">   
        <property name="maxIdle"       value="${redis.maxIdle}" />  
        <property name="maxTotal"       value="${redis.maxTotal}" />  
        <property name="testOnBorrow"  value="${redis.testOnBorrow}" />  
    bean>  
   <bean id="jedisPool" class="redis.clients.jedis.JedisSentinelPool"> <constructor-arg index="0" value="myMaster" /> <constructor-arg index="1"> <set> <value>192.168.0.1:8001value> <value>192.168.0.1:8002value> <value>192.168.0.1:8003value> set> constructor-arg> <constructor-arg index="2" ref="jedisPoolConfig" /> <constructor-arg index="3" value="${redis.sentinel.password}" /> bean> <bean id="jedisBase" class="com.imwinston.redis.jedisbase.JedisBase"> <property name="pool" ref="jedisPool" /> bean> <bean id="sqlDs" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="org.sqlite.JDBC"/> <property name="url" value="jdbc:sqlite:"/> <property name="username" value=""/> <property name="password" value=""/> bean>

 

posted on 2018-09-13 15:10 【cosmo】 阅读( ...) 评论( ...) 编辑 收藏

你可能感兴趣的:(Redis-Sentinel 数据源配置)