跳转至

Spring Cloud OpenFeign#

约 329 个字 17 行代码 2 张图片 预计阅读时间 7 分钟

入门案例#

服务消费者引入依赖:

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.3.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.luguosong</groupId>
    <artifactId>open-feign-consumer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>open-feign-consumer</name>
    <description>open-feign-consumer</description>
    <url/>
    <licenses>
        <license/>
    </licenses>
    <developers>
        <developer/>
    </developers>
    <scm>
        <connection/>
        <developerConnection/>
        <tag/>
        <url/>
    </scm>
    <properties>
        <java.version>17</java.version>
    </properties>
    <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>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
        </dependency>
        <!--OpenFeign-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>2023.0.3</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2023.0.1.2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

服务消费者启动类配置@EnableFeignClients:

OpenFeignConsumerApplication.java
package com.luguosong.openfeignconsumer;

import com.luguosong.openfeignconsumer.config.DefaultFeignConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

@EnableFeignClients(defaultConfiguration = DefaultFeignConfig.class)
@SpringBootApplication
public class OpenFeignConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(OpenFeignConsumerApplication.class, args);
    }

}

定义接口:

ProducerClient.java
package com.luguosong.openfeignconsumer.client;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * @author luguosong
 */
@FeignClient(value = "open-feign-producer")
public interface ProducerClient {

    @GetMapping("/demo")
    String demo();
}

调用:

TestController.java
package com.luguosong.openfeignconsumer.controller;

import com.luguosong.openfeignconsumer.client.ProducerClient;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author luguosong
 */
@RestController
@RequiredArgsConstructor
public class TestController {

    private final ProducerClient client;

    @GetMapping("/test")
    public String test() {
        return client.demo();
    }
}

连接池#

OpenFeign底层默认使用HttpURlConnection进行网络请求,不支持连接池。

可以自定义配置Apache HttpClientOKHttp进行网络请求,这两种都支持连接池。

OKHttp为例,先引入依赖:

XML
<dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-okhttp</artifactId>
</dependency>

在配置文件中进行配置:

YAML
spring:
  cloud:
    openfeign:
      okhttp:
        enabled: true

最佳实践#

方案一#

将服务生产者拆分为service(业务代码)、dto(存放数据传输对象)、api(存放Feign接口)模块。

OpenFeign拆分方式一

方案二#

将Feign接口全部存放在同一个接口中。

OpenFeign拆分方式二

Note

当Open Feign 接口所在包名与服务消费者模块包名不一致时,需要在启动类中手动指定包名:

@EnableFeignClients(basePackages = "com.luguosong.api")

日志记录#

  1. 配置FeignClient所在包的日志级别为DEBUG
  2. 设置OpenFeign的日志级别。

OpenFeign有如下日志级别:

日志级别 日志内容
NONE 默认值,不记录日志
BASIC 仅记录请求的方法、URL以及响应状态码和执行时间
HEADERS 在BASIC的基础上,额外记录了请求和响应头消息
FULL 记录请求和响应的头信息、请求体、元数据
DefaultFeignConfig.java
package com.luguosong.openfeignconsumer.config;

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

/**
 * @author luguosong
 */
public class DefaultFeignConfig {

    @Bean
    public Logger.Level level() {
        return Logger.Level.FULL;
    }
}

局部生效:

Java
@FeignClient(value = "open-feign-producer", configuration = DefaultFeignConfig.class)

全局生效:

Java
@EnableFeignClients(defaultConfiguration = DefaultFeignConfig.class)

评论