Spring FeignClient 回退未调用但出现异常

Spring FeignClient fallback not called but goes to exception

我在尝试基于文档的 feignclient 回退时遇到问题。

假设 myFeignClient 无法连接到 myFeign

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

我的后备 class 是这样的:

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

        return “Error";
    }

}

它没有调用后备方法,而是直接失败了:

2018-05-07 15:19:48.052 ERROR 41592 --- [nio-8081-exec-6] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.RuntimeException: com.netflix.client.ClientException: Load balancer does not have available server for client: myFeign] with root cause

com.netflix.client.ClientException: Load balancer does not have available server for client: myFeign

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

是我用错了还是漏掉了什么?

请在您的配置文件中启用hystrix application.yml,默认为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!";
        }
    }
}