Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· OpenAIAi2 WildChat-4.8M (AI2)

使用OpenFeign进行请求时发生了404错误,是否有办法能够知道请求时的完整路径?

  1. User

    使用OpenFeign进行请求时发生了404错误,是否有办法能够知道请求时的完整路径?

  2. Assistant

    可以在Feign配置类中开启日志记录,这样可以打印出完整的请求路径和响应结果。具体配置方法如下:

    1. 首先在pom.xml文件中添加Feign和Slf4j依赖:
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>1.7.30</version>
    </dependency>
    
    1. 然后在Feign配置类中添加如下代码:
    @Configuration
    public class FeignConfiguration {
    
        /**
         * 开启Feign日志记录
         * @return
         */
        @Bean
        Logger.Level feignLoggerLevel() {
            return Logger.Level.FULL;
        }
    }
    
    1. 在应用程序的配置文件中添加如下配置:
    logging.level.com.example.feignclient=debug
    

    其中com.example.feignclient指的是Feign客户端的包名,将其设置为debug级别可以打印出完整的请求路径和响应结果。

    1. 启动应用程序后,就可以在控制台中看到Feign发送的请求和接收到的响应,类似于下面的日志:
    feign.Logger$Slf4jLogger - [Ribbon-PingTimer] Load balancer stats: Zone stats: {defaultzone=[Zone:defaultzone;    Instance count:2;    Active connections count: 0;  Circuit breaker tripped count: 0;   Active connections per server: 0.0;]
    } Server stats: [["localhost:8080"...
    
    ...
    
    feign.Logger$Slf4jLogger - [feign-myclient-serviceId] ---> GET http://localhost:8080/example/path HTTP/1.1
    feign.Logger$Slf4jLogger - [feign-myclient-serviceId] Accept: application/json
    feign.Logger$Slf4jLogger - [feign-myclient-serviceId] Content-Length: 0
    ...
    feign.Logger$Slf4jLogger - [feign-myclient-serviceId] <--- HTTP/1.1 404 Not Found (114ms)
    feign.Logger$Slf4jLogger - [feign-myclient-serviceId] X-Content-Type-Options: nosniff
    feign.Logger$Slf4jLogger - [feign-myclient-serviceId] Cache-Control: no-cache, no-store, max-age=0, must-revalidate
    feign.Logger$Slf4jLogger - [feign-myclient-serviceId] Pragma: no-cache
    feign.Logger$Slf4jLogger - [feign-myclient-serviceId] Expires: 0
    ...
    
  3. User

    我添加了这些配置,但还是看不到日志,有没有其他办法

  4. Assistant

    如果您按照上面的方法添加了配置但还是无法看到日志,可以考虑添加以下配置:

    1. 根据您的具体情况,可能需要在application.properties或application.yml文件中添加日志级别的配置,比如:
    logging.level.root=debug
    logging.level.org.springframework.cloud=debug
    

    或

    logging:
      level:
        root: debug
        org:
          springframework:
            cloud: debug
    

    这样可以让Spring Cloud和Feign的日志级别都设为debug,更详细的日志信息可能会被打印出来。

    1. 如果您使用的是log4j2或其他日志框架,可以参考对应框架的配置文档,添加对应的配置,确保Feign日志被打印出来。

    2. 还有一种情况就是请求没有被发出去,这种情况下即使开启了Feign的日志记录也看不到任何日志。可以通过在请求方法中添加@RequestMapping("/test")等注解,确保请求路径被正确拼接,从而避免请求没有被发出去的情况。