You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Client Side Load Balancing with Ribbon and Spring Cloud
This guide walks you through the process of providing client-side load balancing for a microservice application using Netflix Ribbon.
What you’ll build
You’ll build a microservice application that uses Netflix Ribbon and Spring Cloud Netflix to provide client-side load balancing in calls to another microservice.
Like most Spring Getting Started guides, you can start from scratch and complete each step, or you can bypass basic setup steps that are already familiar to you. Either way, you end up with working code.
Download and unzip the source repository for this guide, or clone it using Git: git clone https://github.com/spring-guides/gs-client-side-load-balancing.git
It collects all the jars on the classpath and builds a single, runnable "über-jar", which makes it more convenient to execute and transport your service.
It searches for the public static void main() method to flag as a runnable class.
It provides a built-in dependency resolver that sets the version number to match Spring Boot dependencies. You can override any version you wish, but it will default to Boot’s chosen set of versions.
Build with Maven
First you set up a basic build script. You can use any build system you like when building apps with Spring, but the code you need to work with Maven is included here. If you’re not familiar with Maven, refer to Building Java Projects with Maven.
Create the directory structure
In a project directory of your choosing, create the following subdirectory structure; for example, with mkdir -p src/main/java/hello on *nix systems:
It collects all the jars on the classpath and builds a single, runnable "über-jar", which makes it more convenient to execute and transport your service.
It searches for the public static void main() method to flag as a runnable class.
It provides a built-in dependency resolver that sets the version number to match Spring Boot dependencies. You can override any version you wish, but it will default to Boot’s chosen set of versions.
Our “server” service is called Say Hello. It will return a random greeting (picked out of a static list of three) from an endpoint accessible at /greeting.
In src/main/java/hello, create the file SayHelloApplication.java. It should look like this:
The @RestController annotation gives the same effect as if we were using @Controller and @ResponseBody together. It marks SayHelloApplication as a controller class (which is what @Controller does) and ensures that return values from the class’s @RequestMapping methods will be automatically converted appropriately from their original types and written directly to the response body (which is what @ResponseBody does). We have one @RequestMapping method for /greeting and then another for the root path /. (We’ll want that second method when we get to working with Ribbon in just a bit.)
We’re going to run multiple instances of this application locally alongside a client service application, so create the directory src/main/resources, create the file application.yml within it, and then in that file, set a default value for server.port. (We’ll instruct the other instances of the application to run on other ports, as well, so that none of the Say Hello instances will conflict with the client when we get that running.) While we’re in this file, we’ll set the spring.application.name for our service too.
The User application will be what our user sees. It will make a call to the Say Hello application to get a greeting and then send that to our user when the user visits the endpoint at /hi.
In the User application directory, under src/main/java/hello, add the file UserApplication.java:
To get a greeting from Say Hello, we’re using Spring’s RestTemplate template class. RestTemplate makes an HTTP GET request to the Say Hello service’s URL as we provide it and gives us the result as a String. (For more information on using Spring to consume a RESTful service, see the Consuming a RESTful Web Service guide.)
Add the spring.application.name and server.port properties to src/main/resources/application.properties or src/main/resources/application.yml:
user/src/main/resources/application.yml
spring:
application:
name: userserver:
port: 8888
Load balance across server instances
Now we can access /hi on the User service and see a friendly greeting:
To move beyond a single hard-coded server URL to a load-balanced solution, let’s set up Ribbon. In the application.yml file under user/src/main/resources/, add the following properties:
This configures properties on a Ribbon client. Spring Cloud Netflix creates an ApplicationContext for each Ribbon client name in our application. This is used to give the client a set of beans for instances of Ribbon components, including:
an IClientConfig, which stores client configuration for a client or load balancer,
an ILoadBalancer, which represents a software load balancer,
a ServerList, which defines how to get a list of servers to choose from,
an IRule, which describes a load balancing strategy, and
an IPing, which says how periodic pings of a server are performed.
In our case above, the client is named say-hello. The properties we set are eureka.enabled (which we set to false), listOfServers, and ServerListRefreshInterval. Load balancers in Ribbon normally get their server lists from a Netflix Eureka service registry. (See the Service Registration and Discovery guide for information on using a Eureka service registry with Spring Cloud.) For our simple purposes here, we’re skipping Eureka, so we set the ribbon.eureka.enabled property to false and instead give Ribbon a static listOfServers. ServerListRefreshInterval is the interval, in milliseconds, between refreshes of Ribbon’s service list.
In our UserApplication class, switch the RestTemplate to use the Ribbon client to get the server address for Say Hello:
We’ve made a couple of other related changes to the UserApplication class. Our RestTemplate is now also marked as LoadBalanced; this tells Spring Cloud that we want to take advantage of its load balancing support (provided, in this case, by Ribbon). The class is annotated with @RibbonClient, which we give the name of our client (say-hello) and then another class, which contains extra configuration for that client.
We’ll need to create that class. Add a new file, SayHelloConfiguration.java, in the user/src/main/java/hello directory:
We can override any Ribbon-related bean that Spring Cloud Netflix gives us by creating our own bean with the same name. Here, we override the IPing and IRule used by the default load balancer. The default IPing is a NoOpPing (which doesn’t actually ping server instances, instead always reporting that they’re stable), and the default IRule is a ZoneAvoidanceRule (which avoids the Amazon EC2 zone that has the most malfunctioning servers, and might thus be a bit difficult to try out in our local environment).
Our IPing is a PingUrl, which will ping a URL to check the status of each server. Say Hello has, as you’ll recall, a method mapped to the / path; that means that Ribbon will get an HTTP 200 response when it pings a running Say Hello server. The IRule we set up, the AvailabilityFilteringRule, will use Ribbon’s built-in circuit breaker functionality to filter out any servers in an “open-circuit” state: if a ping fails to connect to a given server, or if it gets a read failure for the server, Ribbon will consider that server “dead” until it begins to respond normally.
The @SpringBootApplication annotation on the UserApplication class is equivalent to (among others) the @Configuration annotation that marks a class as a source of bean definitions. This is why we don’t need to annotate the SayHelloConfiguration class with @Configuration: since it’s in the same package as UserApplication, it is already being scanned for bean methods.
This approach does mean that our Ribbon configuration will be part of the main application context and therefore shared by all Ribbon clients in the User application. In a normal application, you can avoid this by keeping Ribbon beans out of the main application context (e.g., in this example, you could put SayHelloConfiguration in a different package from UserApplication).
Trying it out
Run the Say Hello service, using either Gradle:
$ ./gradlew bootRun
or Maven:
$ mvn spring-boot:run
Run other instances on ports 9092 and 9999, again using either Gradle:
$ SERVER_PORT=9092 ./gradlew bootRun
or Maven:
$ SERVER_PORT=9999 mvn spring-boot:run
Then start up the User service. Access localhost:8888/hi and then watch the Say Hello service instances. You can see Ribbon’s pings arriving every 15 seconds:
2016-03-09 21:13:22.115 INFO 90046 --- [nio-8090-exec-1] hello.SayHelloApplication : Access /
2016-03-09 21:13:22.629 INFO 90046 --- [nio-8090-exec-3] hello.SayHelloApplication : Access /
And your requests to the User service should result in calls to Say Hello being spread across the running instances in round-robin form:
2016-03-09 21:15:28.915 INFO 90046 --- [nio-8090-exec-7] hello.SayHelloApplication : Access /greeting
Now shut down a Say Hello server instance. Once Ribbon has pinged the down instance and considers it down, you should see requests begin to be balanced across the remaining instances.
Summary
Congratulations! You’ve just developed a Spring application that performs client-side load balancing for calls to another application.
The text was updated successfully, but these errors were encountered:
Client Side Load Balancing with Ribbon and Spring Cloud
This guide walks you through the process of providing client-side load balancing for a microservice application using Netflix Ribbon.
What you’ll build
You’ll build a microservice application that uses Netflix Ribbon and Spring Cloud Netflix to provide client-side load balancing in calls to another microservice.
What you’ll need
How to complete this guide
Like most Spring Getting Started guides, you can start from scratch and complete each step, or you can bypass basic setup steps that are already familiar to you. Either way, you end up with working code.
To start from scratch, move on to Build with Gradle.
To skip the basics, do the following:
git clone https://github.com/spring-guides/gs-client-side-load-balancing.git
gs-client-side-load-balancing/initial
When you’re finished, you can check your results against the code in
gs-client-side-load-balancing/complete
.Build with Gradle
First you set up a basic build script. You can use any build system you like when building apps with Spring, but the code you need to work with Gradle and Maven is included here. If you’re not familiar with either, refer to Building Java Projects with Gradle or Building Java Projects with Maven.
Create the directory structure
In a project directory of your choosing, create the following subdirectory structure; for example, with
mkdir -p src/main/java/hello
on *nix systems:Create a Gradle build file
Below is the initial Gradle build file.
say-hello/build.gradle
user/build.gradle
The Spring Boot gradle plugin provides many convenient features:
public static void main()
method to flag as a runnable class.Build with Maven
First you set up a basic build script. You can use any build system you like when building apps with Spring, but the code you need to work with Maven is included here. If you’re not familiar with Maven, refer to Building Java Projects with Maven.
Create the directory structure
In a project directory of your choosing, create the following subdirectory structure; for example, with
mkdir -p src/main/java/hello
on *nix systems:say-hello/pom.xml
user/pom.xml
The Spring Boot Maven plugin provides many convenient features:
public static void main()
method to flag as a runnable class.Build with your IDE
Write a server service
Our “server” service is called Say Hello. It will return a random greeting (picked out of a static list of three) from an endpoint accessible at
/greeting
.In
src/main/java/hello
, create the fileSayHelloApplication.java
. It should look like this:say-hello/src/main/java/hello/SayHelloApplication.java
The
@RestController
annotation gives the same effect as if we were using@Controller
and@ResponseBody
together. It marksSayHelloApplication
as a controller class (which is what@Controller
does) and ensures that return values from the class’s@RequestMapping
methods will be automatically converted appropriately from their original types and written directly to the response body (which is what@ResponseBody
does). We have one@RequestMapping
method for/greeting
and then another for the root path/
. (We’ll want that second method when we get to working with Ribbon in just a bit.)We’re going to run multiple instances of this application locally alongside a client service application, so create the directory
src/main/resources
, create the fileapplication.yml
within it, and then in that file, set a default value forserver.port
. (We’ll instruct the other instances of the application to run on other ports, as well, so that none of the Say Hello instances will conflict with the client when we get that running.) While we’re in this file, we’ll set thespring.application.name
for our service too.say-hello/src/main/resources/application.yml
Access from a client service
The User application will be what our user sees. It will make a call to the Say Hello application to get a greeting and then send that to our user when the user visits the endpoint at
/hi
.In the User application directory, under
src/main/java/hello
, add the fileUserApplication.java
:user/src/main/java/hello/UserApplication.java
To get a greeting from Say Hello, we’re using Spring’s
RestTemplate
template class.RestTemplate
makes an HTTP GET request to the Say Hello service’s URL as we provide it and gives us the result as aString
. (For more information on using Spring to consume a RESTful service, see the Consuming a RESTful Web Service guide.)Add the
spring.application.name
andserver.port
properties tosrc/main/resources/application.properties
orsrc/main/resources/application.yml
:user/src/main/resources/application.yml
Load balance across server instances
Now we can access
/hi
on the User service and see a friendly greeting:To move beyond a single hard-coded server URL to a load-balanced solution, let’s set up Ribbon. In the
application.yml
file underuser/src/main/resources/
, add the following properties:user/src/main/resources/application.yml
This configures properties on a Ribbon client. Spring Cloud Netflix creates an
ApplicationContext
for each Ribbon client name in our application. This is used to give the client a set of beans for instances of Ribbon components, including:IClientConfig
, which stores client configuration for a client or load balancer,ILoadBalancer
, which represents a software load balancer,ServerList
, which defines how to get a list of servers to choose from,IRule
, which describes a load balancing strategy, andIPing
, which says how periodic pings of a server are performed.In our case above, the client is named
say-hello
. The properties we set areeureka.enabled
(which we set tofalse
),listOfServers
, andServerListRefreshInterval
. Load balancers in Ribbon normally get their server lists from a Netflix Eureka service registry. (See the Service Registration and Discovery guide for information on using a Eureka service registry with Spring Cloud.) For our simple purposes here, we’re skipping Eureka, so we set theribbon.eureka.enabled
property tofalse
and instead give Ribbon a staticlistOfServers
.ServerListRefreshInterval
is the interval, in milliseconds, between refreshes of Ribbon’s service list.In our
UserApplication
class, switch theRestTemplate
to use the Ribbon client to get the server address for Say Hello:user/src/main/java/hello/UserApplication.java
We’ve made a couple of other related changes to the
UserApplication
class. OurRestTemplate
is now also marked asLoadBalanced
; this tells Spring Cloud that we want to take advantage of its load balancing support (provided, in this case, by Ribbon). The class is annotated with@RibbonClient
, which we give thename
of our client (say-hello
) and then another class, which contains extraconfiguration
for that client.We’ll need to create that class. Add a new file,
SayHelloConfiguration.java
, in theuser/src/main/java/hello
directory:user/src/main/java/hello/SayHelloConfiguration.java
We can override any Ribbon-related bean that Spring Cloud Netflix gives us by creating our own bean with the same name. Here, we override the
IPing
andIRule
used by the default load balancer. The defaultIPing
is aNoOpPing
(which doesn’t actually ping server instances, instead always reporting that they’re stable), and the defaultIRule
is aZoneAvoidanceRule
(which avoids the Amazon EC2 zone that has the most malfunctioning servers, and might thus be a bit difficult to try out in our local environment).Our
IPing
is aPingUrl
, which will ping a URL to check the status of each server. Say Hello has, as you’ll recall, a method mapped to the / path; that means that Ribbon will get an HTTP 200 response when it pings a running Say Hello server. TheIRule
we set up, theAvailabilityFilteringRule
, will use Ribbon’s built-in circuit breaker functionality to filter out any servers in an “open-circuit” state: if a ping fails to connect to a given server, or if it gets a read failure for the server, Ribbon will consider that server “dead” until it begins to respond normally.Trying it out
Run the Say Hello service, using either Gradle:
or Maven:
Run other instances on ports 9092 and 9999, again using either Gradle:
or Maven:
Then start up the User service. Access
localhost:8888/hi
and then watch the Say Hello service instances. You can see Ribbon’s pings arriving every 15 seconds:And your requests to the User service should result in calls to Say Hello being spread across the running instances in round-robin form:
Now shut down a Say Hello server instance. Once Ribbon has pinged the down instance and considers it down, you should see requests begin to be balanced across the remaining instances.
Summary
Congratulations! You’ve just developed a Spring application that performs client-side load balancing for calls to another application.
The text was updated successfully, but these errors were encountered: