Commit eafd0b83 by 谢海旭

新增查询北森课程信息接口

1 parent cd735ea9
package cn.com.glsx.dj.riskmanage.remote.beisen;
import cn.com.glsx.dj.riskmanage.model.course.BeiSenCourse;
import cn.com.glsx.framework.core.annotation.ApiMethod;
import cn.com.glsx.framework.core.annotation.ApiParam;
import cn.com.glsx.framework.core.annotation.ApiResponse;
import cn.com.glsx.framework.core.annotation.ApiService;
import cn.com.glsx.framework.core.beans.rpc.RpcResponse;
import javax.validation.constraints.NotNull;
import java.util.List;
/**
* 北森课程信息
*/
@ApiService(value = "北森课程信息", owner = "xiehx", protocol = ApiService.ProtocolEnum.dubbo, version = "1.0.0")
public interface BeiSenCourseRemote {
@ApiMethod("通过北森课程id列表查询课程信息列表")
@ApiParam(name = "ids", notes = "北森课程id列表", dataType = ApiParam.DataTypeEnum.LIST, required = true)
@ApiResponse("课程信息列表")
RpcResponse<List<BeiSenCourse>> listBeiSenCourseByIds(@NotNull List<String> ids);
}
package cn.com.glsx.dj.riskmanage.remote.beisen;
import cn.com.glsx.dj.riskmanage.model.course.BeiSenCourse;
import cn.com.glsx.dj.riskmanage.service.beisen.BeiSenCourseService;
import cn.com.glsx.framework.core.beans.rpc.RpcResponse;
import cn.com.glsx.framework.core.exception.RpcServiceException;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
/**
* @Title:
* @Description:
* @Author xiehx
* @Date 2026/6/3
**/
@Slf4j
@DubboService(retries = 0, timeout = 30000)
public class BeiSenCourseRemoteImpl implements BeiSenCourseRemote {
@Autowired
private BeiSenCourseService beiSenCourseService;
@Override
public RpcResponse<List<BeiSenCourse>> listBeiSenCourseByIds(List<String> ids) {
try {
return RpcResponse.success(beiSenCourseService.listByIds(ids));
} catch (RpcServiceException e) {
log.error(e.getMessage(), e);
return RpcResponse.error(e);
}
}
}
package cn.com.glsx.dj.riskmanage.service.beisen;
import cn.com.glsx.dj.riskmanage.mapper.beisen.BeiSenCourseMapper;
import cn.com.glsx.dj.riskmanage.model.course.BeiSenCourse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* 北森课程信息
*/
@Slf4j
@Service
public class BeiSenCourseService {
@Autowired
private BeiSenCourseMapper beiSenCourseMapper;
/**
* 通过北森课程id列表查询课程信息列表
*/
public List<BeiSenCourse> listByIds(List<String> ids) {
return beiSenCourseMapper.selectByIds(ids);
}
}
  • @wangxw1
    commented
    Master

    一、关键问题与优化建议,如果必要,给出代码示例(仅限 Top 3)

    1. 参数校验不足导致潜在的 SQL 异常与性能风险(功能正确性与健壮性)

    • 问题描述:在 BeiSenCourseRemote 接口中,入参 ids 仅使用了 @NotNull 注解。这允许调用方传入空集合(Empty List)或超大集合(如包含数万个 ID 的 List)。此外,在 BeiSenCourseService 中直接将 ids 传递给 Mapper,未做防空判断。
    • 影响分析:
      • 空集合传递:若 ids 为空,MyBatis 的 <foreach> 标签通常会生成类似 WHERE id IN () 的非法 SQL,导致数据库报语法错误。
      • 超大集合传递:若 ids 数量过大(如超过 1000 个),会导致 SQL 语句过长,不仅查询性能急剧下降,还可能触发数据库 max_allowed_packet 限制导致报错。
    • 优化建议:在接口层使用 @NotEmpty 和 @Size 限制数量,并在 Service 层增加集合判空逻辑。

    代码示例:

    // 1. 接口层:限制不能为空且单次最多查询 500 条
    public interface BeiSenCourseRemote {
        RpcResponse<List<BeiSenCourse>> listBeiSenCourseByIds(
                @NotEmpty(message = "课程ID列表不能为空") 
                @Size(max = 500, message = "单次查询数量不能超过500") List<String> ids);
    }
    
    // 2. Service层:增加防御性编程
    public List<BeiSenCourse> listByIds(List<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return beiSenCourseMapper.selectByIds(ids);
    }
    

    2. RPC 接口实现类的异常捕获不完整(健壮性与最佳实践)

    • 问题描述:在 BeiSenCourseRemoteImpl 中,仅捕获了 RpcServiceException。
    • 影响分析:如果底层发生其他运行时异常(如 NullPointerException、数据库连接超时、SQL 语法错误等 RuntimeException),异常将直接抛给 Dubbo 框架。这会导致无法返回统一标准的 RpcResponse.error() 结构,可能会引发消费端的序列化异常,或者将服务端内部的堆栈信息直接暴露给调用方。
    • 优化建议:增加对 Exception 的兜底捕获,确保 RPC 接口始终返回标准的响应体。

    代码示例:

    @Override
    public RpcResponse<List<BeiSenCourse>> listBeiSenCourseByIds(List<String> ids) {
        try {
            return RpcResponse.success(beiSenCourseService.listByIds(ids));
        } catch (RpcServiceException e) {
            log.error("查询北森课程业务异常: {}", e.getMessage(), e);
            return RpcResponse.error(e);
        } catch (Exception e) {
            log.error("查询北森课程系统异常: {}", e.getMessage(), e);
            // 假设 RpcResponse 有通用的系统错误构造方式
            return RpcResponse.error("SYSTEM_ERROR", "系统内部异常"); 
        }
    }
    

    3. Dubbo 服务超时时间设置过长(性能与资源利用)

    • 问题描述:BeiSenCourseRemoteImpl 上的 @DubboService 注解设置了 timeout = 30000(30秒)。
    • 影响分析:作为一个通过 ID 列表查询数据的接口,正常响应时间应在毫秒级别。30秒的超时时间极度不合理。如果数据库发生慢查询或锁等待,Dubbo 提供者的线程将被长时间挂起,极易导致线程池耗尽,进而引发服务雪崩(Cascading Failure)。
    • 优化建议:将超时时间缩短至合理的范围,例如 3000 毫秒(3秒)。

    代码示例:

    // 修改 timeout 为 3000
    @DubboService(retries = 0, timeout = 3000)
    public class BeiSenCourseRemoteImpl implements BeiSenCourseRemote { ... }
    

    二、评分明细

    • 功能实现的正确性与健壮性(32/40分):基础业务逻辑连通正常,未违反禁止修改已有请求参数等硬性规则(本次为新增接口)。但未处理空集合导致 SQL 报错的边界情况,扣除 8 分。
    • 安全性与潜在风险(30/30分):未发现明显的 SQL 注入风险(假定 Mapper 层使用 # 占位符)、XSS 或越权漏洞。
    • 最佳实践与可维护性(15/20分):代码结构清晰,使用了标准的框架注解。但 RPC 接口缺少全局异常兜底逻辑,且 Service 层缺少防御性编程,扣除 5 分。
    • 性能与资源利用(3/5分):Dubbo 超时时间(30s)设置极其不合理,存在耗尽线程池的风险;未限制 IN 查询的集合大小,存在潜在性能隐患。扣除 2 分。
    • 提交信息质量(4/5分):Commit 信息("新增查询北森课程信息接口")清晰表达了变更意图,但缺少关联的需求/缺陷卡片 ID 或更详细的上下文,扣除 1 分。

    总分:84分

Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!