Configuring Ribbon without Eureka

We are going to configure Ribbon while making calls from inventory-service to catalog-service, so if you have configured Eureka server, just remove it for the time being while we learn how Ribbon works without Eureka. The very first thing is to add the Ribbon starter dependency. Since we want to handle the call initiated from inventory-service, and add the dependency in inventory-service as follows:

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>

In the previous section, we configured the Feign client to handle REST calls. We will use Ribbon along with the Feign client. Open the proxy class that we created in the previous section and add the Ribbon configuration 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);
}

The @RibbonClient annotation is used to declare the Ribbon configuration with the name attribute pointing to the application on which we want to implement load balancing. The URL that we have configured with FeignClient is now removed and can be defined in the application.properties file as follows:

catalog-service.ribbon.listOfServers=http://localhost:8792,http://localhost:8799

The property name will start with the name of the application that we have used with the @RibbonClient annotation. We need to define comma-separated URLs, each pointing to an individual instance of the catalog-service microservice. As per this configuration, the Ribbon client will handle the call from invoice-service to catalog-service, which has two instances running on ports 8792 and 8799.

We will make the call to the Feign client on inventory-service, which eventually triggers the call to catalog-service. We will observe that the requests are divided into two instances of the catalog-service microservice. To verify which instance the request comes from, we will add the current server port in BookDTO, which will be shown in the response. The current server port can be obtained as follows:

@Autowired
private Environment env;

@GetMapping("/get-book/{bookId}")
public ResponseEntity<BookDTO> getBook(@PathVariable("bookId") Integer bookId) {
......
bookDto.setSmallDesc(bookObject.getSmallDesc());
bookDto.setTitle(bookObject.getTitle());
bookDto.setPort(env.getProperty("local.server.port"));
......
}

Spring injects the instance of the Environment class, which can be used to get current environment details. The current port is accessible with the local.server.port property. Next up, we will run two instances of the catalog-service microservice on these ports.

To start the Spring Boot application on a specific port, you need to right-click on the microservice project, select Run As | Run Configurations, and add the port in the Arguments tab with the -Dserver.port argument. You can also append the port in Name so that it can be easily identified as follows:

To add another instance, you need to right-click on the same instance of catalog-service created in the previous window, select Duplicate, and follow the same steps. The second time, use port 8799 as follows:

Run these two instances along with inventory-service. When you access http://localhost:8793/api/inventory/get-inventory/3, you will see the port is 8792 in the first request and 8799 in the second request. This is how the request is routed to a specific instance turn by turn. 

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

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