@FeignClient使用详细教程图解
作用
@FeignClient
用于创建声明是API接口,该接口是RESTful风格的。Feign被设计成插拔式的,可注入其他组件和Feign一起使用。最典型的是如果Ribbon可用,Feign会和Ribbon相结合进行负载均衡。
@FeignClient标签的常用属性
- 源码截图
name
(和value
相同):指定FeignClient的名称,如果项目使用了Ribbon,name属性会作为微服务(某个中心的名字)的名称,用于服务发现(图解如下)- url: url一般用于调试,可以手动指定@FeignClient调用的地址(图解如下)
- decode404()即404是被解码,还是抛异常。
- configuration()指明FeignClient的配置类,默认的配置类为FeignClientsConfiguration类,在缺省情况下,这个类注入了默认的Decoder、Encoder和Constant等配置的bean。
- fallback()为配置熔断器的处理类。
- FeignClient的配置类(configuration())
public class FeignUserSupportConfig
{
@Bean
@Primary
@Scope("prototype")
public Encoder multipartFormEncoder() {
return new SpringFormEncoder(new SpringEncoder(new ObjectFactory<HttpMessageConverters>() {
@Override
public HttpMessageConverters getObject() throws BeansException
{
return new HttpMessageConverters(new RestTemplate().getMessageConverters());
}
}));
}
@Bean
public feign.Logger.Level multipartLoggerLevel() {
return feign.Logger.Level.FULL;
}
}
使用流程
- 1.在启动类增加
@EnableFeignClients
- 2.定义接口
package com.xxx.xxx.client;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.Map;
@Component
/**
* value = "user" 中 user 为 调用 user 服务的Id
* spring.application.name=user
*/
@FeignClient(value = "user", configuration = UserSupportConfig.class)
public interface TestClientService {
/**
* @param id
* @return /user/selectById 为 userController 中的 /user/selectById 方法地址
*/
@RequestMapping(value = "/user/selectById", method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
/**
* userId 为参数 必须写@RequestParam 否则报错
*/
Map upload(@RequestParam("userId") Integer id);
}
- 3.定义熔断类,发生错误时回调:
import java.util.List;
import org.springframework.stereotype.Component;
@Component
public class Hysitx implements TestClientService {
@Override
public List<String> test(String[] names) {
System.out.println("接口调用失败");
return null;
}
}
- 4.调用
和基本的service调用一致
1.先引入 @Autowired
2.在方法中直接调用
通俗解释可以把它当成一个spring Bean 可以直接当成一个Service
给@FeignClient 添加Header信息
1.在@RequestMapping中添加
@FeignClient(name="custorm",fallback=Hysitx.class)
public interface IRemoteCallService {
@RequestMapping(value="/user/selectById",method = RequestMethod.POST,
headers = {"Content-Type=application/json;charset=UTF-8"})
Map test(@RequestParam("userId") int id);
}
2.在方法参数前面添加@RequestHeader注解:
@FeignClient(name="custorm",fallback=Hysitx.class)
public interface IRemoteCallService {
@RequestMapping(value="/user/selectById",method = RequestMethod.POST,
headers = {"Content-Type=application/json;charset=UTF-8"})
List<String> test(@RequestParam("userId")@RequestHeader("Authorization") int id);
}
3.使用@Header注解
@FeignClient(name="custorm",fallback=Hysitx.class)
public interface IRemoteCallService {
@RequestMapping(value="/user/selectById",method = RequestMethod.POST)
@Headers({"Content-Type: application/json;charset=UTF-8"})
List<String> test(@RequestParam("userId") int id);
}