异步审核2

在文章审核成功以后需要在article库中新增文章数据

ArticleFeign 保存app文章,返回app文章id

 @PostMapping("/api/v1/article/save")
    Long saveArticle(@RequestBody ArticleDto dto);

分布式id生成-雪花算法

在实体类ApArticle中的id上加入如下配置,指定类型为ASSIGN_ID

 @TableId(value = "id",type = IdType.ASSIGN_ID)
private Long id;

第二:在application.yml文件中配置数据中心id和机器id

mybatis-plus:
  #mapper-locations: classpath*:mapper/*.xml
  # 设置别名包扫描路径,通过该属性可以给包中的类注册别名
  #type-aliases-package: com.heima.model.article.pojos
  global-config:
      datacenter-id: 1  #数据中心id
    workerId: 1       #机器id

 @RestController
public class ArticleController {
    @Autowired
    private ApArticleService apArticleService;
    /**
     * 保存文章
     */
    @PostMapping("/api/v1/article/save")
    public Long saveArticle(@RequestBody ArticleDto dto){
        return apArticleService.saveArticle(dto);
    }
}

public interface ApArticleService extends IService<ApArticle> { *

保存文章 
    Long saveArticle(ArticleDto dto);
}

 

package com.heima.article.service.impl;  
 * 文章信息表,存储已发布的文章 服务实现类 
@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {

    @Autowired
    private ApArticleContentService apArticleContentService;
    @Autowired
    private ApAuthorService apAuthorService;
   /**
     * 保存文章
     * 保存ap_article
     * 保存ap_article_content
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long saveArticle(ArticleDto dto) {

//        根据自媒体文章id 查询文章信息
        QueryWrapper<ApArticle> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ApArticle::getWmNewsId,dto.getWmNewsId());
        ApArticle apArticle =this.getOne(queryWrapper);
        if(apArticle == null){
            apArticle = new ApArticle();
            apArticle.setWmNewsId(dto.getWmNewsId());
//            根据自媒体文章作者id查询authorid
            ApAuthor apAuthor = apAuthorService.findByWmUserId(dto.getWmUserId());
            apArticle.setAuthorId(apAuthor.getId());
            apArticle.setAuthorName(apAuthor.getName());
            apArticle.setCreatedTime(new Date());
            apArticle.setIsDelete(false);
            apArticle.setIsDown(false);
            apArticle.setIsComment(true);
            apArticle.setIsForward(true);
            apArticle.setFlag(0);
        }

        apArticle.setPublishTime(dto.getPublishTime()==null?new Date():dto.getPublishTime());
        apArticle.setLabels(dto.getLabels());
        apArticle.setImages(dto.getImages());
        apArticle.setChannelId(dto.getChannelId());
        apArticle.setChannelName(dto.getChannelName());
        apArticle.setTitle(dto.getTitle());
        apArticle.setLayout(dto.getLayout());
//        保存/修改文章表
        boolean b = saveOrUpdate(apArticle);
        if(!b){
            throw new LeadException(AppHttpCodeEnum.SERVER_ERROR);
        }
        Long articleId = apArticle.getId();
        ApArticleContent apArticleContent = new ApArticleContent();
        apArticleContent.setArticleId(articleId);
        apArticleContent.setContent(dto.getContent());
//        保存/修改 文章内容表
        boolean b1 = apArticleContentService.saveOrUpdate(apArticleContent);
        if(!b1){
            throw new LeadException(AppHttpCodeEnum.SERVER_ERROR);
        }
        return articleId;
    }
}

 

### 延迟异步双重删除机制的实现方式 在分布式系统中,延迟异步双重删除(Delayed Asynchronous Double Deletion, DADD)是一种用于处理资源安全释放的技术。该技术旨在确保即使在网络分区或其他异常情况下也能可靠地完成资源清理工作。 #### 1. 设计原则 为了有效实施DADD策略,通常遵循以下设计原则: - **幂等操作**:每次执行删除请求都应具有相同的效果,无论重复多少次都不会影响最终状态[^1]。 - **确认机制**:引入额外的状态机来跟踪每个阶段的结果,并确保只有当所有前置条件满足时才继续下一步骤[^2]。 #### 2. 技术架构概述 一种常见的做法是在服务端设置两个独立的任务队列分别负责初次标记和实际移除两项任务。具体过程如下所示: ##### 初次标记阶段 客户端发起删除命令后并不会立即销毁目标对象;而是先向数据库写入一条记录表示此条目已被逻辑上标记为待删状态。此时其他读取者仍然可以看到这些数据项的存在直到正式清除为止。 ```sql UPDATE items SET deleted_at = NOW() WHERE id = :id; ``` ##### 正式移除阶段 经过一段预设的时间窗口期之后(即所谓的“延迟时间”),后台线程会定期扫描那些被标记超过一定时限但仍处于未物理删除状态的对象并真正将其从存储介质里彻底抹去。 ```python import time from datetime import timedelta def cleanup_expired_items(): threshold_time = (datetime.now() - timedelta(days=7)).timestamp() expired_ids = db.query( "SELECT id FROM items WHERE deleted_at IS NOT NULL AND deleted_at < ?", (threshold_time,) ) for item_id in expired_ids: delete_item_permanently(item_id) while True: try: cleanup_expired_items() time.sleep(86400) # Sleep one day between checks except Exception as e: logger.error(f"Cleanup failed with error {e}") ``` 上述Python脚本展示了如何周期性地检查过期项目并永久删除它们的过程。 #### 3. 安全保障措施 为了避免误删重要资料,在整个过程中还需要采取多种防护手段: - 设置合理的延迟间隔以允许足够长的时间窗供相关人员审核; - 记录详细的日志以便事后追溯任何可疑活动; - 对敏感操作实行多因素认证验证身份合法性。 通过以上介绍可以看出,采用延迟异步双重删除不仅能够提高系统的鲁棒性和安全性,同时也简化了开发人员对于复杂并发场景下的管理难度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值