我目前正在检查如何在我的一个项目中使用Feign作为声明式REST客户端。
下面是Feign Client
@FeignClient(name = "SEARCHCABMS",configuration = AppFeignConfiguration.class)
public interface SearchCabMsClient {
@RequestMapping(value = "api/searchcab/locationcabtimedetail/search/getCabForLocationAfterTimeSlot", method = RequestMethod.GET)
String searchCabDetails(@PathVariable("fromDate") String fromDate,
@PathVariable("locationId") long locationId,
@PathVariable("isdeleted") byte isdeleted,
@PathVariable("hourforbooking")int hourforbooking);
}
此接口在一个服务中自动连接
@Autowired
SearchCabMsClient restService;
添加EnableFeignClients到SpringBootApplication
@EnableFeignClients(basePackages = {"com.gp.cabbooking.services.feign"})
pom.xml中的依赖项、父项等
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
</parent>
依赖<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
我可以看到,而调试弹簧创建代理为我的假客户端我试着在ribbon的帮助下调用这个服务,它工作得很好但是当我在伪客户机中执行方法定义时。我开始假装了。feigexception: status 404 reading
feign.FeignException: status 404 reading SearchCabMsClient#searchCabDetails(String,long,byte,int)
at feign.FeignException.errorStatus(FeignException.java:62) ~[feign-core-8.16.2.jar:8.16.2]
at feign.codec.ErrorDecoder$Default.decode(ErrorDecoder.java:91) ~[feign-core-8.16.2.jar:8.16.2]
at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:134) ~[feign-core-8.16.2.jar:8.16.2]
at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:76) ~[feign-core-8.16.2.jar:8.16.2]
at feign.hystrix.HystrixInvocationHandler$1.run(HystrixInvocationHandler.java:97) ~[feign-hystrix-8.16.2.jar:8.16.2]
at com.netflix.hystrix.HystrixCommand$1.call(HystrixCommand.java:293) ~[hystrix-core-1.5.3.jar:1.5.3]
at com.netflix.hystrix.HystrixCommand$1.call(HystrixCommand.java:289) ~[hystrix-core-1.5.3.jar:1.5.3]
at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:46) ~[rxjava-1.1.5.jar:1.1.5]
at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:35) ~[rxjava-1.1.5.jar:1.1.5]
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:50) ~[rxjava-1.1.5.jar:1.1.5]
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30) ~[rxjava-1.1.5.jar:1.1.5]
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:50) ~[rxjava-1.1.5.jar:1.1.5]
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30) ~[rxjava-1.1.5.jar:1.1.5]
at rx.Observable.unsafeSubscribe(Observable.java:8460) ~[rxjava-1.1.5.jar:1.1.5]
at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:51) ~[rxjava-1.1.5.jar:1.1.5]
at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:35) ~[rxjava-1.1.5.jar:1.1.5]
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:50) ~[rxjava-1.1.5.jar:1.1.5]
需要创建一个feign.ErrorDecoder
。请阅读这篇文章了解更多信息https://github.com/spring-cloud/spring-cloud-openfeign/issues/118
您还可以在FeignClient注释定义中添加decode404=true
。
您是否定义了使用此虚拟客户端所访问的服务的url ?404是未发现的问题。
@FeignClient(name = "SEARCHCABMS", url = "${SEARCHCABMS.service.url}",
configuration = ClientConfiguration.class)
public interface SearchCabMsClient {
}
在url中使用引用持有人有助于将其外部化并针对每个环境进行配置。
如果您配置了URL,则检查其余端点
我也遇到了同样的问题。这是一个路由问题。只需在FeignClient的@RequestMapping path中开始添加/。此外,路径变量不在路径中定义。我建议将它们定义为@RequestParam。那么路径是正确的,除了/在开始。
@FeignClient(name = "SEARCHCABMS",configuration = AppFeignConfiguration.class)
public interface SearchCabMsClient {
@RequestMapping(value = "/api/searchcab/locationcabtimedetail/search/getCabForLocationAfterTimeSlot/{fromDate}/{locationId}/{isdeleted}/{hourforbooking}", method = RequestMethod.GET)
String searchCabDetails(@PathVariable("fromDate") String fromDate,
@PathVariable("locationId") long locationId,
@PathVariable("isdeleted") byte isdeleted,
@PathVariable("hourforbooking")int hourforbooking);
}
大多数答案建议将url
属性放在@FeignClient
,但是当架构涉及到注册服务(Eureka Client)与Eureka Server时,我认为,没有使用url
属性的自由。
必须使用service-id或eureka-id用于rest调用。
对于client-side discovery architecture
,
@FeignClient(name = "service-id", url = "${service.url}", configuration=SomeConfig.class)
可能不再是
的可伸缩解决方案。status 404 reading IFeignClient#someControllerMethod()
错误。
我在client-side discovery architecture
应用程序中遇到了同样的错误。问题是application.yml
文件中的这个条目:
server:
servlet:
context-path: /myapp
删除它有助于通过正确的url
解决问题。我的工作应用程序的确切声明:@FeignClient(name="${service-id}")
希望这能帮助到有类似问题的人。
如果您已经设置了server.servlet。上下文路径:##一些名称##,删除它并设置应用程序名称显示在EUREKA服务器的主页