Microservices 4- service invocation and Ribbon

Posted by Linjon on Wed, 26 Jan 2022 16:45:39 +0100

Inter service call

In microservices, many service systems run in independent processes, and all business functions of a large project are realized through the cooperation between various service systems. Service systems use a variety of cross process ways to communicate and cooperate, and RESTful network request is one of the most common ways of interaction.

http.

Thinking: if we were to write the service call, how would we write it.

  1. Hard coded. No. The ip domain name is written in the code. Purpose: find service.

  2. Find the corresponding ip according to the service name. Purpose: in this way, ip switching or random change has no impact on the caller.

    Map < service name, service list > map;

  3. Plus load balancing. Purpose: high availability.

spring cloud provides:

  1. RestTemplate
  2. Feign

Rest

RESTful Network request refers to RESTful Style network request, where REST yes Resource Representational State Transfer Resource presentation layer state transition.
Resource Represents Internet resources. The so-called "resource" is an entity on the network, or a specific information on the Internet. It can be a piece of text, a song, a service, and you can use one URI Point to it, and each "resource" corresponds to one URI. 
Representational It means "presentation layer". "Resource" is a kind of message entity, which can have a variety of external forms. We call the form of "resource" as its "expression layer". For example, text can be used TXT Format can also be used XML Format JSON Format and binary format; Video can be used MP4 Format representation can also be used AVI Format representation. URI It only represents the entity of the resource, not its form. Its specific form of expression should be determined by HTTP Requested header information Accept and Content-Type Field specifies that these two fields are descriptions of the presentation layer.
State Transfer Refers to "state transition". The process of client accessing services inevitably involves the transformation of data and state. If the client wants to operate the server-side resources, it must use some means to make the server-side resources "state transition". This transformation is based on the presentation layer, so it is called "presentation layer state transition". Client by using HTTP Four verbs in the protocol to achieve the above operations, they are: to obtain resources GET,Create or update a new resource POST,Update resource PUT And delete resources DELETE. 

RestTemplate is a synchronous HTTP network client interface provided by Spring. It can simplify the interaction between the client and the HTTP server, and it forces the use of RESTful style. It handles HTTP connections and closures, requiring only the user to provide the server's address (URL) and template parameters.

First level( Level 0)of Web Services are only used HTTP As a transmission method, it is actually just a remote method call( RPC)A specific form of. SOAP and XML-RPC All belong to this category.
The second level( Level 1)of Web Service introduces the concept of resource. Each resource has a corresponding identifier and expression.
The third level( Level 2)of Web Services use different HTTP Method to perform different operations, and use HTTP Status codes to represent different results. as HTTP GET Method to obtain resources, HTTP DELETE Method to delete the resource.
The fourth level( Level 3)of Web Service usage HATEOAS. Link information is included in the expression of resources. The client can find the actions that can be performed according to the link.

git's restful api

https://developer.github.com/v3/

ribbon

Two kinds of load balancing

When the system is faced with a large number of user access and the load is too high, it usually increases the number of servers for horizontal expansion (cluster). The load of multiple servers needs to be balanced to avoid unbalanced server load, large load on some servers and small load on some servers. Through load balancing, the load of servers in the cluster is maintained in a stable and efficient state, so as to improve the processing capacity of the whole system.

Software load balancing: nginx,lvs
 Hardware load balancing: F5
 We only focus on software load balancing,
The first floor can be used DNS,Configure multiple A Record, let DNS Do the first level distribution.
The second layer is the popular reverse agent. The core principle is that the agent will use the reverse agent according to certain rules http The request is forwarded to a single server in the server cluster.

Software load balancing is divided into: server (centralized) and client.

Server side load balancing: use proxy between client side and server side, nginx.

Client load balancing: load balancing according to your own situation. Ribbon is.

The biggest difference between client-side load balancing and server-side load balancing lies in the storage location of server-side address list and where the load algorithm is.

Client load balancing

