提要:先说明下应用场景,例如:在修改订单的时候,防止同一个订单号,只有一个线程在修改,如果多个修改,则后面的等待一定时间后直接报错(支持集群)。
基础组件:redis、spring
1.spring的配置我这里不再详细说明,具体可以自己去搜搜 如何搭建spring。创建spring-redisson.xml,并且引入到spring配置中:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:redisson="http://redisson.org/schema/redisson"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://redisson.org/schema/redisson
http://redisson.org/schema/redisson/redisson.xsd">
<redisson:client id="redissonClient">
<redisson:single-server address="redis://${redis.host}"
password="${redis.password}" database="${redis.database}" />
</redisson:client>
</beans>
其中 ${redis.host} 配置127.0.0.1:7396之类地址,前面一定要加上redis:// 否则会报错。password、database可以不配置
2.编写Junit test
基础测试类:
package com.ideacome.car.scanner.test.base;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
/**
* 功能介绍: 基础测试类,用于继承</br>
*
* @author: 罗成</br>
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring/applicationContextTest.xml"})
@Transactional
public class BaseTest {
protected static final Logger LOGGER = LoggerFactory.getLogger(BaseTest.class);
@Test
public void doBaseTest() {
LOGGER.info("doBaseTest");
}
}
redisson测试类:
package com.ideacome.car.scanner.test.service;
import java.util.concurrent.TimeUnit;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.annotation.Rollback;
import com.ideacome.business.car.scanner.param.Address;
import com.ideacome.business.car.scanner.param.CarInfo;
import com.ideacome.business.car.scanner.param.QueryCarInfoInParam;
import com.ideacome.business.car.scanner.service.QueryCarInfoService;
import com.ideacome.car.scanner.core.config.HandlerChainFactory;
import com.ideacome.car.scanner.dto.convert.CarInfoConvert;
import com.ideacome.car.scanner.dto.zt.ZTQueryCarModeData;
import com.ideacome.car.scanner.dto.zt.ZTQueryCarModeRequest;
import com.ideacome.car.scanner.test.base.BaseTest;
import com.ideacome.car.scanner.util.ZTSignatureTools;
import com.ideacome.common.entity.result.Result;
import com.ideacome.common.enums.AppMark;
import com.ideacome.common.utils.DateUtils;
import com.ideacome.common.utils.GsonUtils;
import com.ideacome.common.utils.HttpUtils;
/**
* 测试Redisson
*
* @author 罗成
*/
public class RedissonTest extends BaseTest {
@Autowired
private RedissonClient redissonClient;
@Test
public void testLock() throws Exception {
Thread t1 = new MyThread();
Thread t2 = new MyThread();
t1.setName("t1");
t2.setName("t2");
t1.start();
t2.start();
Thread.sleep(100 * 1000);
}
public class MyThread extends Thread {
@Override
public void run() {
try {
LOGGER.info(getName() + "run start");
RLock rLock = redissonClient.getLock("testLcok");
if (rLock.tryLock(5, TimeUnit.SECONDS)) {
LOGGER.info(getName() + "get lock");
Thread.sleep(10 * 1000);
}
LOGGER.info(getName() + "run over");
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
运行结果为:
2017/11/22-16:58:11.216 INFO [t2] com.ideacome.car.scanner.test.service.RedissonTest$MyThread:57>>>>>t2run start
2017/11/22-16:58:11.216 INFO [t1] com.ideacome.car.scanner.test.service.RedissonTest$MyThread:57>>>>>t1run start
2017/11/22-16:58:11.238 INFO [t1] com.ideacome.car.scanner.test.service.RedissonTest$MyThread:60>>>>>t1get lock
2017/11/22-16:58:16.223 INFO [t2] com.ideacome.car.scanner.test.service.RedissonTest$MyThread:63>>>>>t2run over
2017/11/22-16:58:21.239 INFO [t1] com.ideacome.car.scanner.test.service.RedissonTest$MyThread:63>>>>>t1run over
可见 线程t1 运行10秒后runover 线程2 并没有获取锁,等待5秒后 runover