Feign可以帮我们更便捷的调用HTTP API
一、 为消费者整合Feign
1.1 添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
1.2 创建Feign接口,添加@FeignClient
@FeignClient(name="user")
public interface UserFeignClient {
@RequestMapping(value="/{id}",method=RequestMethod.GET)
public User findById(@PathVariable("id") Long id);
}
1.3 修改Controller代码,来调用Feign接口
@RestController
public class MovieController {
@Autowired
private UserFeignClient userFeignClient;
@GetMapping("/user/{id}")
public User findById(@PathVariable Long id){
return this.userFeignClient.findById(id);
}
}
1.4 修改启动类
@SpringBootApplication
@EnableFeignClients
public class ConsumerApplication
{
public static void main( String[] args )
{
SpringApplication.run(ConsumerApplication.class, args);
}
}
二、自定义Feign配置
自定义配置有两种,分别是java自定义配置和属性自定义配置。推荐使用属性自定义配置
2.1 配置指定名称的Feign Client
feign.client.config.feignName.connectTimeout=5000 #相当于代码配置Request.Options
feign.client.config.feignName.readTimeout=5000 #相当于代码配置Request.Options
feign.client.config.feignName.loggerlevel=full #配置日志级别,相当于代码配置Logger
feign.client.config.feignName.errorDecoder=com.example.SimpleErrorDecoder #错误解析码,相当于代码配置ErrorDecoder
feign.client.config.feignName.retryer=com.example.SimpleRetryer #配置重试,相当于代码配置Retryer
feign.client.config.feignName.requestInterceptors=com.example.FooRequestInterceptor #配置拦截器,相当于代码配置RequestInterceptor
feign.client.config.feignName.decode404=false
2.2 通用配置(即配置所有的Feign Client)
将上述feignName替换成default
三、Feign对继承的支持
可将一些公共操作分组到一些父接口中,从而简化Feign的开发。但是不建议在服务器端和客户端共享接口。
3.1 基础接口
public interface UserService {
@RequestMapping(value="/users/{id}",method = RequestMethod.GET)
User getUser(@PathVariable("id") long id);
}
3.2 服务提供者Controller
@RestController
public class UserResource implements UserService {
//....
}
3.3 服务消费者
@FeignClient
public class UserCilent extends UserResource {
//....
}
四、Feign对压缩的支持
feign.compression.request.enabled=true
feign.compression.request.mime-types=text/xml,application/xml,application/json #支持的媒体类型列表
feign.compression.request.min-request-size=2048 #设置请求的最小阈值,默认2048
feign.compression.response.enabled=true
五、Feign日志
5.1 日志级别
NONE:不记录任何日志
BASIC:仅记录请求方法、URL、相应状态代码以及执行时间
HEADERS:在BASIC基础上,记录请求和相应的header
FULL:记录请求和相应的header、body、元数据
5.2 编码方式设置日志级别
5.2.1 配置类
@Configuration
public class FeignLogConfiguration {
@Bean
Logger.Level feignLoggerLevel(){
return Logger.Level.FULL;
}
}
5.2.2 修改Feign的接口,指定配置类
@FeignClient(name="user",configuration=FeignLogConfiguration.class)
public interface UserFeignClient {
@RequestMapping(value="/{id}",method=RequestMethod.GET)
public User findById(@PathVariable("id") Long id);
}
5.2.3 在配置文件中指定Feign接口的日志级别
logging.level.com.itmch.cloud.study.user.feign.UserFeignClient=DEBUG
5.3 属性配置日志级别
feign.client.config.user.loggerLevel=full
logging.level.com.itmch.cloud.study.user.feign.UserFeignClient=DEBUG
注:Feign接口的日志级别设置成DEBUG,因为Feign的Logger.Level只对DEBUG做出响应
六、使用Feign构造多参数请求
6.1 GET请求多参数的URL
方法一:
@FeignClient(name="user")
public interface UserFeignClient {
@RequestMapping(value="/get",method=RequestMethod.GET)
public User get1(@RequestParam("id") Long id,@RequestParam("username") String username);
}
方法二:
@FeignClient(name="user")
public interface UserFeignClient {
@RequestMapping(value="/get",method=RequestMethod.GET)
public User get2(@RequestParam HashMap<String, String> map);
}
在调用时使用类似以下代码
public User get(String username,String password){
HashMap<String, String> map = Maps.newHashMap();
map.put("id", "1");
map.put("username", "aa");
return this.userFeignClient.get2(map);
}
6.2 POST请求包含多个参数
如果服务提供者的Controller是这样的
@RestController
public class UserController {
@PostMapping("/post")
public User post(@RequestBody User user){
//...
}
}
那么使用如下代码用Feign去请求:
@FeignClient(name="user")
public interface UserFeignClient {
@RequestMapping(value="/post",method=RequestMethod.POST)
public User post(@RequestBody User user);
}
七、使用Feign上传文件
7.1 添加依赖
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form-spring</artifactId>
<version>3.0.3</version>
</dependency>
7.2 编写Feign Client
@FeignClient(name="upload",configuration=UploadFeignClient.MultipartSupportConfig.class)
public interface UploadFeignClient {
@RequestMapping(value="/upload",method=RequestMethod.POST,produces={MediaType.APPLICATION_JSON_UTF8_VALUE},consumes=MediaType.MULTIPART_FORM_DATA_VALUE)
@ResponseBody
String handleFileUpload(@RequestPart(value="file") MultipartFile file);
class MultipartSupportConfig{
@Bean
public Encoder feignFormEncoder(){
return new SpringFormEncoder();
}
}
}