【springCloud】[服务调用OpenFeign]------OpenFeign 从入门到精通:原理、实践与配置全解析

OpenFeign 从入门到精通:原理、实践与配置全解析

一、OpenFeign 是什么?

OpenFeign 是 Spring Cloud 生态中的一个声明式 HTTP 客户端工具,它基于 Netflix Feign 进行二次封装,整合了 Spring MVC 的注解(如@RequestMapping@GetMapping等)和 Spring Cloud 的服务发现能力(如 Nacos、Eureka),旨在简化微服务架构中服务间的 HTTP 调用流程。

本质上,OpenFeign 通过接口 + 注解的形式定义 HTTP 请求,开发者无需手动编写 HttpClient(如 OkHttp、RestTemplate)的调用代码,框架会自动根据注解生成动态代理实现类,完成请求的发起、参数封装、结果解析等操作。其核心特性包括:

  1. 声明式 API:通过接口和注解定义请求,代码简洁易维护;

  2. 自动服务发现:结合服务注册中心(如 Nacos),自动从注册中心获取服务地址,无需硬编码 IP / 端口;

  3. 负载均衡集成:默认整合 Spring Cloud LoadBalancer(Spring Boot 3 中替代 Ribbon),实现服务调用的负载均衡;

  4. 熔断降级支持:可与 Sentinel、Resilience4j 等组件集成,应对服务不可用场景;

  5. 日志可配置:支持多级别日志打印,便于调试 HTTP 请求细节。

二、OpenFeign 能干嘛?

在微服务架构中,服务间的远程调用是核心场景之一。OpenFeign 通过封装 HTTP 调用细节,解决了传统手动调用的痛点,具体能力可归纳为以下 4 点:

1. 简化远程调用代码

传统使用RestTemplate调用远程服务时,需手动拼接 URL、设置请求参数、处理响应结果,代码冗余且易出错。例如:

// 传统RestTemplate调用

String url = "http://user-service/user/{id}";

User user = restTemplate.getForObject(url, User.class, 1L);

而使用 OpenFeign 后,只需定义接口并添加注解,无需手动构建请求:

// OpenFeign声明式调用
@FeignClient("user-service") // 服务名
public interface UserFeignClient {
    @GetMapping("/user/{id}") // 对应服务的接口路径
    User getUserById(@PathVariable("id") Long id);
}

// 调用时直接注入接口使用
@Autowired
private UserFeignClient userFeignClient;

User user = userFeignClient.getUserById(1L);

2. 自动集成服务发现与负载均衡

在 Spring Cloud 生态中,OpenFeign 会自动从服务注册中心(如 Nacos)获取目标服务的实例列表,并结合 Spring Cloud LoadBalancer 实现负载均衡(默认轮询策略)。开发者无需关心服务地址的动态变化,只需指定服务名即可完成调用。

3. 支持请求参数与响应的灵活处理

OpenFeign 完全兼容 Spring MVC 的注解,支持@PathVariable(路径参数)、@RequestParam(查询参数)、@RequestBody(请求体)等参数绑定方式,同时支持 JSON、Form 等多种数据格式的响应解析,满足绝大多数微服务调用场景。

4. 可扩展的熔断降级与日志能力

通过集成 Sentinel 或 Resilience4j,OpenFeign 可实现服务调用的熔断降级(即 “兜底” 逻辑),避免因下游服务故障导致的级联失败;同时,OpenFeign 支持多级别日志打印,可清晰查看 HTTP 请求的 URL、参数、响应状态等细节,便于问题排查。

三、Feign 和 OpenFeign 两者区别

Feign 和 OpenFeign 同属 Netflix 开源的 HTTP 客户端工具,但 OpenFeign 是 Spring Cloud 对 Feign 的增强版,两者在起源、功能、依赖等方面存在显著区别,具体对比如下:

