0

使用Feign访问我在Eureka上注册的服务是轻而易举的。我正在尝试使用Feign来访问外部服务,并在基础上挣扎。努力让SpringCloud 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是在假死客户注释作品硬编码。其他人抛出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.access$400(AbstractCommand.java:59) ~[hystrix-core-1.5.3.jar:1.5.3] 
    at com.netflix.hystrix.AbstractCommand$11.call(AbstractCommand.java:573) ~[hystrix-core-1.5.3.jar:1.5.3] 
    at com.netflix.hystrix.AbstractCommand$11.call(AbstractCommand.java:565) ~[hystrix-core-1.5.3.jar:1.5.3] 
    at rx.internal.operators.OperatorOnErrorResumeNextViaFunction$4.onError(OperatorOnErrorResumeNextViaFunction.java:139) ~[rxjava-1.1.5.jar:1.1.5] 
    at rx.internal.operators.OperatorDoOnEach$1.onError(OperatorDoOnEach.java:71) ~[rxjava-1.1.5.jar:1.1.5] 
    at rx.internal.operators.OperatorDoOnEach$1.onError(OperatorDoOnEach.java:71) ~[rxjava-1.1.5.jar:1.1.5] 
    at com.netflix.hystrix.AbstractCommand$HystrixObservableTimeoutOperator$1.run(AbstractCommand.java:1099) ~[hystrix-core-1.5.3.jar:1.5.3] 
    at com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable$1.call(HystrixContextRunnable.java:41) ~[hystrix-core-1.5.3.jar:1.5.3] 
    at com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable$1.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$2.tick(AbstractCommand.java:1116) ~[hystrix-core-1.5.3.jar:1.5.3] 
    at com.netflix.hystrix.util.HystrixTimer$1.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.access$301(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不起作用之前,先让硬编码的方法工作。

我错过了什么? (或在这里做错了)?

回答

1

我只是尝试这种简单的应用程序,它使用 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并查看是否使用feign.hystrix.enabled=false消失。

+0

Ryan,谢谢。我在使用Brixton.SR6和1.4.5版本的弹簧引导。我相信feign.hystrix.enabled不可用(STS不会在代码辅助中显示)。 – Manglu

+0

使用1.4.5(或1.5.1或1.5.2)与Dalston.RC1引发了一些maven错误 - 一个示例错误是:对于工件{org.springframework.cloud:spring-cloud-starter-feign:null :jar}:版本不能为空。 (org.apache.maven.plugins:maven-resources-plugin:2.6:resources:default-resources:process-resources) – Manglu

+0

仅仅因为STS在它的自动完成中没有这个监听并不意味着它是无效的。请参阅Brixton文档http://cloud.spring.io/spring-cloud-static/Brixton.SR7/#spring-cloud-feign-hystrix。 我建议你至少考虑升级到'Brixton.SR7',如果不是最新的卡姆登版本。就Dalston错误而言,请在SO上打开另一个问题/问题。 –