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

问题描述 投票:0回答:3

我正在研究here关于Feign和Hystrix的一个例子。没有Feign后备属性,一切正常。但是当我添加fallback属性并创建实现feign clients接口的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

以下是我的Feign客户端界面:

@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);

}

CustomerClientFallback实现:

@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);

    }

}

春云版:

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());
        }
    }
}
spring-cloud spring-cloud-netflix hystrix spring-cloud-feign
3个回答
0
投票

由于在控制器类中使用CustomerClient.java feign客户端,似乎存在问题。

请确保您添加qualifier

@Autowired
private CustomerClient customerClient;

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

这应该现在有效。

我建议你研究FallBackFactory,以获得更多关于假装异常处理的能力,


0
投票

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


0
投票

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

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);

}
© www.soinside.com 2019 - 2024. All rights reserved.