本章主要介绍Redis的Java实现Jedis的获取与简单使用,本章节主要包括如下几个部分:
Spring-Redis
还有使用ShardRedis以及RedisTemplate进行操作Redis的,本人未深入研究,故略。本文相关代码,可在我的Github项目https://github.com/SeanYanxml/bigdata/ 目录下可以找到。
PS: (如果觉得项目不错,可以给我一个Star。)
使用Jedis
需要导入Jedis.jar
,如果你使用Maven
。需将如下依赖导入你的pom.xml
文件内。
<dependency>
<groupId>redis.clientsgroupId>
<artifactId>jedisartifactId>
<version>2.9.0version>
dependency>
public static Jedis getJedis(){
String redisUrl = "192.168.100.xx";
Jedis jedis = new Jedis(redisUrl);
// 如果有设置密码的话
jedis.auth("admin");
return jedis;
}
// Jedis attribute
private static String address = "192.168.100.xx";
private static int port = 6379;
private static int timeout = 10000;
private static String auth = "admin";
// JedisConfig attribute
private static int max_active = 1024;
private static int max_idle = 200;
private static int max_wait = 10000;
private static boolean test_on_borrow = true;
private static JedisPool jedisPool = null;
static {
try{
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxIdle(max_idle);
config.setMaxWaitMillis(max_wait);
config.setTestOnBorrow(test_on_borrow);
config.setMaxTotal(max_active);
jedisPool = new JedisPool(config,address,port,timeout,auth);
}catch(Exception e){
e.printStackTrace();
}
}
private static int timeout = 10000;
private static String auth = "admin";
private static String redisList = "192.168.100.xx1:26379,192.168.100.xx2:26379";
private static String[] arrRedisList = redisList.split(",");
private static String masterName = "mymaster";
// JedisConfig attribute
private static int max_active = 1024;
private static int max_idle = 200;
private static int max_wait = 10000;
private static boolean test_on_borrow = true;
private static JedisSentinelPool jedisSentinelPool = null;
static {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxIdle(max_idle);
config.setMaxWaitMillis(max_wait);
config.setTestOnBorrow(test_on_borrow);
config.setMaxTotal(max_active);
// 定义集群连接接
Set sentinels = new HashSet();
if(arrRedisList != null){
int redisClusterSize = arrRedisList.length;
for(int i = 0; i< redisClusterSize; i++){
sentinels.add(arrRedisList[i]);
}
jedisSentinelPool = new JedisSentinelPool(masterName, sentinels, config,timeout,auth);
// jedisSentinelPool.set();
}
}
private static final String default_hashtag = "{default}";
private static int max_active = 1024;
private static int max_idle = 200;
private static int max_wait = 10000;
private static boolean test_on_borrow = true;
private static JedisCluster jedisCluster = null;
private static String redisListStr="192.168.xx.76:7000,192.168.xx.76:7001,192.168.xx.76:7002,192.168.xx.76:7003,192.168.xx.76:7004,192.168.xx.76:7005";
private static String auth = "admin";
private static String connectionTimeout = "10000";
private static String soTimeout = "10000";
private static String maxAttempts = "3";
static {
Set redisSets = Stream.of(redisListStr.split(",")).collect(Collectors.toSet());
Set nodes = new HashSet<>();
for(String str:redisSets){
// 切分Redis对象 分成 Host&Port类型
String[] nodeInfo = str.split(":");
nodes.add(new HostAndPort(nodeInfo[0], Integer.parseInt(nodeInfo[1])));
}
// set redis pool config
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxIdle(max_idle);
config.setMaxWaitMillis(max_wait);
config.setTestOnBorrow(test_on_borrow);
config.setMaxTotal(max_active);
// 定义集群连接接
if(null != auth && !auth.equals("")) {
jedisCluster = new JedisCluster(nodes,Integer.parseInt(connectionTimeout),Integer.parseInt(soTimeout),Integer.parseInt(maxAttempts),auth,config);
}else {
jedisCluster = new JedisCluster(nodes,Integer.parseInt(connectionTimeout),Integer.parseInt(soTimeout),Integer.parseInt(maxAttempts),config);
}
// jedisCluster.get();
// need close or not
// jedisCluster.close();
}
[1]. Java连接远程Redis
[2]. Redis学习笔记(五)jedis(JedisCluster)操作Redis集群 redis-cluster