Spring 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] O.A.C.C.C.C.[.[.[/].[dispatcherServlet] : Servlet.service(( for servlet [dispatcherServlet] 在上下文中与路径 [] 抛出异常 [请求处理失败;嵌套异常是 java.lang.RuntimeException: com.netflix.client.ClientException: 负载均衡器没有可用于客户端的服务器: myFeign] 与根本原因

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

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

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

请在配置文件 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}"
}
}

应用程序.yml

eureka:
client:
serviceUrl:
defaultZone: //...
instance:
preferIpAddress: true 
feign:
hystrix:
enabled: true

应用.java

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

框架你好服务.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!";
}
}
}

相关内容

最新更新