概述

达博|ˈdʌbəʊ|是一种高性能,基于java的RPC阿里巴巴的开源框架。 在许多RPC系统,达博的想法是基于定义一个服务,指定可以远程调用的方法的参数和返回类型。 在服务器端,服务器实现这个接口和运行达博服务器处理客户端调用。 在客户端,客户端存根,服务器提供了相同的方法。

达博提供了三个关键的功能,包括基于接口的远程调用、容错和负载平衡、和自动服务注册与发现。 达博框架广泛采用由其他公司包括阿里巴巴内部和外部京东,当当网,去哪儿,kaola,和许多其他人。

快速启动

本指南让你开始达博在Java工作用一个简单的例子。 你可以找到完整的工作样本目录“dubbo-demo”达博项目在github上。

先决条件

  • JDK:版本6或更高
  • Maven:3或更高版本

Maven的依赖

您可能需要使用最新的版本达博应用程序构建。

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

定义服务接口

因为服务提供者和服务消费者依赖于相同的接口,强烈建议把下面的接口定义在一个分离的模块可以由供应商模块和消费模块共享。

package com.alibaba.dubbo.demo;

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

实现服务提供者

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

配置服务提供者

下面的代码片段显示了如何达博服务提供者配置了spring框架,这是推荐的,不过你也可以使用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>

开始服务提供者

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(); System.in.read(); // press any key to exit } }

配置服务消费者

下面的代码演示了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>

运行服务消费者

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(); DemoService demoService = (DemoService) context.getBean("demoService"); // obtain proxy object for remote invocation String hello = demoService.sayHello("world"); // execute remote invocation System.out.println(hello); // show the result } }

接下来是什么

  <footer class="site-footer">
      <span class="site-footer-credits"><font id="WOW_TRANSLATE_59" class="WOW_TRANSLATE_STYLE" data--w-o-w_-i-n-d-e-x="59">©2012 - 2017 | |达博团队访问</font><a href="https://www.aliyun.com/product/edas"><font id="WOW_TRANSLATE_60" class="WOW_TRANSLATE_STYLE" data--w-o-w_-i-n-d-e-x="60">企业版</font></a><font id="WOW_TRANSLATE_61" class="WOW_TRANSLATE_STYLE" data--w-o-w_-i-n-d-e-x="61">在</font><a href="https://www.alibabacloud.com"><img class="inline-image" src="images/aliyun.png"></a></span>
  </footer>
</section>