gpt4 book ai didi

java - Spring FeignClient 回退未调用但进入异常

转载 作者:行者123 更新时间:2023-11-29 04:19:34 25 4
gpt4 key购买 nike

我在根据文档尝试 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] 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 的想法。

我是否使用了这个错误或者我错过了什么?

最佳答案

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

应用程序.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);
}
}

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!";
}
}
}

关于java - Spring FeignClient 回退未调用但进入异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50219164/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com