dubbo之服务配置及启停

前言

dubbo之集群》一文所提到功能已经基本完善,dubbo如何进一步封装,使用之简单易用呢?

服务代理

通过《浅谈AOP》中的Javassist技术,dubbo为我们简化代码如下

public class ProxyTest {

    public static void main(String[] args) throws Exception {

        ServiceRepository repository = ApplicationModel.getServiceRepository();
        repository.registerService(GreetingsService.class);

        URL url = URL.valueOf("dubbo://127.0.0.1:28092/dubbo.GreetingsService?monitor=mm&timeout=12000");

        Protocol protocol = new ProtocolFilterWrapper(new dubboProtocol());

        protocol.export(new  JdkProxyFactory().getInvoker(new GreetingsServiceImpl(), GreetingsService.class, url));


        Invoker<GreetingsService> invoker = protocol.refer(GreetingsService.class, url);

        invoker = Cluster.getCluster(null, false).join(new StaticDirectory(Lists.newArrayList(invoker)));

        GreetingsService greetingsService = new  JdkProxyFactory().getProxy(invoker, new Class[]{ GreetingsService.class});

        System.out.println( greetingsService.sayHi("yoyo"));
    }
}

服务配置

dubbo进一步通过配置的形势缩小代码,demo如下

public class ConfigTest {

    public static void main(String[] args) throws InterruptedException {
        
        // 基本信息全局配置
        ConfigManager configManager = ApplicationModel.getConfigManager();
        configManager.setApplication(new ApplicationConfig("test"));

        // 注册中心全局配置
        RegistryConfig registryConfig  = new RegistryConfig("127.0.0.1", "zookeeper");
        registryConfig.setPort(2181);
        configManager.addRegistry(registryConfig);

        // 服务提供配置
        ServiceConfig<GreetingsService> serviceConfig = new ServiceConfig();
        serviceConfig.setInterface(GreetingsService.class);
        serviceConfig.setRef(new GreetingsServiceImpl());
        serviceConfig.export();

        // 服务调用配置
        ReferenceConfig<GreetingsService> referenceConfig = new ReferenceConfig();
        referenceConfig.setInterface(GreetingsService.class);
        referenceConfig.setProtocol("dubbo");
        GreetingsService greetingsService = referenceConfig.get();
        System.out.println(greetingsService.sayHi("rrrrrr"));
    }
}

除了上述JavaBean的启动方式dubbo还提供了spring的注解及配置文件方式完成以上配置。以配置文件方式为例子:

<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.xsd
     http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <!--定义了提供方应用信息,用于计算依赖关系;在 dubbo-admin 或 dubbo-monitor 会显示这个名字,方便辨识-->
    <dubbo:application name="test"/>
    <!--使用 zookeeper 注册中心暴露服务,注意要先开启 zookeeper-->
    <dubbo:registry address="zookeeper://localhost:2181"/>
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" />
    <!--使用 dubbo 协议实现定义好的 api.PermissionService 接口-->
    <dubbo:service interface="dubbo.GreetingsService" ref="greetingsServiceImpl" protocol="dubbo" />
    <!--具体实现该接口的 bean-->
    <bean id="greetingsServiceImpl" class="dubbo.GreetingsServiceImpl"/>

    <dubbo:reference id="greetingsService" interface="dubbo.GreetingsService"/>
</beans>
public class SpringdubboTest {

    public static void main(String[] args) throws InterruptedException {
        ClasspathXmlApplicationContext context=new ClasspathXmlApplicationContext("spring-dubbo.xml");
        GreetingsService greetingsService = (GreetingsService) context.getBean("greetingsService");
        System.out.println(greetingsService.sayHi("oyooo"));
    }
}
  1. spring通过dubboNamespaceHandler类解析"dubbo"标签,分别对应
标签名称类名
dubbo:applicationApplicationConfig
dubbo:moduleModuleConfig
dubbo:registryRegistryConfig
dubbo:monitorMonitorConfig
dubbo:providerProviderConfig
dubbo:consumerConsumerConfig
dubbo:protocolProtocolConfig
dubbo:serviceServiceBean
dubbo:referenceReferenceBean
  1. service服务提供者启动流程

    在这里插入图片描述

  2. 消费者启动时

    在这里插入图片描述

总结

  1. 服务启动时,先是服务准备好并且本地爆露完再向注册中心提交连接。
  • 防止过早的向注册中心写入,请求找不到服务.
  • @see RegistryProtocol.export
  1. 服务停止时,先是取消注册中心连接,然后等请求完成关闭
  • 阻止新请求过来,然后再等待正在运行的请求处理完成
  • @see RegistryProtocol.ExporterChangeableWrapper.unexport

主要参考

源码分析Dubbo前置篇-寻找注册中心、服务提供者、服务消费者功能入口
源码分析Dubbo服务消费端启动流程
源码分析Dubbo服务提供者启动流程

相关文章

在网络请求时,总会有各种异常情况出现,我们需要提前处理这...
作者:宇曾背景软件技术的发展历史,从单体的应用,逐渐演进...
hello,大家好呀,我是小楼。最近一个技术群有同学at我,问我...
 一个软件开发人员,工作到了一定的年限(一般是3、4年左右...
当一个服务调用另一个远程服务出现错误时的外观Dubbo提供了多...
最近在看阿里开源RPC框架Dubbo的源码,顺带梳理了一下其中用...