Cha*_*had 1 java spring-boot spring-cloud
我在尝试基于文档的 feignclient 回退时遇到问题。
假设 myFeignClient 无法连接到 myFeign
@FeignClient(name = "myFeign", fallback = MyFeignClientFallback.class)
public interface MyFeignClient {
@PostMapping(“/test")
Object test(@RequestParam(“param1") String param1);
}
Run Code Online (Sandbox Code Playgroud)
我的后备类是这样的:
@Component
public class MyFeignClientFallback implements MyFeignClient {
public Object test(@RequestParam(“param1”) String param1) {
return “Error";
}
}
Run Code Online (Sandbox Code Playgroud)
它没有调用回退方法,而是彻底失败:
2018-05-07 15:19:48.052 错误 41592 --- [nio-8081-exec-6] oaccC[.[.[/].[dispatcherServlet] : Servlet.service() for 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 的想法。
我是否错误地使用了它或我错过了什么?
请在你的配置文件application.yml中启用hystrix,默认为false,该功能将不起作用。
构建.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}"
}
}
Run Code Online (Sandbox Code Playgroud)
应用程序.yml
eureka:
client:
serviceUrl:
defaultZone: //...
instance:
preferIpAddress: true
feign:
hystrix:
enabled: true
Run Code Online (Sandbox Code Playgroud)
应用程序.java
@EnableDiscoveryClient
@SpringBootApplication
@EnableFeignClients
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
框架HelloService.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!";
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7238 次 |
| 最近记录: |