• 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 属性的值在这不做具体讲解,有兴趣的可以去研究下

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

  • 相关阅读:
    Java学习小记 29
    Java学习小记 28
    Java学习小记 27
    Java学习小记 26
    Java学习小记 25
    Java学习小记 24
    Java学习小记 23
    Java学习小记 22
    Java学习小记 21
    Java学习小记 20
  • 原文地址:https://www.cnblogs.com/yueli/p/7478305.html
Copyright © 2020-2023  润新知