最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網 會員登陸 & 注冊

自定義starter,注解,實現分布式鎖

2023-07-09 12:08 作者:鱸魚懂個der的Java  | 我要投稿

某乎我也寫了,如果想直接用代碼請直接搜索這個文章標題的某乎

自定義RedissonClient-starter

依賴


<dependencies> ?<dependency> ? ?<groupId>org.springframework.boot</groupId> ? ?<artifactId>spring-boot-starter</artifactId> ? ?<version>3.1.0</version> ?</dependency> ?<dependency> ? ?<groupId>org.springframework.boot</groupId> ? ?<artifactId>spring-boot-autoconfigure</artifactId> ? ?<version>3.1.0</version> ?</dependency> ?<dependency> ? ?<groupId>org.springframework.boot</groupId> ? ?<artifactId>spring-boot-configuration-processor</artifactId> ? ?<version>3.1.0</version> ?</dependency> ?<dependency> ? ?<groupId>org.projectlombok</groupId> ? ?<artifactId>lombok</artifactId> ? ?<version>1.18.26</version> ?</dependency> ?<dependency> ? ?<groupId>org.redisson</groupId> ? ?<artifactId>redisson-spring-boot-starter</artifactId> ? ?<version>3.15.6</version> ?</dependency> </dependencies>

基礎配置類

ConfigurationProperties裝載的時候會在properties/yaml/yml文件中找到前綴位my:caplock之后的配置注解。

請注意:對于裝配的時候如果默認會my:caplock之后一級屬性的作為配置類的的屬性,如果存在my:caplock:lock1:one這種二級屬性,此時one會作為lock1(可以看作新類)的一個屬性,并且會存在裝載配置問題。原因去看springBoot的中文文檔,可以去看看springboot的外部配置這一板塊

@EnableConfigurationProperties這種和@Value的區(qū)別之一是后者可以實現spel表達式的識別,文章下面的自定義注解這種就需要用對應的paser來進行執(zhí)行語句表達獲取value

SPEL:核心技術_Spring 框架文檔 (springref.com)


@Setter @Getter @ConfigurationProperties("my.caplock") public class RedisConfig { private String url; private boolean usage; }

配置類


@Configuration(proxyBeanMethods = false) @EnableConfigurationProperties(RedisConfig.class) @Log4j2 public class MyRedisCapLockConfig { @Bean public RedissonClient redissonClient(RedisConfig redisConfig) { if (redisConfig.isUsage()==false){ log.info("不啟用redis分布式鎖"); return null; ? ? ? ?} ? ? ? ?Config config = new Config(); ? ? ? ?config.setTransportMode(TransportMode.NIO); ? ? ? ?SingleServerConfig singleServerConfig = config.useSingleServer(); ? ? ? ?singleServerConfig.setAddress(redisConfig.getUrl()); return Redisson.create(config); ? ?} }

在當前項目的resources\META-INF的文件夾下出創(chuàng)建spring.factories并附上以下內容。


org.springframework.boot.autoconfigure.EnableAutoConfiguration= com.luyu.config.MyRedisCapLockConfig

點擊idea下的將對應的maven模塊的生命周期進行install保存該項目到本地倉庫中


編輯

自定義注解

注意:請在啟動類上加上掃描組件的注解,防止spring沒有掃描到我們封裝的bean,還有開啟切面代理


@SpringBootApplication @ComponentScan({"com.luyu"}) @EnableAspectJAutoProxy public class RedisLockApplication { ? ?public static void main(String[] args) { ? ? ? ?SpringApplication.run(RedisLockApplication.class, args); ? ?} }


編輯切換為居中

啟動項目依賴


<dependencies> ? ?<dependency> ? ? ? ?<groupId>org.springframework.boot</groupId> ? ? ? ?<artifactId>spring-boot-starter-web</artifactId> ? ?</dependency> ? ?<dependency> ? ? ? ?<groupId>com.luyu</groupId> ? ? ? ?<artifactId>myRedisStarter</artifactId> ? ? ? ?<version>1.1-SNAPSHOT</version> ? ?</dependency> ? ?<dependency> ? ? ? ?<groupId>org.springframework.boot</groupId> ? ? ? ?<artifactId>spring-boot-starter-test</artifactId> ? ? ? ?<scope>test</scope> ? ?</dependency> ? ?<dependency> ? ? ? ?<groupId>org.springframework.boot</groupId> ? ? ? ?<artifactId>spring-boot-starter-aop</artifactId> ? ?</dependency> </dependencies>


目前我們實現的是一個實現分布式鎖功能的注解Lock


