当前位置:网站首页>@Feignclient detailed tutorial (illustration)
@Feignclient detailed tutorial (illustration)
2022-07-23 14:52:00 【Gerald pick Xie】
@FeignClient Use the detailed tutorial illustration
effect
@FeignClient Used to create a declaration yes API Interface , The interface is RESTful Style .Feign Designed to be pluggable , Other components and Feign Use it together . The most typical is if Ribbon You can use ,Feign Hui He Ribbon Combining load balancing .
@FeignClient Common properties of tags
- Source screenshot

name( andvalueidentical ): Appoint FeignClient The name of , If the project is used Ribbon,name Attributes will be used as microservices ( The name of a center ) The name of , For service discovery ( The diagram is as follows )- url: url Generally used for debugging , Can be specified manually @FeignClient The address of the call ( The diagram is as follows )
- decode404() namely 404 Is decoded , Or throw an exception .
- configuration() To specify FeignClient Configuration class , The default configuration class is FeignClientsConfiguration class , By default , This class injects the default Decoder、Encoder and Constant And so on bean.
- fallback() Configure the processing class of fuse .


- FeignClient Configuration class (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;
}
}
Usage flow
- 1. Add... In the startup class
@EnableFeignClients
- 2. Defining interfaces
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" in user by call user Service Id * spring.application.name=user */
@FeignClient(value = "user", configuration = UserSupportConfig.class)
public interface TestClientService {
/** * @param id * @return /user/selectById by userController Medium /user/selectById Method address */
@RequestMapping(value = "/user/selectById", method = RequestMethod.GET, produces = {
MediaType.APPLICATION_JSON_UTF8_VALUE}, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
/** * userId Is the parameter Must write @RequestParam Otherwise, the report will be wrong */
Map upload(@RequestParam("userId") Integer id);
}
- 3. Define fuse class , Callback when an error occurs :
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(" Interface call failed ");
return null;
}
}
- 4. call
And basic service Call consistency
1. First introduced @Autowired
2. Call directly in the method
Popular explanation can regard it as a spring Bean It can be directly regarded as a Service
to @FeignClient add to Header Information
1. stay @RequestMapping Add
@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. Add the parameter before the method @RequestHeader annotation :
@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. Use @Header annotation
@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);
}
边栏推荐
- 【WinForm】关于截图识别数字并计算的桌面程序实现方案
- 关于flex布局justify-content:space-around最后一个不对齐的解决方法和为什么这样子解决是讨论
- CPU, memory, disk speed comparison
- 微信官方出品!小程序自动化框架 minium 分享预告
- QT document reading notes audio example analysis
- Quick introduction to PKI system
- Introduction and mechanism of Aptos
- Building personal network disk based on nextcloud
- Program design of dot matrix Chinese character display of basic 51 single chip microcomputer
- 云呐|公司固定资产如何管理?公司固定资产如何管理比较好?
猜你喜欢
![[applet automation minium] III. element positioning - use of wxss selector](/img/ec/51eadd08bea18f8292aa3521f11e8a.png)
[applet automation minium] III. element positioning - use of wxss selector

基于nextcloud构建个人网盘

什麼是Per-Title編碼?

运维高级作业02

【C語言】猜數字小遊戲+關機小程序

运维高级作业03

C thread lock and single multithreading are simple to use

Vk36n5d anti power interference / mobile phone interference 5-key 5-channel touch detection chip anti freeze function ponding in the touch area can still be operated

微信官方出品!小程序自动化框架 minium 分享预告

Advanced operation and maintenance 02
随机推荐
Advanced operation and maintenance 03
R language practical application case: drawing part (III) - drawing of multiple combination patterns
[C language] number guessing game + shutdown applet
CPU, memory, disk speed comparison
【WinForm】关于截图识别数字并计算的桌面程序实现方案
【无标题】测试【无标题】测试
LeetCode-227-基本计算器||
CAN总线快速了解
It is suggested that Siyuan notes can be compatible with third-party sync disks
Which is a good fixed asset management system? What are the fixed asset management platforms?
Official wechat product! Applet automation framework minium sharing Preview
【测试平台开发】23. 接口断言功能-保存接口断言和编辑回显
Using shell script to block IP with high scanning frequency
右键新建txt,新建文本文件不见了,通过添加注册表就可以解决,找来找去办法解决不了的终极办法
[can I do your first project?] Detailed introduction and Simulation Implementation of gzip
C language implements StrCmp, strstr, strcat, strcpy
直播课堂系统02-搭建项目环境
AI acceleration gesture recognition experience based on efr32mg24
[applet automation minium] III. element positioning - use of wxss selector
【无标题】