@FeignClient标签的常用属性如下:
- name:指定FeignClient的名称,如果项目使用了Ribbon,name属性会作为微服务的名称,用于服务发现
- url: url一般用于调试,可以手动指定@FeignClient调用的地址
- decode404:当发生http 404错误时,如果该字段位true,会调用decoder进行解码,否则抛出FeignException
- configuration: Feign配置类,可以自定义Feign的Encoder、Decoder、LogLevel、Contract
- fallback: 定义容错的处理类,当调用远程接口失败或超时时,会调用对应接口的容错逻辑,fallback指定的类必须实现@FeignClient标记的接口
- fallbackFactory: 工厂类,用于生成fallback类示例,通过这个属性我们可以实现每个接口通用的容错逻辑,减少重复的代码
- path: 定义当前FeignClient的统一前缀,当我们项目中配置了server.context-path,server.servlet-path时使用
1.首先:我们在启动类里面加入注解,声明开启Feign的远程调用,如下:
@EnableEurekaClient
@SpringBootApplication
@EnableFeignClients
public class LoginStart {public static void main(String[] args) {SpringApplication.run(LoginStart.class, args);}
}
2.编写接口类,value="/xxx/xxx"就是我们服务方暴露的接口地址,如下:
import java.util.List;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;@FeignClient(name="custorm",fallback=Hysitx.class)
public interface IRemoteCallService {@RequestMapping(value="/custorm/getTest",method = RequestMethod.POST)List<String> test(@RequestParam("names") String[] names);
}
3.编写熔断类,发生错误时回调:
import java.util.List;
import org.springframework.stereotype.Component;
@Component
public class Hysitx implements IRemoteCallService{@Overridepublic List<String> test(String[] names) {System.out.println("接口调用失败");return null;}
}
4.然后我们准备两个消费者工程,custorm(服务方),login(调用方),然后在login的controller中写前台调用接口:
@Autowired
private IRemoteCallService remot;@RequestMapping("/config")
public String config() {String[] names = {"王五","张柳"};return remot.test(names).toString();
}
5。然后在custorm工程中写一个接口,在这个接口里我们只将传输进来的数据再添加一个数据返回回去
@RestController
@RequestMapping("/custorm")
public class CustormController {@RequestMapping("/getTest")public List<String> Test(String[] names) {List<String> name = new ArrayList<String>(Arrays.asList(names));name.add("王麻子");return name;}
}
6.然后我们启动注册中心,配置中心以及两个消费者服务,需要了解配置中心和注册中心的搭建可以看我前两篇文章,启动后浏览器我们进行访问
可以看到,返回的数据中已经包含了custorm工程中拼接的数据,说明我们远程调用接口成功,以上就是feign的简单使用
另外补充一些面试中长问的如何给@FeignClient添加Header信息
1.在@RequestMapping中添加,如下:
@FeignClient(name="custorm",fallback=Hysitx.class)
public interface IRemoteCallService {@RequestMapping(value="/custorm/getTest",method = RequestMethod.POST,headers = {"Content-Type=application/json;charset=UTF-8"})List<String> test(@RequestParam("names") String[] names);
}
2:在方法参数前面添加@RequestHeader
注解,如下:
@FeignClient(name="custorm",fallback=Hysitx.class)
public interface IRemoteCallService {@RequestMapping(value="/custorm/getTest",method = RequestMethod.POST,headers = {"Content-Type=application/json;charset=UTF-8"})List<String> test(@RequestParam("names")@RequestHeader("Authorization") String[] names);
}
设置多个属性时,可以使用Map,如下:
import java.util.List;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;@FeignClient(name="custorm",fallback=Hysitx.class)
public interface IRemoteCallService {@RequestMapping(value="/custorm/getTest",method = RequestMethod.POST,headers = {"Content-Type=application/json;charset=UTF-8"})List<String> test(@RequestParam("names") String[] names, @RequestHeader MultiValueMap<String, String> headers);
}
3.使用@Header注解,如下:
@FeignClient(name="custorm",fallback=Hysitx.class) public interface IRemoteCallService {@RequestMapping(value="/custorm/getTest",method = RequestMethod.POST)@Headers({"Content-Type: application/json;charset=UTF-8"})List<String> test(@RequestParam("names") String[] names); }
4.实现RequestInterceptor
接口,如下:
@Configuration
public class FeignRequestInterceptor implements RequestInterceptor {@Overridepublic void apply(RequestTemplate temp) {temp.header(HttpHeaders.AUTHORIZATION, "XXXXX");}}