7

我目前正在检查如何在我的一个项目中使用 Feign 作为声明式 REST 客户端。

以下是 Feign 客户端

@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>

在为我的 feign 客户端调试 spring create 代理时,我可以看到我尝试在功能区的帮助下调用此服务,它工作正常,但是当我在我的 feign 客户端中执行方法定义时。我得到feign.FeignException: 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]
4

5 回答 5

5

您需要创建一个feign.ErrorDecoder. 请阅读此内容以获取更多信息 https://github.com/spring-cloud/spring-cloud-openfeign/issues/118

您还可以添加decode404=trueFeignClient 注释定义。

于 2019-04-08T15:57:06.000 回答
2

您是否定义了使用此 feign 客户端访问的服务的 url?404 是未找到的问题。

  @FeignClient(name = "SEARCHCABMS", url = "${SEARCHCABMS.service.url}", 
  configuration = ClientConfiguration.class)
  public interface SearchCabMsClient {

  }

在 url 中有一个引用持有者有助于将其外部化并为每个环境进行配置。

如果您确实配置了 URL,请仔细检查其余端点。

于 2017-04-07T17:57:21.877 回答
1

大多数答案都建议将url属性放在@FeignClient,但是当架构涉及向Eureka Server注册服务( Eureka Client)时,我认为没有使用该属性的自由。 必须使用service-ideureka-id来进行 rest 调用。 所以,随着,url


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}")

希望这可以帮助有类似问题的人。

于 2019-12-28T11:33:50.553 回答
0

如果您已设置 server.servlet.context-path:##some name##,请将其删除并设置在 EUREKA 服务器主页中显示的应用程序名称。

于 2020-07-23T17:59:58.743 回答
0

我有同样的问题。这是一个路由问题。只需在 FeignClient 的 @RequestMapping 路径的开头添加 / 即可。此外,路径变量未在路径中定义。我建议将它们定义为@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);
}
于 2019-07-12T07:07:25.160 回答