图片预览

滚轮缩放 · 拖动移动 · Esc 关闭

Skip to content

迁移常见问题(FAQ) ​

一、启动和注册问题 ​

Q1: 服务启动后无法注册到 Nacos ​

症状:

  • 服务启动成功
  • Nacos 控制台看不到服务

可能原因:

  1. Nacos 服务未启动
  2. Nacos 地址配置错误
  3. 网络连接问题
  4. 命名空间配置错误

解决方案:

bash
# 1. 检查 Nacos 是否启动
curl http://localhost:8848/nacos

# 2. 检查 application.yml 配置
spring:
  cloud:
    nacos:
      discovery:
        server-addr: ${NACOS_HOST:wds-register}:${NACOS_PORT:8848}
        namespace: ${NACOS_NAMESPACE:}  # 确保命名空间正确

# 3. 查看服务日志
tail -f logs/pigx-standard-biz/info.log | grep nacos

# 4. 手动测试 Nacos 连接
curl http://localhost:8848/nacos/v1/ns/instance/list?serviceName=pigx-standard-biz

Q2: 服务名称不一致导致找不到服务 ​

症状:

  • Feign 调用失败:No instances available

原因:

  • application.yml 中的 spring.application.name 与 Feign 接口中的 value 不一致

解决方案:

yaml
# application.yml
spring:
  application:
    name: pigx-standard-biz  # 必须与 Feign 的 value 一致
java
// Feign 接口
@FeignClient(value = "pigx-standard-biz")  // 必须与 application.name 一致
public interface RemoteStandardUserService {
}

二、依赖和编译问题 ​

Q3: 编译失败,找不到 pigx-common-* 依赖 ​

症状:

Could not find artifact com.pig4cloud:pigx-common-core:jar:5.8.0

解决方案:

bash
# 1. 先编译 pigx-common 模块
cd pigx-common
mvn clean install

# 2. 再编译 pigx-standard
cd ../pigx-standard
mvn clean compile

Q4: 实体类找不到,编译报错 ​

症状:

Cannot resolve symbol 'StandardUser'

原因:

  • 实体类应该放在 pigx-standard-api 模块
  • pigx-standard-biz 依赖 pigx-standard-api

解决方案:

xml
<!-- pigx-standard-biz/pom.xml -->
<dependency>
    <groupId>com.pig4cloud</groupId>
    <artifactId>pigx-standard-api</artifactId>
    <version>5.8.0</version>
</dependency>

Q5: Lombok 不生效 ​

症状:

  • 找不到 getter/setter 方法
  • @Data 注解不起作用

解决方案:

bash
# 1. 确认 IDE 安装了 Lombok 插件
# IDEA: Settings → Plugins → 搜索 Lombok

# 2. 启用注解处理
# IDEA: Settings → Build, Execution, Deployment → Compiler → Annotation Processors
# ✅ Enable annotation processing

# 3. 重新编译
mvn clean compile

三、数据库和实体类问题 ​

Q6: 租户ID为null,数据隔离不生效 ​

症状:

  • 查询到了其他租户的数据
  • 插入数据时 tenant_id 为 null

原因:

  1. 实体类未添加 @TenantTable 注解
  2. 用户未登录或上下文中无租户信息
  3. MyBatis Plus 租户插件未配置

解决方案:

java
// 1. 实体类添加注解
@TenantTable
@TableName("standard_user")
public class StandardUser extends BaseEntity {
}

// 2. 确保用户已登录
SysUser user = SecurityUtils.getUser();
Long tenantId = user.getTenantId();  // 不应为 null

// 3. 检查 MyBatis Plus 配置(通常已在 pigx-common-data 中配置)

Q7: 逻辑删除不生效 ​

症状:

  • 删除数据后,数据库记录仍然存在
  • 查询能查到已删除的数据

原因:

  • BaseEntity 中的 delFlag 未标记 @TableLogic
  • 删除操作使用了 SQL 直接删除

解决方案:

java
// BaseEntity 中确保有此配置
@TableLogic
@TableField(fill = FieldFill.INSERT)
private String delFlag;  // "0"-正常, "1"-删除

// 使用 MyBatis Plus 的删除方法(会自动逻辑删除)
userService.removeById(id);  // ✅ 逻辑删除

// 不要使用原生 SQL 删除
// DELETE FROM user WHERE id = ?  // ❌ 物理删除

Q8: 字段自动填充不生效 ​

症状:

  • createTime、updateTime 等字段为 null

