前言
先在pom.xml中配置依赖
<!--redis java 中客户端 jedis-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<!--redis 连接池-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.4.2</version>
</dependency>
一、Java中 使用Redis基础
1、Jedis客户端链接
清单1:链接redis
public static void main(String[] args) {
Jedis jedis = getJedis();
}
public static Jedis getJedis() {
//连接本地的 Redis 服务
Jedis jedis = new Jedis("192.168.0.85", 6379);
// 登录密码
jedis.auth("ftrend");
System.out.println("链接成功");
//查看服务是否运行
System.out.println("服务正在运行: " + jedis.ping());
return jedis;
}
2、实例
清单2、Redis java String
public static void main(String[] args) {
Jedis jedis = getJedis();
jedis.set("name","wcl");
System.out.println("name:"+jedis.get("name"));
}
清单3 Redis Java List
public static void main(String[] args) {
//连接本地的 Redis 服务
Jedis jedis = new Jedis("localhost");
System.out.println("Connection to server sucessfully");
//存储数据到列表中
jedis.lpush("tutorial-list", "Redis");
jedis.lpush("tutorial-list", "Mongodb");
jedis.lpush("tutorial-list", "Mysql");
// 获取存储的数据并输出
List<String> list = jedis.lrange("tutorial-list", 0 ,5);
for(int i=0; i<list.size(); i++) {
System.out.println("Stored string in redis:: "+list.get(i));
}
}
二、Java中使用Redis 分布式锁
清单4 redis链接
/**
* Redis 连接
*/
public class RedisManager {
// 通过Jedis连接池 来实例化
private static JedisPool jedisPool;
static {
JedisPoolConfig jedisPoolConfig=new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(18);
jedisPoolConfig.setMaxIdle(8);
jedisPool=new JedisPool(jedisPoolConfig,"192.168.0.85",6379,2000,"ftrend",1);
}
public static Jedis getJedis() throws Exception{
if(null!=jedisPool){
return jedisPool.getResource();
}
throw new Exception("Jedispool was not init");
}
}
清单5 分布式锁
/**
* 分布式锁
*/
public class RedisLock {
// 分布式锁的具体实现
public String getLock(String key, int timeout) {
Jedis jedis=null;
try {
jedis = RedisManager.getJedis();
String value = UUID.randomUUID().toString();// UUID
long end = System.currentTimeMillis() + timeout;
while (System.currentTimeMillis() < end) {// 阻塞
if (jedis.setnx(key, value) == 1) {
jedis.expire(key, timeout);
//锁设置成功,redis 操作成功
return value;
}
if (jedis.ttl(key) == -1) { // 检测过期时间
jedis.expire(key, timeout);
}
Thread.sleep(1000);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
//用完链接对象之后,要把连接归还给连接池
jedis.close();
}
return null;
}
// 释放锁
public boolean releaseLock(String key ,String value){
try {
Jedis jedis=RedisManager.getJedis();
while (true){
jedis.watch(key);// watch
// 判断获得锁的线程和当前redis中存在的锁是同一个
if(value.equals(jedis.get(key))){
Transaction transaction=jedis.multi();
transaction.del(key);
List<Object> list=transaction.exec();
if(list==null){
continue;
}
return true;
}
jedis.unwatch();
break;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public static void main(String[] args) {
RedisLock redisLock=new RedisLock();
String locakId=redisLock.getLock("local:aaa",1000);
if(null!=locakId){
System.out.println("获得锁成功");
}
System.out.println("失败");
String l=redisLock.getLock("local:aaa",1000);
System.out.println(l);
}
}
网友评论