关于java:Soul-API-网关源码解析-03

3次阅读

共计 8555 个字符,预计需要花费 22 分钟才能阅读完成。

指标

  • 应用 soul 代理 dubbo 服务
  • dubbo 服务如何注册到网关的?
  • dubbo 插件是如何工作的?
  • 理清 http –> 网关 –> dubbo provider 整条链路经验了什么。
  • 总结

一、应用 soul 代理 dubbo 服务

1、dubbo 服务接入网关

1.1 springboot 我的项目接入形式

1.1.1 依赖引入

alibaba dubbo 用户

       <dependency>
            <groupId>org.dromara</groupId>
            <artifactId>soul-spring-boot-starter-client-alibaba-dubbo</artifactId>
            <version>${last.version}</version>
       </dependency>

apache dubbo 用户

       <dependency>
            <groupId>org.dromara</groupId>
            <artifactId>soul-spring-boot-starter-client-apache-dubbo</artifactId>
            <version>${last.version}</version>
       </dependency>

1.1.2 yml 配置

   soul:
     dubbo:
       adminUrl: http://localhost:9095
       contextPath: /dubbo
       appName: dubbo
1.2 spring 我的项目接入形式

1.2.1 依赖引入

alibaba dubbo 用户

      <dependency>
          <groupId>org.dromara</groupId>
          <artifactId>soul-client-alibaba-dubbo</artifactId>
          <version>${last.version}</version>
       </dependency>

apache dubbo 用户

      <dependency>
          <groupId>org.dromara</groupId>
          <artifactId>soul-client-apache-dubbo</artifactId>
          <version>${last.version}</version>
       </dependency>

1.2.2 xml 配置

alibaba dubbo 用户

        <bean id ="alibabaDubboServiceBeanPostProcessor" class ="org.dromara.soul.client.alibaba.dubbo.AlibabaDubboServiceBeanPostProcessor">
             <constructor-arg  ref="dubboConfig"/>
        </bean>

        <bean id="dubboConfig" class="org.dromara.soul.client.dubbo.common.config.DubboConfig">
             <property name="adminUrl" value="http://localhost:9095"/>
             <property name="contextPath" value="/ 你的 contextPath"/>
             <property name="appName" value="你的名字"/>
        </bean>

apache dubbo 用户

         <bean id ="apacheDubboServiceBeanPostProcessor" class ="org.dromara.soul.client.apache.dubbo.ApacheDubboServiceBeanPostProcessor">
              <constructor-arg  ref="dubboConfig"/>
         </bean>

         <bean id="dubboConfig" class="org.dromara.soul.client.dubbo.common.config.DubboConfig">
              <property name="adminUrl" value="http://localhost:9095"/>
              <property name="contextPath" value="/ 你的 contextPath"/>
              <property name="appName" value="你的名字"/>
         </bean>

2、配置 dubbo 插件

启动 soul-adminsoul-bootstrap,返回后盾插件治理页面

启用 dubbo 插件,并配置注册核心 ip + 端口

3、注册 dubbo 服务到网关

dubbo 服务实现类的办法上加上 @SoulDubboClient 注解

    @Override
    @SoulDubboClient(path = "/findById", desc = "Query by Id")
    public DubboTest findById(final String id) {DubboTest dubboTest = new DubboTest();
        dubboTest.setId(id);
        dubboTest.setName("hello world Soul Alibaba Dubbo, findById");
        return dubboTest;
    }

启动 dubbo 提供者我的项目,日志输入 dubbo client register success 注册胜利

4、http 形式申请 dubbo 服务

通过 http post 形式申请网关,通过 body 传递 json 格局参数(contextPath=/dubbo)

dubbo 服务拜访胜利,同时 soul-bootstrap 输入匹配日志

dubbo selector success match , selector name :/dubbo
dubbo selector success match , selector name :/dubbo/findById

二、dubbo 服务如何注册到网关?

alibaba dubbo 与 apache dubbo 差别较小,alibaba dubbo 的注册流程弄明确后,apache dubbo 也天然分明了。

从控制台日志 dubbo client register success 定位到 RegisterUtils 类,在打印注册日志的中央加上断点

重启我的项目,能够看到胜利进入断点,在此处调用 OkHttpTools.post 进行服务注册。

留神此处是注册到 soul-admin,再由网关和 soul-admin 的数据同步机制同步到网关内存。

注册的元数据怎么拿到的呢?

