赞
踩
在Dubbo中,负载均衡是实现高可用和高性能的重要手段之一。Dubbo支持多种负载均衡策略,可以根据业务场景进行选择。本文将详细介绍Dubbo支持的负载均衡策略,并结合代码实践给出操作步骤。
Dubbo支持以下负载均衡策略:
其中,随机、最少活跃数、一致性哈希、轮询是Dubbo自带的负载均衡策略,加权轮询和加权随机是Dubbo的扩展负载均衡策略。
本文以Spring Boot为例,首先需要新建一个Spring Boot项目。在pom.xml文件中添加以下依赖:
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>x.x.x</version>
</dependency>
其中,x.x.x为Dubbo的版本号。本文以2.7.6为例。
在application.properties文件中配置服务提供者信息:
# 服务端口号 server.port=8080 # Dubbo应用名 dubbo.application.name=demo-provider # 注册中心地址 dubbo.registry.address=zookeeper://127.0.0.1:2181 # 服务提供者协议 dubbo.protocol.name=dubbo dubbo.protocol.port=20880 # 服务提供者接口类全限定名 dubbo.scan.base-packages=com.example.demo.service # 服务提供者负载均衡策略 # 随机(Random)、最少活跃数(LeastActive)、一致性哈希(ConsistentHash)、轮询(RoundRobin) # 加权轮询(WeightedRoundRobin)、加权随机(WeightedRandom) dubbo.loadbalance=leastactive
其中,dubbo.loadbalance配置了服务提供者使用的负载均衡策略,本文以最少活跃数为例。
在com.example.demo.service包中新建一个DemoService接口和DemoServiceImpl实现类:
// DemoService.java
public interface DemoService {
String sayHello(String name);
}
// DemoServiceImpl.java
@Service
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello(String name) {
return "Hello, " + name;
}
}
在application.properties文件中配置服务消费者信息:
# Dubbo应用名
dubbo.application.name=demo-consumer
# 注册中心地址
dubbo.registry.address=zookeeper://127.0.0.1:2181
# 服务消费者负载均衡策略
# 随机(Random)、最少活跃数(LeastActive)、一致性哈希(ConsistentHash)、轮询(RoundRobin)
# 加权轮询(WeightedRoundRobin)、加权随机(WeightedRandom)
dubbo.loadbalance=random
其中,dubbo.loadbalance配置了服务消费者使用的负载均衡策略,本文以随机为例。
在com.example.demo.controller包中新建一个DemoController控制器类:
@RestController
public class DemoController {
@Reference(version = "1.0.0")
private DemoService demoService;
@GetMapping("/hello/{name}")
public String sayHello(@PathVariable String name) {
return demoService.sayHello(name);
}
}
其中,@Reference注解用于引用Dubbo提供的服务,version参数指定服务版本号。
启动Zookeeper服务器,并运行服务提供者和服务消费者。访问http://localhost:8080/hello/world,可以看到服务消费者随机选择了一个服务提供者进行调用。如果将服务消费者的负载均衡策略改为leastactive,再次访问http://localhost:8080/hello/world,可以发现服务消费者选择了最少活跃数的服务提供者进行调用。其他负载均衡策略同理。
除了Dubbo自带的负载均衡策略之外,也可以扩展自定义的负载均衡策略。以加权随机为例,我们在com.example.demo.loadbalance包中新建一个WeightedRandomLoadBalance类:
public class WeightedRandomLoadBalance extends RandomLoadBalance { public static final String NAME = "weightedrandom"; @Override protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) { int totalWeight = 0; boolean sameWeight = true; for (int i = 0; i < invokers.size(); i++) { int weight = invokers.get(i).getUrl().getMethodParameter(invocation.getMethodName(), "weight", 100); if (i == 0) { totalWeight = weight; } else { totalWeight += weight; sameWeight = sameWeight && (weight == invokers.get(i - 1).getUrl().getMethodParameter(invocation.getMethodName(), "weight", 100)); } } if (totalWeight > 0 && !sameWeight) { int offset = ThreadLocalRandom.current().nextInt(totalWeight); for (Invoker<T> invoker : invokers) { int weight = invoker.getUrl().getMethodParameter(invocation.getMethodName(), "weight", 100); offset -= weight; if (offset < 0) { return invoker; } } } return super.doSelect(invokers, url, invocation); } }
该类继承自RandomLoadBalance类,使用加权随机的方式选择服务提供者。其中,为了支持加权随机,我们需要在服务提供者URL中添加weight参数,表示该服务提供者的权重。
在application.properties文件中配置服务提供者和服务消费者使用的负载均衡策略:
# 服务提供者负载均衡策略
# 随机(Random)、最少活跃数(LeastActive)、一致性哈希(ConsistentHash)、轮询(RoundRobin)
# 加权轮询(WeightedRoundRobin)、加权随机(WeightedRandom)
dubbo.loadbalance=weightedrandom
# 服务消费者负载均衡策略
# 随机(Random)、最少活跃数(LeastActive)、一致性哈希(ConsistentHash)、轮询(RoundRobin)
# 加权轮询(WeightedRoundRobin)、加权随机(WeightedRandom)
dubbo.consumer.loadbalance=random
其中,dubbo.loadbalance配置了服务提供者使用的负载均衡策略,本文以加权随机为例。dubbo.consumer.loadbalance配置了服务消费者使用的负载均衡策略,本文以随机为例。
启动Zookeeper服务器,并运行服务提供者和服务消费者。访问http://localhost:8080/hello/world,可以看到服务消费者使用了加权随机的方式选择了一个服务提供者进行调用。
至此,我们完成了Dubbo负载均衡策略的介绍与实践。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。