您的位置:首页 > 数据库 > Redis

redis哨兵模式spring配置

2017-05-24 10:04 561 查看
参考:

Tomcat7+Redis存储Session    http://blog.csdn.net/caiwenfeng_for_23/article/details/45666831
测试类

final Set<String> sentinels = new HashSet<String>();
sentinels.add("10.139.5.180:26379");
sentinels.add("10.139.5.181:26379");
sentinels.add("10.139.5.232:26379");
final JedisPoolConfig config = new JedisPoolConfig();

config.setMaxIdle(50);
config.setMaxTotal(500);
config.setTimeBetweenEvictionRunsMillis(30000);
config.setMinEvictableIdleTimeMillis(1800000);
config.setSoftMinEvictableIdleTimeMillis(1800000);
config.setMaxWaitMillis(1500);
config.setTestOnBorrow(true);
config.setTestWhileIdle(false);
config.setTestOnReturn(false);
config.setBlockWhenExhausted(false);
final JedisSentinelPool pool = new JedisSentinelPool("mymaster",
sentinels, config);
final Jedis jedis = pool.getResource();
jedis.set("jedis123", "jedi12s");

spring配置

 <bean id="shardedJedisPool" class="redis.clients.jedis.JedisSentinelPool">

        <constructor-arg index="0" value="${redis.adapter.mymaster}" />

        <constructor-arg index="1">

           <set>

            <value>${redis.adapter.sentinel1}</value>

                <value>${redis.adapter.sentinel2}</value>

                <value>${redis.adapter.sentinel3}</value>

           </set>

        </constructor-arg>

        <constructor-arg index="2" ref="jedisPoolConfig" />

    </bean>

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

        <property name="maxTotal" value="${redis.pool.maxTotal}" />

        <property name="maxIdle" value="${redis.pool.maxIdle}" />

        <property name="timeBetweenEvictionRunsMillis" value="${redis.pool.timeBetweenEvictionRunsMillis}" />

        <property name="minEvictableIdleTimeMillis" value="${redis.pool.minEvictableIdleTimeMillis}" />

        <property name="softMinEvictableIdleTimeMillis" value="${redis.pool.softMinEvictableIdleTimeMillis}" />

        <property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}" />

        <property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />

        <property name="testWhileIdle" value="${redis.pool.testWhileIdle}" />

        <property name="testOnReturn" value="${redis.pool.testOnReturn}" />

        <property name="blockWhenExhausted" value="${redis.pool.blockWhenExhausted}" />

    </bean>

spring配置 对应的代码

@Component

public class RedisClient extends RedisCache {
@Resource(name = "shardedJedisPool")
@Override
protected void setPool(final JedisSentinelPool pool) {
super.pool = pool;
}

}

public abstract class RedisCache implements Cache {
private static final Logger LOGGER = LoggerFactory
.getLogger(RedisClient.class);
protected JedisSentinelPool pool;
protected abstract void setPool(JedisSentinelPool pool);
private Jedis getResource() {
Jedis jedis;
try {
jedis = pool.getResource();
} catch (final Exception e) {
throw e;
}
return jedis;
}

        public String set(final String key, final String value, final String nxxx,

final String expx, final long time) {
if (StringUtils.isEmpty(key) || isNull(value)) {
return null;
}
Jedis jedis = null;
try {
jedis = getResource();
return jedis.set(key, value, nxxx, expx, time);
} catch (final Exception e) {
LOGGER.error("RedisClient-set", e);
throw e;
} finally {
jedis.close();
}
}

      public String get(final String key) {
if (StringUtils.isEmpty(key)) {
return null;
}
Jedis jedis = null;
try {
jedis = getResource();
return jedis.get(key);
} catch (final Exception e) {
LOGGER.error("RedisClient-get", e);
throw e;
} finally {
jedis.close();
}
}

}

public interface Cache {
/**
* 获取 缓存<br>
* 从缓存中根据key取得其String类型的值,如果key不存在则返回null,如果key存在但value不是string类型的,
* 则返回一个error。这个方法只能从缓存中取得value为string类型的值。
*
* @param key
* @return String
*/

String get(final String key);

/**
* 存入 缓存,不存在创建,存在则覆盖<br>
* 存入类型:String
*
* @param key
* @param value
* @return String
*/

String set(final String key, final String value);

/**
* 检查某个key是否在缓存中存在,如果存在返回true,否则返回false;需要注意的是,即使该key所对应的value是一个空字符串,
* 也依然会返回true。
*
* @param key
* @return boolean
*/

boolean exists(final String key);

/**
* 删除一个Key,如果删除的key不存在,则直接忽略。
*
* @param key
* @return 被删除的keys的数量
*/

Long del(final String key);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: