国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Spring Boot 集成 Redisson分布式鎖(注解版)

這篇具有很好參考價(jià)值的文章主要介紹了Spring Boot 集成 Redisson分布式鎖(注解版)。希望對大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

springboot 集成redisson分布式鎖,Redis,spring boot,分布式,java

????????Redisson 是一種基于 Redis 的 Java 駐留集群的分布式對象和服務(wù)庫,可以為我們提供豐富的分布式鎖和線程安全集合的實(shí)現(xiàn)。在 Spring Boot 應(yīng)用程序中使用 Redisson 可以方便地實(shí)現(xiàn)分布式應(yīng)用程序的某些方面,例如分布式鎖、分布式集合、分布式事件發(fā)布和訂閱等。本篇是一個(gè)使用 Redisson 實(shí)現(xiàn)分布式鎖的詳細(xì)示例,在這個(gè)示例中,我們定義了DistributedLock注解,它可以標(biāo)注在方法上,配合DistributedLockAspect切面以及IDistributedLock分布式鎖封裝的接口,來實(shí)現(xiàn)redisson 分布式鎖的 API 調(diào)用。

Spring Boot 集成 Redisson

?1、在 pom.xml 文件中添加 Redisson 的相關(guān)依賴

<dependency>
    <groupId>org.redisson</groupId>
    <artifactId>redisson-spring-boot-starter</artifactId>
    <version>3.16.1</version>
</dependency>

2、在 application.yml 中配置 Redisson單機(jī)模式 的連接信息和相關(guān)參數(shù)

spring:
  redis:
    host: localhost
    port: 6379
    password: null
redisson:
  codec: org.redisson.codec.JsonJacksonCodec
  threads: 4
  netty:
    threads: 4
  single-server-config:
    address: "redis://localhost:6379"
    password: null
    database: 0

3、Redission還支持主從、集群、哨兵配置

//主從模式
spring:
  redis:
    sentinel:
      master: my-master
      nodes: localhost:26379,localhost:26389
    password: your_password
redisson:
  master-slave-config:
    master-address: "redis://localhost:6379"
    slave-addresses: "redis://localhost:6380,redis://localhost:6381"
    password: ${spring.redis.password}

// 集群模式
spring:
  redis:
    cluster:
      nodes: localhost:6379,localhost:6380,localhost:6381,localhost:6382,localhost:6383,localhost:6384
    password: your_password
redisson:
  cluster-config:
    node-addresses: "redis://localhost:6379,redis://localhost:6380,redis://localhost:6381,redis://localhost:6382,redis://localhost:6383,redis://localhost:6384"
    password: ${spring.redis.password}

// 哨兵模式
spring:
  redis:
    sentinel:
      master: my-master
      nodes: localhost:26379,localhost:26389
    password: your_password
redisson:
  sentinel-config:
    master-name: my-master
    sentinel-addresses: "redis://localhost:26379,redis://localhost:26380,redis://localhost:26381"
    password: ${spring.redis.password}

?本地封裝Redisson 分布式鎖

1、定義IDistributedLock分布式鎖接口

public interface IDistributedLock {
    /**
     * 獲取鎖,默認(rèn)30秒失效,失敗一直等待直到獲取鎖
     *
     * @param key 鎖的key
     * @return 鎖對象
     */
    ILock lock(String key);

    /**
     * 獲取鎖,失敗一直等待直到獲取鎖
     *
     * @param key      鎖的key
     * @param lockTime 加鎖的時(shí)間,超過這個(gè)時(shí)間后鎖便自動(dòng)解鎖; 如果lockTime為-1,則保持鎖定直到顯式解鎖
     * @param unit     {@code lockTime} 參數(shù)的時(shí)間單位
     * @param fair     是否公平鎖
     * @return 鎖對象
     */
    ILock lock(String key, long lockTime, TimeUnit unit, boolean fair);

    /**
     * 嘗試獲取鎖,30秒獲取不到超時(shí)異常,鎖默認(rèn)30秒失效
     *
     * @param key     鎖的key
     * @param tryTime 獲取鎖的最大嘗試時(shí)間
     * @return
     * @throws Exception
     */
    ILock tryLock(String key, long tryTime) throws Exception;

