@FeignClient使用详细教程图解 作用@FeignClient标签的常用属性使用流程通俗解释可以把它当成一个spring Bean 可以直接当成一个Service 给@FeignC
@FeignClient
用于创建声明是api接口,该接口是RESTful风格的。Feign被设计成插拔式的,可注入其他组件和Feign一起使用。最典型的是如果Ribbon可用,Feign会和Ribbon相结合进行负载均衡。
name
(和value
相同):指定FeignClient的名称,如果项目使用了Ribbon,name属性会作为微服务(某个中心的名字)的名称,用于服务发现(图解如下)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; }}
@EnableFeignClients
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@FeignClient(value = "user", configuration = UserSupportConfig.class)public interface TestClientService { @RequestMapping(value = "/user/selectById", method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}, consumes = MediaType.MULTIPART_FORM_DATA_VALUE) Map upload(@RequestParam("userId") Integer id);}
import java.util.List;import org.springframework.stereotype.Component;@Componentpublic class Hysitx implements TestClientService {@Overridepublic List<String> test(String[] names) {System.out.println("接口调用失败");return null;}}
在@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);}
在方法参数前面添加@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);}
使用@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);}
来源地址:https://blog.csdn.net/weixin_44684812/article/details/125906729
--结束END--
本文标题: @FeignClient使用详细教程(图解)
本文链接: https://www.lsjlt.com/news/429593.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-04-03
2024-04-03
2024-04-01
2024-01-21
2024-01-21
2024-01-21
2024-01-21
2023-12-23
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0