我正在研究here中有关Feign和Hystrix的示例。没有Feign后备属性,一切正常。但是,当我添加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
下面是我的Feign Client界面:
@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);
}
}
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>
Bellow是一个修改,但效果不佳。
@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());
}
}
}
答案 0 :(得分:0)
由于在控制器类中使用CustomerClient.java
伪客户端,因此似乎存在问题。
请确保您要添加qualifier
@Autowired
private CustomerClient customerClient;
@Autowired
public CustomerController(@Qualifier("customerClientFallback") CustomerClient customerClient ) {
this.customerClient= customerClient;
}
现在应该可以使用。
我建议您研究FallBackFactory以获得更多伪装异常处理的能力
答案 1 :(得分:0)
这是Spring Cloud中的已知错误,请参阅: https://github.com/spring-cloud/spring-cloud-netflix/issues/2677
答案 2 :(得分: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);
}
答案 3 :(得分:0)
该错误似乎是由于在类/接口级别提供的@RequestMapping
造成的。
您的情况是由于@RequestMapping(value = "customer")
中的CustomerClient.java