    /**
     * 嘗試獲取鎖,獲取不到超時(shí)異常
     *
     * @param key      鎖的key
     * @param tryTime  獲取鎖的最大嘗試時(shí)間
     * @param lockTime 加鎖的時(shí)間
     * @param unit     {@code tryTime @code lockTime} 參數(shù)的時(shí)間單位
     * @param fair     是否公平鎖
     * @return
     * @throws Exception
     */
    ILock tryLock(String key, long tryTime, long lockTime, TimeUnit unit, boolean fair) throws Exception;

    /**
     * 解鎖
     *
     * @param lock
     * @throws Exception
     */
    void unLock(Object lock);


    /**
     * 釋放鎖
     *
     * @param lock
     * @throws Exception
     */
    default void unLock(ILock lock) {
        if (lock != null) {
            unLock(lock.getLock());
        }
    }


}

2、IDistributedLock實(shí)現(xiàn)類

@Slf4j
@Component
public class RedissonDistributedLock implements IDistributedLock {

    @Resource
    private RedissonClient redissonClient;
    /**
     * 統(tǒng)一前綴
     */
    @Value("${redisson.lock.prefix:bi:distributed:lock}")
    private String prefix;

    @Override
    public ILock lock(String key) {
        return this.lock(key, 0L, TimeUnit.SECONDS, false);
    }

    @Override
    public ILock lock(String key, long lockTime, TimeUnit unit, boolean fair) {
        RLock lock = getLock(key, fair);
        // 獲取鎖,失敗一直等待,直到獲取鎖,不支持自動(dòng)續(xù)期
        if (lockTime > 0L) {
            lock.lock(lockTime, unit);
        } else {
            // 具有Watch Dog 自動(dòng)延期機(jī)制 默認(rèn)續(xù)30s 每隔30/3=10 秒續(xù)到30s
            lock.lock();
        }
        return new ILock(lock, this);
    }

    @Override
    public ILock tryLock(String key, long tryTime) throws Exception {
        return this.tryLock(key, tryTime, 0L, TimeUnit.SECONDS, false);
    }

    @Override
    public ILock tryLock(String key, long tryTime, long lockTime, TimeUnit unit, boolean fair)
            throws Exception {
        RLock lock = getLock(key, fair);
        boolean lockAcquired;
        // 嘗試獲取鎖,獲取不到超時(shí)異常,不支持自動(dòng)續(xù)期
        if (lockTime > 0L) {
            lockAcquired = lock.tryLock(tryTime, lockTime, unit);
        } else {
            // 具有Watch Dog 自動(dòng)延期機(jī)制 默認(rèn)續(xù)30s 每隔30/3=10 秒續(xù)到30s
            lockAcquired = lock.tryLock(tryTime, unit);
        }
        if (lockAcquired) {
            return new ILock(lock, this);
        }
        return null;
    }

    /**
     * 獲取鎖
     *
     * @param key
     * @param fair
     * @return
     */
    private RLock getLock(String key, boolean fair) {
        RLock lock;
        String lockKey = prefix + ":" + key;
        if (fair) {
            // 獲取公平鎖
            lock = redissonClient.getFairLock(lockKey);
        } else {
            // 獲取普通鎖
            lock = redissonClient.getLock(lockKey);
        }
        return lock;
    }

    @Override
    public void unLock(Object lock) {
        if (!(lock instanceof RLock)) {
            throw new IllegalArgumentException("Invalid lock object");
        }
        RLock rLock = (RLock) lock;
        if (rLock.isLocked()) {
            try {
                rLock.unlock();
            } catch (IllegalMonitorStateException e) {
                log.error("釋放分布式鎖異常", e);
            }
        }
    }
}

?3、定義ILock鎖對象

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Objects;

/**
 * <p>
 * RedissonLock 包裝的鎖對象 實(shí)現(xiàn)AutoCloseable接口,在java7的try(with resource)語法,不用顯示調(diào)用close方法
 * </p>

 * @since 2023-06-08 16:57
 */
@AllArgsConstructor
public class ILock implements AutoCloseable {
    /**
     * 持有的鎖對象
     */
    @Getter
    private Object lock;
    /**
     * 分布式鎖接口
     */
    @Getter
    private IDistributedLock distributedLock;

    @Override
    public void close() throws Exception {
        if(Objects.nonNull(lock)){
            distributedLock.unLock(lock);
        }
    }
}

4、注入IDistributedLock接口使用示例

// 定義接口
public interface IProductSkuSupplierMeasureService {
    /**
     * 保存SKU供應(yīng)商供貨信息
     *
     * @param dto
     * @return
     *
    Boolean saveSupplierInfo(ProductSkuSupplierInfoDTO dto);

    /**
     * 編輯SKU供應(yīng)商供貨信息
     *
     * @param dto
     * @return
     */
    Boolean editSupplierInfo(ProductSkuSupplierInfoDTO dto);
}    


