我在根据文档尝试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 ERROR 41592 --- [nio-8081-exec-6] oaccC [。[。[/]。[dispatcherServlet]:Servlet的Servlet.service()[dispatcherServlet]在path []的上下文中抛出异常[请求处理失败;嵌套异常是java.lang.RuntimeException:com.netflix.client.ClientException:负载均衡器没有客户端的可用服务器:myFeign]有根本原因
com.netflix.client.ClientException:负载均衡器没有客户端的可用服务器:myFeign
我已经让我的假装客户工作了。当我遇到这个问题时,我正在尝试使用Hystrix的想法。
我是否错误地使用了这个或者我错过了什么?
答案 0 :(得分:2)
请在配置文件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!";
}
}
}