spring-data-redis使用哨兵配置一主多从
2017-06-08 09:58
579 查看
redis自带的哨兵确实简化了高可用性的配置,使用起来也比较简单。
首先是spring-redis-sentinel.xml(文件名可以随意命名)配置文件:
上面的p:password="pwdisadmin"指的是redis的密码,mymaster指的是redis哨兵中配置的名字。
然后再程序中就可以通过注解或者getBean获取redisTemplate的实例了。
首先是spring-redis-sentinel.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" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 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"> <!-- Spring自动将该包目录下标记为@Service的所有类作为spring的Bean --> <context:component-scan base-package="com.zz.redis" /> <!-- <context:property-placeholder location="classpath:conf/redis/redis.properties" /> --> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="2048" /> <property name="maxIdle" value="200" /> <property name="numTestsPerEvictionRun" value="1024" /> <property name="timeBetweenEvictionRunsMillis" value="30000" /> <property name="minEvictableIdleTimeMillis" value="-1" /> <property name="softMinEvictableIdleTimeMillis" value="10000" /> <property name="maxWaitMillis" value="1500" /> <property name="testOnBorrow" value="true" /> <property name="testWhileIdle" value="true" /> <property name="testOnReturn" value="false" /> <property name="jmxEnabled" value="true" /> <property name="blockWhenExhausted" value="false" /> </bean> <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="192.168.125.128" /> <constructor-arg name="port" value="26379" /> </bean> <bean class="org.springframework.data.redis.connection.RedisNode"> <constructor-arg name="host" value="192.168.125.129" /> <constructor-arg name="port" value="26379" /> </bean> <bean class="org.springframework.data.redis.connection.RedisNode "> <constructor-arg name="host" value="192.168.125.130" /> <constructor-arg name="port" value="26379" /> </bean> </set> </property> </bean> <bean id="redisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:password="pwdisadmin"> <constructor-arg name="sentinelConfig" ref="redisSentinelConfiguration"></constructor-arg> <constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg> </bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="redisConnectionFactory" /> </bean> </beans>
上面的p:password="pwdisadmin"指的是redis的密码,mymaster指的是redis哨兵中配置的名字。
然后再程序中就可以通过注解或者getBean获取redisTemplate的实例了。
public static void main(String[] args) { ConfigurableApplicationContext context = new ClassPathXmlApplicationContext( "classpath:conf/spring-redis-sentinel.xml"); RedisTemplate<String, String> template = (RedisTemplate<String, String>) context.getBean("redisTemplate"); template.opsForValue().set("aaa", "aaabbb"); System.err.println(template.opsForValue().get("aaa")); }
相关文章推荐
- spring-data-redis使用哨兵配置一主多从
- spring-data-redis 哨兵配置例子
- 【Redis】Redis的基本安装及使用、Jedis的基本使用、spring-data-redis的集成、主从模式、哨兵模式
- spring 中 Spring-data-redis的配置与使用
- Spring-data-redis配置及使用示例
- 关于spring data redis使用javaConfig的基本配置
- 使用 Spring data redis 结合 Spring cache 缓存数据配置
- 使用spring-data-redis配置集群错误
- redis 哨兵模式配置 和 spring data redis 哨兵配置 总结
- 使用Spring Data +Redis实现缓存
- 配置使用Spring Data MongoDB
- 使用spring-data-redis做缓存
- spring data redis使用示例
- 使用spring-data-redis操作Redis的Sentinel
- 使用Spring-data-redis操作Redis的Sentinel
- 【Redis基础】SSH 中Spring-data-redis使用体验
- spring 使用redis集群配置
- 6、redis之使用spring-data-redis的Template
- Spring+redis,spring-data-redis使用
- 使用Spring-Data-Redis存储对象(redisTemplate)