手動(dòng)釋放鎖示例?

// 實(shí)現(xiàn)類
@Service
public class ProductSkuSupplierMeasureServiceImpl 
        implements IProductSkuSupplierMeasureService {

    @Resource
    private IDistributedLock distributedLock;    
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveSupplierInfo(ProductSkuSupplierInfoDTO dto) {
        // 手動(dòng)釋放鎖
        String sku = dto.getSku();
        ILock lock = null;
        try {
            lock = distributedLock.lock(dto.getSku(),10L, TimeUnit.SECONDS, false);
            if (Objects.isNull(lock)) {
                throw new BusinessException("Duplicate request for method still in process");
            }
            // 業(yè)務(wù)代碼
        }catch (BusinessException e) {
            throw new BusinessException(e.getMessage());
        } catch (Exception e) {
            log.error("保存異常", e);
            throw new BusinessException (e.getMessage());
        } finally {
            if (Objects.nonNull(lock)) {
                distributedLock.unLock(lock);
            }
        }
        return Boolean.TRUE;
    }

}

?使用try-with-resources 語法糖自動(dòng)釋放鎖

// 實(shí)現(xiàn)類
@Service
public class ProductSkuSupplierMeasureServiceImpl 
        implements IProductSkuSupplierMeasureService {

    @Resource
    private IDistributedLock distributedLock;    
   

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean editSupplierInfo(ProductSkuSupplierInfoDTO dto) {
       
        String sku = dto.getSku();
        // try-with-resources 語法糖自動(dòng)釋放鎖
        try(ILock lock = distributedLock.lock(dto.getSku(),10L, TimeUnit.SECONDS, false)) {
            if(Objects.isNull(lock)){
                throw new BusinessException ("Duplicate request for method still in process");
            }
            
            // 業(yè)務(wù)代碼
        }catch (BusinessException e) {
            throw new BusinessException (e.getMessage());
        } catch (Exception e) {
            log.error("修改異常", e);
            throw new BusinessException ("修改異常");
        }
        return Boolean.TRUE;
   }
}

5、使用AOP切面實(shí)現(xiàn)分布式鎖的綁定

? 定義DistributedLock注解

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DistributedLock {

    /**
     * 保證業(yè)務(wù)接口的key的唯一性,否則失去了分布式鎖的意義 鎖key
     * 支持使用spEl表達(dá)式
     */
    String key();

    /**
     * 保證業(yè)務(wù)接口的key的唯一性,否則失去了分布式鎖的意義 鎖key 前綴
     */
    String keyPrefix() default "";

    /**
     * 是否在等待時(shí)間內(nèi)獲取鎖,如果在等待時(shí)間內(nèi)無法獲取到鎖,則返回失敗
     */
    boolean tryLok() default false;

    /**
     * 獲取鎖的最大嘗試時(shí)間 ,會(huì)嘗試tryTime時(shí)間獲取鎖,在該時(shí)間內(nèi)獲取成功則返回,否則拋出獲取鎖超時(shí)異常,tryLok=true時(shí),該值必須大于0。
     *
     */
    long tryTime() default 0;

    /**
     * 加鎖的時(shí)間,超過這個(gè)時(shí)間后鎖便自動(dòng)解鎖
     */
    long lockTime() default 30;

    /**
     * tryTime 和 lockTime的時(shí)間單位
     */
    TimeUnit unit() default TimeUnit.SECONDS;

    /**
     * 是否公平鎖,false:非公平鎖,true:公平鎖
     */
    boolean fair() default false;
}

?定義DistributedLockAspect Lock切面

@Aspect
@Slf4j
public class DistributedLockAspect {

    @Resource
    private IDistributedLock distributedLock;

    /**
     * SpEL表達(dá)式解析
     */
    private SpelExpressionParser spelExpressionParser = new SpelExpressionParser();

    /**
     * 用于獲取方法參數(shù)名字
     */
    private DefaultParameterNameDiscoverer nameDiscoverer = new DefaultParameterNameDiscoverer();

    @Pointcut("@annotation(com.yt.bi.common.redis.distributedlok.annotation.DistributedLock)")
    public void distributorLock() {
    }

