Dubbo
|ˈdʌbəʊ|是一个高性能的、由阿里巴巴开源的、基于Java的RPC框架。像其余许多RPC框架同样,Dubbo
基于定义一个服务的思想,并指定一个经过能够传入参数和返回类型被远程调用的方法。在服务器端,服务器实现这个接口并运行一个dubbo
服务器来处理客户端调用。在客户端提供一个与服务端相同方法的副本。 html
Dubbo
提供包含基于接口的远程调用、容错、负载均衡和自动服务注册和发现的三大主要功能。
Dubbo
框架普遍应用于阿里巴巴内外,其余公司包括京东,当当,qunar,kaola等。
本指南让你从一个简单的工做例子开始在Java中使用dubbo
。您能够在github上的dubbo
项目中找到目录dubbo-demo
中的完整工做示例。git
您可能须要使用最新版原本构建您的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
}
}
复制代码
Dubbo
用户指南或下载PDF查找更多细节,或chat on gitter
。dubbo
应用管理主题。Dubbo
是如何设计的,或者想贡献一下? 阅读dubbo开发人员指南或下载pdf,并开始写代码。咱们如今正在收集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