springboot redis分布式锁 序列化 集群

一、集群配置

application.properties配置redis集群

spring.redis.cluster.nodes=192.168.140.151:7000,192.168.140.151:7001,192.168.140.151:7002,192.168.140.151:7003,192.168.140.151:7004,192.168.140.151:7005

二、序列化,防止key值出现\xac\xed\x00\x05t\x00\x04

@Autowired
private RedisTemplate redisTemplate;

@PostConstruct
 public void redisTemplate() {
    redisTemplate.setKeySerializer(new StringRedisSerializer());
    redisTemplate.setHashKeySerializer(new JdkSerializationRedisSerializer());
    redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
    redisTemplate.setHashKeySerializer(new JdkSerializationRedisSerializer());

}

三、分布式锁

 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.concurrent.TimeUnit;

@Component
public class DistributedLock {
    private static final long timeOut=30;
    private static  long goInMethodTime;
    @Autowired
     RedisTemplate redisTemplate;
    /**
     *获取分布式锁
     */
    public  synchronized  <T>  Boolean  lock(String key,T value){
        goInMethodTime=System.currentTimeMillis();
        //如果方法执行时间-当前时间>超时时间则结束,否则继续获取锁
        long nowTime=System.currentTimeMillis();
        if(nowTime-goInMethodTime<timeOut){
            Object execute = redisTemplate.execute(new RedisCallback() {
                @Override
                public Object doInRedis(RedisConnection connection) throws DataAccessException {
                    Boolean aBoolean = connection.setNX(key.getBytes(), Object2Bvalue(value));
                    redisTemplate.expire(key, 1, TimeUnit.MINUTES); // 超时时间1分钟
                    return aBoolean;
                }
            });
            if((Boolean)execute){
                redisTemplate.expire(key, 1, TimeUnit.MINUTES); // 超时时间1分钟
                return true;
            }
            nowTime=System.currentTimeMillis();
        }
        return false;
    }

    public <T> void unLock(T key){
        redisTemplate.delete(key);
    }


   static <T> byte[] Object2Bvalue(T value)  {
       ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
       ObjectOutputStream objectInputStream = null;
       try {
           objectInputStream = new ObjectOutputStream(byteArrayOutputStream);
           objectInputStream.writeObject(value);
       } catch (IOException e) {
           e.printStackTrace();
       }
       byte[] bytes = byteArrayOutputStream.toByteArray();
       return bytes;
   }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值