dubbo 源码解析(二)地址推送流程

566 阅读4分钟

去年看到社区上一个issue,一直拖到现在才尝试进行解决。并且趁这个机会理解dubbo3的新特性 应用级地址推送。也为接下来梳理dubbo-go-pixiu的源码细节做铺垫

issue-9194: github.com/apache/dubb… issue主旨:在多个注册中心中 服务映射失败

社区上负责dubbo3开发的同学认为,这本应该是一个配置中心与元数据中心配置的group属性需要保持一致的问题。但追踪调用链下来其实不止。下面先展开provider侧的调用链路 来验证开发group属性保持一致的需求

dubbo3整体架构: www.processon.com/view/link/6…

风轮-Dubbo3源码流程图.png

dubbo3 地址推动验证文档: www.yuque.com/apache-dubb…

12345.png

看到这个图有些懵了对吧,毕竟有些名词在2.7之前的版本里从未见过

本文将会分为三部分

  • 对接k8s的地址推送设计
  • 在服务注册当中具体的实现时序详解
  • 如何定位并解决相关标签路由根据环境(dev/prod/test)隔离服务发布的分组功能 (正在进行中,目前先写实现思路和代码位置)

先来思考一下 一个简单的rpc框架长什么样子呢? 无非就三个组件 服务提供者,注册中心,服务消费者。但是面向数据量越来越大的公司,微服务势必会拆分成更小的颗粒度。换言之 也就是服务提供者与服务消费者会增多。

那么问题来了,负责承担rpc调用的中介桥梁注册中心(这里假设是阿里的nacos,关于注册中心的选型请见我的另外一文浅析zookeeper的一致性分析).

  • 在多个服务实例的上线下线的时候,是否读写地址的压力也会增大?
  • 如果我们只针对服务级别进行划分,那么有一天应用A当中的服务接口 A' 调用到应用B的服务接口 B'。在注册的interface不同的时候 是否可以实现跨应用调用?
  • 如果有一天公司告诉你,需要根据服务的业务域切分不同比例的流量,要如何做到?

针对于第一个问题dubbo 3.0拆分出了元数据中心,用户有两种选择,可以在dubbo-admin设置相关的三大中心配置选项,也可以通过xml的方式,比如 dubbo源码工程下的dubbo-demo-api模块配置相关信息

文件路径: ~/dubbo/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/src/main/resources/spring/dubbo-provider.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
  -->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <dubbo:application name="demo-provider" metadata-type="remote">
        <dubbo:parameter key="mapping-type" value="metadata"/>
    </dubbo:application>

    <dubbo:config-center address="zookeeper://127.0.0.1:2181"/>
    <dubbo:metadata-report address="zookeeper://127.0.0.1:2181"/>
    <dubbo:registry id="registry1" address="zookeeper://127.0.0.1:2181" group="dubbo"/>



    <dubbo:protocol name="dubbo" port="-1"/>

    <bean id="demoService" class="org.apache.dubbo.demo.provider.DemoServiceImpl"/>
    <bean id="greetingService" class="org.apache.dubbo.demo.provider.GreetingServiceImpl"/>

    <dubbo:service interface="org.apache.dubbo.demo.DemoService" timeout="3000" ref="demoService" registry="registry1"/>
    <dubbo:service version="1.0.0" group="greeting" timeout="5000" interface="org.apache.dubbo.demo.GreetingService"
                   ref="greetingService"/>

</beans>

文件路径: ~/dubbo/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-consumer/src/main/resources/spring/dubbo-consumer.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
  -->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <dubbo:application name="demo-consumer" >
        <dubbo:parameter key="mapping-type" value="metadata"/>
        <dubbo:parameter key="enable-auto-migration" value="true"/>
    </dubbo:application>

    <dubbo:metadata-report address="zookeeper://yuluo-13:2181"/>

    <dubbo:registry address="zookeeper://yuluo-13:2181"/>

    <dubbo:reference id="demoService" check="true"
                     interface="org.apache.dubbo.demo.DemoService"/>

    <dubbo:reference version="1.0.0" group="greeting" id="greetingService" check="false"
                     interface="org.apache.dubbo.demo.GreetingService"/>

</beans>

其中 dubbo:application ,dubbo:metadata-report ,dubbo:config-center dubbo:registry分别为应用级地址标签,元数据中心,配置中心。来看看文档怎么介绍的

三大中心文档: dubbo.apache.org/zh/docs/con…

2022-02-02-20-17.png

服务上下线对注册中心的压力如何解决

在多个服务实例的上线下线的时候,也就是说服务提供者会在启动或者发布的时候,频繁注册服务实例,上述的provider文件提供了两个实例demoService GreetingService

2022-02-02-20-29-01.jpg

如果这个时候线上出现一个bug,需要多个服务滚动到之前的版本才能发布,那么就需要一个中心,具备承载记录注册中心元数据的信息。这个时候你需要根据不同业务接口进行选型,需要一致性为主的服务用zk,需要可用性为主的服务用nacos。比如服务接口A,B用zk,服务接口C,D用nacos。 而不同的注册中心需要一个地方设置心跳存活时间,重试次数等一些操作。元数据中心就是做这事的

应用级服务接口划分

如果我们只针对服务级别进行划分,那么有一天应用A当中的服务接口 A' 调用到应用B的服务接口 B'。在注册的interface不同的时候 是否可以实现跨应用调用

这里需要解释一下什么叫应用,就像我刚刚提到的,你可能有一组应用要用zk1,一组应用要用zk2,一组应用要用naocs1。这样的话会发现每组服务需要进行隔离,每组服务基于一个元数据中心进行服务暴露和发现

如何对接 kuberbenetes 到dubbo3 一文中展示了dubbo3的服务自省架构

2022-02-02-1972.png

revision说白了 就是对服务实例分组,也就是是一组服务组成的应用。