单机版配置
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- 最大连接数 -->
<property name="maxTotal" value="100" />
<!-- 最大空闲连接数 -->
<property name="maxIdle" value="300" />
<!-- 每次释放连接的最大数目 -->
<property name="numTestsPerEvictionRun" value="1024" />
<!-- 释放连接的扫描间隔(毫秒) -->
<property name="timeBetweenEvictionRunsMillis" value="30000" />
<!-- 连接最小空闲时间 -->
<property name="minEvictableIdleTimeMillis" value="1800000" />
<!-- 连接空闲多久后释放, 当空闲时间>该值 且 空闲连接>最大空闲连接数 时直接释放 -->
<property name="softMinEvictableIdleTimeMillis" value="10000" />
<!-- 获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1 -->
<property name="maxWaitMillis" value="1500" />
<!-- 在获取连接的时候检查有效性, 默认false -->
<property name="testOnBorrow" value="true" />
<!-- 在空闲时检查有效性, 默认false -->
<property name="testWhileIdle" value="true" />
<!-- 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true -->
<property name="blockWhenExhausted" value="false" />
</bean>
<bean id="redisClient" class="redis.clients.jedis.JedisPool">
<constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>
<constructor-arg name="host" value="${redis.host}"></constructor-arg>
<constructor-arg name="port" value="${redis.port}"></constructor-arg>
<!-- <constructor-arg name="timeout" value="${redis.timeOut}"></constructor-arg>
<constructor-arg name="password" value="${redis.password}"></constructor-arg> -->
</bean>
<bean id="jedisClient" class="com.yagoo.wificontrolsys.redis.impl.JedisClientSingle"/>
集群版配置,其中constructor-arg name="host" value="192.168.199.203"指定ip更换即可
<bean id="redisClient" class="redis.clients.jedis.JedisCluster">
<constructor-arg name="nodes">
<set>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.199.203"></constructor-arg>
<constructor-arg name="port" value="7001"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.199.203"></constructor-arg>
<constructor-arg name="port" value="7002"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.199.203"></constructor-arg>
<constructor-arg name="port" value="7003"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.199.203"></constructor-arg>
<constructor-arg name="port" value="7004"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.199.203"></constructor-arg>
<constructor-arg name="port" value="7005"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.199.203"></constructor-arg>
<constructor-arg name="port" value="7006"></constructor-arg>
</bean>
</set>
</constructor-arg>
<constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>
</bean>
<bean id="jedisClient" class="com.wenwuyi.study.rest.dao.impl.JedisClientCluster"/>
两个客户端代码
客户端接口
/**
* Copyright (c) 2017. yagoosafe.com All right reserved. This software is the
* confidential and proprietary information of yagoosafe.com ("Confidential Information").
* You shall not disclose such Confidential Information and shall use it only in accordance
* with the terms of the license agreement you entered into with yagoosafe.com.
*/
package com.yagoo.wificontrolsys.redis;
/**
* 类名:JedisClient.java
* 描述:jedis client
* 时间:2018年3月8日 下午4:59:30
* @author yangchangjiang
* @version 1.0
*/
public interface JedisClient {
/**
*
* 根据key获取信息
* @param key
* @return String
*/
String get(String key);
/**
*
* 设置信息
* @param key
* @param value
* @return String
*/
String set(String key,String value);
/**
*
* 设置信息带过期时间
* @param key
* @param value
* @param expire
* @return String
*/
String set(String key, String value, int expire);
/**
*
* hset 带多key值
* @param hkey
* @param key
* @return String
*/
String hget(String hkey,String key);
/**
*
* hset 带多key值和value值
* @param hkey
* @param key
* @param value
* @return long
*/
long hset(String hkey,String key,String value);
/**
*
* Incr键值+1
* @param key
* @return long
*/
long incr(String key);
/**
*
* 设置过期时间
* @param key
* @param second
* @return long
*/
long expire(String key,int second);
/**
*
* 查看过期时间
* @param key
* @return long
*/
long ttl(String key);
/**
*
* 删除对应key值
* @param key
* @return long
*/
long del(String key);
/**
*
* 删除hkey和key
* @param hkey
* @param key
* @return long
*/
long hdel(String hkey,String key);
}
单机版实现
/*
* Copyright 2017 wenwuyi.cn All right reserved. This software is the
* confidential and proprietary information of yagoosafe.com ("Confidential
* Information"). You shall not disclose such Confidential Information and shall
* use it only in accordance with the terms of the license agreement you entered
* into with wenwuyi.cn.
*/
package com.yagoo.wificontrolsys.redis.impl;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import com.yagoo.wificontrolsys.redis.JedisClient;
/**
* 类的名称JedisClientSingle.java类
* 类的作用:Redis单机版
* @author YCJ 作者 E-mail: [email protected]
* @date 创建时间:2017年12月24日 下午12:51:39
* @version 1.0
*/
public class JedisClientSingle implements JedisClient{
@Autowired
private JedisPool jedisPool;
@Override
public String get(String key) {
Jedis jedis = jedisPool.getResource();
String string = jedis.get(key);
jedis.close();
return string;
}
@Override
public String set(String key, String value) {
Jedis jedis = jedisPool.getResource();
String string = jedis.set(key, value);
jedis.close();
return string;
}
@Override
public String hget(String hkey, String key) {
Jedis jedis = jedisPool.getResource();
String string = jedis.hget(hkey, key);
jedis.close();
return string;
}
@Override
public long hset(String hkey, String key, String value) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.hset(hkey, key, value);
jedis.close();
return result;
}
@Override
public long incr(String key) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.incr(key);
jedis.close();
return result;
}
@Override
public long expire(String key, int second) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.expire(key, second);
jedis.close();
return result;
}
@Override
public long ttl(String key) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.ttl(key);
jedis.close();
return result;
}
@Override
public long del(String key) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.del(key);
jedis.close();
return result;
}
@Override
public long hdel(String hkey, String key) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.hdel(hkey,key);
jedis.close();
return result;
}
@Override
public String set(String key, String value, int expire) {
Jedis jedis = jedisPool.getResource();
String string = jedis.set(key, value);
jedis.expire(key, expire);
jedis.close();
return string;
}
}
集群版实现
/*
* Copyright 2017 wenwuyi.cn All right reserved. This software is the
* confidential and proprietary information of yagoosafe.com ("Confidential
* Information"). You shall not disclose such Confidential Information and shall
* use it only in accordance with the terms of the license agreement you entered
* into with wenwuyi.cn.
*/
package com.yagoo.wificontrolsys.redis.impl;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisCluster;
import com.yagoo.wificontrolsys.redis.JedisClient;
/**
* 类的名称JedisClientCluster.java类
* 类的作用:redis集群版
* @author YCJ 作者 E-mail: [email protected]
* @date 创建时间:2017年12月24日 下午12:54:15
* @version 1.0
*/
public class JedisClientCluster implements JedisClient {
@Autowired
private JedisCluster jedisCluster;
@Override
public String get(String key) {
return jedisCluster.get(key);
}
@Override
public String set(String key, String value) {
return jedisCluster.set(key, value);
}
@Override
public String hget(String hkey, String key) {
return jedisCluster.hget(hkey, key);
}
@Override
public long hset(String hkey, String key, String value) {
return jedisCluster.hset(hkey, key, value);
}
@Override
public long incr(String key) {
return jedisCluster.incr(key);
}
@Override
public long expire(String key, int second) {
return jedisCluster.expire(key, second);
}
@Override
public long ttl(String key) {
return jedisCluster.ttl(key);
}
@Override
public long del(String key) {
return jedisCluster.del(key);
}
@Override
public long hdel(String hkey, String key) {
return jedisCluster.hdel(hkey,key);
}
@Override
public String set(String key, String value, int expire) {
String string = jedisCluster.set(key, value);
jedisCluster.expire(key, expire);
return string;
}
}
转载于:https://blog.51cto.com/4577390/2085451