    @Around("distributorLock()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        // 獲取DistributedLock
        DistributedLock distributedLock = this.getDistributedLock(pjp);
        // 獲取 lockKey
        String lockKey = this.getLockKey(pjp, distributedLock);
        ILock lockObj = null;
        try {
            // 加鎖,tryLok = true,并且tryTime > 0時(shí),嘗試獲取鎖,獲取不到超時(shí)異常
            if (distributedLock.tryLok()) {
                if(distributedLock.tryTime() <= 0){
                    throw new IdempotencyException("tryTime must be greater than 0");
                }
                lockObj = this.distributedLock.tryLock(lockKey, distributedLock.tryTime(), distributedLock.lockTime(), distributedLock.unit(), distributedLock.fair());
            } else {
                lockObj = this.distributedLock.lock(lockKey, distributedLock.lockTime(), distributedLock.unit(), distributedLock.fair());
            }

            if (Objects.isNull(lockObj)) {
                throw new IdempotencyException("Duplicate request for method still in process");
            }

            return pjp.proceed();
        } catch (Exception e) {
            throw e;
        } finally {
            // 解鎖
            this.unLock(lockObj);
        }
    }

    /**
     * @param pjp
     * @return
     * @throws NoSuchMethodException
     */
    private DistributedLock getDistributedLock(ProceedingJoinPoint pjp) throws NoSuchMethodException {
        String methodName = pjp.getSignature().getName();
        Class clazz = pjp.getTarget().getClass();
        Class<?>[] par = ((MethodSignature) pjp.getSignature()).getParameterTypes();
        Method lockMethod = clazz.getMethod(methodName, par);
        DistributedLock distributedLock = lockMethod.getAnnotation(DistributedLock.class);
        return distributedLock;
    }

    /**
     * 解鎖
     *
     * @param lockObj
     */
    private void unLock(ILock lockObj) {
        if (Objects.isNull(lockObj)) {
            return;
        }

        try {
            this.distributedLock.unLock(lockObj);
        } catch (Exception e) {
            log.error("分布式鎖解鎖異常", e);
        }
    }

    /**
     * 獲取 lockKey
     *
     * @param pjp
     * @param distributedLock
     * @return
     */
    private String getLockKey(ProceedingJoinPoint pjp, DistributedLock distributedLock) {
        String lockKey = distributedLock.key();
        String keyPrefix = distributedLock.keyPrefix();
        if (StringUtils.isBlank(lockKey)) {
            throw new IdempotencyException("Lok key cannot be empty");
        }
        if (lockKey.contains("#")) {
            this.checkSpEL(lockKey);
            MethodSignature methodSignature = (MethodSignature) pjp.getSignature();
            // 獲取方法參數(shù)值
            Object[] args = pjp.getArgs();
            lockKey = getValBySpEL(lockKey, methodSignature, args);
        }
        lockKey = StringUtils.isBlank(keyPrefix) ? lockKey : keyPrefix + lockKey;
        return lockKey;
    }

    /**
     * 解析spEL表達(dá)式
     *
     * @param spEL
     * @param methodSignature
     * @param args
     * @return
     */
    private String getValBySpEL(String spEL, MethodSignature methodSignature, Object[] args) {
        // 獲取方法形參名數(shù)組
        String[] paramNames = nameDiscoverer.getParameterNames(methodSignature.getMethod());
        if (paramNames == null || paramNames.length < 1) {
            throw new IdempotencyException("Lok key cannot be empty");
        }
        Expression expression = spelExpressionParser.parseExpression(spEL);
        // spring的表達(dá)式上下文對象
        EvaluationContext context = new StandardEvaluationContext();
        // 給上下文賦值
        for (int i = 0; i < args.length; i++) {
            context.setVariable(paramNames[i], args[i]);
        }
        Object value = expression.getValue(context);
        if (value == null) {
            throw new IdempotencyException("The parameter value cannot be null");
        }
        return value.toString();
    }

    /**
     * SpEL 表達(dá)式校驗(yàn)
     *
     * @param spEL
     * @return
     */
    private void checkSpEL(String spEL) {
        try {
            ExpressionParser parser = new SpelExpressionParser();
            parser.parseExpression(spEL, new TemplateParserContext());
        } catch (Exception e) {
            log.error("spEL表達(dá)式解析異常", e);
            throw new IdempotencyException("Invalid SpEL expression [" + spEL + "]");
        }
    }
}

定義分布式鎖注解版啟動(dòng)元注解

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import({DistributedLockAspect.class})
public @interface EnableDistributedLock {
}

