当前位置:网站首页>@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);
}
边栏推荐
- 【PyQt5安装以及使用】
- QT document reading notes audio example analysis
- 云呐|怎样管理固定资产?如何进行固定资产管理?
- 云呐|公司固定资产如何管理?公司固定资产如何管理比较好?
- What is per title encoding?
- 固定资产管理系统哪家好?固定资产管理平台有哪些?
- C language introduction practice (11): enter a group of positive integers and sum the numbers in reverse order
- Due to resource constraints, the namenode fails to start with an error unable to create new native thread
- [software test] MQ abnormal test encountered in disk-to-disk work
- mysql唯一索引无重复值报错重复
猜你喜欢

Opencv calculation outsourcing rectangle

正则表达式常用语法解析

Sword finger offer 46. translate numbers into strings

C# 线程锁和单多线程简单使用

What is per title encoding?

MySQL unique index has no duplicate value, and the error is repeated

Towhee weekly model

mysql唯一索引无重复值报错重复

LZ77 file compression

C thread lock and single multithreading are simple to use
随机推荐
First acquaintance and search set
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
[C language] number guessing game + shutdown applet
Towhee weekly model
[applet automation minium] i. framework introduction and environment construction
JS software unloading prompt expression changes with the mouse JS special effect
PKI体系快速介绍
452. Detonate the balloon with the minimum number of arrows
【小程序自动化Minium】一、框架介绍和环境搭建
【无标题】测试【无标题】测试
【PyQt5安装以及使用】
Yunna | how to manage fixed assets? How to manage fixed assets?
Sword finger offer 46. translate numbers into strings
【WinForm】关于截图识别数字并计算的桌面程序实现方案
C language implementation of classroom random roll call system
Design and implementation of websocket universal packaging
Ffmpeg 1 - Overview / installation
云呐|公司固定资产如何管理?公司固定资产如何管理比较好?
中望CAD专业版 2022软件安装包下载及安装教程
String function of MySQL function summary