Configuring Ribbon with Eureka

The problem in the first approach is that we still have to define the instance URLs manually. With Eureka, we can use its ability to resolve the microservice name dynamically and no more hardcore URLs are required. With Eureka, things are more straightforward. The Ribbon and Feign configuration will be changed as follows:

@FeignClient(name="catalog-service", path="/api/catalog" )
@RibbonClient(name="catalog-service")
public interface CatalogServiceProxy {
@GetMapping("/get-book/{bookId}")
public ResponseEntity<BookDTO> getInventory(@PathVariable("bookId") Integer bookId);
}

No more url attributes are required for the @FeignClient annotation. Also, you can remove the catalog-service.ribbon.listOfServers property from the application.properties file of the inventory-service microservice. Start two instances of catalog-service along with inventory-service and make sure Eureka is running before you do. You will see two instances of catalog-service running in the Eureka console, as follows:

When you go to http://localhost:8793/api/inventory/get-inventory/3, you will get the same behavior. Once the request reaches the instance on port 8792, the second is on 8799, and the third is also on 8792. This is how Ribbon is configured with Feign to achieve load balancing. You can try creating a few more instances and check the behavior. Also, if any of the instances are down, Ribbon will stop sending requests to them, which makes the system fault tolerant.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.3.154