Server load balancing

Ribbon usage process

Ribbon composition

Homepage of official website: https://github.com/Netflix/ribbon

Ribbon core: generic code of the core. api some configuration.

Ribbon eureka: Based on eureka encapsulated module, it can quickly integrate eureka.

Ribbon examples: learn examples.

Ribbon httpclient: Based on the rest client encapsulated by apache httpClient, it integrates the load balancing module and can be used directly in the project.

Ribbon load balancer: load balancing module.

Ribbon transport: multi protocol support based on netty. For example, http, tcp, udp, etc.

java usage

@GetMapping("/client6")
public Object client6() {
   
   // The ribbon completes the load balancing of the client and filters out the down nodes
   ServiceInstance instance = lb.choose("provider");
   
   String url ="http://" + instance.getHost() +":"+ instance.getPort() + "/HelloWorld/getHi";
      
   String respStr = restTemplate.getForObject(url, String.class);

   return respStr;
}

Load balancing algorithm

Default implementation:

ZoneAvoidanceRule (regional trade-off strategy): comprehensively judge the performance of the region where the Server is located and the availability of the Server, and poll to select the Server.

Other rules:

Best available rule (minimum concurrency Policy): it will first filter out the services in the circuit breaker tripping state due to multiple access faults, and then select a service with the minimum concurrency. Find the service one by one. If the circuit breaker is open, ignore it.

Round robin rule: select a server with simple polling. Cycle through a server in order.

Random rule: randomly select a server.

Availabilityfiltering rule: it will first filter out the services with multiple access faults and in the state of circuit breaker tripping, and filter the services with the number of concurrent connections exceeding the valve value, and then install the polling strategy to access the remaining service list.

Weighted response time rule: calculate the weight of all services according to the average response time. The faster the response time, the greater the service weight, and the higher the probability of being easily selected. When starting up, if the statistical information is not, use the round robin rule (polling) strategy. When the statistical information is enough, it will automatically switch to the weighted response timerule. Long response time, low weight and low probability of being selected. On the contrary, the same is true. This strategy combines various factors (network, disk, IO, etc.), which directly affect the response time.

RetryRule (retry Policy): first obtain the service according to the round robin rule (polling) policy. If the obtained service fails, it will be retried at the specified time to obtain the available service. If you fail to obtain a service for many times, you will not obtain the service again. Mainly in a period of time, if the selection of a service is unsuccessful, continue to find available services until it times out.

Switching load balancing strategy

Annotation method

@Bean
	public IRule myRule(){
		//return new RoundRobinRule();
		//return new RandomRule();
		return new RetryRule(); 

Custom mode

@GetMapping("/client7")
public Object client7() {  
   List<ServiceInstance> instances = discoveryClient.getInstances("provider");       
   // Custom rotation training algorithm    
   // random
   int nextInt = new Random().nextInt(instances.size());
   AtomicInteger atomicInteger = new AtomicInteger(); 
   // training in rotation
   int i = atomicInteger.getAndIncrement();
   instances.get(i % instances.size());      
   // Weight..
   for (ServiceInstance serviceInstance : instances) {
	// int quanzhong =    serviceInstance.getMetadata(); //  Weight 1-9     
   }           
   ServiceInstance instance = instances.get(nextInt);     
   String url ="http://" + instance.getHost() +":"+ instance.getPort() + "/getHi";     
   String respStr = restTemplate.getForObject(url, String.class);
   return respStr;
}

configuration file

Set ribbon policies for services:

provider.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule

Set ribbon policy for all services:

ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule

Property configuration takes precedence over Java code.

Ribbon leaves Eureka

ribbon.eureka.enabled=false
ribbon.listOfServers=localhost:80,localhost:81

Set the requested network address list for service SMS.

Ribbon can work with Eureka, the service registry, to obtain the address information of the server from the service registry, or use the listOfServers field in the configuration file to set the address of the server.

Topics: Microservices