dubbo 负载均衡
官网:https://dubbo.apache.org/zh/docs/advanced/loadbalance/
负载均衡
负载均衡:消费端调用服务时,在路由过滤后的服务列表中使用负载均衡算法选择一个服务调用
负载均衡策略
# RandomLoadBalance:随机负载均衡 This class select one provider from multiple providers randomly. * You can define weights for each provider 随机选取一个可用服务,可以给服务设置权重 # RoundRobinLoadBalance:轮询 按照列表顺序选取服务,可对服务设置权重 # LeastActiveLoadBalance:最少连接数 Filter the number of invokers with the least number of active calls and count the weights and quantities of these invokers. 选取最少连接的服务,可对服务设置权重 # ConsistentHashLoadBalance:一致性hash 构建hash环,将请求放置在hash环上, 对请求hash,沿顺时针方向查找第一个服务节点, 参数相同的请求由相同的服务器处理,如果服务下线,请求会由原服务顺时针方向第一个可用的服务器处理; 同时添加虚拟节点,使得请求分散的更均匀 # ShortestResponseLoadbalance:最短响应时间 Filter the number of invokers with the shortest response time ofsuccess calls and count the weights and quantities of these invokers in last slide window. 选取最短响应时间的服务,并可设置权重
LoadBalance:负载均衡接口
@SPI(RandomLoadBalance.NAME) //默认为RandomLoadBalance,随机选取
public interface LoadBalance {
@Adaptive("loadbalance")
Invoker select(List> invokers, URL url, Invocation invocation) throws RpcException;
}
配置示例
@DubboReference:消费端服务调用注解
@documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.ANNOTATION_TYPE})
public @interface DubboReference {
Class> interfaceClass() default void.class;
String interfaceName() default "";
String group() default ""; //服务分组
String version() default ""; //服务版本
String url() default ""; //直连服务提供端(如果配置了,则不使用注册中心)
String client() default ""; //客户端传输方式,默认为netty
@Deprecated
boolean generic() default false; //已禁用
@Deprecated
boolean injvm() default true; //已禁用
boolean check() default true;
boolean init() default true;
boolean lazy() default false;
boolean stubevent() default false;
String reconnect() default "";
boolean sticky() default false;
String proxy() default "";
String stub() default "";
String cluster() default ClusterRules.EMPTY;
int connections() default -1;
int callbacks() default -1;
String onconnect() default "";
String ondisconnect() default "";
String owner() default "";
String layer() default "";
int retries() default -1;
String loadbalance() default LoadbalanceRules.EMPTY; //负载均衡策略
boolean async() default false;
int actives() default -1;
boolean sent() default false;
String mock() default "";
String validation() default "";
int timeout() default -1;
String cache() default "";
String[] filter() default {};
String[] listener() default {};
String[] parameters() default {};
@Deprecated
String application() default "";
String module() default "";
String consumer() default "";
String monitor() default "";
String[] registry() default {};
String protocol() default "";
String tag() default "";
String merger() default "";
Method[] methods() default {};
String id() default "";
@Deprecated
String[] services() default {};
String[] providedBy() default {};
String scope() default "";
boolean referAsync() default false;
}
LoadBalancerRules:负载均衡枚举
public interface LoadbalanceRules {
String RANDOM = "random";
String ROUND_ROBIN = "roundrobin";
String LEAST_ACTIVE = "leastactive";
String CONSISTENT_HASH = "consistenthash";
String SHORTEST_RESPonSE = "shortestresponse";
String EMPTY = "";
}
HelloController
@RestController
public class HelloController {
@DubboReference(loadbalance = LoadbalanceRules.ROUND_ROBIN) //使用轮询负载均衡
private HelloService helloService;
@RequestMapping("/hello")
public String hello(){
helloService.hello().forEach(System.out::print);
return "hello consumer 2";
}
}



