Dubbo(官方翻译)

Dubbo概述(https://dubbo.incubator.apache.org/

 Apache Dubbo(孵化)dbis是一个高性能、基于java的RPC(https://en.wikipedia.org/wiki/remotecall)框架,由阿里巴巴开源。
与许多RPC系统一样,dubbo基于定义服务的思想,指定可以用其参数和返回类型远程调用的方法。在服务器端,服务器实现这个接口,
并运行一个dubbo服务器来处理客户端调用。在客户端,客户端有一个存根,它提供与服务器相同的方法。

Apache Dubbo 提供了三个关键功能,包括基于接口的远程调用、容错和负载平衡,以及自动服务注册和发现。
Apache Dubbo 框架在阿里巴巴内部和其他公司都被广泛采用,包括京东、当当、去哪儿、卡罗拉以及其他许多公司。
image.png

框架解析:

Double 是一种分布式的服务框架,提供服务和软负载均衡;另外还提供了Monitor(监控中心)和调用中心,可选需要单独配置;
Consumer: 服务消费者  保存在磁盘,一分钟同步一次。
Provider: 服务提供者  保存在磁盘,一分钟同步一次。
Container: 服务容器
invoke(调用): 同步

启动流程:Provider 启动start方法; 然后进行Register方法调用注册服务,通常选择到Zookeeper上边去,消费者调用subscribe(init 初始化方法)来
订阅服务,如果没有订阅到自己想要的服务,他会不断的尝试订阅。如果新的服务注册到注册中心以后调用notify方法通知消费者。

Maven使用步骤

1)Maven依赖:

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.6.2</version>  
</dependency>

2)定义服务接口

package com.alibaba.dubbo.demo;

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


由于服务提供者和服务使用者都依赖于同一个接口,所以强烈建议将接口定义放在一个分离的模块中,这个模块可以由提供者模块和使用者模块共享。

3)实现服务接口

package com.alibaba.dubbo.demo.provider;
import com.alibaba.dubbo.demo.DemoService;

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

4)配置Dubbo提供者

下面的代码片段展示了如何使用spring框架配置dubbo服务提供者,但是如果您愿意的话,您也可以使用API配置。

<?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://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <dubbo:application name="demo-provider"/>
    <dubbo:registry address="multicast://224.5.6.7:1234"/>
    <dubbo:protocol name="dubbo" port="20880"/>
    <dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService"/>
    <bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl"/>
</beans>

5)开启服务提供者

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Provider {
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                new String[] {"META-INF/spring/dubbo-demo-provider.xml"});
        context.start();
        // press any key to exit
        System.in.read();
    }
}

6)配置服务(消费者)使用者

同样,下面的代码演示了spring integration
<?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://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <dubbo:application name="demo-consumer"/>
    <dubbo:registry address="multicast://224.5.6.7:1234"/>
    <dubbo:reference id="demoService" interface="com.alibaba.dubbo.demo.DemoService"/>
</beans>

7)运行服务(消费者)使用者

import com.alibaba.dubbo.demo.DemoService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Consumer {
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                new String[]{"META-INF/spring/dubbo-demo-consumer.xml"});
        context.start();
        // obtain proxy object for remote invocation
        DemoService demoService = (DemoService) context.getBean("demoService");
        // execute remote invocation
        String hello = demoService.sayHello("world");
        // show the result
        System.out.println(hello);
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,981评论 19 139
  • 忽然只是为了记什么写下这些东西,人真的会越来越孤独,无论有多少人的陪伴也像在荒野里一般,脑海里有脚步声,有她的脸庞...
    梵夜阅读 159评论 0 0
  • 我想成为一个靠谱的人。 于是在知道今晚可能会加班到很晚的情况下,我还是很爽快的答应了何经理让我早班提前一小时到的要...
    良木树下阅读 646评论 16 11
  • 参考文档:https://www.linuxidc.com/Linux/2017-05/143864.htm 一、...
    joy_蓝蜘蛛阅读 279评论 0 0