6、AOP切面實(shí)現(xiàn)分布式鎖的綁定使用示例

? ?啟動(dòng)類添加@EnableDistributedLock啟用注解支持

@SpringBootApplication
@EnableDistributedLock
public class BiCenterGoodsApplication {

    public static void main(String[] args) {
        
        SpringApplication.run(BiCenterGoodsApplication.class, args);
        
    }
}

@DistributedLock標(biāo)注需要使用分布式鎖的方法?

    @ApiOperation("編輯SKU供應(yīng)商供貨信息")
    
    @PostMapping("/editSupplierInfo")
    //@DistributedLock(key = "#dto.sku + '-' + #dto.skuId", lockTime = 10L, keyPrefix = "sku-")
    @DistributedLock(key = "#dto.sku", lockTime = 10L, keyPrefix = "sku-")
    public R<Boolean> editSupplierInfo(@RequestBody @Validated ProductSkuSupplierInfoDTO dto) {
        return R.ok(productSkuSupplierMeasureService.editSupplierInfo(dto));
    }
#dto.sku是 SpEL表達(dá)式。Spring中支持的它都支持的。比如調(diào)用靜態(tài)方法,三目表達(dá)式。SpEL 可以使用方法中的任何參數(shù)。SpEL表達(dá)式參考

從原理到實(shí)踐,分析 Redis 分布式鎖的多種實(shí)現(xiàn)方案(一)_Ascend JF的博客-CSDN博客




?文章來源地址http://www.zghlxwxcb.cn/news/detail-705167.html

到了這里,關(guān)于Spring Boot 集成 Redisson分布式鎖(注解版)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • SpringBoot+Redisson分布式鎖

    org.redisson.config.Config類是Redisson框架中用于配置Redisson客戶端的類。以下是一些常用的配置項(xiàng): codec(編碼) :默認(rèn)值是org.redisson.codec.JsonJacksonCodec,用于定義與Redis交互時(shí)使用的編解碼器。 useSingleServer :設(shè)置為true時(shí),將使用單節(jié)點(diǎn)模式進(jìn)行連接。 useMasterSlave :設(shè)置為true時(shí),

    2024年01月19日
    瀏覽(25)
  • SpringBoot結(jié)合Redisson實(shí)現(xiàn)分布式鎖

    SpringBoot結(jié)合Redisson實(shí)現(xiàn)分布式鎖

    ?????作者名稱:DaenCode ??作者簡介:啥技術(shù)都喜歡搗鼓搗鼓,喜歡分享技術(shù)、經(jīng)驗(yàn)、生活。 ??人生感悟:嘗盡人生百味,方知世間冷暖。 ??所屬專欄:SpringBoot實(shí)戰(zhàn) 以下是專欄部分內(nèi)容,更多內(nèi)容請前往專欄查看! 標(biāo)題 一文帶你學(xué)會(huì)使用SpringBoot+Avue實(shí)現(xiàn)短信通知功能

    2024年02月08日
    瀏覽(26)
  • Spring Cloud Alibaba 最新版本(基于Spring Boot 3.1.0)整合完整使用及與各中間件集成