对比维度Feign(Netflix Feign)OpenFeign(Spring Cloud OpenFeign)
起源与归属由 Netflix 开发,属于独立组件由 Spring Cloud 基于 Feign 二次开发,属于 Spring Cloud 生态
依赖坐标com.netflix.feign:feign-coreorg.springframework.cloud:spring-cloud-starter-openfeign
注解支持仅支持 Feign 自定义注解(如@RequestLine兼容 Spring MVC 注解(如@GetMapping@PostMapping),更符合 Spring 开发者习惯
服务发现集成需手动集成 Eureka,无自动适配能力自动集成 Spring Cloud 服务发现(Nacos/Eureka),无需额外配置
负载均衡集成需手动集成 Ribbon默认集成 Spring Cloud LoadBalancer(Spring Boot 3),无需额外配置
Spring 生态适配适配性差,需手动整合 Spring 环境完全融入 Spring 生态,支持@Autowired注入、配置文件绑定等 Spring 特性
维护状态Netflix 已停止更新(进入维护模式)Spring Cloud 持续更新,支持 Spring Boot 3 等新版本

核心结论:在当前 Spring Cloud 项目(尤其是 Spring Boot 3+)中,优先选择 OpenFeign,因其更贴合 Spring 生态、配置更简单、功能更丰富,且能兼容最新的 Spring Cloud 组件。

四、Spring Boot 3 整合 OpenFeign(含调用与兜底例子)

本节基于Spring Boot 3.2.xMyBatis-Plus 3.5.xLombok 1.18.xNacos 2.3.x(注册中心 + 配置中心),完整实现 OpenFeign 的整合,包含 “服务提供者” 与 “服务消费者” 两端代码,并集成 Sentinel 实现兜底逻辑。

1. 环境准备

1.1 依赖管理(pom.xml)

首先在父工程中统一管理 Spring Cloud 与 Spring Cloud Alibaba 版本(Spring Boot 3.2.x 对应 Spring Cloud 2023.0.x、Spring Cloud Alibaba 2023.0.0.0):

<dependencyManagement>
    <dependencies>
        <!-- Spring Boot 3 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>3.2.5</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <!-- Spring Cloud -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>2023.0.1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <!-- Spring Cloud Alibaba -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
            <version>2023.0.0.0</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

1.2 服务提供者(user-service)依赖

服务提供者需集成 Nacos 注册中心、MyBatis-Plus、Lombok:

<dependencies>
    <!-- Spring Web -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- Nacos注册中心 -->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
    <!-- Nacos配置中心 -->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
    </dependency>
    <!-- MyBatis-Plus -->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.5.6</version>
    </dependency>
    <!-- MySQL驱动 -->
    <dependency>
        <groupId>com.mysql</groupId>
        <artifactId>mysql-connector-j</artifactId>
        <scope>runtime</scope>
    </dependency>
    <!-- Lombok -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>
 
1.3 服务消费者(order-service)依赖

服务消费者需额外引入 OpenFeign 和 Sentinel(用于兜底):

<dependencies>
    <!-- 基础依赖(同服务提供者) -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
    </dependency>
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.5.6</version>
    </dependency>
    <dependency>
        <groupId>com.mysql</groupId>
        <artifactId>mysql-connector-j</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>

    <!-- OpenFeign -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
    <!-- Sentinel(用于熔断降级兜底) -->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
    </dependency>
    <!-- OpenFeign整合Sentinel -->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-alibaba-sentinel-feign</artifactId>
    </dependency>
</dependencies>
 

2. 配置文件(Nacos 配置中心)

在 Nacos 控制台创建两个配置集,分别对应user-serviceorder-service(配置格式为 YAML)。

2.1 服务提供者(user-service-dev.yaml)
 # 服务端口
server:
  port: 8081

# 服务名(Nacos注册名)
spring:
  application:
    name: user-service
  # Nacos配置
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848 # Nacos注册中心地址
      config:
        server-addr: 127.0.0.1:8848 # Nacos配置中心地址
        file-extension: yaml # 配置文件格式
  # 数据库配置
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/user_db?useSSL=false&serverTimezone=UTC
    username: root
    password: 123456

# MyBatis-Plus配置
mybatis-plus:
  mapper-locations: classpath:mapper/**/*.xml
  type-aliases-package: com.example.user.entity
  configuration:
    map-underscore-to-camel-case: true # 下划线转驼峰

 
2.2 服务消费者(order-service-dev.yaml)
 # 服务端口
server:
  port: 8082

# 服务名
spring:
  application:
    name: order-service
  # Nacos配置
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
      config:
        server-addr: 127.0.0.1:8848
        file-extension: yaml
    # Sentinel配置
    sentinel:
      transport:
        dashboard: 127.0.0.1:8080 # Sentinel控制台地址(需本地启动Sentinel)
  # 数据库配置
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/order_db?useSSL=false&serverTimezone=UTC
    username: root
    password: 123456

# MyBatis-Plus配置
mybatis-plus:
  mapper-locations: classpath:mapper/**/*.xml
  type-aliases-package: com.example.order.entity
  configuration:
    map-underscore-to-camel-case: true

# 开启OpenFeign整合Sentinel
feign:
  sentinel:
    enabled: true

3. 服务提供者代码(user-service)

3.1 实体类(User.java)
package com.example.user.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

@Data
@TableName("t_user") // 对应数据库表名
public class User {
    @TableId(type = IdType.AUTO)
    private Long id;
    private String username;
    private Integer age;
    private String address;
}
3.2 Mapper 接口(UserMapper.java)
package com.example.user.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.user.entity.User;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface UserMapper extends BaseMapper<User> {
    // 继承BaseMapper,无需手动写CRUD方法
}
3.3 服务层(UserService.java)
package com.example.user.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.user.entity.User;
import com.example.user.mapper.UserMapper;
import org.springframework.stereotype.Service;

@Service
public class UserService extends ServiceImpl<UserMapper, User> {
    // 如需自定义方法,可在此扩展
    public User getUserById(Long id) {
        return getById(id); // 继承自ServiceImpl的方法
    }
}
3.4 控制器(UserController.java)
package com.example.user.controller;

import com.example.user.entity.User;
import com.example.user.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
@RequiredArgsConstructor // Lombok生成构造器注入
public class UserController {
    private final UserService userService;

    // 提供给OpenFeign调用的接口
    @GetMapping("/{id}")
    public User getUserById(@PathVariable("id") Long id) {
        return userService.getUserById(id);
    }
}
3.5 启动类(UserApplication.java)
package com.example.user;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient // 开启服务注册与发现(Spring Boot 3可省略,默认开启)
public class UserApplication {
    public static void main(String[] args) {
        SpringApplication.run(UserApplication.class, args);
    }
}

4. 服务消费者代码(order-service)

4.1 实体类(Order.java、User.java)
// Order.java
package com.example.order.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

@Data
@TableName("t_order")
public class Order {
    @TableId(type = IdType.AUTO)
    private Long id;
    private Long userId; // 关联用户ID
    private String orderNo;
    private Double amount;
}

// User.java(与服务提供者一致,可通过API模块共享)
package com.example.order.entity;

import lombok.Data;

@Data
public class User {
    private Long id;
    private String username;
    private Integer age;
    private String address;
}
 
4.2 OpenFeign 客户端接口(UserFeignClient.java)

定义 Feign 客户端,并配置 Sentinel 兜底类:

package com.example.order.feign;

import com.example.order.entity.User;
import com.example.order.feign.fallback.UserFeignFallback;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

// name:目标服务名(Nacos中注册的user-service)
// fallback:兜底类(服务不可用时执行)
@FeignClient(name = "user-service", fallback = UserFeignFallback.class)
public interface UserFeignClient {
    // 与服务提供者的接口路径、参数完全一致
    @GetMapping("/user/{id}")
    User getUserById(@PathVariable("id") Long id);
}
4.3 兜底类(UserFeignFallback.java)

实现 Feign 客户端接口,定义服务熔断 / 降级时的兜底逻辑:

package com.example.order.feign.fallback;

import com.example.order.entity.User;
import com.example.order.feign.UserFeignClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Slf4j
@Component // 必须注入Spring容器
public class UserFeignFallback implements UserFeignClient {
    @Override
    public User getUserById(Long id) {
        // 兜底逻辑:记录日志 + 返回默认数据
        log.error("调用user-service获取用户失败,用户ID:{}", id);
        User defaultUser = new User();
        defaultUser.setId(id);
        defaultUser.setUsername("默认用户(服务暂不可用)");
        defaultUser.setAge(0);
        defaultUser.setAddress("未知地址");
        return defaultUser;
    }
}
4.4 服务层(OrderService.java)

注入 Feign 客户端,调用远程服务:

package com.example.order.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.order.entity.Order;
import com.example.order.entity.User;
import com.example.order.feign.UserFeignClient;
import com.example.order.mapper.OrderMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class OrderService extends ServiceImpl<OrderMapper, Order> {
    private final UserFeignClient userFeignClient;

    // 订单详情查询:关联查询用户信息(调用远程服务)
    public Order getOrderWithUser(Long orderId) {
        // 1. 查询订单基本信息
        Order order = getById(orderId);
        if (order == null) {
            throw new RuntimeException("订单不存在");
        }
        // 2. 调用user-service获取用户信息(OpenFeign)
        User user = userFeignClient.getUserById(order.getUserId());
        // 3. (可选)将用户信息封装到订单扩展类中返回
        // 此处简化,直接返回订单
        return order;
    }
}
4.5 控制器(OrderController.java)
 package com.example.order.controller;

import com.example.order.entity.Order;
import com.example.order.service.OrderService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/order")
@RequiredArgsConstructor
public class OrderController {
    private final OrderService orderService;

    @GetMapping("/{id}")
    public Order getOrderWithUser(@PathVariable("id") Long id) {
        return orderService.getOrderWithUser(id);
    }
}
4.6 启动类(OrderApplication.java)

需添加@EnableFeignClients注解开启 OpenFeign 功能:

package com.example.order;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients // 开启OpenFeign客户端扫描
public class OrderApplication {
    public static void main(String[] args) {
        SpringApplication.run(OrderApplication.class, args);
    }
}

5. 测试验证

  1. 启动依赖服务:启动 Nacos Server(默认端口 8848)、Sentinel Dashboard(默认端口 8080);

  2. 启动服务提供者:运行UserApplication,查看 Nacos 控制台是否有user-service注册;

  3. 启动服务消费者:运行OrderApplication,查看 Nacos 控制台是否有order-service注册;

  4. 正常调用测试:访问http://localhost:8082/order/1(假设订单 ID=1 关联用户 ID=1),返回正常订单与用户信息;

  5. 兜底测试:停止user-service,再次访问上述地址,返回兜底逻辑中的 “默认用户” 信息,证明熔断降级生效。

五、OpenFeign 默认超时时间和配置超时时间

OpenFeign 的超时时间分为连接超时(建立 HTTP 连接的超时时间)和读取超时(获取 HTTP 响应的超时时间),默认配置由 Spring Cloud Feign 的默认参数决定,可通过配置文件自定义调整。

1. 默认超时时间

在 Spring Cloud OpenFeign 中,默认超时时间如下(基于 Spring Cloud 2023.0.x 版本):

  • 连接超时时间(Connect Timeout):10 秒(10000 毫秒);

  • 读取超时时间(Read Timeout):60 秒(60000 毫秒)。

默认超时时间的来源是FeignClientProperties类中的默认值,代码如下:

public class FeignClientProperties {
    private int connectTimeout = 10000; // 连接超时默认10秒
    private int readTimeout = 60000;    // 读取超时默认60秒
    // 其他属性...
}

2. 配置超时时间

OpenFeign 支持全局超时配置(对所有 Feign 客户端生效)和局部超时配置(仅对指定 Feign 客户端生效),配置方式均通过application.yml或 Nacos 配置中心实现。

2.1 全局超时配置

在服务消费者(如 order-service)的配置文件中添加以下配置,对所有 Feign 客户端生效:

feign:
  client:
    config:
      default: # default表示全局配置
        connect-timeout: 5000 # 连接超时5秒(5000毫秒)
        read-timeout: 30000   # 读取超时30秒(30000毫秒)
2.2 局部超时配置

若需对某个 Feign 客户端(如user-service)单独配置超时时间,只需将default替换为目标服务名:

feign:
  client:
    config:
      user-service: # 仅对user-service的Feign客户端生效
        connect-timeout: 3000 # 连接超时3秒
        read-timeout: 10000   # 读取超时10秒
2.3 注意事项
  • 超时时间的单位是毫秒,配置时需注意数值大小(如 1 秒 = 1000 毫秒);

  • 局部配置优先级高于全局配置,若同时配置,以局部配置为准;

  • 若服务调用需要处理大量数据(如文件下载),需适当延长读取超时时间,避免提前断开连接。

六、设置 OpenFeign 日志打印级别

OpenFeign 支持打印 HTTP 请求的详细日志(如 URL、参数、响应状态、响应体等),便于调试和问题排查。日志级别分为 4 种,默认级别为NONE(不打印日志),可通过配置文件或 Java 代码调整。

1. OpenFeign 日志级别说明

OpenFeign 定义了 4 种日志级别,从低到高分别为:

日志级别说明
NONE默认级别,不打印任何日志
BASIC仅打印请求方法、URL、响应状态码(如GET /user/1 HTTP/1.1 200 OK
HEADERS打印 BASIC 级别内容 + 请求头、响应头(如Content-Type: application/json
FULL打印最详细日志,包含 BASIC、HEADERS 级别内容 + 请求体、响应体、请求参数等

2. 配置日志打印级别

2.1 方式 1:通过配置文件配置(推荐)

在服务消费者的配置文件中,可通过logging.level指定 Feign 客户端接口的日志级别(需配合feign.client.config开启日志):

# 1. 开启Feign客户端的日志(指定日志级别)
feign:
  client:
    config:
      user-service: # 目标服务名(default表示全局)
        logger-level: FULL # 日志级别:NONE/BASIC/HEADERS/FULL

# 2. 配置Spring日志,让Feign客户端接口的日志生效(必须配置)
logging:
  level:
    com.example.order.feign.UserFeignClient: DEBUG # Feign客户端接口的全类名

说明

  • feign.client.config.user-service.logger-level:指定user-service对应的 Feign 客户端的日志级别;

  • logging.level.com.example.order.feign.UserFeignClient:将 Feign 客户端接口的日志级别设置为DEBUG(Spring 日志默认级别为INFO,若不设置,DEBUG级别日志不会输出)。

2.2 方式 2:通过 Java 代码配置

通过@Configuration注解创建配置类,手动设置 Feign 日志级别:

 package com.example.order.config;

import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FeignLogConfig {
    // 配置Feign日志级别
    @Bean
    public Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL; // 返回需要的日志级别
    }
}

应用到指定 Feign 客户端

@FeignClient注解中通过configuration属性指定配置类,仅对当前 Feign 客户端生效:

@FeignClient(
    name = "user-service",
    fallback = UserFeignFallback.class,
    configuration = FeignLogConfig.class // 指定日志配置类
)
public interface UserFeignClient {
    // 接口方法...
}

全局生效

若需让配置类对所有 Feign 客户端生效,可在启动类上添加@EnableFeignClients(defaultConfiguration = FeignLogConfig.class)

@SpringBootApplication
@EnableFeignClients(defaultConfiguration = FeignLogConfig.class)
public class OrderApplication {
    public static void main(String[] args) {
        SpringApplication.run(OrderApplication.class, args);
    }
}

3. 验证日志输出

配置完成后,调用 Feign 客户端接口(如访问http://localhost:8082/order/1),查看控制台日志,FULL 级别日志示例如下:

2025-08-23 15:30:00.123 DEBUG 1234 --- [nio-8082-exec-1] c.e.o.feign.UserFeignClient              : [UserFeignClient#getUserById] ---> GET http://user-service/user/1 HTTP/1.1
2025-08-23 15:30:00.124 DEBUG 1234 --- [nio-8082-exec-1] c.e.o.feign.UserFeignClient              : [UserFeignClient#getUserById] ---> END HTTP (0-byte body)
2025-08-23 15:30:00.156 DEBUG 1234 --- [nio-8082-exec-1] c.e.o.feign.UserFeignClient              : [UserFeignClient#getUserById] <--- HTTP/1.1 200 OK (32ms)
2025-08-23 15:30:00.157 DEBUG 1234 --- [nio-8082-exec-1] c.e.o.feign.UserFeignClient              : [UserFeignClient#getUserById] content-type: application/json
2025-08-23 15:30:00.157 DEBUG 1234 --- [nio-8082-exec-1] c.e.o.feign.UserFeignClient              : [UserFeignClient#getUserById] date: Sat, 23 Aug 2025 07:30:00 GMT
2025-08-23 15:30:00.157 DEBUG 1234 --- [nio-8082-exec-1] c.e.o.feign.UserFeignClient              : [UserFeignClient#getUserById] transfer-encoding: chunked
2025-08-23 15:30:00.158 DEBUG 1234 --- [nio-8082-exec-1] c.e.o.feign.UserFeignClient              : [UserFeignClient#getUserById] 
2025-08-23 15:30:00.158 DEBUG 1234 --- [nio-8082-exec-1] c.e.o.feign.UserFeignClient              : [UserFeignClient#getUserById] {"id":1,"username":"张三","age":25,"address":"北京"}
2025-08-23 15:30:00.158 DEBUG 1234 --- [nio-8082-exec-1] c.e.o.feign.UserFeignClient              : [UserFeignClient#getUserById] <--- END HTTP (56-byte body)

4. 生产环境建议

  • 生产环境中建议使用BASICNONE级别,避免FULL级别日志打印大量敏感信息(如请求体中的密码)和占用过多磁盘空间;

  • 调试阶段可临时使用FULL级别,问题排查完成后及时切换回低级别。

如果文章对你有一点点帮助,欢迎【点赞、留言、+ 关注】
您的关注是我持续创作的重要动力!有问题欢迎随时交流!多一个朋友多一条路!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值