Spring Cloud Feignのステータスコードと例外
Spring Cloud Feignの例外を調べてみたのでメモします。
(11/22修正)Root Causeのスタックトレースしか貼り付けていなかったので、すべてのCauseに修正しました。
結果
- エラーのステータスコードを受け取った場合、
feign.FeignException
が発生します。 - 接続できなかった場合、
feign.RetryableException
が発生します。 - TCP接続確立やレスポンス受信に時間がかかった場合、Hystrixがタイムアウトします。
前提
Feign、Sleuthを有効にした場合で調査しています。
// build.gradle dependencies { compile 'org.springframework.boot:spring-boot-starter-web' compile 'org.springframework.cloud:spring-cloud-starter-feign:1.2.1.RELEASE' }
依存関係にspring-cloud-starter-feignを追加すると、自動的にHystrixも有効になります。
Hystrixが有効な場合、Feignの例外は HystrixRuntimeException
でラップされます。Hystrixのエラーハンドリングについては下記を参照してください。
https://github.com/Netflix/Hystrix/wiki/How-To-Use#hystrixobservablecommand-equivalent-4
Hystrixのタイムアウトは execution.isolation.thread.timeoutInMilliseconds
で変更できます。
hystrix.command.default: execution.isolation.thread.timeoutInMilliseconds: 3000
エラーのステータスコードを受け取った場合
APIが200番台以外のステータスコードを返した場合、 feign.FeignException
が発生します。
Caused by: com.netflix.hystrix.exception.HystrixRuntimeException: HelloClient#hello() failed and no fallback available. at com.netflix.hystrix.AbstractCommand$16.call(AbstractCommand.java:806) (中略) at com.netflix.hystrix.strategy.concurrency.HystrixContexSchedulerAction.call(HystrixContexSchedulerAction.java:69) at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745) Caused by: feign.FeignException: status 500 reading HelloClient#hello(); content: {"message":"HelloClient#hello() failed and no fallback available."} at feign.FeignException.errorStatus(FeignException.java:62) at feign.codec.ErrorDecoder$Default.decode(ErrorDecoder.java:91) at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:138) at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:76) at feign.hystrix.HystrixInvocationHandler$1.run(HystrixInvocationHandler.java:108) at com.netflix.hystrix.HystrixCommand$1.call(HystrixCommand.java:294) ... 23 more
接続できなかった場合
APIに接続できなかった場合、 feign.RetryableException
が発生します。
Caused by: com.netflix.hystrix.exception.HystrixRuntimeException: HelloClient#hello() failed and no fallback available. at com.netflix.hystrix.AbstractCommand$16.call(AbstractCommand.java:806) (中略) at com.netflix.hystrix.strategy.concurrency.HystrixContexSchedulerAction.call(HystrixContexSchedulerAction.java:69) at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745) Caused by: feign.RetryableException: Connection refused executing GET http://localhost:8082/hello at feign.FeignException.errorExecuting(FeignException.java:67) at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:104) at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:76) at feign.hystrix.HystrixInvocationHandler$1.run(HystrixInvocationHandler.java:108) at com.netflix.hystrix.HystrixCommand$1.call(HystrixCommand.java:294) ... 23 more Caused by: java.net.ConnectException: Connection refused at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) at java.net.Socket.connect(Socket.java:589) at sun.net.NetworkClient.doConnect(NetworkClient.java:175) at sun.net.www.http.HttpClient.openServer(HttpClient.java:432) at sun.net.www.http.HttpClient.openServer(HttpClient.java:527) at sun.net.www.http.HttpClient.<init>(HttpClient.java:211) at sun.net.www.http.HttpClient.New(HttpClient.java:308) at sun.net.www.http.HttpClient.New(HttpClient.java:326) at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1169) at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1105) at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:999) at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:933) at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1513) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441) at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480) at feign.Client$Default.convertResponse(Client.java:152) at feign.Client$Default.execute(Client.java:74) at org.springframework.cloud.sleuth.instrument.web.client.feign.TraceFeignClient.execute(TraceFeignClient.java:79) at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:97) ... 26 more
Hystrixのタイムアウト
TCP接続確立やレスポンス受信に時間がかかった場合、Hystrixがタイムアウトします。
Caused by: com.netflix.hystrix.exception.HystrixRuntimeException: HelloClient#waitMillis(long) timed-out and no fallback available. at com.netflix.hystrix.AbstractCommand$16.call(AbstractCommand.java:806) (中略) at com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable.run(HystrixContextRunnable.java:57) at com.netflix.hystrix.AbstractCommand$HystrixObservableTimeoutOperator$2.tick(AbstractCommand.java:976) at com.netflix.hystrix.util.HystrixTimer$1.run(HystrixTimer.java:100) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745) Caused by: java.util.concurrent.TimeoutException at com.netflix.hystrix.AbstractCommand$9.call(AbstractCommand.java:601) at com.netflix.hystrix.AbstractCommand$9.call(AbstractCommand.java:581) at rx.internal.operators.OperatorOnErrorResumeNextViaFunction$4.onError(OperatorOnErrorResumeNextViaFunction.java:140) ... 16 more