欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

dubbo的调用原理及泛化调用

程序员文章站 2024-02-21 12:16:34
...

简单介绍

dubbo是阿里开源出来的一个rpc框架,主要是用于微服务分布式项目的远程调用,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现,下面是调用的原理图:
dubbo的调用原理及泛化调用
dubbo框架的整体设计:
dubbo的调用原理及泛化调用
图例说明:

1,图中左边淡蓝背景的为服务消费方使用的接口,右边淡绿色背景的为服务提供方使用的接口,位于中轴线上的为双方都用到的接口。
2,图中从下至上分为十层,各层均为单向依赖,右边的黑色箭头代表层之间的依赖关系,每一层都可以剥离上层被复用,其中,Service 和 Config 层为 API,其它各层均为 SPI。
3,图中绿色小块的为扩展接口,蓝色小块为实现类,图中只显示用于关联各层的实现类。
4,图中蓝色虚线为初始化过程,即启动时组装链,红色实线为方法调用过程,即运行时调时链,紫色三角箭头为继承,可以把子类看作父类的同一个节点,线上的文字为调用的方法

远程调用细节

服务提供者暴露一个服务的详细过程

dubbo的调用原理及泛化调用
上图是服务提供者暴露服务的主过程:

首先 ServiceConfig 类拿到对外提供服务的实际类 ref(如:HelloWorldImpl),然后通过 ProxyFactory 类的 getInvoker 方法使用 ref 生成一个 AbstractProxyInvoker 实例,到这一步就完成具体服务到 Invoker 的转化。接下来就是 Invoker 转换到 Exporter 的过程。

Dubbo 处理服务暴露的关键就在 Invoker 转换到 Exporter 的过程,上图中的红色部分。下面我们以 Dubbo 和 RMI 这两种典型协议的实现来进行说明:

Dubbo 的实现
Dubbo 协议的 Invoker 转为 Exporter 发生在 DubboProtocol 类的 export 方法,它主要是打开 socket 侦听服务,并接收客户端发来的各种请求,通讯细节由 Dubbo 自己实现。

RMI 的实现
RMI 协议的 Invoker 转为 Exporter 发生在 RmiProtocol类的 export 方法,它通过 Spring 或 Dubbo 或 JDK 来实现 RMI 服务,通讯细节这一块由 JDK 底层来实现,这就省了不少工作量。

服务消费者消费一个服务的详细过程

dubbo的调用原理及泛化调用
上图是服务消费的主过程:

首先 ReferenceConfig 类的 init 方法调用 Protocol 的 refer 方法生成 Invoker 实例(如上图中的红色部分),这是服务消费的关键。接下来把 Invoker 转换为客户端需要的接口(如:HelloWorld)。

dubbo直接引用

1,xml形式

服务提供者:

public interface DemoService {
    String sayHello(String name);
}

具体实现:

public class DemoServiceImpl implements DemoService {
    public String sayHello(String name) {
        return "Hello " + name;
    }
}

spring声明暴露服务:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
    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="hello-world-app"  />
 
    <!-- 使用multicast广播注册中心暴露服务地址 -->
    <dubbo:registry address="multicast://224.5.6.7:1234" />
 
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" />
 
    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="org.apache.dubbo.demo.DemoService" ref="demoService" />
 
    <!-- 和本地bean一样实现服务 -->
    <bean id="demoService" class="org.apache.dubbo.demo.provider.DemoServiceImpl" />
</beans>

服务消费者:
通过 Spring 配置引用远程服务:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
    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="consumer-of-helloworld-app"  />
 
    <!-- 使用multicast广播注册中心暴露发现服务地址 -->
    <dubbo:registry address="multicast://224.5.6.7:1234" />
 
    <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
    <dubbo:reference id="demoService" interface="org.apache.dubbo.demo.DemoService" />
</beans>

远程调用:

public class Consumer {
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"http://10.20.160.198/wiki/display/dubbo/consumer.xml"});
        context.start();
        DemoService demoService = (DemoService)context.getBean("demoService"); // 获取远程服务代理
        String hello = demoService.sayHello("world"); // 执行远程方法
        System.out.println( hello ); // 显示调用结果
    }
}
2,api形式

