Ribbon
The Ribbon is a load balancer based on HTTP and TCP clients. The feign also uses the Ribbon, which is followed by an introduction to the use of feign.
The ribbon can poll for access in a ribbonserverlist server-side list configured through the client to achieve a balanced load.
When the ribbon is used in conjunction with Eureka, Ribbonserverlist is discoveryenabledniwsserverlist rewritten to get a list of the server from the Eureka Registry. It also replaces iping with niwsdiscoveryping, which delegates responsibilities to Eureka to determine whether the server has been started.
Let's take a look at how the Ribbon is used to invoke the service and achieve a balanced load on the client.
Preparatory work
Start the Service registration center: Eureka-server
Service provider started: Compute-service
- Modify the Server-port in Compute-service to 2223, and then start a service provider: Compute-service
Visit Now: http://localhost:1111/
Alt
You can see that the Compute-service service has two units running:
- 192.168.21.101:compute-service:2222
- 192.168.21.101:compute-service:2223
Consumer with ribbon for client load Balancing
Build a basic spring boot project and include the following in Pom.xml:
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency > <groupId>org.springframework.cloud</groupId> <artifactId> spring-cloud-starter-feign</artifactid> </dependency> <dependency> <groupid >org.springframework.boot</groupId> <artifactid>spring-boot-starter-test</artifactid > <scope>test</scope> </dependency> </dependencies>
In the application main class, the @EnableDiscoveryClient
Discovery service capability is added through annotations. Creates a Resttemplate instance and @LoadBalanced
opens the load balancing capability with annotations.
@SpringBootApplication @enablediscoveryclient Public class ribbonapplication { @Bean @LoadBalanced resttemplate resttemplate () { return New resttemplate (); } Public Static void Main (string[] args) { springapplication.run (ribbonapplication. class , args);} }
Create ConsumerController
COMPUTE-SERVICE
An add service to consume. The service is invoked by direct resttemplate, and the value of 10 + 20 is computed.
@RestController Public class Consumercontroller { @Autowired resttemplate resttemplate; = "/add", method = requestmethod.get) public String Add () { return resttemplate.getforentity ("http://COMPUTE-SERVICE/add?a=10&b=20", String. Class). GetBody (); }}
application.properties
Configuring the Eureka Service Registry in
spring.application.name=ribbon-consumerserver.port=3333eureka.client.serviceUrl.defaultZone= http://localhost:1111/eureka/
Launch the app and visit two times: Http://localhost:3333/add
Then, open the Compute-service's two service providers, output a log content similar to the following:
- The port is
2222
the service-provider log:
- As you can see, the two Compute-service service ports that were previously started were called once. Here we have already implemented a balanced load of service calls through the Ribbon at the client.
- Feign
Feign is a declarative Web service client that makes it easier to write Web Serivce clients. We only need to use feign to create an interface and configure it with annotations to be done. It features pluggable annotation support, including feign annotations and jax-rs annotations. The feign also supports pluggable encoders and decoders. Spring Cloud adds support for feign to spring MVC annotations, and integrates the Ribbon and Eureka to provide an HTTP client implementation that provides a balanced load.
Here is an example of how feign can easily declare the definition and invocation of the Computer-service service described above.
Create a Spring boot project, configuration pom.xml
, to replace the Ribbon dependencies in the above configuration with feign dependencies, as follows:
-
<dependencies> <dependency> <groupid>org.springframework.cloud</groupi d> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <de Pendency> <groupId>org.springframework.cloud</groupId> <artifactid>spring-cloud -starter-feign</artifactid> </dependency> <dependency> <groupid>org.sprin Gframework.boot</groupid> <artifactId>spring-boot-starter-test</artifactId> <SC ope>test</scope> </dependency> </dependencies>
In the application main class through @EnableFeignClients annotation opens the feign function as follows:
-
@SpringBootApplication @enablediscoveryclient@enablefeignclients Span style= "COLOR: #0000ff" >public class feignapplication { public static Span style= "COLOR: #0000ff" >void main (string[] args) {Springapplication.run (Fe Ignapplication. class
Definition Compute-servic
E service interface, as follows:
-
@FeignClient ("Compute-service" Public interface computeclient {@ Requestmapping (Method = requestmethod.get, value = "/add" = "a") integer A, @RequestParam (value = "B" ) integer b);}
- use
@FeignClient ("Compute-service")
annotations to bind the interface corresponding to Compute-service service
- through spring MVC annotations To configure specific implementations under the Compute-service service.
Invoke the computeclient
defined above in the Web layer, as follows:
-
@RestController public class Consumercontroller {@Autowired computeclient Compu Teclient; @RequestMapping (Value = "/add", Method = Requestmethod.get) public Integer Add () { retu RN Computeclient.add (20
application.properties
do not change, specify the Eureka service registry, such as:
spring.application.name=feign-consumerserver.port=3333eureka.client.serviceUrl.defaultZone= http://localhost:1111/eureka/
Looking at the log again, you can get the same results as before using the Ribbon, which provides a balanced load on the service provider.
In this section we can easily bind the Compute-service service by feign in the way of interface and annotation configuration, which allows us to invoke it as a local service in the local application and to do the client-side load balancing.
Spring Cloud Building MicroServices Architecture (ii) Service consumers