I am creating a springboot application.I have 2 applications Producer and Consumer. I am using Hystrix and Feign together. When I am calling wrong producer endpoint then fallback is not executing instead it is giving 404 path not found exception. Please help
Here is the code snippet
Producer
bootstrap.properties
spring.application.name=cst-employee-producer
PizzaController.java
@RestController
public class PizzaController {
@Autowired
private Environment environment;
private static Map<Integer, PizzaOrderEntity> mapOfPizzas = new LinkedHashMap<Integer, PizzaOrderEntity>();
static{
mapOfPizzas.put(10001, new PizzaOrderEntity(101,"Farm House1",5,467.54,"7905947915"));
mapOfPizzas.put(10002, new PizzaOrderEntity(102,"Farm House2",6,567.54,"7905947916"));
mapOfPizzas.put(10003, new PizzaOrderEntity(103,"Farm House3",7,667.54,"7905947917"));
}
@RequestMapping(value = "/producer/getPizzaDetails", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Collection<PizzaOrderEntity>> getEmployeeDetails() {
Collection<PizzaOrderEntity> pizzaList = mapOfPizzas.values();
System.out.println("I am server with port number "+environment.getProperty("server.port")+". I am hit");
return new ResponseEntity<Collection<PizzaOrderEntity>>(pizzaList, HttpStatus.OK);
}
}
Consumer
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<!-- For Dashboard -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId> <!-- to feed the data to the dashboards -->
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
application.properties
feign.circuitbreaker.enabled=true
SpringEurekaConsumerAppPizzaApplication.java- main class
@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker
@EnableHystrixDashboard
@EnableFeignClients
public class SpringEurekaConsumerAppPizzaApplication {
public static void main(String[] args)throws RestClientException, IOException {
HystrixPlugins.getInstance().registerEventNotifier(new CustomHystrixStateNotifier());
SpringApplication.run(SpringEurekaConsumerAppPizzaApplication.class, args);
}
PizzaController.java
@RestController
public class PizzaConsumerController {
@Autowired
private MyFeignClient feignClient;
@RequestMapping(value="/consumer/getPizzaDetails", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Collection<PizzaOrderEntity>> getPizzaDetails() throws RestClientException, IOException
{
ResponseEntity<Collection<PizzaOrderEntity>> pizzasList=null;
try {
pizzasList= feignClient.findAllPizzas();
}
catch (Exception ex) {
System.out.println(ex);
}
return pizzasList;
}
}
MyFeignClient
@FeignClient(name="cst-employee-producer", fallbackFactory = HystrixMyServiceFallbackFactory.class)
public interface MyFeignClient {
@RequestMapping(value = "/producer/getsPizzaDetails", method = RequestMethod.GET)
ResponseEntity<Collection<PizzaOrderEntity>> findAllPizzas();
}
HystrixMyServiceFallbackFactory.java
public class HystrixMyServiceFallbackFactory implements FallbackFactory<MyFeignClient> {
@Override
public MyFeignClient create(Throwable arg0) {
// TODO Auto-generated method stub
return new MyFeignClient() {
@Override
public ResponseEntity<Collection<PizzaOrderEntity>> findAllPizzas() {
// TODO Auto-generated method stub
Map<Integer, PizzaOrderEntity> mapOfPizzas = new LinkedHashMap<Integer, PizzaOrderEntity>();
mapOfPizzas.put(-1, new PizzaOrderEntity(
"OOPs working on it, please try Again after sometime"));
return new ResponseEntity<Collection<PizzaOrderEntity>>(
mapOfPizzas.values(), HttpStatus.OK);
}
};
}
}
CustomHystrixStateNotifier.java
public class CustomHystrixStateNotifier extends HystrixEventNotifier {
@Override
public void markEvent(HystrixEventType eventType, HystrixCommandKey commandKey){
System.out.printf("HystrixStateNotifier: EventType=%s, MethodTriggeredFrom/Key=%s%n", eventType, commandKey);
}
}
PizzaOrderEntity.java
public class PizzaOrderEntity {
Integer orderId;
String pizzaName;
Integer quantity;
Double bill;
String cutomerContactNumber;
public PizzaOrderEntity(String pizzaName) {
super();
this.pizzaName = pizzaName;
}
public PizzaOrderEntity() {
super();
}
@Override
public String toString() {
return "PizzaOrderEntity [orderId=" + orderId + ", pizzaName="
+ pizzaName + ", quantity=" + quantity + ", bill=" + bill
+ ", cutomerContactNumber=" + cutomerContactNumber + "]";
}
public PizzaOrderEntity(Integer orderId, String pizzaName,
Integer quantity, Double bill, String cutomerContactNumber) {
super();
this.orderId = orderId;
this.pizzaName = pizzaName;
this.quantity = quantity;
this.bill = bill;
this.cutomerContactNumber = cutomerContactNumber;
}
public Integer getOrderId() {
return orderId;
}
public void setOrderId(Integer orderId) {
this.orderId = orderId;
}
public String getPizzaName() {
return pizzaName;
}
public void setPizzaName(String pizzaName) {
this.pizzaName = pizzaName;
}
public Integer getQuantity() {
return quantity;
}
public void setQuantity(Integer quantity) {
this.quantity = quantity;
}
public Double getBill() {
return bill;
}
public void setBill(Double bill) {
this.bill = bill;
}
public String getCutomerContactNumber() {
return cutomerContactNumber;
}
public void setCutomerContactNumber(String cutomerContactNumber) {
this.cutomerContactNumber = cutomerContactNumber;
}
}
Error
feign.FeignException: status 404 reading MyFeignClient#findAllPizzas(); content:
{"timestamp":1701325896573,"status":404,"error":"Not Found","message":"No message available","path":"/producer/getsPizzaDetails"}