Distributed RPC system framework Dubbo-09 load balancing

1. Build a load balancing environment

For example, in load balancing, there will be three providers for the same service, which provide the same functions as the service, and there is only one consumer.

Step 1 create provider loadbalance XXX

Create three providers, with project tail names distinguished (one, two, three);

A. Create project add dependency

<properties> 
	<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
	<maven.compiler.source>1.8</maven.compiler.source> 
	<maven.compiler.target>1.8</maven.compiler.target> 
	<!-- Custom version number --> 
	<spring-version>4.3.16.RELEASE</spring-version> 
</properties> 
<dependencies> 
	<!--Business interface engineering dependency--> 
	<dependency> 
		<groupId>com.zxy</groupId>
  		<artifactId>0-api</artifactId>
  		<version>0.0.1-SNAPSHOT</version> 
	</dependency> 
	<!-- dubbo rely on --> 
	<dependency>
	    <groupId>org.apache.dubbo</groupId>
	    <artifactId>dubbo</artifactId>
	    <version>2.7.0</version>
	</dependency>

	<!-- Spring rely on --> 
	<dependency> 
		<groupId>org.springframework</groupId> 
		<artifactId>spring-beans</artifactId> 
		<version>${spring-version}</version> 
	</dependency> 
	<dependency> 
		<groupId>org.springframework</groupId> 
		<artifactId>spring-core</artifactId> 
		<version>${spring-version}</version> 
	</dependency> 
	<dependency>
		<groupId>org.springframework</groupId> 
		<artifactId>spring-context</artifactId> 
		<version>${spring-version}</version> 
	</dependency> 
	<dependency> 
		<groupId>org.springframework</groupId> 
		<artifactId>spring-expression</artifactId> 
		<version>${spring-version}</version> 
	</dependency> 
	<dependency> 
		<groupId>org.springframework</groupId> 
		<artifactId>spring-aop</artifactId> 
		<version>${spring-version}</version> 
	</dependency> 
	<dependency> 
		<groupId>org.springframework</groupId> 
		<artifactId>spring-aspects</artifactId> 
		<version>${spring-version}</version> 
	</dependency> 
	<dependency> 
		<groupId>org.springframework</groupId> 
		<artifactId>spring-tx</artifactId> 
		<version>${spring-version}</version> 
	</dependency> 
	<dependency> 
		<groupId>org.springframework</groupId> 
		<artifactId>spring-jdbc</artifactId> 
		<version>${spring-version}</version> 
	</dependency> 
	
	<!-- zk Client dependencies: curator --> 
	<dependency> 
		<groupId>org.apache.curator</groupId> 
		<artifactId>curator-recipes</artifactId> 
		<version>2.13.0</version> 
	</dependency> <dependency> 
		<groupId>org.apache.curator</groupId> 
		<artifactId>curator-framework</artifactId> 
		<version>2.13.0</version> 
	</dependency>
	
	<!-- commons-logging rely on --> 
	<dependency> 
		<groupId>commons-logging</groupId> 
		<artifactId>commons-logging</artifactId> 
		<version>1.2</version> 
	</dependency> 
	
	<dependency> 
		<groupId>org.springframework</groupId> 
		<artifactId>spring-web</artifactId> 
		<version>${spring-version}</version> 
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId> 
		<artifactId>spring-webmvc</artifactId> 
		<version>${spring-version}</version>
	</dependency>
	
	<!-- Servlet rely on --> 
	<dependency> 
		<groupId>javax.servlet</groupId> 
		<artifactId>javax.servlet-api</artifactId> 
		<version>3.1.0</version> 
		<scope>provided</scope> 
	</dependency> 
	<!-- JSP rely on --> 
	<dependency> 
		<groupId>javax.servlet.jsp</groupId> 
		<artifactId>javax.servlet.jsp-api</artifactId> 
		<version>2.2.1</version> 
		<scope>provided</scope> 
	</dependency>
	
</dependencies>

B. Configuration file

There are several differences in the configuration:

  • qos.port needs to be different

  • The port s in the protocol should be different

