add
This commit is contained in:
12
pom.xml
12
pom.xml
@ -39,6 +39,8 @@
|
||||
<druid.version>1.2.17</druid.version>
|
||||
<!-- HuTool工具包 -->
|
||||
<hutool.version>5.8.18</hutool.version>
|
||||
<!-- lock4j -->
|
||||
<lock4j.version>2.2.5</lock4j.version>
|
||||
<!-- mysql驱动 -->
|
||||
<mysql-connector.version>8.0.33</mysql-connector.version>
|
||||
<!-- PgSql -->
|
||||
@ -73,6 +75,11 @@
|
||||
<artifactId>hutool-all</artifactId>
|
||||
<version>${hutool.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>lock4j-redis-template-spring-boot-starter</artifactId>
|
||||
<version>${lock4j.version}</version>
|
||||
</dependency>
|
||||
<!-- easy poi -->
|
||||
<dependency>
|
||||
<groupId>cn.afterturn</groupId>
|
||||
@ -153,6 +160,11 @@
|
||||
<artifactId>qiaoba-module-monitor</artifactId>
|
||||
<version>${qiaoba.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.qiaoba</groupId>
|
||||
<artifactId>qiaoba-module-demo</artifactId>
|
||||
<version>${qiaoba.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.qiaoba</groupId>
|
||||
<artifactId>qiaoba-common-base</artifactId>
|
||||
|
@ -48,7 +48,7 @@ public class SpringSecurityConfig {
|
||||
registry.antMatchers(url).permitAll();
|
||||
}
|
||||
// 文件资源 无需鉴权
|
||||
registry.antMatchers(BaseConstant.RESOURCE_PREFIX + "/**").permitAll();
|
||||
registry.antMatchers(BaseConstant.RESOURCE_PATTERN).permitAll();
|
||||
// 由于使用的是JWT,我们这里不需要csrf
|
||||
httpSecurity.csrf()
|
||||
.disable()
|
||||
|
@ -59,6 +59,11 @@ public class BaseConstant {
|
||||
*/
|
||||
public static final String RESOURCE_PREFIX = "/resource";
|
||||
|
||||
/**
|
||||
* 资源映射路径 正则
|
||||
*/
|
||||
public static final String RESOURCE_PATTERN = RESOURCE_PREFIX + "/**";
|
||||
|
||||
/**
|
||||
* 是
|
||||
*/
|
||||
|
@ -12,10 +12,10 @@
|
||||
<artifactId>qiaoba-common-redis</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<!-- redis -->
|
||||
<!-- lock4j -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>lock4j-redis-template-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.qiaoba</groupId>
|
||||
|
@ -1,5 +1,7 @@
|
||||
package com.qiaoba.common.web.utils;
|
||||
|
||||
import cn.hutool.http.ContentType;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.qiaoba.common.base.result.AjaxResult;
|
||||
|
||||
@ -18,7 +20,7 @@ public class ResponseUtil {
|
||||
|
||||
public static void response(HttpServletResponse response, String msg) throws IOException {
|
||||
response.setStatus(HttpServletResponse.SC_OK);
|
||||
response.setContentType("text/plain; charset=UTF-8");
|
||||
response.setContentType(ContentType.JSON.getValue());
|
||||
PrintWriter writer = response.getWriter();
|
||||
writer.write(msg);
|
||||
writer.close();
|
||||
|
@ -17,6 +17,7 @@
|
||||
<module>qiaoba-module-file</module>
|
||||
<module>qiaoba-module-monitor</module>
|
||||
<module>qiaoba-module-tenant</module>
|
||||
<module>qiaoba-module-demo</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
||||
|
28
qiaoba-modules/qiaoba-module-demo/pom.xml
Normal file
28
qiaoba-modules/qiaoba-module-demo/pom.xml
Normal file
@ -0,0 +1,28 @@
|
||||
<?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>
|
||||
|
||||
<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>
|
||||
</dependencies>
|
||||
</project>
|
@ -0,0 +1,37 @@
|
||||
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.Lock4jService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/demo/lock4j")
|
||||
@RequiredArgsConstructor
|
||||
public class Lock4jController {
|
||||
|
||||
private final Lock4jService lock4jService;
|
||||
|
||||
@GetMapping("/api-lock")
|
||||
public AjaxResult apiLock() {
|
||||
lock4jService.apiLock();
|
||||
return AjaxResult.success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 注解加锁
|
||||
* expire-Redis中Key过期时间-防止死锁-默认值: 30s
|
||||
* acquireTimeout-排队时长-超过就退出排队,抛出获取锁超时异常-默认值: 3s
|
||||
*
|
||||
* @return AjaxResult
|
||||
*/
|
||||
@GetMapping("/annotation-lock")
|
||||
@Lock4j(keys = {"#accountId"}, expire = 20000, acquireTimeout = 10000)
|
||||
public AjaxResult annotationLock() {
|
||||
lock4jService.annotationLock();
|
||||
return AjaxResult.success();
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package com.qiaoba.module.demo.service;
|
||||
|
||||
public interface Lock4jService {
|
||||
|
||||
/**
|
||||
* 分布式锁-编程式加锁
|
||||
*/
|
||||
void apiLock();
|
||||
|
||||
/**
|
||||
* 分布式锁-注解加锁
|
||||
*/
|
||||
void annotationLock();
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
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.exceptions.ServiceException;
|
||||
import com.qiaoba.module.demo.service.Lock4jService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class Lock4jServiceImpl implements Lock4jService {
|
||||
|
||||
private final LockTemplate lockTemplate;
|
||||
|
||||
@Override
|
||||
public void apiLock() {
|
||||
//... 各种不需要上锁的操作
|
||||
String lockKey = "lock4j:apiLock";
|
||||
final LockInfo lockInfo = lockTemplate.lock(lockKey);
|
||||
//申请锁失败
|
||||
if (null == lockInfo) {
|
||||
throw new ServiceException("业务处理中,请稍后再试...");
|
||||
}
|
||||
//申请锁成功
|
||||
try {
|
||||
ThreadUtil.sleep(10 * 1000);
|
||||
log.info("执行 apiLock, 当前线程{}", Thread.currentThread().getName());
|
||||
} finally {
|
||||
lockTemplate.releaseLock(lockInfo);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void annotationLock() {
|
||||
// 休眠 10s
|
||||
ThreadUtil.sleep(10 * 1000);
|
||||
log.info("执行 apiLock, 当前线程{}", Thread.currentThread().getName());
|
||||
}
|
||||
}
|
@ -26,7 +26,7 @@ public class FileConfig implements WebMvcConfigurer {
|
||||
|
||||
@Override
|
||||
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||
registry.addResourceHandler(BaseConstant.RESOURCE_PREFIX + "/**")
|
||||
registry.addResourceHandler(BaseConstant.RESOURCE_PATTERN)
|
||||
.addResourceLocations(URLUtil.FILE_URL_PREFIX + uploadPath);
|
||||
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package com.qiaoba.module.tenant.filters;
|
||||
import cn.hutool.core.collection.ListUtil;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import com.qiaoba.common.base.code.TenantErrorCode;
|
||||
import com.qiaoba.common.base.constants.BaseConstant;
|
||||
import com.qiaoba.common.base.constants.TenantConstant;
|
||||
import com.qiaoba.common.base.context.BaseContext;
|
||||
import com.qiaoba.common.database.config.DynamicDataSourceConfig;
|
||||
@ -43,8 +44,13 @@ public class DynamicDataSourceFilter extends OncePerRequestFilter {
|
||||
@Resource
|
||||
private DynamicDataSourceConfig dynamicDataSourceConfig;
|
||||
|
||||
private static final List<String> NOT_NEED_TENANT_URIS = ListUtil.toList("/**/*.html",
|
||||
"/**/*.css", "/**/*.js", "/swagger-resources", "/v3/api-docs/**", "/favicon.ico","/resource/**");
|
||||
private static final List<String> NOT_NEED_TENANT_URIS = ListUtil.toList(
|
||||
// 静态资源
|
||||
"/**/*.html", "/**/*.css", "/**/*.js",
|
||||
// Knife4j
|
||||
"/swagger-resources", "/v3/api-docs/**", "/favicon.ico",
|
||||
// File
|
||||
BaseConstant.RESOURCE_PATTERN);
|
||||
|
||||
@Override
|
||||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
|
||||
|
Reference in New Issue
Block a user