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 屬性的值在這不做具體講解,有興趣的可以去研究下

 暫時遇到以上問題, 后續深入學習 時 如有問題 會及時更新, 希望對你有幫助 謝謝

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM