当前位置: 首页 > 知识库问答 >
问题:

假装Hystrix Fallback在启动时抛出bean错误

顾乐心
2023-03-14

我正在制作一个关于Feign和Hystrix的示例。没有虚假的后备属性,一切正常。但是,当我添加fallback属性并创建实现外部客户机接口的fallback类时,我得到了以下错误

 Description:

Field customerClient in com.feign.demo.controllers.CustomerController required a single bean, but 2 were found:
    - customerClientFallback: defined in file [../ApplicationFeign/target/classes/com/feign/demo/clients/fallback/CustomerClientFallback.class]
    - com.feign.demo.clients.CustomerClient: defined in null


Action:

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed

下面是我的虚拟客户端界面:

@FeignClient(name = "CUSTOMERSERVICE", fallback = CustomerClientFallback.class, primary = false)
@RequestMapping(value = "customer")
public interface CustomerClient {

    @RequestMapping(method = RequestMethod.GET, value = "/getAllCustomers")
    List<Customer> getAllCustomers();

    @RequestMapping(method = RequestMethod.PATCH, value = "/{customerId}", consumes = "application/json")
    Customer update(@PathVariable("customerId") long customerId, @RequestBody Customer customer);

    @RequestMapping(method = RequestMethod.GET, value = "/{customerId}")
    Customer getCustomerById(@PathVariable("customerId") long customerId);

    @RequestMapping(method = RequestMethod.POST, value = "/", consumes = "application/json")
    Customer saveCustomer(@RequestBody Customer customer);

}

CustomerClient回调实现:

@Component
public class CustomerClientFallback implements CustomerClient {

    @Override
    public List<Customer> getAllCustomers() {

        return new ArrayList<Customer>();
    }

    @Override
    public Customer update(long customerId, Customer customer) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Customer getCustomerById(long customerId) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Customer saveCustomer(Customer customer) {
        // TODO Auto-generated method stub
        return null;
    }

}

应用类别:

@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
@EnableHystrix
@EnableHystrixDashboard
public class ApplicationFeignApplication {

    public static void main(String[] args) {
        SpringApplication.run(ApplicationFeignApplication.class, args);

    }

}

Spring云版本:

Greenwich.SR1




<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        </dependency>

贝娄是一个修改,但它不工作,以及。

@RestController
public class CustomerController {

    @Autowired
    private CustomerClient customerClient;

    @Autowired
    public CustomerController(@Qualifier("customerClientFallback") CustomerClient customerClient) {
        this.customerClient = customerClient;
    }

    @RequestMapping(path = "/getAllCustomers", method = RequestMethod.GET)
    public ResponseEntity<Object> getAllCustomers() {
        List<Customer> customers = customerClient.getAllCustomers();
        return new ResponseEntity<>(customers, HttpStatus.OK);

    }

