努力让SpringCloud Feign与外部服务协同工作
Struggling to get SpringCloud Feign to work with external services
使用 Feign 访问我在 Eureka 上注册的服务是一件轻而易举的事。我正在尝试使用 Feign 访问外部服务并在基础上苦苦挣扎。
我正在使用 Bluemix 上的一项服务,但是为了简化手头的问题,我使用的是一个简单的服务。
我的代理显示如下:
//@FeignClient(name = "country-service-client", url = "https://country.io")
@FeignClient(name = "another-country-service-client", url = "http://restcountries.eu/rest/v2/name/Australia")
public interface SimpleServiceProxy {
//This one works
@RequestMapping(method = RequestMethod.GET, value = "/names.json", produces = "application/json")
String getCountries();
//This one does not work... This is used in conjunction where the url in the Fiegn Client annotation reads as - http://restcountries.eu/rest/v2
@RequestMapping(method = RequestMethod.GET, value = "/name/{country}", produces = "application/json")
public String getCountryInfo(@PathVariable("country") String country);
//This one doesn't work either
//@RequestMapping(method = RequestMethod.GET, value = "/name/Australia", produces = "application/json")
public String getCountryInfoHardcodedWithinMethod();
}
//This works however I would want to pass parameters and path variables to the URL
@RequestMapping(method = RequestMethod.GET, produces = "application/json")
public String getCountryInfoHardcodedAtFeignClientAnnotation();
}
我尝试了一些变体(参见上面的代码),最后一个 URL 在 Feign Client 注释中被硬编码。其他人抛出 TimeoutException。
java.util.concurrent.TimeoutException: null
at com.netflix.hystrix.AbstractCommand.handleTimeoutViaFallback(AbstractCommand.java:958) ~[hystrix-core-1.5.3.jar:1.5.3]
at com.netflix.hystrix.AbstractCommand.access0(AbstractCommand.java:59) ~[hystrix-core-1.5.3.jar:1.5.3]
at com.netflix.hystrix.AbstractCommand.call(AbstractCommand.java:573) ~[hystrix-core-1.5.3.jar:1.5.3]
at com.netflix.hystrix.AbstractCommand.call(AbstractCommand.java:565) ~[hystrix-core-1.5.3.jar:1.5.3]
at rx.internal.operators.OperatorOnErrorResumeNextViaFunction.onError(OperatorOnErrorResumeNextViaFunction.java:139) ~[rxjava-1.1.5.jar:1.1.5]
at rx.internal.operators.OperatorDoOnEach.onError(OperatorDoOnEach.java:71) ~[rxjava-1.1.5.jar:1.1.5]
at rx.internal.operators.OperatorDoOnEach.onError(OperatorDoOnEach.java:71) ~[rxjava-1.1.5.jar:1.1.5]
at com.netflix.hystrix.AbstractCommand$HystrixObservableTimeoutOperator.run(AbstractCommand.java:1099) ~[hystrix-core-1.5.3.jar:1.5.3]
at com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable.call(HystrixContextRunnable.java:41) ~[hystrix-core-1.5.3.jar:1.5.3]
at com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable.call(HystrixContextRunnable.java:37) ~[hystrix-core-1.5.3.jar:1.5.3]
at com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable.run(HystrixContextRunnable.java:57) ~[hystrix-core-1.5.3.jar:1.5.3]
at com.netflix.hystrix.AbstractCommand$HystrixObservableTimeoutOperator.tick(AbstractCommand.java:1116) ~[hystrix-core-1.5.3.jar:1.5.3]
at com.netflix.hystrix.util.HystrixTimer.run(HystrixTimer.java:99) ~[hystrix-core-1.5.3.jar:1.5.3]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) ~[na:1.8.0_112]
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308) ~[na:1.8.0_112]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access1(ScheduledThreadPoolExecutor.java:180) ~[na:1.8.0_112]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294) ~[na:1.8.0_112]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) ~[na:1.8.0_112]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) ~[na:1.8.0_112]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_112]
我很困惑,想弄明白。在尝试找出 PathVariables 不工作的原因之前,我想让硬编码方法工作。
我错过了什么? (或者这里做错了)?
我刚刚尝试了这个简单的应用程序,它对我来说运行良好
Dalston.RC1
@SpringBootApplication
@EnableFeignClients
@RestController
public class DemoApplication {
@Autowired
SimpleServiceProxy proxy;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@RequestMapping("/")
public String getCountry() {
return proxy.getCountryInfo("Australia");
}
}
@FeignClient(name = "another-country-service-client", url ="http://restcountries.eu/rest/v2")
interface SimpleServiceProxy {
@RequestMapping(method = RequestMethod.GET, value = "/name/{country}", produces = "application/json")
public String getCountryInfo(@PathVariable("country") String country);
}
您问题中的异常是在发出请求时通过 Hystrix 指示超时。您可以尝试禁用 Hystrix 并使用 feign.hystrix.enabled=false
.
查看它是否消失
使用 Feign 访问我在 Eureka 上注册的服务是一件轻而易举的事。我正在尝试使用 Feign 访问外部服务并在基础上苦苦挣扎。
我正在使用 Bluemix 上的一项服务,但是为了简化手头的问题,我使用的是一个简单的服务。
我的代理显示如下:
//@FeignClient(name = "country-service-client", url = "https://country.io")
@FeignClient(name = "another-country-service-client", url = "http://restcountries.eu/rest/v2/name/Australia")
public interface SimpleServiceProxy {
//This one works
@RequestMapping(method = RequestMethod.GET, value = "/names.json", produces = "application/json")
String getCountries();
//This one does not work... This is used in conjunction where the url in the Fiegn Client annotation reads as - http://restcountries.eu/rest/v2
@RequestMapping(method = RequestMethod.GET, value = "/name/{country}", produces = "application/json")
public String getCountryInfo(@PathVariable("country") String country);
//This one doesn't work either
//@RequestMapping(method = RequestMethod.GET, value = "/name/Australia", produces = "application/json")
public String getCountryInfoHardcodedWithinMethod();
}
//This works however I would want to pass parameters and path variables to the URL
@RequestMapping(method = RequestMethod.GET, produces = "application/json")
public String getCountryInfoHardcodedAtFeignClientAnnotation();
}
我尝试了一些变体(参见上面的代码),最后一个 URL 在 Feign Client 注释中被硬编码。其他人抛出 TimeoutException。
java.util.concurrent.TimeoutException: null
at com.netflix.hystrix.AbstractCommand.handleTimeoutViaFallback(AbstractCommand.java:958) ~[hystrix-core-1.5.3.jar:1.5.3]
at com.netflix.hystrix.AbstractCommand.access0(AbstractCommand.java:59) ~[hystrix-core-1.5.3.jar:1.5.3]
at com.netflix.hystrix.AbstractCommand.call(AbstractCommand.java:573) ~[hystrix-core-1.5.3.jar:1.5.3]
at com.netflix.hystrix.AbstractCommand.call(AbstractCommand.java:565) ~[hystrix-core-1.5.3.jar:1.5.3]
at rx.internal.operators.OperatorOnErrorResumeNextViaFunction.onError(OperatorOnErrorResumeNextViaFunction.java:139) ~[rxjava-1.1.5.jar:1.1.5]
at rx.internal.operators.OperatorDoOnEach.onError(OperatorDoOnEach.java:71) ~[rxjava-1.1.5.jar:1.1.5]
at rx.internal.operators.OperatorDoOnEach.onError(OperatorDoOnEach.java:71) ~[rxjava-1.1.5.jar:1.1.5]
at com.netflix.hystrix.AbstractCommand$HystrixObservableTimeoutOperator.run(AbstractCommand.java:1099) ~[hystrix-core-1.5.3.jar:1.5.3]
at com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable.call(HystrixContextRunnable.java:41) ~[hystrix-core-1.5.3.jar:1.5.3]
at com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable.call(HystrixContextRunnable.java:37) ~[hystrix-core-1.5.3.jar:1.5.3]
at com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable.run(HystrixContextRunnable.java:57) ~[hystrix-core-1.5.3.jar:1.5.3]
at com.netflix.hystrix.AbstractCommand$HystrixObservableTimeoutOperator.tick(AbstractCommand.java:1116) ~[hystrix-core-1.5.3.jar:1.5.3]
at com.netflix.hystrix.util.HystrixTimer.run(HystrixTimer.java:99) ~[hystrix-core-1.5.3.jar:1.5.3]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) ~[na:1.8.0_112]
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308) ~[na:1.8.0_112]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access1(ScheduledThreadPoolExecutor.java:180) ~[na:1.8.0_112]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294) ~[na:1.8.0_112]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) ~[na:1.8.0_112]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) ~[na:1.8.0_112]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_112]
我很困惑,想弄明白。在尝试找出 PathVariables 不工作的原因之前,我想让硬编码方法工作。
我错过了什么? (或者这里做错了)?
我刚刚尝试了这个简单的应用程序,它对我来说运行良好
Dalston.RC1
@SpringBootApplication
@EnableFeignClients
@RestController
public class DemoApplication {
@Autowired
SimpleServiceProxy proxy;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@RequestMapping("/")
public String getCountry() {
return proxy.getCountryInfo("Australia");
}
}
@FeignClient(name = "another-country-service-client", url ="http://restcountries.eu/rest/v2")
interface SimpleServiceProxy {
@RequestMapping(method = RequestMethod.GET, value = "/name/{country}", produces = "application/json")
public String getCountryInfo(@PathVariable("country") String country);
}
您问题中的异常是在发出请求时通过 Hystrix 指示超时。您可以尝试禁用 Hystrix 并使用 feign.hystrix.enabled=false
.