使用分布式锁保证幂等性

String lockKey = "company_pickup_config:" + getCustomerNo() + ":" + getCurrentAccount().getCountryCode();
RLock lock = redissonClient.getLock(lockKey);
if (!lock.tryLock(3, 10, TimeUnit.SECONDS)) {
    throw new BusinessException("操作过于频繁,请稍后重试");
}
try {
    //查询客户配置是否存在
        CompanyPickupConfig companyPickupConfig = companyPickupConfigMapper.selectOneByExample(Example.builder(CompanyPickupConfig.class).where(
                WeekendSqls.<CompanyPickupConfig>custom().andEqualTo(CompanyPickupConfig::getCustomerCode, getCustomerNo())
                        .andEqualTo(CompanyPickupConfig::getCountryCode, getCurrentAccount().getCountryCode())
        ).build());
        if(Objects.nonNull(companyPickupConfig)){
            companyPickupConfig.setStatus(status);
            companyPickupConfig.setUpdateBy(getCurrentName());
            companyPickupConfig.setUpdateTime(DateUtils.getCurrentDate());
            companyPickupConfigMapper.updateByPrimaryKeySelective(companyPickupConfig);
        }else{
            add(status);
        }
} finally {
    lock.unlock();
}

  

posted @ 2026-10-16 09:44  客至在水一方  阅读(3)  评论(0)    收藏  举报