美文网首页
Dubbo(官方翻译)

Dubbo(官方翻译)

作者: Marlon666 | 来源:发表于2018-06-19 10:49 被阅读8次

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);
    }
}

相关文章

网友评论

      本文标题:Dubbo(官方翻译)

      本文链接:https://www.haomeiwen.com/subject/yifaeftx.html