Spring Boot 和 Spring Cloud Feign调用服务及传递参数踩坑记录
背景 :在Spring Cloud Netflix栈中,各个微服务都是以HTTP接口的形式暴露自身服务的,因此在调用远程服务时就必须使用HTTP客户端。我们可以使用JDK原生的URLConnection、Apache的Http Client、Netty的异步HTTP Client, Spring的RestTemplate。但是,用起来最方便、最优雅的还是要属Feign了。Feign是一种声明式、模板化的HTTP客户端。
Contronller层通过feignClient调用微服务 获取所有任务
@Controller
@RequestMapping("tsa/task")
public class TaskController{
@Autowired
TaskFeignClient taskFeignClient; @PostMapping("/getAll")
@ResponseBody
public List<TaskVO> getAll() {
List<TaskVO> all = taskFeignClient.getAll();
return all;
}
}
@FeignClient用于通知Feign组件对该接口进行代理(不需要编写接口实现),使用者可直接通过@Autowired注入。
@FeignClient(qualifier = "taskFeignClient", name = "service-tsa",fallback = TaskFeignClientDegraded.class)
public interface TaskFeignClient {
@PostMapping(value = "taskApiController/getAll")
List<TaskVO> getAll(); }
微服务端
@Slf4j
@RestController
@RequestMapping("taskApiController")
public class TaskApiController{ @Autowired
private TaskService taskService; @PostMapping("/getAll")
public List<TaskVO> getAll() {
log.info("--------getAll-----");
List<TaskVO> all = taskService.getAll();
return all;
} }
坑1:
com.netflix.hystrix.exception.HystrixRuntimeException: TaskFeignClient#getAll() failed and no fallback available.
at com.netflix.hystrix.AbstractCommand$22.call(AbstractCommand.java:819)
at com.netflix.hystrix.AbstractCommand$22.call(AbstractCommand.java:804)
坑2:
坑3
报错:RequestParam.value() was empty on parameter 0
如果在FeignClient中的方法有参数传递一般要加@RequestParam(“xxx”)注解
错误写法:
@FeignClient(qualifier = "taskFeignClient", name = "service-tsa",fallback = TaskFeignClientDegraded.class)
public interface TaskFeignClient {
@PostMapping(value = "taskApiController/getAll")
List<TaskVO> getAll(String name);
} //或者
@FeignClient(qualifier = "taskFeignClient", name = "service-tsa",fallback = TaskFeignClientDegraded.class)
public interface TaskFeignClient {
@PostMapping(value = "taskApiController/getAll")
List<TaskVO> getAll(@RequestParam String name);
}
正确写法:
@PostMapping(value = "taskApiController/getAll")
List<TaskVO> getAll(@RequestParam("name") String name);
微服务那边可以不写这个注解
参考 https://blog.csdn.net/jxm007love/article/details/80109974
Fiegn Client with Spring Boot: RequestParam.value() was empty on parameter 0
Feign bug when use @RequestParam but not have value
https://www.xttblog.com/ 业余草
坑四:
FeignClient中post传递对象和consumes = "application/json",按照坑三的意思,应该这样写。
@FeignClient(qualifier = "taskFeignClient", name = "service-tsa",fallback = TaskFeignClientDegraded.class)
public interface TaskFeignClient {
@PostMapping(value = "taskApiController/getAll")
List<TaskVO> getAll(@RequestParam("vo") TaskVO vo); }
很意外报错
16:00:33.770 [http-apr-8086-exec-1] DEBUG c.b.p.a.s.PrimusCasAuthenticationFilter - proxyTicketRequest = false
16:00:33.770 [http-apr-8086-exec-1] DEBUG c.b.p.a.s.PrimusCasAuthenticationFilter - requiresAuthentication = false
16:00:34.415 [hystrix-service-tsa-2] DEBUG c.b.p.m.b.f.PrimusSoaFeignErrorDecoder - error json:{
"timestamp":1543564834395,
"status":500,
"error":"Internal Server Error",
"exception":"org.springframework.web.method.annotation.MethodArgumentConversionNotSupportedException","message":"Failed to convert value of type 'java.lang.String' to required type 'com.model.tsa.vo.TaskVO';
nested exception is java.lang.IllegalStateException:
Cannot convert value of type 'java.lang.String' to required type 'com.model.tsa.vo.TaskVO':
no matching editors or conversion strategy found","path":"/taskApiController/getAll" }
@FeignClient(qualifier = "taskFeignClient", name = "service-tsa",fallback = TaskFeignClientDegraded.class)
public interface TaskFeignClient {
@PostMapping(value = "taskApiController/getAll",,consumes = "application/json")
List<TaskVO> getAll(TaskVO vo);
}
也可以这样写
@PostMapping(value = "taskApiController/getAll")
List<TaskVO> getAll(@RequestBody TaskVO vo);
此时不用,consumes = "application/json"
@Slf4j
@RestController
@RequestMapping("taskApiController")
public class TaskApiController{
@Autowired
private TaskService taskService; @PostMapping("/getAll")
public List<TaskVO> getAll(@RequestBody TaskVO vo) {
log.info("--------getAll-----");
List<TaskVO> all = taskService.getAll();
return all;
}
}
我第一次写这个的时候方法参数里面什么注解都没加,可以正常跑通,但是传过去的对象却为初始值,实际上那是因为对象根本就没传
传递对象的另一种方法和多参传递
@FeignClient("microservice-provider-user")
public interface UserFeignClient {
@RequestMapping(value = "/get", method = RequestMethod.GET)
public User get0(User user);
}
然而我们测试时会发现该写法不正确,我们将会收到类似以下的异常:
{"timestamp":1482676142940,"status":405,"error":"Method Not Allowed","exception":"org.springframework.web.HttpRequestMethodNotSupportedException","message":"Request method 'POST' not supported","path":"/get"}
@FeignClient(name = "microservice-provider-user")
public interface UserFeignClient { @RequestMapping(value = "/get", method = RequestMethod.GET)
public User get1(@RequestParam("id") Long id, @RequestParam("username") String username); }
这是最为直观的方式,URL有几个参数,Feign接口中的方法就有几个参数。使用@RequestParam注解指定请求的参数是什么。
(2) 方法二
@FeignClient(name = "microservice-provider-user")
public interface UserFeignClient {
@RequestMapping(value = "/get", method = RequestMethod.GET)
public User get2(@RequestParam Map<String, Object> map); }
多参数的URL也可以使用Map去构建
当目标URL参数非常多的时候,可使用这种方式简化Feign接口的编写。
POST请求包含多个参数
下面我们来讨论如何使用Feign构造包含多个参数的POST请求。实际就是坑四,把参数封装成对象传递过去就可以了。
最后拓展一下
Feign的Encoder、Decoder和ErrorDecoder
@RequestMapping(value = "/group/{groupId}", method = RequestMethod.GET)
void update(@PathVariable("groupId") Integer groupId, @RequestParam("groupName") String groupName, DataObject obj);
此时因为声明的是GET请求没有请求体,所以obj参数就会被忽略。
Feign的HTTP Client
Feign在默认情况下使用的是JDK原生的URLConnection发送HTTP请求,没有连接池,但是对每个地址会保持一个长连接,即利用HTTP的persistence connection 。我们可以用Apache的HTTP Client替换Feign原始的http client, 从而获取连接池、超时时间等与性能息息相关的控制能力。Spring Cloud从Brixtion.SR5版本开始支持这种替换,首先在项目中声明Apache HTTP Client和feign-httpclient依赖:
<!-- 使用Apache HttpClient替换Feign原生httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<dependency>
<groupId>com.netflix.feign</groupId>
<artifactId>feign-httpclient</artifactId>
<version>${feign-httpclient}</version>
</dependency>
feign.httpclient.enabled=true
通过Feign, 我们能把HTTP远程调用对开发者完全透明,得到与调用本地方法一致的编码体验。这一点与阿里Dubbo中暴露远程服务的方式类似,区别在于Dubbo是基于私有二进制协议,而Feign本质上还是个HTTP客户端。如果是在用Spring Cloud Netflix搭建微服务,那么Feign无疑是最佳选择。
原文链接:
https://blog.csdn.net/uotail/article/details/84673347
Spring Boot 和 Spring Cloud Feign调用服务及传递参数踩坑记录的更多相关文章
- Spring Boot 和 Spring Cloud Feign调用服务及传递参数踩坑记录(转)
https://blog.csdn.net/uotail/article/details/84673347
- 学习加密(四)spring boot 使用RSA+AES混合加密,前后端传递参数加解密
学习加密(四)spring boot 使用RSA+AES混合加密,前后端传递参数加解密 技术标签: RSA AES RSA AES 混合加密 整合 前言: 为了提高安全性采用了RS ...
- spring cloud feign 调用服务注意问题
服务端 rest api @RequestMapping(value = "/phone") public ResponsePhone getPhone(@RequestParam ...
- 微信接口调用'updateTimelineShareData','updateAppMessageShareData' 的踩坑记录
6月份新版微信客户端发布后,用户从微信内的网页分享消息给微信好友,以及分享到朋友圈,开发者将无法获知用户是否分享完成.具体调整点为: ()分享接口调用后,不再返回用户是否分享完成事件,即原先的canc ...
- 基于Spring Boot、Spring Cloud、Docker的微服务系统架构实践
由于最近公司业务需要,需要搭建基于Spring Cloud的微服务系统.遍访各大搜索引擎,发现国内资料少之又少,也难怪,国内Dubbo正统治着天下.但是,一个技术总有它的瓶颈,Dubbo也有它捉襟见肘 ...
- maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目
项目的简单介绍: 项目采用maven聚合工程 用spring boot 搭建 spring cloud的微服务 模块式开发 项目的截图: 搭建开始: 能上图 我少打字 1.首先搭建maven的聚合工程 ...
- 基于Spring Boot和Spring Cloud实现微服务架构学习
转载自:http://blog.csdn.net/enweitech/article/details/52582918 看了几周Spring相关框架的书籍和官方demo,是时候开始总结下这中间的学习感 ...
- 基于Spring Boot和Spring Cloud实现微服务架构学习--转
原文地址:http://blog.csdn.net/enweitech/article/details/52582918 看了几周spring相关框架的书籍和官方demo,是时候开始总结下这中间的学习 ...
- 基于Spring Boot和Spring Cloud实现微服务架构
官网的技术导读真的描述的很详细,虽然对于我们看英文很费劲,但如果英文不是很差,请选择沉下心去读,你一定能收获好多.我的学习是先从Spring boot开始的,然后接触到微服务架构,当然,这一切最大的启 ...
随机推荐
- SDKStyle的Framework项目使用旧版项目文件生成的Nuget包遇到的问题
随笔-2021-11-10 SDKStyle的Framework项目使用旧版项目文件生成的Nuget包遇到的问题 简介 C#从NetCore之后使用了新版的项目文件,SDK-Style项目,新版本的项 ...
- k8s endpoints controller分析
k8s endpoints controller分析 endpoints controller简介 endpoints controller是kube-controller-manager组件中众多控 ...
- 大爽Python入门教程 3-6 答案
大爽Python入门公开课教案 点击查看教程总目录 1 求平方和 使用循环,计算列表所有项的平方和,并输出这个和. 列表示例 lst = [8, 5, 7, 12, 19, 21, 10, 3, 2, ...
- 大白话讲解Mybatis的plugin(Interceptor)的使用
mybatis提供了一个入口,可以让你在语句执行过程中的某一点进行拦截调用.官方称之为插件plugin,但是在使用的时候需要实现Interceptor接口,默认情况下,MyBatis 允许使用插件来拦 ...
- Python 常见运算符表达式
常见运算符表达式 1.算数运算符 2.逻辑运算符 3.比较运算符 4.成员运算符 5.位运算符 6.身份运算符a.赋值运算符 = 格式:变量= 表达式 ...
- [gym101981F]Frank
在本题中,每一步是独立的,因此即可以看作从$s$移动到$t$的期望步数(对于每一对$s$和$t$都求出答案) 令$f_{i,j}$表示当$s=i$且$t=j$时的答案,则有$f_{i,j}=\begi ...
- [cf674E]Bear and Destroying Subtrees
令$f_{i,j}$表示以$i$为根的子树中,深度小于等于$j$的概率,那么$ans_{i}=\sum_{j=1}^{dep}(f_{i,j}-f_{i,j-1})j$ 大约来估计一下$f_{i,j} ...
- [nowcoder5667H]Happy Triangle
可以发现合法的答案有两种可能: 1.询问的$x$即为最大值(或之一),那么只需要找到x前两个数并判断即可 2.询问的$x$不是最大值,那么就要保证另外两边之差小于$x$,维护后缀中$的前驱k-k的前驱 ...
- Roslyn+T4+EnvDTE项目完全自动化 (一)
前言 以前做一个金融软件项目,软件要英文.繁体版本,开始甲方弄了好几个月,手动一条一条替换,发现很容易出错,因为有金融专业术语,字符串在不同语义要特殊处理,第三方工具没法使用.最后我用Roslyn写了 ...
- IDEA安装JavaFx
jdk11之后jdk就不内置javafx了,需要自己下载 在idea中新建JavaFx项目: 创建成功后发现代码标红 这个时候要把刚刚下载的JavaFx包解压后添加进去 选择到自己解压的路径的文件 ...