Dubbo官方入门Demo(翻译自http://dubbo.io/主页入门教程)

1、总览

Dubbo|ˈdʌbəʊ|是一个高性能的、由阿里巴巴开源的、基于Java的RPC框架。像其余许多RPC框架同样,Dubbo基于定义一个服务的思想,并指定一个经过能够传入参数和返回类型被远程调用的方法。在服务器端,服务器实现这个接口并运行一个dubbo服务器来处理客户端调用。在客户端提供一个与服务端相同方法的副本。 html

Dubbo提供包含基于接口的远程调用、容错、负载均衡和自动服务注册和发现的三大主要功能。 Dubbo框架普遍应用于阿里巴巴内外,其余公司包括京东,当当,qunar,kaola等。

2、快速入门

本指南让你从一个简单的工做例子开始在Java中使用dubbo。您能够在github上的dubbo项目中找到目录dubbo-demo中的完整工做示例。git

一、先决条件

  • JDK:6或6以上
  • Maven:3或3以上

二、Maven 依赖

您可能须要使用最新版原本构建您的dubbo应用程序。github

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.5.8</version>
</dependency>
复制代码

三、定义服务接口

由于服务提供者和服务消费者都依赖于相同的接口,所以咱们强烈推荐您将接口的定义放在能被服务提供者模块、服务消费者模块共享的独立模块中(用Maven构建的话能够放在子Maven项目中)。spring

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框架配置一个服务提供者。使用Spring框架配置是咱们推荐的作法,固然,你也可使用API的方式进行配置。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框架:bash

<?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
    }
}
复制代码

3、接下来读什么?

4、咱们须要你的帮助

咱们如今正在收集Dubbo用户信息,以帮助咱们更好地提升Dubbo,请经过issue#1012: Wanted: who’s using dubbo提供你的帮助!服务器


如下是小编的下载github上的dubbo项目中的dubbo-demo并导入 Eclipse中运行的bug,Bug老是那么恶心,但能帮你少走点弯路。微信

  • 官方的实例使用的是广播的形式注册咱们的服务,咱们也可使用zookeeper进行服务的注册。
  • 我首先启动Provider,再启动Consumer时发现报dubbo:com.alibaba.dubbo.rpc.RpcException: Failed to invoke the method错误,此时咱们只须要检查本地是否搭建了虚拟机,若是有,把虚拟机服务禁用掉。若是没有安装虚拟机而有无线wifi等,也将其关闭,由于这会致使你的服务发布ip和你的consumer发现服务的ip不一致。
  • 启动Provider

  • 启动Consumer


OK!官方入门就翻译完成了!虽然翻译的质量不是特别滴好,但我以为翻译的过程是很是享受且学的最多的。若有翻译不对的地方,请您在评论区发表评论告诉小编,么么哒!小编在校大四学生、年后实习工做咯!小编微信号:lifvalue,加我请备注掘金,你们一块儿交流学习!app

相关文章
相关标签/搜索