How micro-service architecture for client load balancing

 Spring Cloud Ribbon eleven is based on HTTP and TCP client load balancing tool, which is based on NetflixRibbon achieve. By Spring Cloud package, it allows us to easily service-oriented REST template automatically converted to service calls requesting client load balancing. Spring Cloud Ribbon although only a utility class framework, it is not like the service registry, distribution center, API gateway as the need to deploy independent, but it exists in almost every child SpringCloud build a micro-service and infrastructure. Because the calls between micro-services, API gateway forwards the request to other content, are actually implemented by the Ribbon, including follow-up Feign we will be introduced, it is also a tool based on the Ribbon to achieve. So, to understand and use Spring Cloud Ribbon, for us to use Spring Cloud to build a micro-service is very important.

Here we will describe how to use the Ribbon to achieve load balancing the client, and to understand the basic principles Ribbon client load balancing by source code analysis.

Client load balancing

Load balancing is a very important in the system architecture, and content had to implementation. Load balancing is because high availability of the system, one of the important means to ease network pressure and processing capacity expansion. We usually refer to both load balancing it refers to a server load balancing, which is divided into hardware and software load balancing load balancing. Mainly by hardware load balancers mounted between a server node dedicated to the load balancing device, such as F5, etc.; and load balancing software is installed on the server with a number of software modules of the load balance to complete the request or the distribution of work, such as Nginx and so on. Regardless of hardware or software load balancing load balancing, server load balancing as long as it can be built up in a way architecture:

  Hardware load balancing device or software load balancing software module will maintain a list of available services linked to a lower end, to eliminate the server node failure through the heartbeat to ensure that the server node list can all be normal access. When the client sends a request to the load balancing apparatus when the apparatus according to an algorithm (such as a linear polling, according to the weight load, according to traffic load, etc.) and then the address of a server from the list of available server maintenance, followed by forwarding.

The client and server load balancing negative I balanced the biggest difference is that the position of the list of services mentioned above are stored. Client load balancing, all client node maintains its own list of the server to be accessed, and the list of such services from the end of the service registry, such as Eureka server. Similar to the server load balancing architecture, load balancing at the client also needs to maintain heart health of the server list, but with the completion of this step requires registration with the service center. In Spring Cloud Service Governance Framework implementation, the default configuration is created integrated governance framework for the Ribbon automation of various services, such as Eureka in org.springframework.cloud.netflix.ribbon.eureka.RibbonEurekaAutoConfiguration, Consul of org.springframework.cloud .consul.discovery.RibbonConsulAuto- Configurationo when in actual use, we can see the implementation of these two classes to find their configuration details to help us make better use of it.

By SpringCloudRibbon package, we use client load balancing call micro-services architecture is very simple, just two steps as follows:

1. Service providers just need to start multiple service instances and registered to a registry or registry service more associated.

2. Service consumers directly @LoadBalanced comment by calling the RestTemplate been modified to implement a service-oriented interface call.

  In this way, we can be a service provider of high availability and load balancing service consumer calls to achieve together. Among them, we use a very useful objects RestTemplate. The object is to use automated configuration Ribbon, but can also be configured @LoadBalanced open client load balancing. Before we demonstrated through RestTemplate achieve the most simple service visit, we will detail below RestTemplate realized for several different request types and parameter types of service calls.

  GET request

  In RestTemplate, the GET request can be invoked to be achieved by two methods. The first: getForEntity function. This method returns ResponseEntity, the object is encapsulated Spring HTTP request response, which mainly stores several important elements of HTTP, such as HTTP request status horse enumerable object HttpStatus (that is, we often say that these 404, 500 error code), in its parent class ittpEntity also stored as header information when the object HttpHeaders HTTP request and the request body of the generic type. For example the following example, USER-SERVER is access service / user request, while the last parameter replaces idi (1} placeholder in the url, but ResponseEntity returned object type in the body content: The second parameter converter String type.

    Template Residual rest template 二 newRestTemplate (); 

   ResponseEntityStri xesponseEntity 一restTemplate.getFor ("http: //USER-RVICE/user?name={1}",string.class,"didi") ;  

String body= responseEntity.getBody () ;  

If we want to return the body is a User object type, this can be achieved: 

   Restremplate restTemplate Restremplate = new ();  

  ResponseEntity responseEntity -restremplate.get "http: /USER-  vICE/user?name={1}",User.class,"didi") ;

  user body= responseEntity.getBody () ; 

 The above example is a relatively common method

POST request

In RestTemplate in

Can be achieved by three methods invoked when a POST request.

The first: postEorEntity function. The same GET request method similar getrorEntity will

After the call returns ResponseEntity object, where T is a response to the request type of body.

For example, following this

For example, use postForEntity POST request submitted to the USER-SERVICE service / user interfaces,

filed body content user object request response type body returns a String.

Template rest Rest Rest template = new Template ();

User user = new User ("didi",30) ;

ResponseEntity responseEntity=

restTemplate.postForEntity ("http: //USER-SERVICE/user",user,String.class) ;

String body= responseEntity.getBody () ;

postForEntity function also achieved three different overloaded methods.

PUT request

GResTemplate, a PUT request can be called for by a pot method implemented, for example,    

   Rest Rest Template Template Template Residual = new ();

  Long id = 100011;

  User user=new User("didi",40);

  restemplate.put(http://USER-SERVICE/user/{1} ”,user,id ;

put function also achieved three different overloaded methods:

1.put (string url,Object request,0bject...urlVariables)

2.put (Stringurl,Object request,Map urlVariables)

3.put (URI url,object request )

put the function of type void, so there is no return to the content, there is no responserype arguments to other functions defined by other incoming parameter definition and usage and postforobject addition of basically the same.

DELETE requests

In RestTemplate in a DELETE call request can be achieved by a delete method, such as:

Template rest Rest Rest template = new Template ();

Long id = 10001L;restTemplate.delete ("http: //USER-SERVICE/user/ (1}",id) ;

delete function also achieved three different overloaded methods:

1.delete (String url,0bject...urlVariables)

2.delete (String url,Map urlVariables)

3.delete (URi url) due to the unique identification REST request during splicing when we usually will DELETE requests in the url, the body does not need to DELETE request information equest, just as in the above three functions to achieve the same, very simple. DELETE request specifies a location, urlVariables binding parameters to the url.

Want to learn more detailed knowledge, and I recommend to you an exchange study group: 744 642 380 which will share some senior architect video recording: There are Spring, MyBatis, Netty source code analysis, high concurrency, high performance, distributed type, the principle of micro-services architecture, JVM performance optimization of these knowledge necessary to become an architect. Also receive free learning resources, currently benefited

Guess you like

Origin www.cnblogs.com/772933011qq/p/11583870.html