java使用 redis-sentinel

八角塘塘主 2017-08-28

redis.properties:

session.time.out=900

#redis.ip=127.0.0.1

#redis.port=6379

# Redis settings  

#sentinel1\u7684IP\u548C\u7AEF\u53E3  

im.hs.server.redis.sentinel1.host=192.168.13.62

im.hs.server.redis.sentinel1.port=26382

#sentinel2\u7684IP\u548C\u7AEF\u53E3  

im.hs.server.redis.sentinel2.host=192.168.13.63

im.hs.server.redis.sentinel2.port=26382

#sentinel\u7684\u9274\u6743\u5BC6\u7801  

im.hs.server.redis.sentinel.masterName=redis-sentinel(要和sentinel中的名字一样)

#im.hs.server.redis.sentinel.password=hezhixiong  

#\u6700\u5927\u95F2\u7F6E\u8FDE\u63A5\u6570  

im.hs.server.redis.maxIdle=500  

#\u6700\u5927\u8FDE\u63A5\u6570\uFF0C\u8D85\u8FC7\u6B64\u8FDE\u63A5\u65F6\u64CD\u4F5Credis\u4F1A\u62A5\u9519  

im.hs.server.redis.maxTotal=5000  

im.hs.server.redis.maxWaitTime=1000  

im.hs.server.redis.testOnBorrow=true  

#\u6700\u5C0F\u95F2\u7F6E\u8FDE\u63A5\u6570\uFF0Cspring\u542F\u52A8\u7684\u65F6\u5019\u81EA\u52A8\u5EFA\u7ACB\u8BE5\u6570\u76EE\u7684\u8FDE\u63A5\u4F9B\u5E94\u7528\u7A0B\u5E8F\u4F7F\u7528\uFF0C\u4E0D\u591F\u7684\u65F6\u5019\u4F1A\u7533\u8BF7\u3002  

im.hs.server.redis.minIdle=300 

spring-redis.xml:

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop"

xsi:schemaLocation="http://www.springframework.org/schema/beans

      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

      http://www.springframework.org/schema/context

      http://www.springframework.org/schema/context/spring-context-3.0.xsd

      http://www.springframework.org/schema/mvc

      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd

        http://www.springframework.org/schema/tx

        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

        http://www.springframework.org/schema/aop

    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

        http://www.springframework.org/schema/util 

      http://www.springframework.org/schema/util/spring-util-3.0.xsd">

<context:annotation-config />

<context:component-scan base-package="core" />

  <!--    <bean id="propertyConfigurer2" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

<property name="locations">

<list>

</list>

</property>

</bean> -->

<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">

<property name="maxIdle" value="5" />

<property name="maxTotal" value="10" />

<property name="blockWhenExhausted" value="true" />

<property name="maxWaitMillis" value="30000" />

<property name="testOnBorrow" 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="${im.hs.server.redis.sentinel.masterName}"></property>  

            </bean>  

        </property>  

        <property name="sentinels">  

            <set>  

                <bean class="org.springframework.data.redis.connection.RedisNode">  

                    <constructor-arg name="host"  

                        value="${im.hs.server.redis.sentinel1.host}"></constructor-arg>  

                    <constructor-arg name="port"  

                        value="${im.hs.server.redis.sentinel1.port}"></constructor-arg>  

                </bean>  

                <bean class="org.springframework.data.redis.connection.RedisNode">  

                    <constructor-arg name="host"  

                        value="${im.hs.server.redis.sentinel2.host}"></constructor-arg>  

                    <constructor-arg name="port"  

                        value="${im.hs.server.redis.sentinel2.port}"></constructor-arg>  

                </bean>  

            </set>  

        </property>  

    </bean>  

  

    <bean id="jedisConnectionFactory"  

        class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:password="abcd_123456">  //集群的时候这里要用密码才能连接,之后正常使用

        <constructor-arg name="sentinelConfig" ref="sentinelConfiguration"></constructor-arg>  

<!--         <constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>  

 -->    </bean>  

<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">

<property name="connectionFactory" ref="jedisConnectionFactory" />

<property name="keySerializer">

<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />

</property>

<property name="valueSerializer">

<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />

</property>

<property name="hashKeySerializer">

<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />

</property>

<property name="hashValueSerializer">

<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />

</property>

</bean>

<bean id="redisTemplateDelegate" class="core.session.manager.RedisTemplateDelegate">

<property name="redisTemplate" ref="redisTemplate" />

</bean>

</beans>

java代码:

 @Autowired(required=false)

 private RedisTemplateDelegate<String, Map<String, Object>> redisService;

public RedisTemplateDelegate<String, Map<String, Object>> getRedisService() {

return redisService;

}

public void setRedisService(RedisTemplateDelegate<String, Map<String, Object>> redisService) {

this.redisService = redisService;

}

//Map<String,Object> ma =new HashMap<String,Object>();

//Map<String,Object> maq =new HashMap<String,Object>();

//ma.put("11", 22);

//redisService.set("kk", ma);

//maq=redisService.get("kk");

//maq.get("11");

当然如果不用spring集成的话java写法不一样,需要代码中写密码

参看:

http://www.cnblogs.com/qlong8807/p/5893422.html

相关推荐