从堆栈定位到 doRegister 办法的调用方 AlibabaDubboServiceBeanPostProcessor.handler

    private void handler(final ServiceBean<?> serviceBean) {Class<?> clazz = serviceBean.getRef().getClass();
        // 如果是 cglib 代理,则取带有泛型的父类
        if (ClassUtils.isCglibProxyClass(clazz)) {String superClassName = clazz.getGenericSuperclass().getTypeName();
            try {clazz = Class.forName(superClassName);
            } catch (ClassNotFoundException e) {log.error(String.format("class not found: %s", superClassName));
                return;
            }
        }
        final Method[] methods = ReflectionUtils.getUniqueDeclaredMethods(clazz);
        for (Method method : methods) {
            // 如果办法上有 SoulDubboClient 注解,则调用下面的 doRegister 办法注册元数据信息
            SoulDubboClient soulDubboClient = method.getAnnotation(SoulDubboClient.class);
            if (Objects.nonNull(soulDubboClient)) {RegisterUtils.doRegister(buildJsonParams(serviceBean, soulDubboClient, method), url, RpcTypeEnum.DUBBO);
            }
        }
    }

handler 办法通过辨认 serviceBean 上的 SoulDubboClient 来判断须要注册哪些服务的元数据。

留神到这里应用 buildJsonParams 来结构元数据,其内容如下:

    private String buildJsonParams(final ServiceBean<?> serviceBean, final SoulDubboClient soulDubboClient, final Method method) {
        // 从 dubboConfig 取 appName,若未配置则应用 applicationName
        String appName = dubboConfig.getAppName();
        if (StringUtils.isEmpty(appName)) {appName = serviceBean.getApplication().getName();}
        String path = dubboConfig.getContextPath() + soulDubboClient.path();
        String desc = soulDubboClient.desc();
        String serviceName = serviceBean.getInterface();
        String configRuleName = soulDubboClient.ruleName();
        String ruleName = ("".equals(configRuleName)) ? path : configRuleName;
        String methodName = method.getName();
        Class<?>[] parameterTypesClazz = method.getParameterTypes();
        // 拼装办法参数类型名
        String parameterTypes = Arrays.stream(parameterTypesClazz).map(Class::getName)
                .collect(Collectors.joining(","));
        // 结构元数据并序列化为 json 串
        MetaDataDTO metaDataDTO = MetaDataDTO.builder()
                .appName(appName)
                .serviceName(serviceName)
                .methodName(methodName)
                .contextPath(dubboConfig.getContextPath())
                .path(path)
                .ruleName(ruleName)
                .pathDesc(desc)
                .parameterTypes(parameterTypes)
                .rpcExt(buildRpcExt(serviceBean))
                .rpcType("dubbo")
                .enabled(soulDubboClient.enabled())
                .build();
        return OkHttpTools.getInstance().getGson().toJson(metaDataDTO);

    }

持续回到下面的 handler,通过堆栈找到其调用方 AlibabaDubboServiceBeanPostProcessor.handler

    @Override
    public void onApplicationEvent(final ContextRefreshedEvent contextRefreshedEvent) {if (Objects.nonNull(contextRefreshedEvent.getApplicationContext().getParent())) {return;}
        // Fix bug(https://github.com/dromara/soul/issues/415), upload dubbo metadata on ContextRefreshedEvent
        Map<String, ServiceBean> serviceBean = contextRefreshedEvent.getApplicationContext().getBeansOfType(ServiceBean.class);
        for (Map.Entry<String, ServiceBean> entry : serviceBean.entrySet()) {executorService.execute(() -> handler(entry.getValue()));
        }
    }

通过实现 ApplicationListener<ContextRefreshedEvent> 接口监听容器刷新事件,容器刷新后取到 ServiceBean 调用 handler 进行解决。

至此,alibaba dubbo 服务注册到网关的流程梳理分明:

1)AlibabaDubboServiceBeanPostProcessor 监听到容器刷新后,从 spring 上下文取出 ServiceBean 调用 handler 进行解决

2)handler 拿到 ServiceBean 的接口或带泛型父类带 @SoulDubboClient 的办法,调用 RegisterUtils.doRegister 进行注册

3)RegisterUtils.doRegister 应用 OkHttpTools 将元数据 post 到 soul-admin 落库

4)soul-admin 再将元数据同步到网关内存

三、dubbo 插件如何工作的?