原因:

  • MetaObjectHandler 未配置或未生效

解决方案:

java
// PigX 已在 pigx-common-data 中配置了 MetaObjectHandler
// 确保以下注解正确
@TableField(fill = FieldFill.INSERT)
private LocalDateTime createTime;

@TableField(fill = FieldFill.INSERT_UPDATE)
private LocalDateTime updateTime;

四、权限和安全问题 ​

Q9: 访问接口返回 401 Unauthorized ​

症状:

json
{
  "code": 401,
  "msg": "Unauthorized"
}

原因:

  1. 未登录或 JWT 令牌无效
  2. 令牌已过期
  3. 请求头未携带 Authorization

解决方案:

bash
# 1. 重新登录获取令牌
curl -X POST http://localhost:9999/auth/oauth/token \
  -d "username=admin&password=123456&grant_type=password"

# 2. 使用令牌访问
curl -H "Authorization: Bearer YOUR_TOKEN" \
     http://localhost:9999/standard/user/list

# 3. 检查令牌是否过期
# 令牌通常有效期为 2 小时

Q10: 访问接口返回 403 Forbidden ​

症状:

json
{
  "code": 403,
  "msg": "Access Denied"
}

原因:

  • 当前用户没有相应的权限

解决方案:

java
// 1. 检查 Controller 上的权限注解
@PreAuthorize("@pms.hasPermission('standard_user_view')")
public R<StandardUser> getById(@PathVariable Long id) {
}

// 2. 在 pigx-upms 中为用户/角色分配权限
// 权限标识: standard_user_view

// 3. 临时测试:移除权限注解
// @PreAuthorize("@pms.hasPermission('standard_user_view')")  // 注释掉测试

Q11: 内部接口被外部访问 ​

症状:

  • 带 @Inner 注解的接口可以被外部直接访问

原因:

  • Gateway 未配置拦截
  • @Inner 注解处理器未生效

解决方案:

java
// 确保内部接口添加了 @Inner 注解
@Inner
@GetMapping("/user/inner/{id}")
public R<StandardUser> getById(@PathVariable Long id) {
}

// Gateway 会自动拦截未带 from=inner 标识的请求

五、网关和路由问题 ​

Q12: 通过 Gateway 访问返回 404 ​

症状:

bash
curl http://localhost:9999/standard/user/list
# 404 Not Found

原因:

  1. Gateway 路由未配置
  2. 路由配置错误
  3. 服务未注册

解决方案:

bash
# 1. 检查 Nacos 配置中的 Gateway 路由
# Data ID: pigx-gateway-dev.yml

