Java Feign client Fallback - 无法调用 fallback
Java Feign client Fallback - can't call fallback
我正在尝试通过 Java 中 Feign 客户端 的工作来理解并调用 Fallback 以防万一错误,我遇到了一些困难。
我写了最简单的例子来理解它是如何工作的,但我似乎很困惑。
请帮助我了解我的错误所在。
我还在 github 上发布了这个例子以方便使用:
https://github.com/ksereda/feign-fallback-example
client通过Feign调用service-one,接收字符串"Get String from SERVICE-ONE".
сlient也是通过Feign调用service-two得到字符串"Get String from SERVICE-TWO".
先决条件:
在 service-two 不可用的情况下,client 必须调用 Fallback class,其中调用 service-three 被指示而不是 "Get String from SERVICE-TWO",client 应该得到 "Get String from SERVICE-THREE" 来自 服务三.
service-two和service-three是一样的,只是return个字符串不同
服务一码:
@RestController
@RequestMapping("/")
public class OneController {
Logger logger = Logger.getLogger(OneController.class.getName());
@GetMapping("/getStringFromServiceOne")
public String getString() {
logger.info("Get String from SERVICE-ONE");
return "Get String from SERVICE-ONE";
}
}
服务二码:
@RestController
@RequestMapping("/")
public class TwoController {
Logger logger = Logger.getLogger(TwoController.class.getName());
@GetMapping("/getStringFromServiceTwo")
public String getString() {
logger.info("Get String from SERVICE-TWO");
return "Get String from SERVICE-TWO";
}
}
服务-三码:
@RestController
@RequestMapping("/")
public class ThreeController {
Logger logger = Logger.getLogger(ThreeController.class.getName());
@GetMapping("/getStringFromServiceThree")
public String getString() {
logger.info("Get String from SERVICE-THREE");
return "Get String from SERVICE-THREE";
}
}
客户代码:
控制器:
@RestController
@RequestMapping("/")
public class ClientController {
Logger logger = Logger.getLogger(ClientController.class.getName());
@Autowired
private ServiceOneFeignClient serviceOneFeignClient;
@Autowired
private ServiceTwoFeignClient serviceTwoFeignClient;
@RequestMapping(path = "/getDataFromServiceOneByFeign")
public String getDataFromServiceOne() {
String result = serviceOneFeignClient.getString();
logger.info("Calling through Feign Client");
return result;
}
@RequestMapping(path = "/getDataFromServiceTwoByFeign")
public String getDataFromServiceTwo() {
String result = serviceTwoFeignClient.getString();
logger.info("Calling through Feign Client");
return result;
}
}
伪装 1:
@FeignClient(name = "service-one", url = "http://localhost:8081/")
public interface ServiceOneFeignClient {
@GetMapping("/getStringFromServiceOne")
String getString();
}
和伪装 2:
@FeignClient(name = "service-two", url = "http://localhost:8082/", fallback = Fallback.class)
public interface ServiceTwoFeignClient {
@GetMapping("/getStringFromServiceTwo")
String getString();
}
和伪装 3:
@FeignClient(name = "service-three", url = "http://localhost:8083/")
public interface ServiceThreeFeignClient {
@GetMapping("/getStringFromServiceThree")
String getString();
}
主要class:
@SpringBootApplication
@EnableFeignClients
@EnableCircuitBreaker
@EnableHystrix
public class ClientApplication {
public static void main(String[] args) {
SpringApplication.run(ClientApplication.class, args);
}
}
和application.yml文件
server:
port: 8070
feign:
hystrix:
enabled: true
我在这里创建了 Fallback class
@Component
public class Fallback implements ServiceThreeFeignClient {
@Override
public String getString() {
System.out.println("Called MOVIE-SERVICE with Fallback class!");
return new String("FALLBACK STRING");
}
}
这里如果service-two不可用,我想调用service-three
但是当我启动我的应用程序时,我立即收到错误消息:
Caused by: java.lang.IllegalStateException: Incompatible fallback instance. Fallback/fallbackFactory of type class com.example.client.service.Fallback is not assignable to interface com.example.client.service.ServiceTwoFeignClient for feign client service-two
at org.springframework.cloud.openfeign.HystrixTargeter.getFromContext(HystrixTargeter.java:86) ~[spring-cloud-openfeign-core-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.cloud.openfeign.HystrixTargeter.targetWithFallback(HystrixTargeter.java:70) ~[spring-cloud-openfeign-core-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.cloud.openfeign.HystrixTargeter.target(HystrixTargeter.java:46) ~[spring-cloud-openfeign-core-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.cloud.openfeign.FeignClientFactoryBean.getTarget(FeignClientFactoryBean.java:284) ~[spring-cloud-openfeign-core-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.cloud.openfeign.FeignClientFactoryBean.getObject(FeignClientFactoryBean.java:247) ~[spring-cloud-openfeign-core-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:171) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE]
他写的我看懂了:必须注明
implements ServiceTwoFeignClient
改为
implements ServiceThreeFeignClient
但如果服务二不可用,我想呼叫服务三。
我不明白该怎么做。
我将不胜感激你的帮助。
谢谢
我在后备方案中决定了 class:
@Component
public class Fallback implements ServiceTwoFeignClient {
@Autowired
private ServiceThreeFeignClient serviceThreeFeignClient;
@Override
public String getString() {
return serviceThreeFeignClient.getString();
}
}
我正在尝试通过 Java 中 Feign 客户端 的工作来理解并调用 Fallback 以防万一错误,我遇到了一些困难。 我写了最简单的例子来理解它是如何工作的,但我似乎很困惑。 请帮助我了解我的错误所在。
我还在 github 上发布了这个例子以方便使用: https://github.com/ksereda/feign-fallback-example
client通过Feign调用service-one,接收字符串"Get String from SERVICE-ONE".
сlient也是通过Feign调用service-two得到字符串"Get String from SERVICE-TWO".
先决条件: 在 service-two 不可用的情况下,client 必须调用 Fallback class,其中调用 service-three 被指示而不是 "Get String from SERVICE-TWO",client 应该得到 "Get String from SERVICE-THREE" 来自 服务三.
service-two和service-three是一样的,只是return个字符串不同
服务一码:
@RestController
@RequestMapping("/")
public class OneController {
Logger logger = Logger.getLogger(OneController.class.getName());
@GetMapping("/getStringFromServiceOne")
public String getString() {
logger.info("Get String from SERVICE-ONE");
return "Get String from SERVICE-ONE";
}
}
服务二码:
@RestController
@RequestMapping("/")
public class TwoController {
Logger logger = Logger.getLogger(TwoController.class.getName());
@GetMapping("/getStringFromServiceTwo")
public String getString() {
logger.info("Get String from SERVICE-TWO");
return "Get String from SERVICE-TWO";
}
}
服务-三码:
@RestController
@RequestMapping("/")
public class ThreeController {
Logger logger = Logger.getLogger(ThreeController.class.getName());
@GetMapping("/getStringFromServiceThree")
public String getString() {
logger.info("Get String from SERVICE-THREE");
return "Get String from SERVICE-THREE";
}
}
客户代码:
控制器:
@RestController
@RequestMapping("/")
public class ClientController {
Logger logger = Logger.getLogger(ClientController.class.getName());
@Autowired
private ServiceOneFeignClient serviceOneFeignClient;
@Autowired
private ServiceTwoFeignClient serviceTwoFeignClient;
@RequestMapping(path = "/getDataFromServiceOneByFeign")
public String getDataFromServiceOne() {
String result = serviceOneFeignClient.getString();
logger.info("Calling through Feign Client");
return result;
}
@RequestMapping(path = "/getDataFromServiceTwoByFeign")
public String getDataFromServiceTwo() {
String result = serviceTwoFeignClient.getString();
logger.info("Calling through Feign Client");
return result;
}
}
伪装 1:
@FeignClient(name = "service-one", url = "http://localhost:8081/")
public interface ServiceOneFeignClient {
@GetMapping("/getStringFromServiceOne")
String getString();
}
和伪装 2:
@FeignClient(name = "service-two", url = "http://localhost:8082/", fallback = Fallback.class)
public interface ServiceTwoFeignClient {
@GetMapping("/getStringFromServiceTwo")
String getString();
}
和伪装 3:
@FeignClient(name = "service-three", url = "http://localhost:8083/")
public interface ServiceThreeFeignClient {
@GetMapping("/getStringFromServiceThree")
String getString();
}
主要class:
@SpringBootApplication
@EnableFeignClients
@EnableCircuitBreaker
@EnableHystrix
public class ClientApplication {
public static void main(String[] args) {
SpringApplication.run(ClientApplication.class, args);
}
}
和application.yml文件
server:
port: 8070
feign:
hystrix:
enabled: true
我在这里创建了 Fallback class
@Component
public class Fallback implements ServiceThreeFeignClient {
@Override
public String getString() {
System.out.println("Called MOVIE-SERVICE with Fallback class!");
return new String("FALLBACK STRING");
}
}
这里如果service-two不可用,我想调用service-three
但是当我启动我的应用程序时,我立即收到错误消息:
Caused by: java.lang.IllegalStateException: Incompatible fallback instance. Fallback/fallbackFactory of type class com.example.client.service.Fallback is not assignable to interface com.example.client.service.ServiceTwoFeignClient for feign client service-two
at org.springframework.cloud.openfeign.HystrixTargeter.getFromContext(HystrixTargeter.java:86) ~[spring-cloud-openfeign-core-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.cloud.openfeign.HystrixTargeter.targetWithFallback(HystrixTargeter.java:70) ~[spring-cloud-openfeign-core-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.cloud.openfeign.HystrixTargeter.target(HystrixTargeter.java:46) ~[spring-cloud-openfeign-core-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.cloud.openfeign.FeignClientFactoryBean.getTarget(FeignClientFactoryBean.java:284) ~[spring-cloud-openfeign-core-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.cloud.openfeign.FeignClientFactoryBean.getObject(FeignClientFactoryBean.java:247) ~[spring-cloud-openfeign-core-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:171) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE]
他写的我看懂了:必须注明
implements ServiceTwoFeignClient
改为
implements ServiceThreeFeignClient
但如果服务二不可用,我想呼叫服务三。
我不明白该怎么做。 我将不胜感激你的帮助。 谢谢
我在后备方案中决定了 class:
@Component
public class Fallback implements ServiceTwoFeignClient {
@Autowired
private ServiceThreeFeignClient serviceThreeFeignClient;
@Override
public String getString() {
return serviceThreeFeignClient.getString();
}
}