什么是Feign,又为什么使用Feign。Feign是一个声明式的Web Service客户端, Feign满足JSR 356规范、满足JAX-RS(Java API for RESTful)规范 ,可以快速地构建具有RESTful风格的网络请求应用。为什么用Feign,在微服务架构中, 庞杂的系统被分成了一个个微小的服务,服务与服务之间是通过HTTP接口的形式对外提供服务,Feign让服务间的调用变得简单。
官方的定义:
Feign is a Java to HTTP client binder inspired by Retrofit, JAXRS-2.0, and WebSocket. Feign's first goal was reducing the complexity of binding Denominator uniformly to HTTP APIs regardless of ReSTfulness.
它具有如下特性:
1. 支持可插拔的注解支持,包括Feign注解和JAX-RS注解
2. 支持可插拔的HTTP编码器和解码器
3. 支持Hystrix和回滚
4. 支持Ribbon的负载均衡
5. 支持HTTP请求和响应的压缩
接下来将通过几个实战的案例讲解Feign的特性,关于Ribbon和Hystrix的部分会在接下来关于RIbbon和Hytrix的文章中整合,此篇文章暂不包括。
1 入门案例,根据FeignClient指定调用URL的方式,根据查询的参数搜索GitHub上的项目信息,下图为正常通过github查询到的关于spring-framework的搜索结果,目前有11395个项目包含spring-framework相关内容
在业务代码中,通过注解@FeignClient,指定要调用URL为https://api.github.com,Service会根据URL和@RequestMapping对应的方法转换成最终的请求地址https://api.github.com/search/repositories?q=spring-framework,然后返回对应的仓库信息,如图1所示:

package com.bruce.sample.demo.service;
import com.bruce.sample.demo.config.HelloFeignServiceConfig;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
/**
* @Author: Bruce
* @Date: 2019/6/20 18:01
* @Version 1.0
*/
@FeignClient(name = "github-client", url = "https://api.github.com", configuration = HelloFeignServiceConfig.class)
public interface HelloFeignService {
@RequestMapping(value = "/search/repositories", method = RequestMethod.GET)
String searchRepo(@RequestParam("q") String quer