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

2025-11-21 15:10:30

1、准备Dubbo的开发环境,包括Java,Maven,Eclipse

Apache Maven 3.5.2 

Maven home: E:\apache-maven-3.5.2\bin\..

Java version: 1.8.0_151, vendor: Oracle Corporation

Java home: C:\Program Files\Java\jdk1.8.0_151\jre

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

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

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

2、Dubbo入门实例规划

1)使用Maven构建工程

2)工程1:服务提供者,如test.love

3)工程2:服务消费者,如 love.comsumer

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

3、构建工程1基本框架:服务提供者 ,需要在maven工程文件pom.xml中加入dubbo的依赖,dubbo版本号选2.5.8.

Group ID: test

artifact Id: love

pom.xml文件内容如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>test</groupId>

  <artifactId>love</artifactId>

  <version>0.0.1-SNAPSHOT</version>

  

 <dependencies>

 <dependency>

    <groupId>com.alibaba</groupId>

    <artifactId>dubbo</artifactId>怕包昆

    <version>2.5.8</version>

</dependency>

</dependencies>

</project>

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

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

<?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="love.DemoService" ref="demoService"/>

    <bean id="demoService" class="love.DemoServiceImpl"/>

</beans>

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

5、创建工程2基本结构,在maven的工程文件pom.xml文件中增加对dubbo和工程1的依赖

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>love</groupId>

  <artifactId>comsumer</artifactId>

  <version>0.0.1-SNAPSHOT</version>

  

   <dependencies>

 <dependency>

    <groupId>com.alibaba</groupId>

    <artifactId>dubbo</artifactId>

    <version>2.5.8</version>

</dependency>

 <dependency>

  <groupId>test</groupId>

  <artifactId>love</artifactId>

  <version>0.0.1-SNAPSHOT</version>

 </dependency>

   </dependencies>

</project>

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

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

<?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="love.DemoService"/>

</beans>

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

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

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

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&register.ip=192.168.26.157&remote.timestamp=1514516779019&side=consumer&timestamp=1514517590228, dubbo version: 2.5.8, current host: 192.168.26.157

Hello 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

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

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