Java Micro Services (III): load balancing, serialization, fuse Java micro-services (b): Service consumers and providers to build

  Then write a paper on " the Java Micro Services (2): The service consumers and providers to build ," the article focuses on the structures and simple realization of consumers and service providers. Where the focus needs to pay attention to the configuration file several pit.

This section describes some of the scattered contents: load balancing services, serialization and fuse

1. Load Balancing Service

Load balancing can be divided into software load balancing and hardware load balancing. In our daily development in general, difficult to access hardware load balancing. But software load balancing can still come into contact with, such as Nginx. dubbo also provide soft load.

 

 

 

 

 

Details can read dubbo official gateway to introduce load balancing, load balancing summarize here the way:

  • Weight RandomLoadBalance random algorithm

  RandomLoadBalance is weighted random algorithm realization, its algorithm idea is simple. Suppose we have a set of servers servers = [A, B, C], to the right of their corresponding heavy weights = [5, 3, 2], is a sum of the weights 10. Then there is the request reaches the server A 5/10, 3/10, and 2/10, respectively B and C. As long as the random number generator produces a random distribution of good, after multiple selections, each server selected percentage of times close proportion to its weight. When comparing the number of calls is small, Random generated random number may be more concentrated, then most of the requests will fall on the same server.

  • Minimum number of active LeastActiveLoadBalance call algorithm

  Each service provider corresponds to a number of active active. Initially, all the service providers of active are zero. Each receives a request, the number of active plus one, minus one to complete, after a period of running the service, the service provider good speed performance faster processing request, the request will be active and therefore decrease the number of active is also faster, in this case such a service provider to priority access to the new service request, which is the basic idea of ​​the minimum number of active load-balancing algorithm, the algorithm also introduces the current weight value.

  • Based ConsistentHashLoadBalance hash consistency

  The first ip or other information for the cache node generates a hash, the hash and projects to [0, 2 32  - Upper ring 1]. When a write request or query, for the cache entry key to generate a hash value. Then find the first cache node is greater than or equal to the hash value of the node and the query or write cache entries. If the current node is hung up, the next time the query or write caching, caching node can find another hash value is greater than the cache entry.

  • Based weighted round-robin algorithm RoundRobinLoadBalance

  It refers to the so-called polling request in turn assigned to each server. For example, we have three servers A, B, C. We will assign the first request to the server A, the second requests to the server B, a third request to the allocated server C, the fourth request to the server again assigned A. This process is called polling. Polling is a stateless load balancing algorithm is simple and applicable to similar server performance under each scenario. WRR server is assigned a weight, and then in rotation in accordance with the weighting values.

 

Building codes, the present embodiment used in rotation algorithms do demo

Loadbalance add annotations directly in the configuration file can yml

 

 Open two service providers, consumer spending and use the service, view the log

 

 2. Serialization

  Dubbo in support of serialization:

  • dubbo Serialization: Ali mature and efficient java serialization realize untapped, Ali is not recommended to use it in a production environment
  • hessian2 Serialization: hessian efficient binary serialization way across languages. But this is not the actual native hessian2 serialization, but Ali modified hessian lite, it is dubbo RPC enabled by default serialization
  • json serialization: Currently there are two implementations, one is the use of Ali's fastjson library, and the other is the use of json library dubbo simple in themselves to achieve, but its implementation is not particularly sophisticated, but this text json serialization performance generally inferior to the above two binary serialization.
  • java serialization: mainly used JDK that comes with Java serialization implementation, performance is far from ideal.

  dubbo comes serialization immature, while json java serialization and unsatisfactory performance. dubbo can use hessian2 serialization, but hessian2 is cross-language, do not have a separate optimization of the java language, so a lot of tools java performance alone to provide optimized better than hessian2. We introduce Kryo and FST is dubbo both efficient Java serialization to achieve, to gradually replace hessian2.

For instance serialized dubbo follows:

 

 Building codes, first increased reliance

 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>

Increase the property you can configure in the configuration file:

 

 

 

At this serialized configuration is complete, the following summarizes the performance of common serialization

 

 

 3. Fuse

  As the network and their own reasons, calls between RPC does not guarantee 100% available, if the server had downtime, while there are a large number of requests over, there will be an avalanche, in order to solve this problem, the industry proposed blown. After the fuse is opened, in order to avoid cascading failure, by  fallback the method can return a fixed value. At this point we can do a lot of fallback logic processing, e-mail or by analogy log developers, timely server troubleshooting and reduce risk.

Building codes, first increased reliance

 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>

Wherein the second dependent fuse is used in the dashboard. Specific codes and associated explained as follows below:

 

 

 

 Fuse dashboard configuration to note here the spring configuration and boot2 1 is different, specifically refer to the official website of documents

 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, see the results. Here only started the service to consumers, does not start the service provider, manufacturing services timeout.

 Visit http: // localhost: 9090 / hystrix Check the fuse interface, additional details can view detailed information, visit the address dashboard from config, the dashboard as follows:

 

 

  Access HTTP: // localhost: 9090 / hystrix .stream View blown dashboard interface, more detailed view of the fuse-related information

 Dashboard-related parameters are explained as follows:

 

Guess you like

Origin www.cnblogs.com/dz-boss/p/11717063.html