Browse Source

spring-boot-demo-zookeeper 完成

pull/1/head
Yangkai.Shen 5 years ago
parent
commit
c53ce59abf
2 changed files with 86 additions and 43 deletions
  1. +0
    -42
      spring-boot-demo-zookeeper/src/main/java/com/xkcoding/zookeeper/controller/TestController.java
  2. +86
    -1
      spring-boot-demo-zookeeper/src/test/java/com/xkcoding/zookeeper/SpringBootDemoZookeeperApplicationTests.java

+ 0
- 42
spring-boot-demo-zookeeper/src/main/java/com/xkcoding/zookeeper/controller/TestController.java View File

@@ -1,42 +0,0 @@
package com.xkcoding.zookeeper.controller;

import cn.hutool.core.lang.Dict;
import com.xkcoding.zookeeper.annotation.LockKeyParam;
import com.xkcoding.zookeeper.annotation.ZooLock;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
* <p>
* 测试Controller
* </p>
*
* @package: com.xkcoding.zookeeper.controller
* @description: 测试Controller
* @author: yangkai.shen
* @date: Created in 2018-12-27 15:51
* @copyright: Copyright (c) 2018
* @version: V1.0
* @modified: yangkai.shen
*/
@RestController
@Slf4j
public class TestController {

@GetMapping("/buy")
public Dict buy(int userId) throws InterruptedException {
log.info("{} 购买中。。。", userId);
doBuy(userId);
return Dict.create().set("flag", true).set("msg", "秒杀成功").set("data", userId);
}

@ZooLock(key = "buy", timeout = 1, timeUnit = TimeUnit.MINUTES)
private void doBuy(@LockKeyParam int userId) throws InterruptedException {
log.info("{} 正在出库。。。", userId);
TimeUnit.SECONDS.sleep(5);
log.info("{} 扣库存成功。。。", userId);
}
}

+ 86
- 1
spring-boot-demo-zookeeper/src/test/java/com/xkcoding/zookeeper/SpringBootDemoZookeeperApplicationTests.java View File

@@ -1,16 +1,101 @@
package com.xkcoding.zookeeper; package com.xkcoding.zookeeper;


import com.xkcoding.zookeeper.annotation.ZooLock;
import com.xkcoding.zookeeper.aspectj.ZooLockAspect;
import lombok.extern.slf4j.Slf4j;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.aop.aspectj.annotation.AspectJProxyFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;


import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest @SpringBootTest
@Slf4j
public class SpringBootDemoZookeeperApplicationTests { public class SpringBootDemoZookeeperApplicationTests {


public Integer getCount() {
return count;
}

private Integer count = 10000;
private ExecutorService executorService = Executors.newFixedThreadPool(1000);

@Autowired
private CuratorFramework zkClient;

/**
* 不使用分布式锁,程序结束查看count的值是否为0
*/
@Test @Test
public void contextLoads() {
public void test() throws InterruptedException {
IntStream.range(0, 10000).forEach(i -> executorService.execute(this::doBuy));
TimeUnit.MINUTES.sleep(1);
log.error("count值为{}", count);
}

/**
* 测试AOP分布式锁
*/
@Test
public void testAopLock() throws InterruptedException {
// 测试类中使用AOP需要手动代理
SpringBootDemoZookeeperApplicationTests target = new SpringBootDemoZookeeperApplicationTests();
AspectJProxyFactory factory = new AspectJProxyFactory(target);
ZooLockAspect aspect = new ZooLockAspect(zkClient);
factory.addAspect(aspect);
SpringBootDemoZookeeperApplicationTests proxy = factory.getProxy();
IntStream.range(0, 10000).forEach(i -> executorService.execute(() -> proxy.aopBuy(i)));
TimeUnit.MINUTES.sleep(1);
log.error("count值为{}", proxy.getCount());
}

/**
* 测试手动加锁
*/
@Test
public void testManualLock() throws InterruptedException {
IntStream.range(0, 10000).forEach(i -> executorService.execute(this::manualBuy));
TimeUnit.MINUTES.sleep(1);
log.error("count值为{}", count);
}

@ZooLock(key = "buy", timeout = 1, timeUnit = TimeUnit.MINUTES)
public void aopBuy(int userId) {
log.info("{} 正在出库。。。", userId);
doBuy();
log.info("{} 扣库存成功。。。", userId);
}

public void manualBuy() {
String lockPath = "/buy";
log.info("try to buy sth.");
try {
InterProcessMutex lock = new InterProcessMutex(zkClient, lockPath);
try {
if (lock.acquire(1, TimeUnit.MINUTES)) {
doBuy();
log.info("buy successfully!");
}
} finally {
lock.release();
}
} catch (Exception e) {
log.error("zk error");
}
}

public void doBuy() {
count--;
log.info("count值为{}", count);
} }


} }


Loading…
Cancel
Save