This commit is contained in:
2023-07-13 17:30:09 +08:00
parent 672f66b40e
commit 6ed1224410
373 changed files with 566 additions and 409 deletions

View File

@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>qiaoba-modules</artifactId>
<groupId>com.qiaoba</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>qiaoba-module-demo</artifactId>
<description>
示例模块
此模块主要用于典型案例的demo, 切勿引入到 qiaoba-application 模块中
</description>
<dependencies>
<dependency>
<groupId>com.qiaoba</groupId>
<artifactId>qiaoba-common-redis</artifactId>
</dependency>
<dependency>
<groupId>com.qiaoba</groupId>
<artifactId>qiaoba-common-base</artifactId>
</dependency>
<dependency>
<groupId>com.qiaoba</groupId>
<artifactId>qiaoba-common-web</artifactId>
</dependency>
<dependency>
<groupId>com.qiaoba</groupId>
<artifactId>qiaoba-common-doc</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,18 @@
package com.qiaoba.module.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* DemoApplication
*
* @author ailanyin
* @version 1.0
* @since 2023/6/25 8:52
*/
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}

View File

@ -0,0 +1,50 @@
package com.qiaoba.module.demo.controller;
import com.baomidou.lock.annotation.Lock4j;
import com.qiaoba.common.base.result.AjaxResult;
import com.qiaoba.module.demo.service.DemoLock4jService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Demo-Lock4j web层
*
* @author ailanyin
* @version 1.0
* @since 2022-09-22 04:20:28
*/
@RestController
@RequestMapping("/demo/lock4j")
@RequiredArgsConstructor
@Tag(name = "Demo-Lock4j")
@Slf4j
public class DemoLock4jController {
private final DemoLock4jService demoLock4JService;
@GetMapping("/api-lock")
public AjaxResult apiLock() {
log.info("开始执行 apiLock, 当前线程{}", Thread.currentThread().getName());
demoLock4JService.apiLock();
return AjaxResult.success();
}
/**
* 注解加锁
* expire-Redis中Key过期时间-防止死锁-默认值: 30s
* acquireTimeout-排队时长-超过就退出排队,抛出获取锁超时异常-默认值: 3s
*
* @return AjaxResult
*/
@GetMapping("/annotation-lock")
@Lock4j(name = "annotationLock", acquireTimeout = 1000)
public AjaxResult annotationLock() {
log.info("开始执行 annotationLock, 当前线程{}", Thread.currentThread().getName());
demoLock4JService.annotationLock();
return AjaxResult.success();
}
}

View File

@ -0,0 +1,21 @@
package com.qiaoba.module.demo.service;
/**
* Demo-Lock4j 业务层
*
* @author ailanyin
* @version 1.0
* @since 2022-09-22 04:20:28
*/
public interface DemoLock4jService {
/**
* 分布式锁-编程式加锁
*/
void apiLock();
/**
* 分布式锁-注解加锁
*/
void annotationLock();
}

View File

@ -0,0 +1,51 @@
package com.qiaoba.module.demo.service.impl;
import cn.hutool.core.thread.ThreadUtil;
import com.baomidou.lock.LockInfo;
import com.baomidou.lock.LockTemplate;
import com.qiaoba.common.base.exception.ServiceException;
import com.qiaoba.module.demo.service.DemoLock4jService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
/**
* Demo-Lock4j 业务层实现
*
* @author ailanyin
* @version 1.0
* @since 2022-09-22 04:20:28
*/
@Service
@RequiredArgsConstructor
@Slf4j
public class DemoLock4jServiceImpl implements DemoLock4jService {
private final LockTemplate lockTemplate;
@Override
public void apiLock() {
//... 各种不需要上锁的操作
String lockKey = "lock4j:apiLock";
// expire = -1 锁自动续期
final LockInfo lockInfo = lockTemplate.lock(lockKey, -1, 1000);
//申请锁失败
if (null == lockInfo) {
throw new ServiceException("业务处理中,请稍后再试...");
}
//申请锁成功
try {
ThreadUtil.sleep(30 * 1000);
log.info("执行完毕 apiLock, 当前线程{}", Thread.currentThread().getName());
} finally {
lockTemplate.releaseLock(lockInfo);
}
}
@Override
public void annotationLock() {
// 休眠 10s
ThreadUtil.sleep(10 * 1000);
log.info("执行完毕 annotationLock, 当前线程{}", Thread.currentThread().getName());
}
}

View File

@ -0,0 +1,15 @@
server:
port: 8080
springdoc:
swagger-ui:
path: /swagger-ui.html
tags-sorter: alpha
operations-sorter: alpha
api-docs:
path: /v3/api-docs
group-configs:
- group: 'Demo'
paths-to-match: '/**'
packages-to-scan: com.qiaoba.module.demo.controller