Netflix microservices stack, part 2: Feign+Ribbon

Netflix microservices stack tutorials:
Hello again! Following up on the previous post about Netflix Feign (REST client),  here goes the next post on Ribbon - the load balancer.

Most tutorials that teach how to incorporate load balancing using Ribbon describe how to do it with an additional service discovery application, usually Eureka. In this scenario, each additional microservice announces itself to Eureka, and Eureka proceeds with dynamically forming the list of available microservices and probing each microservice's status individually to cross it out from the list when it's dead. Ribbon distributes the requests between microservices based on the list from Eureka.

This tutorial will focus on Feign-Ribbon bundle exclusively. We'll add Ribbon-powered load balancing capability to our Feign client, but the list of services is going to be declared statically. This is suitable for some cases where you just need load balancing without the additional fuss of setting up service discovery.

Get yourself a Feign client

In this tutorial we're going to build on top of the previous one, the one where we created a Feign client that consumes JSON data from the REST API testing service, so if you already followed that, just skip to the next step. If you've just started reading this, you may just clone my repo containing the full project.

Make a REST API service

Now that we've decided to start switching between our services, one online service is not enough. We'll have to make our own user service with the same REST API that will adhere to the same contract as the online service from part one.

If you want to learn how to make a REST API service, read on. Or you can just download it from my repository (link), launch runServices shell script (located in user-service directory) that will start two instances of User Service on ports 8090 and 8091. Those are the instances we will use for load-balancing.

Detailed walkthrough of creating a REST API service:
  • Create a new Spring Boot application.
  • We don't want any extra work, so let's just copy the models over from the previous Feign tutorial where we created a REST client with Feign to consume data from online REST API service:
    Same models = same API contract = no changes needed in Feign client!
  • Since we're going to generate user data from scratch, we're going to add one more dependency for Apache Commons text library to generate random Strings easily.
    With that library in our classpath, we can add randomize() function to our models:
  • The controller that'll have all the CRUD operations for User model can be easily implemented now:
    As you must've noticed, I'm using the LOGGER object to indicate that this particular instance of user service was used, as we'll be launching more that one. To do that, I added one more dep for SL4J logging framework:

  • The REST API service is ready. Launch the application and make sure it works:
  • Now, purely for convenience, I wrote a shell script to launch 2 instances of this user service to balance between:

Add Ribbon load balancer to our Feign client

As promised, we're not going to build from the ground up. If you followed my previous tutorial, you'll have to modify the client we built in it:
  • In Config.java: add two new annotations to the class to enable Ribbon and change the target type:
    This will connect the client to the named load balancing target that Ribbon will create (http://targetname)
  • In UserController.java: load Ribbon configuration from .properties file
    ... and reduce userClient invocations to one per one endpoint, to make API calls easily traceable.
  • In UserClient.java: just add one more annotation - @FeignClient("user-client")
  • Rename application.properties to ribbon-client.properties, and replace its contents: Eureka is enabled by default when using Ribbon, so we'll have to explicitly switch it off. The second line creates the load balancing target named "user-client" with two hard-coded instances (syntax for this configuration can be found here).
  • Add two new deps into pom.xml: Archaius just configures Ribbon with what we specified in .properties file. Normally, when used with Eureka, this is done automatically, but given that we've omitted Eureka completely we'll have to do it all manually.
The client can be launched now and the default Round-Robin algorithm of balancing can be seen in action:

Comments