OpenFeign是一款基于Netflix Feign开发的声明式HTTP客户端工具。它可以帮助我们方便的与远程HTTP服务进行通信,让我们在使用Restful服务时更加简单和快速。它提供了可靠的、可扩展的、易于维护的HTTP请求和响应的处理方式,是一个优秀的REST API客户端。一些主要特性如下:
声明式API:以接口方式定义RESTful请求,不再需要手动构建HTTP请求对象。
Spring Cloud集成:通过Spring Cloud可快速地与服务注册中心和配置中心进行集成。
充分利用HTTP:与HTTP客户端(如:OkHttp、HttpClient)相比,OpenFeign提供了更丰富的HTTP功能。
快速开始:使用OpenFeign开发REST API客户端,可以更快速地实现测试、开发、部署。
支持多种序列化方式:可以通过配置实现多种JSON序列化方式。
环境准备
插件 | 版本 |
jdk | 21 |
springboot | 3.0.11 |
springcloud | 2022.0.4 |
springcloudalibaba | 2022.0.0.0 |
nacos | 2.2.3(稳定版) |
代码编写
pom
<?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.0.11</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>platform</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>platform</name>
<description>platform</description>
<properties>
<java.version>21</java.version>
<spring-cloud.version>2022.0.0</spring-cloud.version>
<spring-cloud-alibaba.version>2022.0.0.0</spring-cloud-alibaba.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.2</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.cloud</groupId>-->
<!-- <artifactId>spring-cloud-starter-openfeign</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3.2</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter-test</artifactId>
<version>3.0.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>edge-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.41</version>
</dependency>
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>4.4.0</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<repositories>
<repository>
<id>projectlombok.org</id>
<url>https://projectlombok.org/edge-releases</url>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置类
@Configuration
public class FeignConfig {
@Bean
public Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
}
rpc类
@FeignClient(url = "https://api.weixin.qq.com", name = "wx")
public interface WxService {
@GetMapping("/sns/jscode2session")
String jscode2session(@RequestParam String appid, @RequestParam String secret, @RequestParam String js_code, @RequestParam String grant_type);
@PostMapping("/wxa/business/getuserphonenumber")
WxResponse getuserphonenumber(@RequestBody WxRequest wxRequest, @RequestParam String access_token);
@GetMapping("/cgi-bin/token")
TokenResponse token(@RequestParam String grant_type, @RequestParam String appid, @RequestParam String secret);
}
dto类
public record TokenResponse(String access_token, Integer expires_in) {
}
public record WxResponse(
String openid,
String session_key,
String unionid,
int errcode,
String errmsg
) {
}
启动类
@SpringBootApplication
// 这里必须有
@EnableFeignClients
public class PlatformApplication {
public static void main(String[] args) {
SpringApplication.run(PlatformApplication.class, args);
}
}
测试类
@Test
void testCode() {
TokenResponse token = wxService.token("client_credential", "wxxxxxxxxx", "xxxxxxxxxx");
System.out.println(token);
var wxResponse = wxService.getuserphonenumber(new WxRequest("xxxxxxxxxxxxxxxxxxxxxxx"), token.access_token());
System.out.println(wxResponse);
}