import java.lang.annotation.*; /** * @Author luYu * @Date 2023/7/8 13:19 */ @Retention(RetentionPolicy.RUNTIME) //在執(zhí)行時有效 @Target({ElementType.METHOD}) //允許在方法注解 @Repeatable(Lock.Locks.class) //允許重復注解,前提必須是實現了擁有該目標注解的名字為value的注釋數組 @Documented //javadoc 上下文運行 @Inherited ?// 繼承 public @interface Lock { ? ?// 鎖住的key ? ?String lockKey()default ""; ? ?// 鎖住的時間 ? ?int lockTime()default 5; ? ?@Retention(RetentionPolicy.RUNTIME) ? ?@Target(ElementType.METHOD) ? ?@Inherited ? ?@Documented ? ?public @interface Locks { ? ? ? ?Lock[] value(); ? ?} }

相關切面


@Component @Aspect public class LockAspect { @Resource private RedissonClient redissonClient; @Around("@annotation(com.luyu.redislock.anno.Lock)||@annotation(com.luyu.redislock.anno.Lock.Locks)") public void getvoid(ProceedingJoinPoint joinPoint){ ? ? ? ?MethodSignature signature = ((MethodSignature) joinPoint.getSignature()); Lock[] annos = signature.getMethod().getAnnotationsByType(Lock.class); getLock(toLock -> { ? ? ? ? ? ?ArrayList<RLock> rLocks = new ArrayList<>(); for (Lock anno : toLock) { ? ? ? ? ? ? ? ?RLock lock = redissonClient.getLock(anno.lockKey()); try { boolean b = lock.tryLock(anno.lockTime(), TimeUnit.SECONDS); if (!b){ throw new RuntimeException("上鎖失敗:"+lock.getName()); ? ? ? ? ? ? ? ? ? ?} ? ? ? ? ? ? ? ? ? ?rLocks.add(lock); joinPoint.proceed(); ? ? ? ? ? ? ? ?} catch (InterruptedException e) { ? ? ? ? ? ? ? ? ? ?e.printStackTrace(); throw new RuntimeException("上鎖失?。?#34;+lock.getName()); ? ? ? ? ? ? ? ?} catch (Throwable e) { throw new RuntimeException(e); ? ? ? ? ? ? ? ?} finally { unLock(rLocks); ? ? ? ? ? ? ? ?} ? ? ? ? ? ?} ? ? ? ?}, annos); ? ?} public static void getLock(Consumer<Lock[]> consumer, Lock... locks) { ? ? ? ?consumer.accept(locks); ? ?} public static void unLock(ArrayList<RLock> rLocks) { if (rLocks.isEmpty()) { return; ? ? ? ?} for (RLock rLock : rLocks) { if (rLock.isLocked() && rLock.isHeldByCurrentThread()){ ? ? ? ? ? ? ? ?rLock.unlock(); ? ? ? ? ? ? ? ?System.out.println("解鎖:"+rLock.getName()); ? ? ? ? ? ?} ? ? ? ?} ? ?} }


接口


@RestController @RequestMapping("/1") public class LockController { @GetMapping("/ad") @Lock(lockKey = "luyu",lockTime = 4) @Lock(lockKey = "luyu1",lockTime = 3) @SneakyThrows public void ?getLock(){ try { ? ? ? ? ? ?TimeUnit.SECONDS.sleep(2); ? ? ? ?} finally { ? ? ? ? ? ?System.out.println("鱸魚來了"); ? ? ? ?} ? ?} }


結果:

因為我在aop重復執(zhí)行joinPoint.proceed();就輸出了兩次鱸魚來了,不要在意細節(jié)哈哈。


編輯切換為居中

給大家留了一個坑,@SneakyThrows和很多catach,可以去了解一下。

覺得可以的話點個贊是我持續(xù)分享的動力(努力提高我分享的水平)

自定義starter,注解,實現分布式鎖的評論 (共 條)

分享到微博請遵守國家法律
平谷区| 名山县| 邢台县| 汝阳县| 马边| 阿坝县| 泽库县| 佛冈县| 沈阳市| 巨鹿县| 邢台市| 临夏县| 常山县| 庆城县| 钟祥市| 建宁县| 临清市| 保山市| 类乌齐县| 海原县| 兖州市| 石河子市| 宁安市| 阿拉善左旗| 梨树县| 湘潭市| 云和县| 上饶县| 济源市| 达拉特旗| 黄龙县| 临澧县| 栾城县| 博客| 榆树市| 阿坝县| 秀山| 赤峰市| 罗源县| 连江县| 湟中县|