Sleuth+Zipkin集成分布式鏈路追蹤

    Spring Cloud Alibaba 最新版本(基于Spring Boot 3.1.0)整合完整使用及與各中間件集成 Sleuth+Zipkin集成分布式鏈路追蹤

    目錄 前言 源碼地址 官方中文文檔 使用版本 spring Spring Boot 3.1.0 中間件 使用到的組件與功能 環(huán)境安裝 虛擬機(jī) nexus nacos 集成過程 工程搭建 父工程搭建 子工程 服務(wù)集成 nacos集成 配置文件 服務(wù)注冊與發(fā)現(xiàn)-discovery 服務(wù)注冊 啟動(dòng) 服務(wù)發(fā)現(xiàn) 測試 配置管理-config 新增配置 ?測試

    2024年02月12日
    瀏覽(57)
  • 自定義redission裝配和集成分布式開源限流業(yè)務(wù)組件ratelimiter-spring-boot-starter的正確姿勢

    自定義redission裝配和集成分布式開源限流業(yè)務(wù)組件ratelimiter-spring-boot-starter的正確姿勢 ??由于使用了redisson-spring-boot-starter,在自定義redisson裝配的時(shí)候會(huì)被redisson-spring-boot-starter里面的start默認(rèn)裝配了,同時(shí)在使用開源分布式限流組件ratelimiter-spring-boot-starter的時(shí)候,這個(gè)里面

    2024年02月07日
    瀏覽(27)
  • SpringBoot 定時(shí)任務(wù) @Scheduled 集群環(huán)境優(yōu)化 (使用分布式鎖, 注解形式)

    SpringBoot提供了 Schedule模塊完美支持定時(shí)任務(wù)的執(zhí)行 在實(shí)際開發(fā)中由于項(xiàng)目部署在分布式或集群服務(wù)器上 會(huì)導(dǎo)致定時(shí)任務(wù)多次觸發(fā) 因此,使用redis分布鎖機(jī)制可以有效避免多次執(zhí)行定時(shí)任務(wù) ? 核心方法是org.springframework.data.redis.core包下的 ?setIfAbsent() 方法 返回值為布爾類型

    2024年02月15日
    瀏覽(29)
  • Spring Boot如何實(shí)現(xiàn)分布式消息隊(duì)列

    Spring Boot如何實(shí)現(xiàn)分布式消息隊(duì)列

    在分布式系統(tǒng)中,消息隊(duì)列是非常重要的一部分,可以幫助開發(fā)人員實(shí)現(xiàn)異步處理、解耦系統(tǒng)、提高系統(tǒng)可靠性等。本文將介紹如何使用 Spring Boot 實(shí)現(xiàn)分布式消息隊(duì)列。 消息隊(duì)列是一種存儲(chǔ)消息的容器,可以緩存消息并在需要的時(shí)候按照一定的規(guī)則將消息發(fā)送給消費(fèi)者。常

    2024年02月14日
    瀏覽(22)
  • Spring Boot 中的 Redis 分布式鎖

    Spring Boot 中的 Redis 分布式鎖

    在分布式系統(tǒng)中,多個(gè)進(jìn)程同時(shí)訪問共享資源時(shí),很容易出現(xiàn)并發(fā)問題。為了避免這些問題,我們可以使用分布式鎖來保證共享資源的獨(dú)占性。Redis 是一款非常流行的分布式緩存,它也提供了分布式鎖的功能。在 Spring Boot 中,我們可以很容易地使用 Redis 分布式鎖來管理并發(fā)

    2024年02月11日
    瀏覽(24)
  • Spring Boot 中的 Seata 分布式事務(wù)

    Spring Boot 中的 Seata 分布式事務(wù)

    在分布式系統(tǒng)中,保證數(shù)據(jù)的一致性是一個(gè)非常重要的問題。傳統(tǒng)的 ACID 事務(wù)模型雖然能夠保證單個(gè)數(shù)據(jù)庫的數(shù)據(jù)一致性,但是在分布式系統(tǒng)中卻很難實(shí)現(xiàn)。因此,近年來出現(xiàn)了一些新的事務(wù)模型,其中 Seata 就是一種比較流行的模型。 在本文中,我們將介紹 Spring Boot 中的

    2024年02月09日
    瀏覽(17)
  • 【Spring Boot 3】【Redis】分布式鎖

    【Spring Boot 3】【Redis】分布式鎖

    軟件開發(fā)是一門實(shí)踐性科學(xué),對大多數(shù)人來說,學(xué)習(xí)一種新技術(shù)不是一開始就去深究其原理,而是先從做出一個(gè)可工作的DEMO入手。但在我個(gè)人學(xué)習(xí)和工作經(jīng)歷中,每次學(xué)習(xí)新技術(shù)總是要花費(fèi)或多或少的時(shí)間、檢索不止一篇資料才能得出一個(gè)可工作的DEMO,這占用了我大量的時(shí)

    2024年01月18日
    瀏覽(22)
  • spring boot + minio 分布式文件上傳

    spring boot + minio 分布式文件上傳

    1、分布式文件系統(tǒng) 簡單理解為:一個(gè)計(jì)算機(jī)無法存儲(chǔ)海量的文件,通過網(wǎng)絡(luò)將若干計(jì)算機(jī)組織起來共同去存儲(chǔ)海量的文件,去接收海量用戶的請求,這些組織起來的計(jì)算機(jī)通過網(wǎng)絡(luò)進(jìn)行通信。 好處: 一臺(tái)計(jì)算機(jī)的文件系統(tǒng)處理能力擴(kuò)充到多臺(tái)計(jì)算機(jī)同時(shí)處理。 一臺(tái)計(jì)算機(jī)

    2024年02月08日
    瀏覽(22)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包