版本权重功能的详细设计-下-功能的执行
本章节将详细介绍如何根据权重值来选择服务
还是直接来到 LinkFlowRoundRobinLoadBalancer#getInstanceResponse 方法
WeightInfoWrapper weightInfoWrapper = linkFlowWeight.parseWeightInfo();
if (linkFlowWeight.isServiceWeight(instances,weightInfoWrapper)) {
ServiceInstance serviceInstance = linkFlowWeight.selectServiceInstance(instances, weightInfoWrapper);
instances.clear();
instances.add(serviceInstance);
}
本章节就是要从 linkFlowWeight.selectServiceInstance(instances, weightInfoWrapper) 这里开始进行分析
版本权重的执行
WeightDefinitionInfo 的作用是将服务实例和对应的权重值关联起来
@Data
@AllArgsConstructor
public class WeightDefinitionInfo {
private ServiceInstance serviceInstance;
private Integer weightValue;
}
public ServiceInstance selectServiceInstance(List<ServiceInstance> servers, WeightInfoWrapper weightInfoWrapper){
//解析权重信息,将服务实例和权重值封装到一个对象中,然后放到集合中
List<WeightDefinitionInfo> weightDefinitionInfoList = servers.stream().map(serviceInstance -> {
//解析权重值
int weightValue = getWeightValue(serviceInstance, weightInfoWrapper);
//将服务实例和权重值进行封装
return new WeightDefinitionInfo(serviceInstance, weightValue);
}).toList();
//创建一个权重随机选择器
WeightedRandomSelector weightedRandomSelector = new WeightedRandomSelector(weightDefinitionInfoList);
//获取随机的服务实例
return weightedRandomSelector.getRandomServer();
}
版本权重的解析
关于解析部分的在上一章节已经做了详细讲解,这里就不再重复了,当获取到具体的权重值后,就会将服务实例和权重值封装到 WeightDefinitionInfo 对象里
付费内容提示
该文档的全部内容仅对「JavaUp项目实战&技术讲解」知识星球用户开放
加入星球后,你可以获得:
- 超级八股文:100万+字的全栈技术知识库,涵盖技术核心、数据库、中间件、分布式等深度剖析的讲解
- 讲解文档:黑马点评Plus、大麦、大麦pro、大麦AI、流量切换、数据中台的从0到1的550+详细文档
- 讲解视频:黑马点评Plus、大麦、大麦pro、大麦AI、流量切换、数据中台的核心业务详细讲解
- 1 对 1 解答:可以对我进行1对1的问题提问,而不仅仅只限于项目
- 针对性服务:有没理解的地方,文档或者视频还没有讲到可以提出,本人会补充
- 面试与简历指导:提供面试回答技巧,项目怎样写才能在简历中具有独特的亮点
- 中间件环境:对于项目中需要使用的中间件,可直接替换成我提供的云环境
- 面试后复盘:小伙伴去面试后,如果哪里被面试官问住了,可以再找我解答
- 远程的解决:如果在启动项目遇到问题,本人可以帮你远程解决
进入星球后,即可享受上述所有服务,保证不会再有其他隐藏费用。