在此就不开展插件的对立工作流程了,只聚焦于 dubbo 插件自身的工作内容。

关上 soul-plugin-alibaba-dubbo 插件工程,定位到 AlibabaDubboPlugin.doExecute

    @Override
    protected Mono<Void> doExecute(final ServerWebExchange exchange, final SoulPluginChain chain, final SelectorData selector, final RuleData rule) {String body = exchange.getAttribute(Constants.DUBBO_PARAMS);
        SoulContext soulContext = exchange.getAttribute(Constants.CONTEXT);
        assert soulContext != null;
        MetaData metaData = exchange.getAttribute(Constants.META_DATA);
        // 元数据查看未通过解决
        if (!checkMetaData(metaData)) {
            assert metaData != null;
            log.error("path is :{}, meta data have error.... {}", soulContext.getPath(), metaData.toString());
            exchange.getResponse().setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
            Object error = SoulResultWrap.error(SoulResultEnum.META_DATA_ERROR.getCode(), SoulResultEnum.META_DATA_ERROR.getMsg(), null);
            return WebFluxResultUtils.result(exchange, error);
        }
        // 申请体缺失解决
        if (StringUtils.isNoneBlank(metaData.getParameterTypes()) && StringUtils.isBlank(body)) {exchange.getResponse().setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
            Object error = SoulResultWrap.error(SoulResultEnum.DUBBO_HAVE_BODY_PARAM.getCode(), SoulResultEnum.DUBBO_HAVE_BODY_PARAM.getMsg(), null);
            return WebFluxResultUtils.result(exchange, error);
        }
        // dubbo 泛化调用
        Object result = alibabaDubboProxyService.genericInvoker(body, metaData);
        if (Objects.nonNull(result)) {exchange.getAttributes().put(Constants.DUBBO_RPC_RESULT, result);
        } else {exchange.getAttributes().put(Constants.DUBBO_RPC_RESULT, Constants.DUBBO_RPC_RESULT_EMPTY);
        }
        exchange.getAttributes().put(Constants.CLIENT_RESPONSE_RESULT_TYPE, ResultEnum.SUCCESS.getName());
        return chain.execute(exchange);
    }

能够看到,进入 dubbo 插件解决当前,最外围的是从 exchange 拿到元数据和申请体后进行 dubbo 泛化调用。

进入 AlibabaDubboProxyService.genericInvoker

    public Object genericInvoker(final String body, final MetaData metaData) throws SoulException {
        // 拿到 ReferenceConfig
        ReferenceConfig<GenericService> reference = ApplicationConfigCache.getInstance().get(metaData.getPath());
        if (Objects.isNull(reference) || StringUtils.isEmpty(reference.getInterface())) {ApplicationConfigCache.getInstance().invalidate(metaData.getPath());
            reference = ApplicationConfigCache.getInstance().initRef(metaData);
        }
        // 组织办法名、参数类型和参数,再交由 genericService 进行泛化调用
        GenericService genericService = reference.get();
        try {Pair<String[], Object[]> pair;
            if (ParamCheckUtils.dubboBodyIsEmpty(body)) {pair = new ImmutablePair<>(new String[]{}, new Object[]{});
            } else {pair = dubboParamResolveService.buildParameter(body, metaData.getParameterTypes());
            }
            return genericService.$invoke(metaData.getMethodName(), pair.getLeft(), pair.getRight());
        } catch (GenericException e) {log.error("dubbo invoker have exception", e);
            throw new SoulException(e.getExceptionMessage());
        }
    }

能够看到,AlibabaDubboProxyService 只是封装了 alibaba 的 GenericService,先组织办法名、参数类型和参数,再交由 genericService 进行泛化调用。

四、总结

本篇从应用侧先梳理 dubbo 服务接入流程,再从 dubbo 服务注册到网关以及 dubbo 插件的工作内容进行剖析,最终将 soul 代理 dubbo 服务的整个脉络串起来。

1)客户端公布 dubbo 服务,同时注册 dubbo 服务元数据到 soul-admin

2)soul-admin 同步元数据到 网关 jvm 内存

3)客户向网关发动 http 申请,拜访指标为后端 dubbo 服务

4)网关拿到 requestBody 和内存中的元数据,解析成办法名、参数类型和参数值后发动 dubbo 泛化调用

5)客户端响应 dubbo 泛化调用并回吐响应报文

下一篇,将剖析 soul 如何代理 sofa-rpc 服务。

正文完
 0