redis分布式事务锁案例教学

什么是分布式事务?

分布式事务,说的就是JTA(XA)的事务,只要你能回答出来两段式提交,基本就算你过了,在J2EE企业开发场景比较常用,跨数据库跨应用,准备阶段,提交阶段,这些,做互联网开发的用的比较少,不过因为JTA是事务标准,spring的事务也要基于这个标准来实现,所以了解一些对于开发是有好处的,所以才会问。

什么是分布式锁?

第二个好一些,分布式锁应用场景很多,比如并发场景下对于数据库的更改,或者分布式运行的JOB之类的,都需要一个分布式的锁来让多机对于竞争资源实现序列化的访问,然后实现的话现在通过zookeeper实现的比较多见(但是性能不是很好),也可以通过公用的缓存或者数据库自己来实现。

为了解决并发的问题,需要用到redis分布式事务锁。(会用才是王道!!!)

redis分布式事务锁案例配置:
applicationContext.xml:

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-4.1.xsd
                        http://www.springframework.org/schema/tx
                        http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
                        http://www.springframework.org/schema/aop
                        http://www.springframework.org/schema/aop/spring-aop.xsd">

    
    

             destroy-method="destroy">
        
    

             init-method="init" destroy-method="destroy" />

    
             class="com.baidu.disconf.client.addons.properties.ReloadablePropertiesFactoryBean">
        
            
                classpath*:job.properties
                classpath*:redis.properties
                classpath*:dubbo-pvd.properties
                classpath*:dubbo-consumer.properties
                classpath*:active-MQ.properties
            

        

    


    
    

                 class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        
        
        
            
                
            

        

    

    

    
             class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        
        
            
                classpath*:properties/*.properties
            

        

    


    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    
    
    
    
    
    
    


repay-redis.xml:


       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
           http://www.springframework.org/schema/cache
           http://www.springframework.org/schema/cache/spring-cache.xsd">
           
             
              p:host-name="${repay.string.redis.host}" p:port="${repay.string.redis.port}" p:timeout="${repay.string.redis.timeout}" p:usePool="${repay.string.redis.usePool}"
           p:password="${repay.string.redis.password}">
       
   


    

   
              p:connection-factory-ref="stringJedisConnectionFactory" p:enableTransactionSupport="false"/>
          
   
       
       
       
       
       
       
           
   

 


参数配置:
repay.string.redis.host=192.168.0.172
repay.string.redis.port=6379
repay.string.redis.timeout=15000
repay.string.redis.usePool=TRUE
repay.string.redis.password=123
#repay.string.redis.database=2
repay.string.redis.maxTotal=100
repay.string.redis.maxIdle=50
repay.string.redis.minIdle=20
repay.string.redis.maxWaitMillis=10000
repay.string.redis.testOnBorrow=TRUE

java代码:
@Resource(name = "stringRedisTemplate")
private StringRedisTemplate stringRedisTemplate;

String key = "A_" + realPayer.getLid();//程序唯一key

            // REDIS锁住在线还款,避免并发问题()
            if (stringRedisTemplate.opsForValue().setIfAbsent(key, realPayer.getLid().toString())) {
                stringRedisTemplate.expire(key, 5, TimeUnit.MINUTES);
            } else {
                throw new BusinessException("");
            }

                  try {
                        //逻辑处理
            } catch (Exception e) {
            } finally {
                stringRedisTemplate.delete(key);
            }
谨记 不管处理如何都要有 finally {
                stringRedisTemplate.delete(key);
            }

maven 包:

    org.springframework.data
    spring-data-redis
    1.6.0.RELEASE

你可能感兴趣的:(java总结实用)