提交 38fcc5c7 编写于 作者: X xiongchun

优化pangu-cache-spring-boot-starter 增加分布式锁支持

上级 801f9078
......@@ -52,6 +52,10 @@
<groupId>com.github.xiaolyuh</groupId>
<artifactId>layering-cache-starter</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>lock4j-redis-template-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
......
......@@ -29,6 +29,9 @@
- **pangu-examples-cache-single**
1. 如何使用原生的一级缓存RedisTemplate API
- **pangu-examples-lock-redis-based**
1. 如何使用基于Redis的分布式锁(注解式、API式)
- **pangu-examples-dubbo-api**
1. 开发Dubbo服务时接口文件和POJO相关类的打包模块
......
......@@ -16,7 +16,7 @@
#
spring.application.name=pangu-examples-cache-single
# spring-cache \u4E00\u7EA7\u7F13\u5B58
# spring-redis \u4E00\u7EA7\u7F13\u5B58
spring.redis.host=localhost
spring.redis.database=1
spring.redis.port=6379
......
/target/
/bin/
!.mvn/wrapper/maven-wrapper.jar
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/build/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
#### :mushroom: 本范例演示功能
1. 如何使用基于Redis的分布式锁(注解式、API式)。
**更多开发指南请参考盘古平台相关文档说明。**
\ No newline at end of file
<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.gitee.pulanos.pangu</groupId>
<artifactId>pangu-parent</artifactId>
<version>5.0.7</version>
<relativePath/>
</parent>
<groupId>com.gitee.pulanos.pangu</groupId>
<artifactId>pangu-examples-lock-redis-based</artifactId>
<packaging>jar</packaging>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>com.gitee.pulanos.pangu</groupId>
<artifactId>pangu-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.gitee.pulanos.pangu</groupId>
<artifactId>pangu-cache-spring-boot-starter</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot-maven-plugin.version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.gitee.pulanos.pangu.showcases.lock.redis;
import com.gitee.pulanos.pangu.framework.starter.PanGuApplicationBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author xiongchun
*/
@Slf4j
@SpringBootApplication
public class LockPanguApplication {
public static void main(String[] args) {
PanGuApplicationBuilder.init(LockPanguApplication.class).run(args);
}
}
\ No newline at end of file
package com.gitee.pulanos.pangu.showcases.lock.redis.service;
import cn.hutool.core.thread.ThreadUtil;
import com.baomidou.lock.annotation.Lock4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
/**
* 基于注解方式的分布式锁
*
* @author xiongchun
*/
@Slf4j
@Component
public class AnnotationLockService {
/**
* 默认获取锁超时3秒,30秒锁过期
*/
@Lock4j
public void doBiz() {
log.info("执行doBiz...");
ThreadUtil.sleep(25*1000);
}
/**
* 配置获取锁超时时间和锁过期时间 支持SPEL
* @param accountId
*/
@Lock4j(keys = {"#accountId"}, expire = 20000, acquireTimeout = 10000)
public void doBiz2(Long accountId) {
log.info("执行doBiz2...");
ThreadUtil.sleep(20*1000);
}
}
package com.gitee.pulanos.pangu.showcases.lock.redis.service;
import cn.hutool.core.thread.ThreadUtil;
import com.baomidou.lock.LockInfo;
import com.baomidou.lock.LockTemplate;
import com.baomidou.lock.exception.LockException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* 基于API方式的分布式锁
*
* @author xiongchun
*/
@Slf4j
@Component
public class ApiLockService {
@Autowired
private LockTemplate lockTemplate;
public void apiLock(String userId) {
//... 各种不需要上锁的操作
String lockKey = "lock4j:ApiLockService:apiLock:" + userId;
final LockInfo lockInfo = lockTemplate.lock(lockKey);
//申请锁失败
if (null == lockInfo) {
throw new LockException("业务处理中,请稍后再试...");
}
//申请锁成功
try {
ThreadUtil.sleep(10000L);
log.info("执行apiLock, 当前线程{}", Thread.currentThread().getName());
} finally {
lockTemplate.releaseLock(lockInfo);
}
}
}
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
spring.application.name=pangu-examples-lock-redis-based
# spring-redis
spring.redis.host=localhost
spring.redis.database=1
spring.redis.port=6379
spring.redis.password=
logging.level.root=INFO
logging.level.com.gitee.pulanos.pangu=INFO
logging.level.com.baomidou.lock=DEBUG
\ No newline at end of file
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
spring.profiles.active=${spring.profiles.active:dev}
\ No newline at end of file
package com.gitee.pulanos.pangu.showcases.lock.redis.service;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.*;
@RunWith(SpringRunner.class)
@SpringBootTest
public class AnnotationLockServiceTest {
@Autowired
private AnnotationLockService annotationLockService;
@Test
public void doBiz() {
annotationLockService.doBiz();
}
@Test
public void doBizAgain() {
annotationLockService.doBiz();
}
@Test
public void doBiz2() {
annotationLockService.doBiz2(100L);
}
}
\ No newline at end of file
package com.gitee.pulanos.pangu.showcases.lock.redis.service;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.*;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ApiLockServiceTest {
@Autowired
private ApiLockService apiLockService;
@Test
public void apiLock() {
apiLockService.apiLock("1000");
}
}
\ No newline at end of file
......@@ -19,6 +19,7 @@
<module>pangu-examples-crud</module>
<module>pangu-examples-cache-single</module>
<module>pangu-examples-cache-layering</module>
<module>pangu-examples-lock-redis-based</module>
<module>pangu-examples-dubbo-api</module>
<module>pangu-examples-dubbo-service</module>
<module>pangu-examples-dubbo-consumer</module>
......
......@@ -52,6 +52,7 @@
<nacos-config-spring-boot-starter.version>0.2.7</nacos-config-spring-boot-starter.version>
<nacos-config-spring-boot-actuator.version>0.2.7</nacos-config-spring-boot-actuator.version>
<mybatis-plus.version>3.4.3.2</mybatis-plus.version>
<lock4j.version>2.2.1</lock4j.version>
<shenyu.version>2.4.1</shenyu.version>
<layering-cache.version>3.3.4</layering-cache.version>
<mysql-connector-java.version>8.0.26</mysql-connector-java.version>
......@@ -135,6 +136,11 @@
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>lock4j-redis-template-spring-boot-starter</artifactId>
<version>${lock4j.version}</version>
</dependency>
<dependency>
<groupId>com.github.xiaolyuh</groupId>
<artifactId>layering-cache-starter</artifactId>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册