提供者暴露服务:

/**
 * @author shihaowei
 * @date 2020-06-11 11:43
 */
@Configuration
//@EnableDubbo
@DubboComponentScan("person.shw.dubbo.provider")
public class DubboConfig {

    @Value("${nacos.data.id:f21d3b6b-20ed-4091-ab17-ac073abc3eba}")
    private String namespace;

    @Bean
    public ApplicationConfig applicationConfig(){
        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setName("provider-nacos");
        return applicationConfig;
    }

    @Bean
    public RegistryConfig registryConfig(){
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setAddress("nacos://120.79.76.230:8848");
        registryConfig.setGroup("ONE_GROUP");
        Map<String,String> paramMap = new HashMap<String, String>();
        paramMap.put("namespace",namespace);
        registryConfig.setParameters(paramMap);
        return registryConfig;
    }


    @Bean
    public ProtocolConfig protocolConfig(){
        ProtocolConfig protocolConfig = new ProtocolConfig();
        protocolConfig.setName("dubbo");
        protocolConfig.setPort(12347);
        return protocolConfig;
    }

}

暴露服务的接口实现:

import org.apache.dubbo.config.annotation.Service;
import person.shw.dubbo.api.DubboApi;

/**
 * @author shihaowei
 * @date 2020-06-09 17:21
 */
@Service
public class ProviderServiceImpl implements DubboApi {
    public String getConsumerData() {
        return "333333333333333333";
    }
}

org.apache.dubbo.config.annotation.Service是引用的dubbo的包,而不是Spring的@Service

消费者消费服务:

@Configuration
public class DubboNacosConfig {

    @Bean
    public ApplicationConfig applicationConfig(RegistryConfig registryConfig){
        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setName("consumer-nacos");
        applicationConfig.setRegistry(registryConfig);
        return applicationConfig;
    }

    @Bean
    public RegistryConfig registryConfig(){
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setCheck(false);
        registryConfig.setAddress("nacos://120.79.76.230:8848");
        registryConfig.setUsername("nacos");
        registryConfig.setPassword("nacos");
        Map<String,String> paramMap = new HashMap<String, String>();
        paramMap.put("namespace","f21d3b6b-20ed-4091-ab17-ac073abc3eba");
        registryConfig.setParameters(paramMap);
        return registryConfig;
    }
}

调用服务

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import person.shw.dubbo.api.DubboApi;

/**
 * @author shihaowei
 * @date 2020-06-09 17:22
 */
@RestController
public class ConsumerController {

    @Reference
    private DubboApi dubboApi;

    @GetMapping(value = "/consumer/getMessage")
    public String getMessage(){
        return "【收到发来的消息】---->"+dubboApi.getConsumerData();
    }

}

dubbo的泛化调用

为什么要使用泛化调用?

一般使用dubbo,provider端需要暴露出接口和方法,consumer端要十分明确服务使用的接口定义和方法定义(还有入参返参类型等等信息,常常还需要基于provider端提供的API),两端才能正常通信调用。

然而存在一种使用场景,调用方并不关心要调用的接口的详细定义,它只关注我要调用哪个方法,需要传什么参数,我能接收到什么返回结果即可,这样可以大大降低consumer端和provider端的耦合性。

所以为了应对以上的需求,dubbo提供了泛化调用,也就是在consumer只知道一个接口全限定名以及入参和返参的情况下,就可以调用provider端的调用,而不需要传统的接口定义这些繁杂的结构。

比如微服务的网关就可以采用泛化调用,又不需要强引用众多需要调用的服务

例子

继续使用上面使用api的配置,当我调用的时候,不时用@reference注解,而是用genericservice泛化调用,参数及返回值中的所有 POJO 均用 Map 表示,通常用于框架集成

@RestController
public class ConsumerController {

    @Autowired
    private ApplicationConfig applicationConfig;

