** When using Dubbo as the project architecture, nGINx provides the consumer with a load balancing strategy and clustering implementation, **However, after a second thought, there are many consumers, but there is still one provider. Finally, they all fall on this one provider.
Dubbo implements load balancing, which generally implements our cluster management on the service providers, i.e. load balancing, and then the service consumers are addressed (weighted) by certain algorithms when requesting consumption.
Dubbo provides four load balancing strategies
Random Random LoadBalance Indicates the size set by the weight
RoundRobin LoadBalance Example: A B C A After executing b execute c, and then go to A…
Minimum Number of active calls (Weight) LeastActive LoadBalance The number of active calls that have different counts before and after an invocation and have the highest priority. Make slower providers receive fewer requests, because slower providers have a larger difference in the count before and after the invocation.
Hash ConsistentHash LoadBalance The same parameters are always sent to the same provider, and if this provider fails, it will be spread to other servers based on its virtual nodes without causing huge changes
Note * : The default is random random (that is, when no load balancing policy is configured, the default is random)
You can configure a load balancing level as follows:
Services can be configured at levels that are precise to each method
Server service level configuration:
<dubbo:service interface=" loadBalance ="roundrobin"/>Copy the code
Server-side method level configuration:
<dubbo:service interface=" interface name "><dubbo:method name=" method name "loadBalance =" balancing policy name "/></dubbo:service>Copy the code
Client service level configuration:
<dubbo:reference interface="" loadbalance="roundrobin" /> Copy the code
Client method level configuration:
<dubbo:reference interface="" loadBalance ="roundrobin"> <dubbo:method name=" loadBalance ="roundrobin" /> </dubbo:reference>Copy the code
We can also operate through the visual interface management platform
After configuring load balancing, it’s time to configure our Dubbo cluster
Dubbo cluster coordination
This is done by modifying the service provider’s configuration file. If the application name in the configuration file is the same, Dubbo will assume that it is the same cluster.
Deploying multiple clusters requires only the following steps:
The application name is the same
<dubbo:application name=" service name "/>Copy the code
The protocol port needs to be modified differently
<dubbo:protocol name="dubbo" port=" The port needs to be modified and cannot be repeated "/>Copy the code
Here is an example of how I configured the service:
Requirement: Configure one cluster environment with two service providers
provider01
<? The XML version = "1.0" encoding = "utf-8"? > <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"> <! <dubbo: Application name="dubbo-provider"/> <! Address: registry IP :port If the registry is ZooKeeper and is a cluster, -protocol :registry type --> <dubbo:registry Address = "169.254.18.25:2181169254 18.25:2182169254:18.25 2183" protocol = "zookeeper" / > <! <dubbo:protocol name="dubbo" port="20880"/> <dubbo:protocol name="hessian" port="20881"/> <! Ref: references the implementation class, Because we added @ Service scan annotations in the implementation class - > < dubbo: Service protocol = "dubbo" interface = "cn. Arebirth. Dubbo. Service. UserDubboService" ref="userDubboServiceImpl" loadbalance="roundrobin"/> <dubbo:service protocol="hessian" interface="cn.arebirth.dubbo.service.CarDubboService" ref="carDubboServiceImpl" loadbalance="roundrobin"/> </beans>Copy the code
After the service is configured, it is packaged and published to run on the server, which we will find on the dubbo-admin console
provider02
<? The XML version = "1.0" encoding = "utf-8"? > <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"> <! <dubbo: Application name="dubbo-provider"/> <! Address: registry IP :port If the registry is ZooKeeper and is a cluster, -protocol :registry type --> <dubbo:registry Address = "169.254.18.25:2181169254 18.25:2182169254:18.25 2183" protocol = "zookeeper" / > <! <dubbo:protocol name="dubbo" port="20881"/> <dubbo:protocol name="hessian" port="20991"/> <! Ref: references the implementation class, Because we added @ Service scan annotations in the implementation class - > < dubbo: Service protocol = "dubbo" interface = "cn. Arebirth. Dubbo. Service. UserDubboService" ref="userDubboServiceImpl" loadbalance="roundrobin"/> <dubbo:service protocol="hessian" interface="cn.arebirth.dubbo.service.CarDubboService" ref="carDubboServiceImpl" loadbalance="roundrobin"/> </beans>Copy the code
We are packaging provider02 and publishing it to run on the server. We will see that our services have been successfully registered.
This also means that the cluster is set up
Ps:
Setting up a cluster environment Note:
Application names must be the same in the same cluster environment
Ports must be different