spring cloud 使用feign 遇到问题

spring cloud 使用feign 项目的搭建 在这里就不写了,本文主要讲解在使用过程中遇到的问题以及解决办法

1:示例

1 @RequestMapping(value = "/generate/password", method = RequestMethod.POST)
2 KeyResponse generatePassword(@RequestBody String passwordSeed);
3 在这里 只能使用 @RequestMapping(value = "/generate/password", method = RequestMethod.POST) 注解 不能使用 @PostMapping 否则项目启动会报
4 Caused by: java.lang.IllegalStateException: Method generatePassword not annotated with HTTP method type (ex. GET, POST) 异常

2:首次访问超时问题

原因: Hystrix默认的超时时间是1秒,如果超过这个时间尚未响应,将会进入fallback代码。
而首次请求往往会比较慢(因为Spring的懒加载机制,要实例化一些类),这个响应时间可能就大于1秒了。
解决方法:

<1: 配置Hystrix的超时时间改为5秒

hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000

<2:禁用Hystrix的超时时间

hystrix.command.default.execution.timeout.enabled: false

<3:禁用feign的hystrix 功能

feign.hystrix.enabled: false

注: 个人推荐 第一 或者第二种 方法

3:FeignClient接口中,如果使用到@PathVariable ,必须指定其value

spring cloud feign 使用 Apache HttpClient

问题:1 没有指定 Content-Type 是情况下 会出现如下异常 ? 这里很纳闷?

1 Caused by: java.lang.IllegalArgumentException: MIME type may not contain reserved characters

在这里有兴趣的朋友可以去研究下源码

 1 ApacheHttpClient.class
 2   private ContentType getContentType(Request request) {
 3     ContentType contentType = ContentType.DEFAULT_TEXT;
 4     for (Map.Entry<String, Collection<String>> entry : request.headers().entrySet())
 5     // 这里会判断 如果没有指定 Content-Type 属性 就使用上面默认的 text/plain; charset=ISO-8859-1
 6     // 问题出在默认的 contentType : 格式 text/plain; charset=ISO-8859-1
 7     // 转到 ContentType.create(entry.getValue().iterator().next(), request.charset()); 方法中看
 8     if (entry.getKey().equalsIgnoreCase("Content-Type")) {
 9       Collection values = entry.getValue();
10       if (values != null && !values.isEmpty()) {
11         contentType = ContentType.create(entry.getValue().iterator().next(), request.charset());
12         break;
13       }
14     }
15     return contentType;
16   }
 1 ContentType.class
 2    public static ContentType create(final String mimeType, final Charset charset) {
 3         final String normalizedMimeType = Args.notBlank(mimeType, "MIME type").toLowerCase(Locale.ROOT);
 4     // 问题在这 check  中 valid f方法中
 5         Args.check(valid(normalizedMimeType), "MIME type may not contain reserved characters");
 6         return new ContentType(normalizedMimeType, charset);
 7     }
 8    private static boolean valid(final String s) {
 9         for (int i = 0; i < s.length(); i++) {
10             final char ch = s.charAt(i);
11         // 这里 在上面 text/plain;charset=UTF-8 中出现了 分号 导致检验没有通过
12             if (ch == ‘"‘ || ch == ‘,‘ || ch == ‘;‘) {
13                 return false;
14             }
15         }
16         return true;
17     }

解决办法 :

@RequestMapping(value = "/generate/password", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)

注解中指定: Content-Type  即 指定 consumes 的属性值 : 这里 consumes 属性的值在这不做具体讲解,有兴趣的可以去研究下

暂时遇到以上问题, 后续深入学习 时 如有问题 会及时更新, 希望对你有帮助 谢谢

时间: 2024-08-03 15:24:48

spring cloud 使用feign 遇到问题的相关文章

spring cloud 之 Feign 使用HTTP请求远程服务

一.Feign 简介 在spring Cloud Netflix栈中,各个微服务都是以HTTP接口的形式暴露自身服务的,因此在调用远程服务时就必须使用HTTP客户端.我们可以使用JDK原生的URLConnection.Apache的Http Client.Netty的异步HTTP Client, Spring的RestTemplate.但是,用起来最方便.最优雅的还是要属Feign了. Feign是一种声明式.模板化的HTTP客户端.在Spring Cloud中使用Feign, 我们可以做到使用

解决Spring Cloud中Feign/Ribbon第一次请求失败的方法

前言 在Spring Cloud中,Feign和Ribbon在整合了Hystrix后,可能会出现首次调用失败的问题,要如何解决该问题呢? 造成该问题的原因 Hystrix默认的超时时间是1秒,如果超过这个时间尚未响应,将会进入fallback代码.而首次请求往往会比较慢(因为Spring的懒加载机制,要实例化一些类),这个响应时间可能就大于1秒了.知道原因后,我们来总结一下解决放你. 解决方案有三种,以feign为例. 方法一 ? 1 hystrix.command.default.execut

Spring Cloud 之 Feign

新建Spring Boot工程,命名为feign 1.pom.xml添加依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=&quo

spring cloud学习--Feign

Feign简介: Feign是一个声明式的Web服务客户端,使用Feign可使得Web服务客户端的写入更加方便.它具有可插拔注释支持,包括Feign注解和JAX-RS注解.Feign还支持可插拔编码器和解码器.Spring Cloud增加了对Spring MVC注释的支持,并HttpMessageConverters在Spring Web中使用了默认使用的相同方式.Spring Cloud集成了Ribbon和Eureka,在使用Feign时提供负载平衡的http客户端.Fegin对Robbin进

Spring Cloud之Feign客户端调用工具

feign介绍 Feign客户端是一个web声明式http远程调用工具,提供了接口和注解方式进行调用. Spring Cloud 支持 RestTemplate  Fetin Feign客户端实际开发中用的最多 ,易读性比较强. 主要调用部分: pom: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

Spring Cloud之Feign

资料来源: <<重新定义Spring Cloud>>以及博客和官网 源码地址:https://gitee.com/08081/hello-springcloud 什么是feign ? fegin是一种声明式 模板化的HTTP客户端(仅在consumer中使用) 1. 什么是声明式呢? 声明式调用就像调用本地方法一样调用远程方法,无感知远程http请求 Spring Cloud的声明式调用,可以做到使用HTTP请求远程服务时就像调用本地方法一样的体验,开发者完全感知不到这是远程调用,

解决Spring Cloud中Feign第一次请求失败的问题

在Spring Cloud中,Feign和Ribbon在整合了Hystrix后,可能会出现首次调用失败的问题 com.netflix.hystrix.exception.HystrixTimeoutException: null at com.netflix.hystrix.AbstractCommand$HystrixObservableTimeoutOperator$1.run(AbstractCommand.java:1142) ~[hystrix-core-1.5.18.jar:1.5.

spring cloud之Feign的使用

原始的调用客户端的方式是通过注入restTemplate的方式 restTemplate.getForObject("http://CLIENT/hello", String.class) 通过feign的方式 配置消费者项目cloud-consume pom.xml 依赖jar <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-clo

Spring Cloud之Feign 转发请求头(header参数)

在做接口请求时,我们经常会在header头中增加一些鉴权信息,如token 或 jwt,那么在通过fegin从A server去调用B server的接口时,如果B server的接口需要header信息,我们需要将A sever获取的header转发到B上. 解决方式 我们需要实现Feign提供的一个接口RequestInterceptor @Configuration public class FeignConfiguration implements RequestInterceptor{