package io.renren.utils; import org.redisson.Redisson; import org.redisson.api.RedissonClient; import org.redisson.config.Config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * redisson Configuration */ @configuration public class RedissonManager {@bean public RedissonClient getRedisson(){ RedissonClient redisson = null; // RedissonClient redisson = null; // RedissonClient redisson = null; Config config = new Config(); Config. UseSingleServer (.) setAddress (" redis: / / 127.0.0.1:6379 "); redisson = Redisson.create(config); // Check whether the configuration is successful by printing redisson.getConfig().tojson ().tostring () }}Copy the code

 

 

package io.renren.utils;

import java.util.concurrent.TimeUnit;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
 
public interface DistributedLocker {
 
    RLock lock(String lockKey);
 
    RLock lock(String lockKey, long timeout);
 
    RLock lock(String lockKey, TimeUnit unit, long timeout);
 
    boolean tryLock(String lockKey, TimeUnit unit, long waitTime, long leaseTime);
 
    void unlock(String lockKey);
 
    void unlock(RLock lock);
 
}
Copy the code

 

package io.renren.utils; import org.redisson.api.RLock; import org.redisson.api.RedissonClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; import java.util.concurrent.TimeUnit; @Component public class RedissonDistributedLocker implements DistributedLocker { @Autowired private RedissonClient redissonClient; //RedissonClient is generated by the configuration class, which can be automatically assembled. Override public RLock lock(String lockKey) {RLock lock = redissonClient.getLock(lockKey); lock.lock(); return lock; } //leaseTime is the lock time, @override public RLock lock(String lockKey, long leaseTime) {RLock lock = redissonclient.getLock (lockKey); lock.lock(leaseTime, TimeUnit.SECONDS); return lock; } //timeout indicates the lock time. @override public RLock lock(String lockKey, TimeUnit unit ,long timeout) { RLock lock = redissonClient.getLock(lockKey); lock.lock(timeout, unit); return lock; } //tryLock(), return immediately, return true on lock, return false otherwise. // tryLock() with time limit, can't get lock, wait for some time, @override public Boolean tryLock(String lockKey, TimeUnit unit, long waitTime, long leaseTime) { RLock lock = redissonClient.getLock(lockKey); try { return lock.tryLock(waitTime, leaseTime, unit); } catch (InterruptedException e) { return false; } } @Override public void unlock(String lockKey) { RLock lock = redissonClient.getLock(lockKey); lock.unlock(); } @Override public void unlock(RLock lock) { lock.unlock(); }}Copy the code
package io.renren.api.controller; import io.renren.utils.DistributedLocker; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.concurrent.TimeUnit; @RestController @RequestMapping("/redisson") public class AdvMaterialController { @Autowired private DistributedLocker distributedLocker; private int flag = 10; @RequestMapping("/test") public void redissonTest() { String key = "redisson_key"; for (int i = 0; i < 30; i++) { Thread t = new Thread(new Runnable() { @Override public void run() { try { System. Err. Println (" = = = = = = = = = = = = = Thread open = = = = = = = = = = = = "+ Thread. The currentThread (). The getName ()); /*distributedLocker.lock(key,10L); Thread.sleep(100); thread. sleep(100); System.err.println("====== ======"+ thread.currentThread ().getName()); distributedLocker.unlock(key); / / unlock System. Err. Println (" = = = = = = = = = = = = = = = = = = = = = = = = = = = = = "+ Thread. The currentThread (). The getName ()); */ boolean isGetLock = distributedLocker.tryLock(key, TimeUnit.SECONDS,5L,10L); If (isGetLock){if(flag! =0){ flag= flag - 1; } System. Err. Println (" = = = = = = get after the lock for the corresponding operation = = = = = = "+ Thread. The currentThread (). The getName ()); distributedLocker.unlock(key); System.err.println("============================="+Thread.currentThread().getName()); }else{system.out.println (" end "+flag); } } catch (Exception e) { e.printStackTrace(); }}}); t.start(); } System.out.println(flag); }}Copy the code