    @GetMapping(value = "/consumer/getMessage")
    public String getMessage(){
        /** dubbo的泛化调用 */
        ReferenceConfig<GenericService> reference = new ReferenceConfig<GenericService>();
        reference.setApplication(applicationConfig);
        reference.setInterface("person.shw.dubbo.api.DubboApi");
        reference.setTimeout(200000);
        reference.setGeneric(true);
        /** 添加缓存策略 */
        /*ReferenceConfigCache cache = ReferenceConfigCache.getCache();
        GenericService genericService = cache.get(reference);*/
        GenericService genericService = reference.get();

        /** 设置接口所需要的参数类型 */
        String[] parametertypes = new String[]{};
        String[] args = new String[]{};
        Object data = genericService.$invoke("getConsumerData", parametertypes, args);
        System.out.println(JSON.toJSONString(data));
        return "【收到consumer发来的消息】---->"+JSON.toJSONString(data);
    }

}

调用服务,打印日志

2020-06-20 17:44:25.482  INFO 3033 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 924 ms
2020-06-20 17:44:25.664  WARN 3033 --- [           main] org.apache.dubbo.config.AbstractConfig   :  [DUBBO] There's no valid metadata config found, if you are using the simplified mode of registry url, please make sure you have a metadata address configured properly., dubbo version: 2.7.3, current host: 192.168.31.132
2020-06-20 17:44:25.669  INFO 3033 --- [           main] org.apache.dubbo.config.AbstractConfig   :  [DUBBO] There's no valid monitor config found, if you want to open monitor statistics for Dubbo, please make sure your monitor is configured properly., dubbo version: 2.7.3, current host: 192.168.31.132
2020-06-20 17:44:25.689  INFO 3033 --- [           main] o.a.d.qos.protocol.QosProtocolWrapper    :  [DUBBO] qos won't be started because it is disabled. Please check dubbo.application.qos.enable is configured either in system property, dubbo.properties or XML/spring-boot configuration., dubbo version: 2.7.3, current host: 192.168.31.132
2020-06-20 17:44:25.786  INFO 3033 --- [           main] o.a.dubbo.registry.nacos.NacosRegistry   :  [DUBBO] Load registry cache file /Users/edz/.dubbo/dubbo-registry-consumer-nacos-120.79.76.230:8848.cache, data: {person.shw.dubbo.api.DubboApi=dubbo://192.168.31.132:12347/person.shw.dubbo.api.DubboApi?anyhost=true&application=provider-nacos&bean.name=ServiceBean:person.shw.dubbo.api.DubboApi&category=providers&deprecated=false&dubbo=2.0.2&dynamic=true&generic=false&interface=person.shw.dubbo.api.DubboApi&methods=getConsumerData&path=person.shw.dubbo.api.DubboApi&pid=2846&protocol=dubbo&register=true&release=2.7.3&side=provider&timestamp=1592646130152}, dubbo version: 2.7.3, current host: 192.168.31.132
2020-06-20 17:44:25.804  INFO 3033 --- [           main] o.a.dubbo.registry.nacos.NacosRegistry   :  [DUBBO] Register: consumer://192.168.31.132/org.apache.dubbo.rpc.service.GenericService?application=consumer-nacos&category=consumers&check=false&dubbo=2.0.2&generic=true&interface=person.shw.dubbo.api.DubboApi&lazy=false&pid=3033&qos.enable=false&release=2.7.3&side=consumer&sticky=false&timeout=200000&timestamp=1592646265664, dubbo version: 2.7.3, current host: 192.168.31.132
2020-06-20 17:44:25.967  INFO 3033 --- [           main] o.a.dubbo.registry.nacos.NacosRegistry   :  [DUBBO] Subscribe: consumer://192.168.31.132/org.apache.dubbo.rpc.service.GenericService?application=consumer-nacos&category=providers,configurators,routers&dubbo=2.0.2&generic=true&interface=person.shw.dubbo.api.DubboApi&lazy=false&pid=3033&qos.enable=false&release=2.7.3&side=consumer&sticky=false&timeout=200000&timestamp=1592646265664, dubbo version: 2.7.3, current host: 192.168.31.132
2020-06-20 17:44:26.009  INFO 3033 --- [           main] o.a.dubbo.registry.nacos.NacosRegistry   :  [DUBBO] Notify urls for subscribe url consumer://192.168.31.132/org.apache.dubbo.rpc.service.GenericService?application=consumer-nacos&category=providers,configurators,routers&dubbo=2.0.2&generic=true&interface=person.shw.dubbo.api.DubboApi&lazy=false&pid=3033&qos.enable=false&release=2.7.3&side=consumer&sticky=false&timeout=200000&timestamp=1592646265664, urls: [dubbo://192.168.31.132:12347/person.shw.dubbo.api.DubboApi?anyhost=true&application=provider-nacos&bean.name=ServiceBean:person.shw.dubbo.api.DubboApi&category=providers&deprecated=false&dubbo=2.0.2&dynamic=true&generic=false&interface=person.shw.dubbo.api.DubboApi&methods=getConsumerData&path=person.shw.dubbo.api.DubboApi&pid=2846&protocol=dubbo&register=true&release=2.7.3&side=provider&timestamp=1592646130152], dubbo version: 2.7.3, current host: 192.168.31.132
2020-06-20 17:44:26.181  INFO 3033 --- [           main] o.a.d.remoting.transport.AbstractClient  :  [DUBBO] Succeed connect to server /192.168.31.132:12347 from NettyClient 192.168.31.132 using dubbo version 2.7.3, channel is NettyChannel [channel=[id: 0x5cf0891f, L:/192.168.31.132:55666 - R:/192.168.31.132:12347]], dubbo version: 2.7.3, current host: 192.168.31.132
2020-06-20 17:44:26.181  INFO 3033 --- [           main] o.a.d.remoting.transport.AbstractClient  :  [DUBBO] Start NettyClient /192.168.31.132 connect to the server /192.168.31.132:12347, dubbo version: 2.7.3, current host: 192.168.31.132
2020-06-20 17:44:26.216  INFO 3033 --- [client.listener] o.a.dubbo.registry.nacos.NacosRegistry   :  [DUBBO] Notify urls for subscribe url consumer://192.168.31.132/org.apache.dubbo.rpc.service.GenericService?application=consumer-nacos&category=providers,configurators,routers&dubbo=2.0.2&generic=true&interface=person.shw.dubbo.api.DubboApi&lazy=false&pid=3033&qos.enable=false&release=2.7.3&side=consumer&sticky=false&timeout=200000&timestamp=1592646265664, urls: [dubbo://192.168.31.132:12347/person.shw.dubbo.api.DubboApi?anyhost=true&application=provider-nacos&bean.name=ServiceBean:person.shw.dubbo.api.DubboApi&category=providers&deprecated=false&dubbo=2.0.2&dynamic=true&generic=false&interface=person.shw.dubbo.api.DubboApi&methods=getConsumerData&path=person.shw.dubbo.api.DubboApi&pid=2846&protocol=dubbo&register=true&release=2.7.3&side=provider&timestamp=1592646130152], dubbo version: 2.7.3, current host: 192.168.31.132
2020-06-20 17:44:26.218  INFO 3033 --- [           main] org.apache.dubbo.config.AbstractConfig   :  [DUBBO] Refer dubbo service org.apache.dubbo.rpc.service.GenericService from url nacos://120.79.76.230:8848/org.apache.dubbo.registry.RegistryService?anyhost=true&application=consumer-nacos&bean.name=ServiceBean:person.shw.dubbo.api.DubboApi&category=providers&check=false&deprecated=false&dubbo=2.0.2&dynamic=true&generic=true&interface=person.shw.dubbo.api.DubboApi&lazy=false&methods=getConsumerData&path=person.shw.dubbo.api.DubboApi&pid=3033&protocol=dubbo&qos.enable=false&register=true&register.ip=192.168.31.132&release=2.7.3&remote.application=provider-nacos&side=consumer&sticky=false&timeout=200000&timestamp=1592646265664, dubbo version: 2.7.3, current host: 192.168.31.132
[泛化调用成功]----->"333333333333333333"
2020-06-20 17:44:26.409  INFO 3033 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-06-20 17:44:26.627  INFO 3033 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 10009 (http) with context path ''
2020-06-20 17:44:26.630  INFO 3033 --- [           main] p.s.dubbo.consumer.DubboConsumerLanuch   : Started DubboConsumerLanuch in 2.383 seconds (JVM running for 2.875)

倒数第四行打印日志,泛化调用成功。

// TODO 后面补上一个网关gateway使用dubbo泛化调用的例子