This article follows the previous one< Java microservices (2): service consumer and provider building >The last article mainly talks about the construction and simple realization of consumers and service providers. The focus is on several pits in the configuration file.
This chapter introduces some scattered contents: load balancing, serialization and fusing of services
1. Service load balancing
Load balancing can be divided into software load balancing and hardware load balancing. In our daily development, it is generally difficult to access the hardware load balancing. But software load balancing can be contacted, such as Nginx. dubbo also provides soft loads.
For details, please read the introduction of load balancing on dubbo official website. Here is a summary of the load balancing methods:
- RandomLoadBalance of weight random algorithm
RandomLoadBalance is the concrete implementation of weighted Random algorithm. Its algorithm idea is very simple. Suppose we have A set of servers = [A, B, C], their corresponding weights are weights = [5, 3, 2], and the total weight is 10. Then 5 / 10 of the requests reach server A, and 3 / 10 and 2 / 10 reach server B and server C respectively. As long as the Random number generated by the Random number generator is well distributed, after multiple selections, the proportion of times each server is selected is close to its weight proportion. When the number of calls is relatively small, Random numbers generated by Random may be relatively centralized, and most requests will fall to the same server.
- Least active load balance algorithm
Each service provider corresponds to an active number. Initially, all service providers are active at 0. Every time a request is received, the active number is increased by 1, and after the request is completed, the active number is decreased by 1. After the service runs for a period of time, the service provider with good performance can process the request faster, so the active number is also decreased faster. At this time, such service provider can get new service requests first, which is the basic idea of the minimum active number load balancing algorithm. At present, this calculation The method also introduces the weight value.
- ConsistentHashLoadBalance based on hash consistency
First, a hash is generated for the cache node according to ip or other information, and the hash is projected on the [0, 232 - 1] ring. When there is a query or write request, a hash value is generated for the key of the cache item. Then find the first cache node that is greater than or equal to the hash value, and query or write the cache entry to this node. If the current node is hung, another cache node larger than its hash value can be found for the cache item at the next query or write to the cache.
- Round robin load balance based on weighted polling algorithm
Polling refers to the allocation of requests to each server in turn. For example, we have three servers A, B and C. We assign the first request to server A, the second to server B, the third to server C, and the fourth to server A again. This process is called polling. Polling is A kind of stateless load balancing algorithm, which is simple to implement and suitable for the scenario with similar performance of each server. Weighted polling is to assign A weight to the server, and then rotate training according to the weight.
Code construction, this example uses the rotation training algorithm to do the demo
Just add the loadbalance annotation to the yml configuration file.
Open 2 service providers, consume with service consumers, and view logs
2. Serialization
Serialization supported in Dubbo:
- dubbo serialization: Alibaba has not developed a mature and efficient java serialization implementation, and it is not recommended to use it in the production environment.
- hessian 2 serialization: hessian is an efficient cross language binary serialization method. However, it is not the native hessian 2 serialization, but the modified hessian lite by Ali. It is the default serialization method enabled by dubbo RPC.
- json serialization: at present, there are two implementations, one is the fast json Library of Ali, the other is the simple json Library of dubbo, but its implementation is not particularly mature, and the text serialization performance of json is generally not as good as the above two binary serialization.
- Java serialization: mainly implemented by Java serialization of JDK, with poor performance.
dubbo's own serialization method is not mature, while json and Java serialization performance is not ideal. dubbo can use Hessian 2 to serialize, but Hessian 2 is cross language and does not optimize the Java language alone, so many tools that provide optimization to Java alone have better performance than Hessian 2. We introduce two efficient Java serialization implementations, Kryo and FST, for dubbo to gradually replace Hessian 2.
Examples of dubbo's serialization are as follows:
Code building, first increase dependency
1 <dependency> 2 <groupId>de.javakaffee</groupId> 3 <artifactId>kryo-serializers</artifactId> 4 <version>0.42</version> 5 </dependency> 6 <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-hystrix --> 7 <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-hystrix --> 8 <dependency> 9 <groupId>org.springframework.cloud</groupId> 10 <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> 11 <version>2.0.1.RELEASE</version> 12 </dependency> 13 <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-hystrix-dashboard --> 14 <dependency> 15 <groupId>org.springframework.cloud</groupId> 16 <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> 17 <version>2.0.1.RELEASE</version> 18 </dependency>
Add the configuration properties in the configuration file to:
At this point, the serialization configuration is complete, and the following summarizes the performance of common serialization methods
3. fuses
Due to the network and its own reasons, the call between RPC s can not guarantee 100% availability. If the server is down and there are a large number of requests coming, there will be an avalanche. In order to solve this problem, the industry put forward a fuse. After the fuse is opened, in order to avoid interlock failure, a fixed value can be directly returned through the fallback method. At this time, many logic processing can be done in fallback, which means that logs or e-mails can be checked by developers in time to reduce the risk.
Code building, first increase dependency
1 <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-hystrix --> 2 <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-hystrix --> 3 <dependency> 4 <groupId>org.springframework.cloud</groupId> 5 <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> 6 <version>2.0.1.RELEASE</version> 7 </dependency> 8 <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-hystrix-dashboard --> 9 <dependency> 10 <groupId>org.springframework.cloud</groupId> 11 <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> 12 <version>2.0.1.RELEASE</version> 13 </dependency>
The second one is used in the fuse panel. The specific codes and related explanations are as follows:
The configuration of the fusing instrument panel. Note that there are differences between the configuration of spring boot2 and 1. Please refer to the official website for details.
1 package com.edu.hello.dubbo.service.user.consumer.config; 2 3 import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet; 4 import org.springframework.boot.web.servlet.ServletRegistrationBean; 5 import org.springframework.context.annotation.Bean; 6 import org.springframework.context.annotation.Configuration; 7 8 /** 9 * @ClassName HystrixDashboardConfiguration 10 * @Deccription TODO 11 * @Author DZ 12 * @Date 2019/9/3 23:10 13 **/ 14 @Configuration 15 public class HystrixDashboardConfiguration { 16 @Bean 17 public ServletRegistrationBean getServlet() { 18 HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet(); 19 ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet); 20 registrationBean.setLoadOnStartup(1); 21 registrationBean.addUrlMappings("/hystrix.stream"); 22 registrationBean.setName("HystrixMetricsStreamServlet"); 23 return registrationBean; 24 } 25 }
Start the service and view the results. Here, only the service consumer is started, no service provider is started, and the manufacturing service times out.
Visit http://localhost:9090/hystrix View the fuse interface. Other details can be viewed. The access address of the dashboard is from config. The dashboard is as follows:
Visit http://localhost:9090/hystrix . stream view the fusing dashboard interface, and view the fusing related information in more detail.
The relevant parameters in the instrument cluster are explained as follows: