SpringBoot 使用 Feign 无废话 All-in-one 指南

开篇Feign 是声明式、模板化的 HTTP 客户端,可以帮助我们更快捷、优雅地调用 HTTP API;Spring Cloud 为 Feign 添加了 Spring MVC 的注解支持,并整合了 Ribbon 和 Eureka 来为使用 Feign 时提供负载均衡;在 Spring Cloud 中使用 Feign 是非常容易的 。
本篇主要介绍 SpringBoot 中要玩转 Feign 需要掌握的如添加 pom 依赖、客户端注解启用、切换底层 HttpClient、配置数据压缩、调整日志级别、定制配置、配置的优先级机制、增加拦截器以及拦截器的追加机制等知识 。
一、使用 Feign 的示例1.1 添加依赖<dependencies><!--openfein的依赖--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId><version>2.1.3.RELEASE</version></dependency></dependencies>复制代码1.2 启用 Feign在 SpringBoot 的启用类上添加注解@EnableFeignClients,@EnableFeignClients用于开启 Feign,会自动扫描@FeignClient标注的 FeignClient 接口 。
【SpringBoot 使用 Feign 无废话 All-in-one 指南】@SpringBootApplication@EnableFeignClients@EnableWebpublic class FeignApplication {public static void main(String[] args) {SpringApplication.run(FeignApplication.class,args);}}复制代码1.3 编写 FeignClient 接口@FeignClient(name = "demo-service",url = "http://localhost:8080/feign/server/",configuration = FeignInterceptor.class,fallback = TestService.DefaultFallback.class)public interface TestService {@RequestMapping(value = https://www.isolves.com/it/cxkf/kj/2022-11-27/"/getError/{id}", method = RequestMethod.GET)public String getError(@RequestParam("id") Integer id);@RequestMapping(value = "/get1", method = RequestMethod.GET)public String get1();@RequestMapping(value = "/get2/{param}", method = RequestMethod.GET)public String get2(@RequestParam("param") String param);@RequestMapping(value = "/post1", method = RequestMethod.POST)public FeignDemo post1(@RequestBody FeignDemo demo);复制代码1.4 编写对应的服务端@RestController@RequestMapping("/feign/server")public class FeignServerController {@GetMapping("/get1")public String get1() {return "get1";}@GetMapping("/get2/{para}")public String get2(@PathVariable("para") String para){return para;}@PostMapping("/post1")public FeignDemopost1(@RequestBody FeignDemo demo) {return demo;}}复制代码public class FeignDemo {private String name;private Integer age;public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}@Overridepublic String toString() {return "FeignDemo{" +"name='" + name + ''' +", age=" + age +'}';}}复制代码1.5 调用 FeignClient@RunWith(SpringJUnit4ClassRunner.class)@SpringBootTest(classes = {FeignApplication.class},webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)@ActiveProfiles("dev,feign")public class FeignClientTest {@Autowiredprivate TestService testService;@Testpublic void testFallback(){testService.getError(1);}@Testpublic void testGet1(){System.out.println(testService.get1());System.out.println(testService.get2("abc"));System.out.printf("..");FeignDemo feignDemo = new FeignDemo();feignDemo.setName("name");feignDemo.setAge(1);System.out.println(testService.post1(feignDemo));}@Componentpublic class DefaultFallback implements TestService {@Overridepublic String getError(@RequestParam("id") Integer id){return "";}@Overridepublic String get1() {return null;}@Overridepublic String get2(String param) {return null;}@Overridepublic FeignDemo post1(FeignDemo demo) {return null;}}}复制代码二、如何切换 ClientFeign 中自带的是 HttpURLConnection,这个 client 健壮性差,可替换为成熟的 Apache HttpClient 或 OkHttp 来进行网络请求 。
2.1 使用 Apache 的 HTTP Client使用 Apache 的 httpclient 替换 Feign 中默认的 client 。
2.1.1 添加依赖<!--httpclient的依赖,因为选择了使用httpclient--><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId></dependency><dependency><groupId>io.github.openfeign</groupId><artifactId>feign-httpclient</artifactId><version>10.4.0</version></dependency>复制代码2.1.2 配置启用配置中添加如下信息,表示启用httpclient 。
feign:httpclient:enabled: true复制代码2.2 使用 OkHttp2.2.1 添加依赖在 Feign 中使用OkHttp作为网络请求框架,则只需要在 pom 文件中加上feign-okhttp的依赖,代码如下:
<dependency><groupId>io.github.openfeign</groupId><artifactId>feign-okhttp</artifactId><version>10.2.0</version></dependency>复制代码


推荐阅读