Soul-源码阅读9-divide插件2

回顾前天写的divide插件文章:Soul-源码阅读5-回顾总结插件相关

那天做了总结并分析了AbstractSoulPlugin方法(主要是选择器和规则的设定)。

今天深入看了divide插件是怎么根据selector和rule进行http请求的代理转发的。

DividePlugin

看之类DividePlugindoExecute()方法

  1. 根据选择器id获取selector配置的服务地址list, 如果为空会抛出NPE.

            final List<DivideUpstream> upstreamList = UpstreamCacheManager.getInstance().findUpstreamListBySelectorId(selector.getId());
            if (CollectionUtils.isEmpty(upstreamList)) {
                log.error("divide upstream configuration error: {}", rule.toString());
                Object error = SoulResultWrap.error(SoulResultEnum.CANNOT_FIND_URL.getCode(), SoulResultEnum.CANNOT_FIND_URL.getMsg(), null);
                return WebFluxResultUtils.result(exchange, error);
            }
    
    
  2. 根据selector配置的负载均衡策略、业务地址和请求IP,决定将http请求代理到哪个业务地址(具体可看AbstractLoadBalancedoSelect())。
    在这里插入图片描述

    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);
            }
    
  3. 构建真实调用的业务地址url(buildRealURL)。

            String domain = buildDomain(divideUpstream);
            String realURL = buildRealURL(domain, soulContext, exchange);
           
    
  4. 把上面3获得真实URL、超时时间、重试次数放到ServerWebExchangeattribute里。

     // set the http url、timeout、retry
      exchange.getAttributes().put(Constants.HTTP_URL, realURL);
            exchange.getAttributes().put(Constants.HTTP_TIME_OUT, ruleHandle.getTimeout());
            exchange.getAttributes().put(Constants.HTTP_RETRY, ruleHandle.getRetry());
    

    WebClientPlugin

从exchange里面取出了URL、超时时间、重试次数(urlPath、timeout、和retryTimes),并发送了http请求(代理到业务服务去)。

public Mono<Void> execute(final ServerWebExchange exchange, final SoulPluginChain chain) {
        final SoulContext soulContext = exchange.getAttribute(Constants.CONTEXT);
        assert soulContext != null;
        String urlPath = exchange.getAttribute(Constants.HTTP_URL);
       。。。
        long timeout = (long) Optional.ofNullable(exchange.getAttribute(Constants.HTTP_TIME_OUT)).orElse(3000L);
        int retryTimes = (int) Optional.ofNullable(exchange.getAttribute(Constants.HTTP_RETRY)).orElse(0);
        log.info("The request urlPath is {}, retryTimes is {}", urlPath, retryTimes);
        HttpMethod method = HttpMethod.valueOf(exchange.getRequest().getMethodValue());
        WebClient.RequestBodySpec requestBodySpec = webClient.method(method).uri(urlPath);
        return handleRequestBody(requestBodySpec, exchange, timeout, retryTimes, chain);
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值