多语言展示
当前在线:204今日阅读:113今日分享:31

Dubbo RPC框架入门实例完整操作,轻松入门

Dubbo是比较流行的RPC服务框架,用于开发分布式应用,因为它是开源同时又背靠A公司的品牌,所以用的人还是很多的!看了Dubbo官网的实例,对于初学者还是有些坑的,这里给初学者一个入门完整的实例以补齐官网资料的不足。
工具/原料
1

Eclipse

2

Java

3

Maven

4

Windows 10 电脑需要连接互联网

方法/步骤
1

准备Dubbo的开发环境,包括Java,Maven,EclipseApache Maven 3.5.2 Maven home: E:\apache-maven-3.5.2\bin\..Java version: 1.8.0_151, vendor: Oracle CorporationJava home: C:\Program Files\Java\jdk1.8.0_151\jre

2

Dubbo入门实例规划1)使用Maven构建工程2)工程1:服务提供者,如test.love3)工程2:服务消费者,如 love.comsumer

3

构建工程1基本框架:服务提供者 ,需要在maven工程文件pom.xml中加入dubbo的依赖,dubbo版本号选2.5.8.Group ID: testartifact Id: lovepom.xml文件内容如下:  4.0.0  test  love  0.0.1-SNAPSHOT        com.alibaba    dubbo    2.5.8

4

在工程1中,创建服务提供接口和实现类1)DemoService.java 文件package love;public interface DemoService { String sayHello(String name);}2)DemoServiceImpl.java文件package love;public class DemoServiceImpl implements DemoService { public String sayHello(String name) { return 'Hello ' + name; }}3) Lover.java文件package love;import java.io.IOException;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Lover { public static void main(String[] args) throws IOException {   ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(                new String[] {'META/spring/dubbo-demo-provider.xml'});        context.start();        // press any key to exit        System.in.read(); }}4)dubbo-spring服务配置文件META/spring/dubbo-demo-provider.xml                   

5

创建工程2基本结构,在maven的工程文件pom.xml文件中增加对dubbo和工程1的依赖  4.0.0  love  comsumer  0.0.1-SNAPSHOT          com.alibaba    dubbo    2.5.8   test  love  0.0.1-SNAPSHOT    

6

在工程2中实现服务消费的功能1)Consumer.java文件package consumer;import org.springframework.context.support.ClassPathXmlApplicationContext;import love.DemoService;public class Consumer { public static void main(String[] args) {  ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(                new String[]{'META/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); }}2)spring服务配置文件 META/spring/dubbo-demo-consumer.xml           

7

启动工程1:服务提供者,开启服务运行工程1的lover类,看到如下日志信息表示启动成功十二月 29, 2017 11:06:18 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1099f62: startup date [Fri Dec 29 11:06:18 CST 2017]; root of context hierarchy十二月 29, 2017 11:06:18 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions信息: Loading XML bean definitions from class path resource [META/spring/dubbo-demo-provider.xml]十二月 29, 2017 11:06:18 上午 com.alibaba.dubbo.common.logger.LoggerFactory info信息: using logger: com.alibaba.dubbo.common.logger.jcl.JclLoggerAdapter十二月 29, 2017 11:06:18 上午 com.alibaba.dubbo.config.AbstractConfig info信息:  [DUBBO] The service ready on spring started. service: love.DemoService, dubbo version: 2.5.8, current host: 127.0.0.1十二月 29, 2017 11:06:19 上午 com.alibaba.dubbo.config.AbstractConfig info信息:  [DUBBO] Export dubbo service love.DemoService to local registry, dubbo version: 2.5.8, current host: 127.0.0.1

8

启动工程2:消费者服务运行consumer.java 类,可以看到日志输出服务调用的结果:“Hello world”信息:  [DUBBO] Refer dubbo service love.DemoService from url multicast://224.5.6.7:1234/com.alibaba.dubbo.registry.RegistryService?anyhost=true&application=demo-consumer&check=false&dubbo=2.5.8&generic=false&interface=love.DemoService&methods=sayHello&pid=9036®ister.ip=192.168.26.157&remote.timestamp=19&side=consumer×tamp=28, dubbo version: 2.5.8, current host: 192.168.26.157Hello world十二月 29, 2017 11:19:50 上午 com.alibaba.dubbo.config.AbstractConfig info信息:  [DUBBO] Run shutdown hook now., dubbo version: 2.5.8, current host: 192.168.26.157

推荐信息