Spring統合redisson分散ロックの実現

12880 ワード

        ,                 (      ) ,             ,                        。

 Java      synchronized               ,                   , jvm    synchronized       。    redis            ,      redis setNx         ,                  (   Redis       ),           
      github        ,                   

      ,        ,              
https://github.com/redisson/redisson


pom.xml      redisson   

    org.redisson
    redisson-all
    2.10.5


2.     
    2.1.        
    Redisson             Config        。  :
    public class RedissonUtils {
        private static RedissonClient redissonClient;
        static{
            Config config = new Config();
            config.useSingleServer().setAddress("redis://127.0.0.1:6379").setPassword(null);
            redissonClient = Redisson.create(config);
        }

        public static RedissonClient getRedisson(){
            return redissonClient;
        }


    public static void main(String[] args) throws InterruptedException{

        RLock fairLock = getRedisson().getLock("TEST_KEY");
        System.out.println(fairLock.toString());
//      fairLock.lock(); 
        //     ,    10 ,    10     
        boolean res = fairLock.tryLock(10, 10, TimeUnit.SECONDS);
        System.out.println(res);
        fairLock.unlock();

        //      
//      RBoundedBlockingQueue queue = getRedisson().getBoundedBlockingQueue("anyQueue");
        //       (  )       ` (true)`,
        //       (  )       ` (false)`。
//      System.out.println(queue.trySetCapacity(10));
//      JSONObject o=new JSONObject();
//      o.put("name", 1);
//      if(!queue.contains(o)){
//          queue.offer(o);
//      }

//      JSONObject o2=new JSONObject();
//      o2.put("name", 2);
        //       ,         ,       。

//      if(!queue.contains(o2)){
//          queue.offer(o2);
//      }

//      //             ;       ,    null。
//      JSONObject obj = queue.peek();
//      //           ,                (     )。
//      JSONObject ob = queue.poll(10, TimeUnit.MINUTES);                                                    

        //          ,       ,    null。
//       Iterator iterator=queue.iterator();
//       while (iterator.hasNext()){
//            JSONObject i =iterator.next();
//            System.out.println(i.toJSONString());
//            iterator.remove();
//          
//        }
//          while(queue.size()>0){
//              JSONObject ob = queue.poll();     
//              System.out.println(ob.toJSONString());
//          }
//      
//      JSONObject someObj = queue.poll();
//      System.out.println(someObj.toJSONString());
    }
2 spring     

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
     xmlns:redisson="http://redisson.org/schema/redisson" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
                        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
                        http://www.springframework.org/schema/context  
                        http://www.springframework.org/schema/context/spring-context-4.0.xsd  
                        http://www.springframework.org/schema/mvc  
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
                        http://redisson.org/schema/redisson
                        http://redisson.org/schema/redisson/redisson.xsd">

    <context:component-scan base-package="component.controller.RedisServiceImpl" />

      
    <bean id="jedisConfig" class="redis.clients.jedis.JedisPoolConfig">          
        <property name="maxIdle" value="${redis_max_idle}">property>   
        <property name="testOnBorrow" value="${redis_test_on_borrow}">property>  
    bean>  
      
    <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">  
        <property name="hostName" value="${redis_addr}">property>  
        <property name="port" value="${redis_port}">property>  
        <property name="password" value="${redis_auth}">property>  
        <property name="poolConfig" ref="jedisConfig">property>  
    bean>  
      
    <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">  
        <property name="connectionFactory" ref="connectionFactory" />  
      
        <property name="keySerializer">  
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />  
        property>  
        <property name="valueSerializer">  
            <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />  
        property>  
    bean>     

    
    <redisson:client id="redissonClient">
       <redisson:single-server address="redis://127.0.0.1:6379" connection-pool-size="30" />  
    redisson:client>
beans>

  RedissonClient      
@Autowired  
private RedissonClient redissonClient;

 //  redissson    
 RLock fairLock = redissonClient.getLock("TEST_KEY");       
 //         100  20      
boolean res = fairLock.tryLock(100, 20, TimeUnit.SECONDS);
 //   
fairLock.unlock();

  :redis     3.0         ,redis lua