下载
这里下载的是windows
直接解压,打开解压目录,进入powerShell 启动服务端:
打开另一个powerShell启动客户端:
ping pong成功;
废话不多说:
先封装一个JedisPool 能够调出JedisPool,我把它加入common包:
简而言之就是配置好JedisPool然后用JedisPool返回Jedis类
package com.van.mall.common;
import com.van.mall.util.PropertiesUtil;
import org.springframework.context.annotation.PropertySource;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
* @author Van
* @date 2020/3/21 - 13:51
*/
@PropertySource(value = {"classpath:application.properties"})
public class RedisPool {
private static JedisPool pool;//connection pool
private static Integer maxTotal =Integer.parseInt(PropertiesUtil.getPropertity("redis.max.total")) ;//max connection
private static Integer maxIdle =Integer.parseInt(PropertiesUtil.getPropertity("redis.max.idle"));//max idle connection
private static Integer minIdle=Integer.parseInt(PropertiesUtil.getPropertity("redis.min.idle"));//min idle connection
private static Boolean testOnBorrow=Boolean.parseBoolean(PropertiesUtil.getPropertity("redis.test.borrow"));//whether test when borrow a jedis if true then it's available
private static Boolean testOnReturn=Boolean.parseBoolean(PropertiesUtil.getPropertity("redis.test.return"));//whether test when return a jedis if true then it's available
private static String redisIp=PropertiesUtil.getPropertity("redis.ip");
private static Integer redisPort=Integer.parseInt(PropertiesUtil.getPropertity("redis.port")) ;
private static void initPool(){
JedisPoolConfig config=new JedisPoolConfig();
config.setMaxIdle(maxIdle);
config.setMaxTotal(maxTotal);
config.setMinIdle(minIdle);
config.setTestOnBorrow(testOnBorrow);
config.setTestOnReturn(testOnReturn);
//whether block when resource is exhausted: false will throw a exception,true will block until timeout, default is true
config.setBlockWhenExhausted(true);
pool=new JedisPool(config,redisIp,redisPort,1000*2);
}
static {
initPool();
}
public static Jedis getJedis(){
return pool.getResource();
}
public static void returnBrokenResource(Jedis jedis){
pool.returnBrokenResource(jedis);
}
public static void returnResource(Jedis jedis){
pool.returnResource(jedis);
}
}
能够得到Jedis类之后我给Jedis封装一个工具类
里面有get set setex expire(给一个key设置过期时间)这些方法
package com.van.mall.util;
import com.van.mall.common.RedisPool;
import lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.Jedis;
/**
* @author Van
* @date 2020/3/21 - 22:59
*/
@Slf4j
public class RedisPoolUtil {
public static Long expire(String key,int exTime){
Long result=null;
Jedis jedis=null;
try {
jedis= RedisPool.getJedis();
result=jedis.expire(key,exTime);
} catch (Exception e) {
log.error("expire key{} exTime{} error",key,exTime,e);
RedisPool.returnBrokenResource(jedis);
return result;
}
RedisPool.returnResource(jedis);
return result;
}
//exTime is second
public static String setEx(String key,String value,int exTime){
String result=null;
Jedis jedis=null;
try {
jedis= RedisPool.getJedis();
result=jedis.setex(key,exTime,value);
} catch (Exception e) {
log.error("setex key{} value{} exTime{} error",key,value,exTime,e);
RedisPool.returnBrokenResource(jedis);
return result;
}
RedisPool.returnResource(jedis);
return result;
}
public static String set(String key,String value){
String result=null;
Jedis jedis=null;
try {
jedis= RedisPool.getJedis();
result=jedis.set(key,value);
} catch (Exception e) {
log.error("set key{} value{} error",key,value,e);
RedisPool.returnBrokenResource(jedis);
return result;
}
RedisPool.returnResource(jedis);
return result;
}
public static String get(String key){
String result=null;
Jedis jedis=null;
try {
jedis= RedisPool.getJedis();
result=jedis.get(key);
} catch (Exception e) {
log.error("get key{} error",key,e);
RedisPool.returnBrokenResource(jedis);
return result;
}
RedisPool.returnResource(jedis);
return result;
}
public static Long del(String key){
Long result=null;
Jedis jedis=null;
try {
jedis= RedisPool.getJedis();
result=jedis.del(key);
} catch (Exception e) {
log.error("del key{} error",key,e);
RedisPool.returnBrokenResource(jedis);
return result;
}
RedisPool.returnResource(jedis);
return result;
}
}
sharded
先构造出pool
package com.van.mall.common;
import com.van.mall.util.PropertiesUtil;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
import redis.clients.jedis.util.Hashing;
import redis.clients.jedis.util.Sharded;
import java.util.ArrayList;
import java.util.List;
/**
* @author Van
* @date 2020/3/22 - 22:24
*/
public class RedisShardedPool {
private static ShardedJedisPool pool;//connection pool
private static Integer maxTotal =Integer.parseInt(PropertiesUtil.getPropertity("redis.max.total")) ;//max connection
private static Integer maxIdle =Integer.parseInt(PropertiesUtil.getPropertity("redis.max.idle"));//max idle connection
private static Integer minIdle=Integer.parseInt(PropertiesUtil.getPropertity("redis.min.idle"));//min idle connection
private static Boolean testOnBorrow=Boolean.parseBoolean(PropertiesUtil.getPropertity("redis.test.borrow"));//whether test when borrow a jedis if true then it's available
private static Boolean testOnReturn=Boolean.parseBoolean(PropertiesUtil.getPropertity("redis.test.return"));//whether test when return a jedis if true then it's available
private static String redis1Ip=PropertiesUtil.getPropertity("redis1.ip");
private static Integer redis1Port=Integer.parseInt(PropertiesUtil.getPropertity("redis1.port")) ;
private static String redis2Ip=PropertiesUtil.getPropertity("redis2.ip");
private static Integer redis2Port=Integer.parseInt(PropertiesUtil.getPropertity("redis2.port")) ;
private static void initPool(){
JedisPoolConfig config=new JedisPoolConfig();
config.setMaxIdle(maxIdle);
config.setMaxTotal(maxTotal);
config.setMinIdle(minIdle);
config.setTestOnBorrow(testOnBorrow);
config.setTestOnReturn(testOnReturn);
//whether block when resource is exhausted: false will throw a exception,true will block until timeout, default is true
config.setBlockWhenExhausted(true);
JedisShardInfo jedisShardInfo1=new JedisShardInfo(redis1Ip,redis1Port,1000*2);
JedisShardInfo jedisShardInfo2=new JedisShardInfo(redis2Ip,redis2Port,1000*2);
List<JedisShardInfo>jedisShardInfos=new ArrayList<>();
jedisShardInfos.add(jedisShardInfo1);
jedisShardInfos.add(jedisShardInfo2);
pool=new ShardedJedisPool(config,jedisShardInfos, Hashing.MURMUR_HASH, Sharded.DEFAULT_KEY_TAG_PATTERN);
}
static {
initPool();
}
public static ShardedJedis getJedis(){
return pool.getResource();
}
public static void returnBrokenResource(ShardedJedis jedis){
returnBrokenResource(jedis);
}
public static void returnResource(ShardedJedis jedis){
returnResource(jedis);
}
public static void main(String[] args) {
ShardedJedis jedis=getJedis();
for (int i = 0; i < 10; i++) {
jedis.set("key"+i,"value"+i);
}
returnResource(jedis);
}
}
2.封装为工具类
package com.van.mall.util;
import com.van.mall.common.RedisShardedPool;
import lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.ShardedJedis;
/**
* @author Van
* @date 2020/3/22 - 22:42
*/
@Slf4j
public class RedisShardedPoolUtil {
public static Long expire(String key,int exTime){
Long result=null;
ShardedJedis jedis=null;
try {
jedis= RedisShardedPool.getJedis();
result=jedis.expire(key,exTime);
} catch (Exception e) {
log.error("expire key{} exTime{} error",key,exTime,e);
RedisShardedPool.returnBrokenResource(jedis);
return result;
}
RedisShardedPool.returnResource(jedis);
return result;
}
//exTime is second
public static String setEx(String key,String value,int exTime){
String result=null;
ShardedJedis jedis=null;
try {
jedis= RedisShardedPool.getJedis();
result=jedis.setex(key,exTime,value);
} catch (Exception e) {
log.error("setex key{} value{} exTime{} error",key,value,exTime,e);
RedisShardedPool.returnBrokenResource(jedis);
return result;
}
RedisShardedPool.returnResource(jedis);
return result;
}
public static String set(String key,String value){
String result=null;
ShardedJedis jedis=null;
try {
jedis= RedisShardedPool.getJedis();
result=jedis.set(key,value);
} catch (Exception e) {
log.error("set key{} value{} error",key,value,e);
RedisShardedPool.returnBrokenResource(jedis);
return result;
}
RedisShardedPool.returnResource(jedis);
return result;
}
public static String get(String key){
String result=null;
ShardedJedis jedis=null;
try {
jedis= RedisShardedPool.getJedis();
result=jedis.get(key);
} catch (Exception e) {
log.error("get key{} error",key,e);
RedisShardedPool.returnBrokenResource(jedis);
return result;
}
RedisShardedPool.returnResource(jedis);
return result;
}
public static Long del(String key){
Long result=null;
ShardedJedis jedis=null;
try {
jedis= RedisShardedPool.getJedis();
result=jedis.del(key);
} catch (Exception e) {
log.error("del key{} error",key,e);
RedisShardedPool.returnBrokenResource(jedis);
return result;
}
RedisShardedPool.returnResource(jedis);
return result;
}
}