    @RequestMapping(path = "/{customerId}", method = RequestMethod.GET)
    public ResponseEntity<Object> get(@PathVariable() long customerId) {
        try {
            Customer c = customerClient.getCustomerById(customerId);
            if (c != null) {
                return new ResponseEntity<>(c, HttpStatus.OK);
            } else {
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Customer Not Found");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
        }
    }

    @RequestMapping(path = "/{customerId}", method = RequestMethod.PATCH)
    public ResponseEntity<Object> UpdateCustomer(@PathVariable() Long customerId, @RequestBody Customer customer) {
        Customer c;
        try {
            c = customerClient.update(customerId, customer);
            if (c != null) {
                return new ResponseEntity<>(c, HttpStatus.OK);
            } else {
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Customer Not Found");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
        }
    }

    @RequestMapping(path = "", method = RequestMethod.POST)
    public ResponseEntity<Object> saveCustomer(@RequestBody Customer customer) {
        Customer c;
        try {
            c = customerClient.saveCustomer(customer);
            return new ResponseEntity<>(c, HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
        }
    }
}

共有3个答案

薛文斌
2023-03-14

从字段中删除自动连接的注释,您已经在构造函数中注入了依赖项。

private CustomerClient customerClient;

@Autowired
public CustomerController(@Qualifier("customerClientFallback") CustomerClient customerClient) {
    this.customerClient = customerClient;
}

使用构造函数依赖项注入而不是字段注入也更安全——通过字段注入,任何人都可以在无效状态下创建类的实例。在构造函数中明确指定了依赖项,并且更容易测试代码(模拟依赖项并在构造函数中使用它们)

此外,当您用@RequestMapping注释接口或类时,即使您有@FeignClient注释,Spring也会注册一个处理程序。由于您有这个接口的实现,您应该删除它以避免任何不明确的映射问题。

像这样

@FeignClient(name = "CUSTOMERSERVICE", fallback = CustomerClientFallback.class, primary = false)
public interface CustomerClient {

    @RequestMapping(method = RequestMethod.GET, value = "/getAllCustomers")
    List<Customer> getAllCustomers();

    @RequestMapping(method = RequestMethod.PATCH, value = "/{customerId}", consumes = "application/json")
    Customer update(@PathVariable("customerId") long customerId, @RequestBody Customer customer);

    @RequestMapping(method = RequestMethod.GET, value = "/{customerId}")
    Customer getCustomerById(@PathVariable("customerId") long customerId);

    @RequestMapping(method = RequestMethod.POST, value = "/", consumes = "application/json")
    Customer saveCustomer(@RequestBody Customer customer);

}
程俊健
2023-03-14

这是Spring Cloud中的已知错误,请参阅:https://github.com/spring-cloud/spring-cloud-netflix/issues/2677

鲁淇
2023-03-14

由于使用CustomerClient,似乎出现了问题。javacontroller类中的外部客户端。

请确保您正在添加限定符

@Autowired
private CustomerClient customerClient;

@Autowired
public CustomerController(@Qualifier("customerClientFallback") CustomerClient customerClient ) {
    this.customerClient= customerClient;
}

现在应该可以了。

我建议您研究FallBackFactory,以获得更强大的外部异常处理能力,

 类似资料:
  • 我创建了一个简单的flash网站,并将其部署在一台服务器上,使用Nginx作为前端,gunicorn作为后端来运行python代码。有了这个设置,一切正常。但是,如果我使用supervisor模块运行服务器,则会出现以下错误。我已经确保所有的环境变量都是可访问的,它们都被放置在了。主目录下的bashrc。 不知道我错过了什么。我的python代码部署在pipenv虚拟环境中的服务器中。superv

  • 我在清单文件中为我的一个活动定义了一个意图过滤器。当我试图使用以下命令从adb外壳启动此活动时: $adb shell am start 我得到以下错误: 开始:Intent{act=android.Intent.action.VIEW dat=http://www.example.com/gizmospkg=com。实例Android} 错误:活动未启动,无法解析Intent{act=andro

  • 我有一个简单的控制器,它接受文件路径的JSON字符串,并对这些文件运行spring批处理作业。为了实现spring batch,我遵循了一个教程,该教程最终将在https://github.com/michaelhoffmantech/patter-batch-loader中生成代码。 继续下去,直到它抛出StackOverflowError。 任何关于改变什么来修复此问题的建议或帮助都将不胜感激

  • 我在模拟器上运行一个Android应用程序。它一直工作到昨天和今天我更新了Android Studio。我得到以下错误。如何解决? 模拟器:警告:将内存大小增加到1GB模拟器:错误: x86仿真目前需要硬件加速!请确保英特尔HAXM已正确安装并可用。CPU加速状态:HAXM必须更新(版本1.1.1

  • 所以我一直在寻找12个小时,现在这个错误的解决方案,我在尝试安装Laravel框架的PHP。我发现了许多不同的帖子,但似乎没有一个工作。这里的问题,我通过所有的步骤得到Laravel在我的电脑,这是下载和安装作曲家到我的 /usr/local/bin/目录,然后运行这个命令: 我得到这个错误: 我已经删除并重新安装了php,我已经运行了一个脚本,查看是否安装了mcrypt,它是否运行正确,我已经尝

  • 我正在intellij上启动一个tomcat服务器(没有部署任何东西),但我得到以下错误。我一直在找它,但没有运气。你能帮帮我吗? 编辑: 应用程序.属性: