当前位置:网站首页>@FeignClient使用詳細教程(圖解)
@FeignClient使用詳細教程(圖解)
2022-07-23 14:51:00 【傑拉德·皮克·謝】
@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);
}
边栏推荐
- Solve a series of problems in using Bert encoder
- PKI体系快速介绍
- Argocd user management, RBAC control, script login, APP synchronization
- (heavy chain dissection) Magic Tree
- Advanced operation and maintenance 02
- The win11 installation system prompts that VirtualBox is incompatible and needs to uninstall the solution of virtual, but the uninstall list cannot find the solution of virtual
- [applet automation minium] i. framework introduction and environment construction
- C语言项目实战:24点游戏计算器(基于结构体、指针、函数、数组、循环等知识点)
- 【测试平台开发】十七、接口编辑页面实现下拉级联选择,绑定接口所属模块...
- Due to resource constraints, the namenode fails to start with an error unable to create new native thread
猜你喜欢

直播课堂系统03补充-model类及实体

工作小记:一次抓包

FastAPI应用加入Nacos

【 langage c】 devinez jeux numériques + applet d'arrêt

Yunna - how to strengthen fixed asset management? How to strengthen the management of fixed assets?

Openharmony South learning notes - hi3861+hc-sr04 ultrasonic testing

Authing supports Zadig! Unified authentication and rapid docking of cloud native users
![[WinForm] desktop program implementation scheme for screenshot recognition and calculation](/img/9f/e67af4386cff9febf0fb5203da03f0.jpg)
[WinForm] desktop program implementation scheme for screenshot recognition and calculation

【小程序自动化Minium】三、元素定位- WXSS 选择器的使用

Which is a good fixed asset management system? What are the fixed asset management platforms?
随机推荐
【我可以做你的第一个项目吗?】GZIP的细节简介和模拟实现
Qt|模仿文字浮动字母
俄方希望有效落实农产品外运“一揽子”协议
C language implementation of classroom random roll call system
Openharmony South learning notes - hi3861+hc-sr04 ultrasonic testing
Qt| imitation text floating letter
452. Detonate the balloon with the minimum number of arrows
运维高级作业02
右键新建txt,新建文本文件不见了,通过添加注册表就可以解决,找来找去办法解决不了的终极办法
Due to resource constraints, the namenode fails to start with an error unable to create new native thread
Generate order number
OpenCV计算外包矩形
Solve a series of problems in using Bert encoder
LZ77文件压缩
Sword finger offer19 regular expression
【PyQt5安装以及使用】
linux定时备份数据库脚本
Yunna | how to manage fixed assets? How to manage fixed assets?
Chapter 3 complex query
【测试平台开发】21. 完成发送接口请求显示响应头信息