# 2. 确认路由配置
spring:
  cloud:
    gateway:
      routes:
        - id: pigx-standard
          uri: lb://pigx-standard-biz
          predicates:
            - Path=/standard/**
          filters:
            - StripPrefix=1

# 3. 测试路径
# Gateway: /standard/user/list
# 去除前缀: /user/list (StripPrefix=1)
# 转发到: pigx-standard-biz:6064/user/list

Q13: Gateway 路由配置修改后不生效 ​

原因:

  • Nacos 配置未刷新
  • Gateway 未订阅配置变更

解决方案:

bash
# 1. 在 Nacos 控制台重新发布配置

# 2. 查看 Gateway 日志确认配置刷新
tail -f logs/pigx-gateway/info.log | grep "Refresh"

# 3. 如果仍不生效,重启 Gateway

六、Feign 调用问题 ​

Q14: Feign 调用失败 - No instances available ​

症状:

feign.RetryableException: No instances available for pigx-standard-biz

原因:

  • 被调用服务未启动
  • 服务名称不匹配
  • Nacos 未注册成功

解决方案:

bash
# 1. 确认服务已启动并注册
curl http://localhost:8848/nacos/v1/ns/instance/list?serviceName=pigx-standard-biz

# 2. 检查 Feign 接口定义
@FeignClient(value = "pigx-standard-biz")  # 必须与 Nacos 中的服务名一致

# 3. 检查启动类是否启用 Feign
@EnablePigxFeignClients

Q15: Feign 调用超时 ​

症状:

Read timed out executing GET http://pigx-standard-biz/user/inner/1

原因:

  • 被调用服务响应慢
  • 超时配置过短

解决方案:

yaml
# application.yml 或 Nacos 配置
feign:
  client:
    config:
      default:
        connectTimeout: 10000
        readTimeout: 30000
      pigx-standard-biz:
        connectTimeout: 5000
        readTimeout: 15000

Q16: Feign 降级不生效 ​

症状:

  • 服务故障时,直接抛异常而不是走降级逻辑

原因:

  • 未配置降级工厂
  • Sentinel/Hystrix 未启用

解决方案:

java
// 1. 确保定义了降级工厂
@FeignClient(
    value = "pigx-standard-biz",
    fallbackFactory = RemoteStandardUserServiceFallbackFactory.class  // ✅
)

// 2. 确保配置了熔断器
// application.yml
feign:
  circuitbreaker:
    enabled: true

七、Swagger 文档问题 ​

Q17: Swagger 文档无法访问 ​

症状:

原因:

  • Swagger 依赖未添加
  • 启动类未启用 Swagger

解决方案:

java
// 1. 启动类添加注解
@EnableOpenApi("standard")  // ✅
@SpringBootApplication
public class PigxStandardApplication {
}

// 2. 确认依赖已添加(pigx-standard-biz/pom.xml)
<dependency>
    <groupId>com.pig4cloud</groupId>
    <artifactId>pigx-common-swagger</artifactId>
</dependency>

// 3. 访问文档
http://localhost:6064/doc.html

Q18: Swagger 文档中看不到接口 ​

原因:

  • Controller 未添加 Swagger 注解
  • 包扫描路径不正确

解决方案:

java
// Controller 添加注解
@Tag(name = "用户管理", description = "用户管理接口")
@RestController
@RequestMapping("/user")
public class StandardUserController {

    @Operation(summary = "查询用户详情")
    @GetMapping("/{id}")
    public R<StandardUser> getById(@PathVariable Long id) {
    }
}

八、配置和环境问题 ​

Q19: 端口冲突 ​

症状:

Web server failed to start. Port 6064 was already in use.

解决方案:

bash
# 1. 查看端口占用
netstat -ano | findstr 6064  # Windows
lsof -i:6064                 # Linux/Mac

# 2. 杀掉占用进程或修改端口
# application.yml
server:
  port: 6065  # 修改为其他端口

Q20: 配置文件不生效 ​

症状:

  • 修改 application.yml 后不生效

原因:

  • Nacos 配置优先级更高
  • Profile 配置错误

解决方案:

yaml
# 1. 检查 bootstrap.yml
spring:
  profiles:
    active: dev  # 确认环境

# 2. 检查 Nacos 配置
# Data ID: pigx-standard-biz-dev.yml
# 优先级: Nacos 配置 > application-dev.yml > application.yml

# 3. 如果需要覆盖 Nacos 配置,在本地配置中使用更高优先级
spring:
  cloud:
    config:
      override-none: true  # 本地配置优先

九、其他常见问题 ​

Q21: 日志文件找不到 ​

位置:

logs/pigx-standard-biz/info.log
logs/pigx-standard-biz/error.log

Q22: 如何调试 Feign 请求 ​

yaml
# application.yml
logging:
  level:
    com.pig4cloud.pigx.standard.feign: DEBUG

# 或在 Nacos 配置中设置

Q23: 如何查看当前登录用户 ​

java
import com.pig4cloud.pigx.common.core.util.SecurityUtils;

SysUser user = SecurityUtils.getUser();
System.out.println("当前用户: " + user.getUsername());
System.out.println("租户ID: " + user.getTenantId());

十、快速诊断工具 ​

健康检查脚本 ​

bash
#!/bin/bash
# check-service.sh

SERVICE_NAME="pigx-standard-biz"
PORT=6064

echo "=== 服务健康检查 ==="

# 1. 检查服务是否启动
echo "1. 检查服务进程..."
ps aux | grep $SERVICE_NAME

# 2. 检查端口监听
echo "2. 检查端口监听..."
netstat -an | grep $PORT

# 3. 检查 Nacos 注册
echo "3. 检查 Nacos 注册..."
curl -s "http://localhost:8848/nacos/v1/ns/instance/list?serviceName=$SERVICE_NAME" | grep healthy

# 4. 检查健康端点
echo "4. 检查健康端点..."
curl -s "http://localhost:$PORT/actuator/health"

# 5. 检查 Gateway 路由
echo "5. 检查 Gateway 路由..."
curl -s "http://localhost:9999/actuator/gateway/routes" | grep $SERVICE_NAME

文档版本: v1.0 创建时间: 2025-12-16 最后更新: 2025-12-16

提示: 遇到问题时,请先查看日志文件,大部分问题都能从日志中找到线索。