【问题标题】:Spring FeignClient fallback not called but goes to exceptionSpring FeignClient 回退未调用但进入异常
【发布时间】:2018-10-17 13:15:44
【问题描述】:

我在根据文档尝试 feignclient 回退时遇到问题。

假设 myFeignClient 无法连接到 myFeign

@FeignClient(name = "myFeign", fallback = MyFeignClientFallback.class)
public interface MyFeignClient {
    @PostMapping(“/test")
    Object test(@RequestParam(“param1") String param1);
}

我的后备类是这样的:

@Component
public class MyFeignClientFallback implements MyFeignClient {
    public Object test(@RequestParam(“param1”) String param1) {

        return “Error";
    }

}

它没有调用回退方法,而是彻底失败了:

2018-05-07 15:19:48.052 错误 41592 --- [nio-8081-exec-6] oaccC[.[.[/].[dispatcherServlet]:Servlet.service() 用于 servlet [dispatcherServlet]在路径 [] 的上下文中抛出异常 [请求处理失败;嵌套异常是 java.lang.RuntimeException: com.netflix.client.ClientException: Load balancer does not have available server for client: myFeign] 根本原因

com.netflix.client.ClientException:负载均衡器没有可用于客户端的服务器:myFeign

我已经让我的假客户工作了。当我遇到这个问题时,我正在尝试使用 Hystrix 的想法。

我是用错了还是错过了什么?

【问题讨论】:

  • 这是功能区错误,不是假装的。你在用@EnableDiscoveryClient吗?如果,myFeign 是注册客户吗?否则你配置了listOfServers
  • @spencergibb 看起来你来晚了。我们快到这里了。 :)

标签: java spring-boot spring-cloud


【解决方案1】:

请在你的配置文件application.yml中启用hystrix,默认为false,该功能不起作用。

build.gradle

buildscript {
    ext {
        springBootVersion = '2.0.1.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

bootJar {
    launchScript ()
}

archivesBaseName = 'framework' 
version = '1.0'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
    maven { url "https://repo.spring.io/milestone" }
}

dependencies {
    compile('org.springframework.boot:spring-boot-starter-actuator')
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client')
    compile('org.springframework.cloud:spring-cloud-starter-openfeign')

    testCompile('org.springframework.boot:spring-boot-starter-test')
}

ext {
    springCloudVersion = 'Finchley.RC1'
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

application.yml

eureka:
  client:
    serviceUrl:
      defaultZone: //...
  instance:
    preferIpAddress: true 

feign:
  hystrix:
    enabled: true

Application.java

@EnableDiscoveryClient
@SpringBootApplication
@EnableFeignClients
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

FrameworkHelloService.java

package framework.root.service;

import framework.root.service.FrameworkHelloService.HelloServiceFallback;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;

@FeignClient(name = "framework", fallback = HelloServiceFallback.class)
public interface FrameworkHelloService {
    @GetMapping("/api/hello")
    public String hello();

    @GetMapping("/api/exception")
    public void exception();

    @GetMapping("/none")
    public String none();

    @Component
    class HelloServiceFallback implements FrameworkHelloService {
        @Override
        public String hello() {
            return null;
        }

        @Override
        public void exception() { }

        @Override
        public String none() {
            return "Fallback!";
        }
    }
}

【讨论】:

  • 不再支持该键。 @EnableFeignClients 已经这样做了。
  • 我最近刚刚解决了这个问题。我正在使用 spring boot 2.0.1.RELEASE 和 Cloud Finchley.RC1。这个方法真的很管用。当然我之前已经添加了注释。你确定它不起作用吗?如果是这样,我可以给你看一个演示。
  • 显示你的 build.gradle 文件,因为无论格式如何,spring 都无法识别该键。
  • 在对数据交换进行了一些摸索之后,我能够让它工作。毕竟它是 feign.hystrx.enabled 的。不知道为什么它告诉我它无法识别。我暂时不理会它,但是在使用该密钥之后,相关的错误开始出现。这只是满足前进中缺少的东西的问题。谢谢!
猜你喜欢
  • 1970-01-01
  • 2021-02-03
  • 2017-03-14
  • 2021-11-13
  • 2020-10-29
  • 1970-01-01
  • 1970-01-01
  • 2017-03-29
  • 2022-07-22
相关资源
最近更新 更多