当前位置: 代码迷 >> 综合 >> Soul 源码阅读之 divide 插件源码
  详细解决方案

Soul 源码阅读之 divide 插件源码

热度:56   发布时间:2023-12-15 21:34:14.0

divide 的入口逻辑(省略了非本次文章重点)如下:

	protected Mono<Void> doExecute(final ServerWebExchange exchange, final SoulPluginChain chain, final SelectorData selector, final RuleData rule) {
    final SoulContext soulContext = exchange.getAttribute(Constants.CONTEXT);assert soulContext != null;final DivideRuleHandle ruleHandle = GsonUtils.getInstance().fromJson(rule.getHandle(), DivideRuleHandle.class);// 获取选择器定义的路由列表final List<DivideUpstream> upstreamList = UpstreamCacheManager.getInstance().findUpstreamListBySelectorId(selector.getId());...// 获取负载均衡策略DivideUpstream divideUpstream = LoadBalanceUtils.selector(upstreamList, ruleHandle.getLoadBalance(), ip);if (Objects.isNull(divideUpstream)) {
    log.error("divide has no upstream");Object error = SoulResultWrap.error(SoulResultEnum.CANNOT_FIND_URL.getCode(), SoulResultEnum.CANNOT_FIND_URL.getMsg(), null);return WebFluxResultUtils.result(exchange, error);}// 根据负载均衡策略最终确定的实际 URL 信息,写入上下文中// set the http urlString domain = buildDomain(divideUpstream);String realURL = buildRealURL(domain, soulContext, exchange);exchange.getAttributes().put(Constants.HTTP_URL, realURL);// set the http timeoutexchange.getAttributes().put(Constants.HTTP_TIME_OUT, ruleHandle.getTimeout());exchange.getAttributes().put(Constants.HTTP_RETRY, ruleHandle.getRetry());return chain.execute(exchange);}

其中有两处关键信息,首先是路由信息的缓存,调用了 UpstreamCacheManager。这个类会在每次 admin 修改选择器内容后使用如下代码更新缓存(这里涉及到数据同步的内容,会在后续的文章中陆续解析):

	public void submit(final SelectorData selectorData) {
    final List<DivideUpstream> upstreamList = GsonUtils.getInstance().fromList(selectorData.getHandle(), DivideUpstream.class);if (null != upstreamList && upstreamList.size() > 0) {
    UPSTREAM_MAP.put(selectorData.getId(), upstreamList);UPSTREAM_MAP_TEMP.put(selectorData.getId(), upstreamList);} else {
    UPSTREAM_MAP.remove(selectorData.getId());UPSTREAM_MAP_TEMP.remove(selectorData.getId());}}

接下来,则调用了 LoadBalanceUtils.selector 方法在给出的 upstream 列表中选择最终的 URL 路径。
负载均衡策略被封装在 org.dromara.soul.plugin.divide.balance.spi 包中。

RandomLoadBalance

根据权重进行随机路由。

	public DivideUpstream doSelect(final List<DivideUpstream> upstreamList, final String ip) {
    // 计算所有 upstream 的总权重int totalWeight = calculateTotalWeight(upstreamList);// 判断各个 upstream 的权重是否相同boolean sameWeight = isAllUpStreamSameWeight(upstreamList);if (totalWeight > 0 && !sameWeight) {
    return random(totalWeight, upstreamList);}// If the weights are the same or the weights are 0 then randomreturn random(upstreamList);}private DivideUpstream random(final int totalWeight, final List<DivideUpstream> upstreamList) {
    // If the weights are not the same and the weights are greater than 0, then random by the total number of weights// 计算一个随机数作为偏移量int offset = RANDOM.nextInt(totalWeight);// Determine which segment the random value falls on// 循环判断得出的随机数最终落在哪里for (DivideUpstream divideUpstream : upstreamList) {
    offset -= getWeight(divideUpstream);if (offset < 0) {
    return divideUpstream;}}return upstreamList.get(0);}
  相关解决方案