首页
看点啥
插画图片
首页 看点啥 spring-boot使用tools-redis实现分布式锁

spring-boot使用tools-redis实现分布式锁

2026-08-05 0

1.在pom文件中导入tools-redis

spring-boot使用tools-redis实现分布式锁

cn.gjingtools-redis1.0.0

2.在application.properties文件中添加redis连接

#redis数据库链接配置spring.redis.host=127.0.0.1spring.redis.port=6379spring.redis.password=spring.redis.database=0

3.在DemoApplication.java中添加

@EnableRedisLock

4.编写一个例子给到

4.1创建DemoController

@RestControllerpublic class DemoController {@Autowiredprivate DemoService demoService;@GetMapping("demo")public boolean demo(){return demoService.demo();}}

4.2创建DemoService

@Servicepublic class DemoService {private static Integer thread=20;@Lock(key = "demo")public boolean demo(){System.out.println("当前线程:"   Thread.currentThread().getName() "未消费:" thread);if(thread==0) {System.out.println("消费完成"); return true;}--thread;return false;}}

5.测试

http://127.0.0.1:1111/demo
喜欢(0)

上一篇

spring-boot整合缓存的初步使用

spring-boot整合缓存的初步使用

下一篇

10、web爬虫讲解2—Scrapy框架爬虫—Scrapy安装—Scrapy指令

10、web爬虫讲解2—Scrapy框架爬虫—Scrapy安装—Scrapy指令
猜你喜欢