当前位置: 首页 > news >正文

网站建设与维护期末试卷成都网站推广创新互联

网站建设与维护期末试卷,成都网站推广创新互联,网站建设合同封面,网页设计实训报告1200SpringBoot整合Redis实现分布式锁 分布式系统为什么要使用分布式锁#xff1f; 首先#xff0c;分布式系统是由多个独立节点组成的#xff0c;这些节点可能运行在不同的物理或虚拟机器上#xff0c;它们通过网络进行通信和协作。在这样的环境中#xff0c;多个节点可能同…SpringBoot整合Redis实现分布式锁 分布式系统为什么要使用分布式锁 首先分布式系统是由多个独立节点组成的这些节点可能运行在不同的物理或虚拟机器上它们通过网络进行通信和协作。在这样的环境中多个节点可能同时尝试访问和修改共享资源这就可能导致数据不一致和并发冲突的问题。 为了解决这个问题分布式锁被引入作为一种同步机制。分布式锁能够在分布式系统中提供全局唯一的锁确保在任意时刻只有一个节点能够访问和修改共享资源。当节点需要访问共享资源时它必须先获取分布式锁如果锁已经被其他节点持有则当前节点需要等待或执行其他操作。只有当节点成功获取到锁后才能对共享资源进行访问和修改。 此外分布式锁还可以帮助实现一些复杂的分布式系统需求如分布式事务、分布式缓存一致性等。通过使用分布式锁我们可以更好地控制并发访问保证数据的一致性和完整性提高系统的可靠性和稳定性。 实现分布式锁需要考虑到多种因素如锁的粒度、锁的公平性、锁的释放机制等。同时由于分布式系统的复杂性和不确定性分布式锁的实现也可能存在一些挑战和限制如网络延迟、节点故障等问题都可能导致锁的行为变得复杂和难以预测。因此在使用分布式锁时我们需要根据实际情况进行权衡和选择确保能够满足系统的需求并避免潜在的问题。 分布式系统使用分布式锁是为了实现资源的同步访问和并发控制保证数据的一致性和完整性提高系统的可靠性和稳定性。 redis配置类 package com.test.config;import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer;/*** author TANGSHUAI* version 1.0* date 2023-06-09 10:58*/ Configuration public class RedisConfig {Beanpublic RedisTemplateString, String redisTemplate(RedisConnectionFactory factory) {RedisTemplateString, String redisTemplate new RedisTemplate();redisTemplate.setConnectionFactory(factory);//设置序列化Key的实例化对象redisTemplate.setKeySerializer(new StringRedisSerializer());//设置序列化Value的实例化对象redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());return redisTemplate;}} 分布式锁工具类 package com.test.config;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.script.DefaultRedisScript; import org.springframework.stereotype.Component;import java.util.Collections; import java.util.concurrent.TimeUnit;/*** author TANGSHUAI* version 1.0* date 2024-03-08 14:33*/ Component public class RedisDistributedLock {Autowiredprivate StringRedisTemplate stringRedisTemplate;/*** 尝试获取分布式锁** param lockKey 锁* param requestId 请求标识* param expireTime 超期时间* return 是否获取成功*/public boolean tryGetDistributedLock(String lockKey, String requestId, int expireTime) {return stringRedisTemplate.opsForValue().setIfAbsent(lockKey, requestId, expireTime, TimeUnit.SECONDS);}/*** 释放分布式锁** param lockKey 锁* param requestId 请求标识* return 是否释放成功*/public boolean releaseDistributedLock(String lockKey, String requestId) {String script if redis.call(get, KEYS[1]) ARGV[1] then return redis.call(del, KEYS[1]) else return 0 end;Long result stringRedisTemplate.execute(new DefaultRedisScriptLong(script, Long.class),Collections.singletonList(lockKey),requestId);System.out.println(result);return result.equals(1L);} } 定时任务测试分布式锁 package com.test.service;import com.test.config.RedisDistributedLock; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional;/*** author TANGSHUAI* version 1.0* date 2024-03-08 14:44*/ Service public class TestService {Autowiredprivate RedisDistributedLock redisDistributedLock;Scheduled(cron 0/10 * * * * ?)//Scheduled(cron 0 10 16 * * ?)Transactional(rollbackFor Exception.class)public void testLock(){System.out.println(进入分布式锁方法);// 尝试获取分布式锁if (redisDistributedLock.tryGetDistributedLock(tangshuai, 123, 30)) {System.out.println(释放分布式锁);redisDistributedLock.releaseDistributedLock(tangshuai, 123);} else {// 未能获取到锁可以选择重试或返回失败throw new RuntimeException(未能获取到锁,请重试);}} } 分别启动两个相同的服务 package com.test;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling;/*** author TANGSHUAI* version 1.0* date 2023-06-08 17:04*/ SpringBootApplication EnableScheduling public class SaTokenDemoApp {public static void main(String[] args) {SpringApplication.run(SaTokenDemoApp.class, args);} } 查看两个控制台打印情况两个服务谁谁拿到锁谁开始执行业务代码没拿到锁的服务抛出异常
http://www.zqtcl.cn/news/239988/

相关文章:

  • php做的网站如何该样式如何卸载安装wordpress
  • 建设网站公司哪家性价比高最新推广注册app拿佣金
  • 丽水网站建设公司排名wordpress 去掉图片链接
  • 以下属于网站页面设计的原则有2345网址下载
  • 为网站的特色功能设计各种模板温州网页模板建站
  • 广州微网站建设企业网站建设网站优化推广
  • 大连模板网建站项目网络的关键路径
  • 迅雷黄冈网站推广软件徐州模板自助建站
  • 怎么做不占CPU的网站修改wordpress的登陆地址
  • 网站制作毕业设计论文软件ui设计培训机构
  • 物业网站模板哪里建设网站
  • 达州城乡建设网站手机网站 方案
  • 平台兼职网站开发许昌做网站优化
  • 婴幼儿用品网站开发意义基因网站开发
  • 自己网站页面设计软件传奇世界游戏官网
  • 淘宝网网站开发部技术部三亚私人高清影院品牌加盟
  • 网站是用什么软件做的山西网络科技有限公司
  • 汕头网站建设开发做购物网站 营业范围是什么
  • 网站建设 企业短视频运营计划书
  • 网站仿静态网站城市分站织梦系统
  • 淄博网站建设高端企业最新商业资讯
  • 百度推广太原网站建设wordpress的页面和首页一样
  • 无为网站定制php网站 mysql数据库配置文件
  • 如何利用div做网站wordpress替换百度站内搜索
  • 大德通网站建设互动营销网站
  • 网站建设与管理实训主要内容响应式网站建设智能优化
  • 佛山市企业网站建设报价网站建
  • 广州网站营销推广设计孝义网站开发
  • 新站网站如何做Seo那个网站点击率高
  • 个体做外贸的网站罗浮视窗网站建设