File tree Expand file tree Collapse file tree
main/java/com/distributed
test/java/com/distributed Expand file tree Collapse file tree Original file line number Diff line number Diff line change 5858 <encoding >UTF-8</encoding >
5959 </configuration >
6060 </plugin >
61+ <plugin >
62+ <groupId >org.apache.maven.plugins</groupId >
63+ <artifactId >maven-surefire-plugin</artifactId >
64+ <configuration >
65+ <skip >true</skip >
66+ </configuration >
67+ </plugin >
6168 </plugins >
6269 </build >
6370 <developers >
Original file line number Diff line number Diff line change 33import java .util .concurrent .TimeUnit ;
44
55/**
6- * 限制规则
6+ * 限制规则
77 * Created by sunyujia@aliyun.com on 2015/9/30.
88 */
99public class LimitRule {
1010
1111 /**
12- * 单位时间
12+ * 单位时间
1313 */
1414 private int seconds ;
1515
1616 /**
17- * 单位时间内限制的访问次数
17+ * 单位时间内限制的访问次数
1818 */
1919 private int limitCount ;
2020
Original file line number Diff line number Diff line change 1414import java .util .concurrent .locks .ReentrantLock ;
1515
1616/**
17- * 基于Zookeeper的可重入互斥锁(关于重入:仅限于持有zk锁的jvm内重入)
17+ * 基于Zookeeper的可重入互斥锁(关于重入:仅限于持有zk锁的jvm内重入)
1818 * Created by sunyujia@aliyun.com on 2016/2/24.
1919 */
2020public class ZkReentrantLock implements DistributedReentrantLock {
2121 private static final org .slf4j .Logger log = LoggerFactory .getLogger (ZkReentrantLock .class );
2222
2323 /**
24- * 线程池
24+ * 线程池
2525 */
2626 private static final ScheduledExecutorService executorService = Executors .newScheduledThreadPool (10 );
2727
2828 /**
29- * 所有PERSISTENT锁节点的根位置
29+ * 所有PERSISTENT锁节点的根位置
3030 */
3131 public static final String ROOT_PATH = "/ROOT_LOCK/" ;
3232
3333 /**
34- * 每次延迟清理PERSISTENT节点的时间 Unit:MILLISECONDS
34+ * 每次延迟清理PERSISTENT节点的时间 Unit:MILLISECONDS
3535 */
3636 private long delayTimeForClean = 1000 ;
3737
3838 /**
39- * zk 共享锁实现
39+ * zk 共享锁实现
4040 */
4141 private InterProcessMutex interProcessMutex = null ;
4242
4343
4444 /**
45- * 锁的ID,对应zk一个PERSISTENT节点,下挂EPHEMERAL节点.
45+ * 锁的ID,对应zk一个PERSISTENT节点,下挂EPHEMERAL节点.
4646 */
4747 private String path ;
4848
4949
5050 /**
51- * zk的客户端
51+ * zk的客户端
5252 */
5353 private CuratorFramework client ;
5454
@@ -107,7 +107,7 @@ public void run() {
107107 } catch (KeeperException .NotEmptyException e2 ) {
108108 //nothing
109109 } catch (Exception e ) {
110- log .error (e .getMessage (), e );//准备删除时,正好有线程创建锁
110+ log .error (e .getMessage (), e );//准备删除时,正好有线程创建锁
111111 }
112112 }
113113 }
Original file line number Diff line number Diff line change @@ -21,7 +21,7 @@ public class ZkReentrantLockCleanerTask extends TimerTask {
2121 private Timer timer ;
2222
2323 /**
24- * 检查周期
24+ * 检查周期
2525 */
2626 private long period =5000 ;
2727 /**
@@ -70,7 +70,7 @@ public void run() {
7070 private void cleanNode (String path ){
7171 try {
7272 if (isEmpty (this .client .getChildren ().forPath (path ))){
73- this .client .delete ().forPath (path );//利用存在子节点无法删除和zk的原子性这两个特性.
73+ this .client .delete ().forPath (path );//利用存在子节点无法删除和zk的原子性这两个特性.
7474 }
7575 } catch (Exception e ) {
7676 e .printStackTrace ();
Original file line number Diff line number Diff line change @@ -18,7 +18,7 @@ public void test1() throws InterruptedException {
1818 AccessSpeedLimit accessSpeedLimit =new AccessSpeedLimit (jp );
1919 SimpleDateFormat sdf =new SimpleDateFormat (" mm:ss" );
2020 while (true ){
21- //10.0.0.1这个ip每1秒钟最多访问5次if块内代码.
21+ //10.0.0.1这个ip每1秒钟最多访问5次if块内代码.
2222 if (accessSpeedLimit .tryAccess ("10.0.0.1" , 1 ,5 )){
2323 System .out .println ("yes" +sdf .format (new Date ()));
2424 }else {
@@ -40,7 +40,7 @@ public void test2() throws InterruptedException {
4040 AccessSpeedLimit accessSpeedLimit =new AccessSpeedLimit (jp );
4141 SimpleDateFormat sdf =new SimpleDateFormat (" mm:ss" );
4242 while (true ){
43- //10.0.0.1这个ip每1秒钟最多访问5次if块内代码.1秒超过10次后,锁定2秒,2秒内无法访问.
43+ //10.0.0.1这个ip每1秒钟最多访问5次if块内代码.1秒超过10次后,锁定2秒,2秒内无法访问.
4444 if (accessSpeedLimit .tryAccess ("10.0.0.1" ,limitRule )){
4545 System .out .println ("yes" +sdf .format (new Date ()));
4646 }else {
Original file line number Diff line number Diff line change 1515public class SimpleTest {
1616
1717 public static void main (String [] args ) throws Exception {
18- JedisPool jedisPool =new JedisPool ("127.0.0.1" ,6379 );//实际应用时可通过spring注入
19- RedisReentrantLock lock =new RedisReentrantLock (jedisPool ,"订单流水号" );
18+ JedisPool jedisPool =new JedisPool ("127.0.0.1" ,6379 );//实际应用时可通过spring注入
19+ RedisReentrantLock lock =new RedisReentrantLock (jedisPool ,"订单流水号" );
2020 try {
2121 if (lock .tryLock (5000L , TimeUnit .MILLISECONDS )) {
22- //TODO 获得锁后要做的事
22+ //TODO 获得锁后要做的事
2323 }else {
24- //TODO 获得锁超时后要做的事
24+ //TODO 获得锁超时后要做的事
2525 }
2626 }finally {
2727 lock .unlock ();
2828 }
2929 }
3030
3131 public static void test1 (){
32- JedisPool jedisPool =new JedisPool ("127.0.0.1" ,6379 );//实际应用时可通过spring注入
33- final RedisDistributedLockTemplate template =new RedisDistributedLockTemplate (jedisPool );//本类多线程安全,可通过spring注入
34- template .execute ("订单流水号" , 5000 , new Callback () {
32+ JedisPool jedisPool =new JedisPool ("127.0.0.1" ,6379 );//实际应用时可通过spring注入
33+ final RedisDistributedLockTemplate template =new RedisDistributedLockTemplate (jedisPool );//本类多线程安全,可通过spring注入
34+ template .execute ("订单流水号" , 5000 , new Callback () {
3535 @ Override
3636 public Object onGetLock () throws InterruptedException {
37- //TODO 获得锁后要做的事
37+ //TODO 获得锁后要做的事
3838 return null ;
3939 }
4040
4141 @ Override
4242 public Object onTimeout () throws InterruptedException {
43- //TODO 获得锁超时后要做的事
43+ //TODO 获得锁超时后要做的事
4444 return null ;
4545 }
4646 });
Original file line number Diff line number Diff line change @@ -63,17 +63,17 @@ public static void main(String[] args){
6363 CuratorFramework client = CuratorFrameworkFactory .newClient ("127.0.0.1:2181" , retryPolicy );
6464 client .start ();
6565
66- final ZkDistributedLockTemplate template =new ZkDistributedLockTemplate (client );//本类多线程安全,可通过spring注入
67- template .execute ("订单流水号" , 5000 , new Callback () {
66+ final ZkDistributedLockTemplate template =new ZkDistributedLockTemplate (client );//本类多线程安全,可通过spring注入
67+ template .execute ("订单流水号" , 5000 , new Callback () {
6868 @ Override
6969 public Object onGetLock () throws InterruptedException {
70- //TODO 获得锁后要做的事
70+ //TODO 获得锁后要做的事
7171 return null ;
7272 }
7373
7474 @ Override
7575 public Object onTimeout () throws InterruptedException {
76- //TODO 获得锁超时后要做的事
76+ //TODO 获得锁超时后要做的事
7777 return null ;
7878 }
7979 });
You can’t perform that action at this time.
0 commit comments