五月天青色头像情侣网名,国产亚洲av片在线观看18女人,黑人巨茎大战俄罗斯美女,扒下她的小内裤打屁股

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

自定義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,注解,實現分布式鎖的評論 (共 條)

分享到微博請遵守國家法律
新邵县| 石屏县| 宣武区| 庄浪县| 丁青县| 临沧市| 佛冈县| 贵州省| 合水县| 灵台县| 宝清县| 井冈山市| 广德县| 吉隆县| 和顺县| 东丰县| 巩留县| 南安市| 江阴市| 文水县| 德钦县| 龙泉市| 东台市| 汉寿县| 舒兰市| 景泰县| 陈巴尔虎旗| 根河市| 孝昌县| 白山市| 乌拉特后旗| 泰安市| 岳普湖县| 车致| 封开县| 项城市| 淮安市| 巢湖市| 漳浦县| 伊宁县| 承德市|