<!-- add to DUBBO SCHEMA -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
        
	<!-- Specify the current project in Monitor The name displayed in the monitoring center is generally the same as the project name -->
	<dubbo:application name="provider-loadbalance-one" >
	       <dubbo:parameter key="qos.port" value="11111"/>
	</dubbo:application>
	<!-- Specify registry, do not use -->
	<dubbo:registry address="zookeeper://192.168.85.129:2181" />
	
	<dubbo:protocol name="dubbo" port="20881"></dubbo:protocol>
	
	<!-- Registered business implementation class, real service provider -->
	<bean id="someService" class="com.zxy.service.SomeServiceImpl"/>
	
	<!-- Expose service: the interface is the service name, pointing to the real service provider -->
	<dubbo:service interface="com.zxy.service.SomeService" ref="someService"
				   loadbalance="roundrobin"/>
</beans>

C. Create Service implementation class

Implement the interface in api Engineering https://blog.csdn.net/zxylwj/article/details/108540657;

public class SomeServiceImpl implements SomeService{

	@Override
	public String helloDubbo(String msg) {

		System.out.println(msg+"I am provider-one !");
		return "provider-one" + msg;
	}

}

D. Create startup class

public class ProviderRun {

	
	public static void main(String[] args) throws IOException {
		
		Main.main(args);
	}

}

 

Step 2 create consumer

A. Create project add dependency

Consumer engineering relies on consistency with providers;

B. Configuration file

<!-- add to DUBBO SCHEMA -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
        
	<!-- Specify the current project in Monitor The name displayed in the monitoring center is generally the same as the project name -->
	<dubbo:application name="consumer-loadbalance" >
		<dubbo:parameter key="qos.port" value="21212"/>
	</dubbo:application>
	
	
	<!-- statement zookeeper Stand alone registration zk -->
	<dubbo:registry address="zookeeper://192.168.85.129:2181" />
	
	<dubbo:protocol name="dubbo"></dubbo:protocol>
	
	
	<!-- Call the specified polling load balancing algorithm -->
	<dubbo:reference id="someService" check="false"
					 interface="com.zxy.service.SomeService"
					 loadbalance="roundrobin"/>
		
</beans>

C. Create startup class

public class ConsumerRun {

	
	public static void main(String[] args) throws IOException {
		// Create Spring container
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-consumer.xml");
		//Get service from container
		SomeService service = (SomeService)applicationContext.getBean("someService");
		String result = service.helloDubbo("Consumer ASK ");
		
		System.out.println(result);
	}

}

2 load balancing algorithm

If both consumers and providers have set load balancing policies, the priority set by consumers is high.

If the consumer side has no explicit settings, but the provider side has explicit settings, and the load balancing policies of the same service (interface name, version number and grouping are the same), the consumer will call according to the policies set by the provider.

If the settings of multiple providers are different, the last registered will overwrite the previously registered information.

2.1 Dubbo built-in load balancing algorithm

Dubbo has built-in four load balancing algorithms.

A,random

Random algorithm is Dubbo's default load balancing algorithm. There is a problem of service accumulation.

B,roundrobin

The polling algorithm is scheduled according to the set weight.

C,leastactive

The least activity scheduling algorithm, that is, the fewer times it is scheduled, the higher its optimization level and the higher the probability of being scheduled.

D,consistenthash

Consistent hash algorithm, for requests with the same parameters, it will be routed to the same provider.

2.2 specify load balancing algorithm

The load balancing algorithm can be specified on the provider side or on the consumer side.

A. Consumer side designation

B. Provider side designation

C. specify different load balancing algorithms for different methods of a service

<!-- Expose services: specify different load balancing algorithms for different methods of the same service -->
	<dubbo:service interface="com.zxy.service.SomeService" ref="someService">
				   <dubbo:method name="helloDubbo" loadbalance="roundrobin"/>
				   <dubbo:method name="doSome" loadbalance="random"/>
				   <dubbo:method name="first" loadbalance="leastactive"/>
	</dubbo:service>

 

Tags: Java Dubbo Distribution

Posted by mastercool on Tue, 17 May 2022 23:13:27 +0300