本篇是基于spring框架的XML配置开发的dubbo应用程序,开发工具intellij idea,旨在对dubbo的快速理解和上手。

废话不多说,代码撸起来!!!

1.首先,新建一个maven工程,命名为dubbodemo,右击dubbodemo,新建module,如图

建立三个子项目,分别命名为dubbo-demo-api,dubbo-demo-consumer,dubbo-demo-provider

2.根目录下pom.xml文件配置如下

 <?xml version="1.0" encoding="UTF-8"?>
<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>com.nari</groupId>
<artifactId>dubbo-demo</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>dubbo-demo-api</module>
<module>dubbo-demo-provider</module>
<module>dubbo-demo-consumer</module>
</modules> <dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency> <dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
</dependency> <dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency> <!-- spring -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.5.RELEASE</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.1.5.RELEASE</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.1.5.RELEASE</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.1.5.RELEASE</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.5.RELEASE</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.5.RELEASE</version>
<scope>test</scope>
</dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.5.RELEASE</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.1.5.RELEASE</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-oxm -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>5.1.5.RELEASE</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.0</version>
</dependency>
<!--<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.13</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.8</version>
</dependency>-->
</dependencies> </project>

3.服务的提供方提供接口,如图所示,在dubbo-demo-api中新建一个interface接口,DemoService

 package com.nari.api;

 public interface DemoService {

     String sayHello(String name);

 }

pom文件如下

 <?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>dubbo-demo</artifactId>
<groupId>com.nari</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>dubbo-demo-api</artifactId> </project>

4.dubbo-demo-provider目录下,引入对dubbo-demo-api依赖。新建DemoServiceImpl类实现DemoService接口,如图所示

pom.xml文件如下

 <?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>dubbo-demo</artifactId>
<groupId>com.nari</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>dubbo-demo-provider</artifactId> <dependencies>
<dependency>
<artifactId>dubbo-demo-api</artifactId>
<groupId>com.nari</groupId>
<version>1.0-SNAPSHOT</version>
</dependency> </dependencies> </project>

DemoServiceImpl.java

 package com.nari.provider;

 import com.nari.api.DemoService;

 public class DemoServiceImpl implements DemoService {
public String sayHello(String name) {
return "hello " + name;
}
}

在resources目录下新建provider.xml文件,使用广播multicast作为注册中心,优点是易于操作,不用做其他配置。dubbo服务端口为20880,<dubbo:service>暴露服务端口demoService

 <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"> <!-- provider's application name, used for tracing dependency relationship -->
<dubbo:application name="demo-provider">
<dubbo:parameter key="qos.enable" value="true" />
<dubbo:parameter key="qos.accept.foreign.ip" value="false" />
<dubbo:parameter key="qos.port" value="22222" />
</dubbo:application>
<!-- use multicast registry center to export service -->
<dubbo:registry address="multicast://224.5.6.7:1234" check="false"/>
<!-- use dubbo protocol to export service on port 20880 -->
<dubbo:protocol name="dubbo" port="20880"/>
<!-- service implementation, as same as regular local bean -->
<bean id="demoService" class="com.nari.provider.DemoServiceImpl"/>
<!-- declare the service interface to be exported -->
<dubbo:service interface="com.nari.api.DemoService" ref="demoService"/>
</beans>

新建provider.java,读取xml配置文件,代码如下

 package com.nari.provider.main;

 import org.springframework.context.support.ClassPathXmlApplicationContext;

 import java.io.IOException;

 public class Provider {

     public static void main(String[] args) {
try {
System.setProperty("java.net.preferIPv4Stack", "true");
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/provider.xml"});
context.start();
System.out.println("Provider started....");
System.in.read();
}catch (IOException e){
e.printStackTrace();
}
} }

在resources目录下引入log4j.properties,开启日志功能

###set log levels###
log4j.rootLogger=info, stdout
###output to the console###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%d{dd/MM/yy hh:mm:ss:sss z}] %t %5p %c{2}: %m%n

5.dubbo-demo-consumer目录下,对暴露的接口调用,获取服务,文件目录如下

pom.xml文件配置如下

 <?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>dubbo-demo</artifactId>
<groupId>com.nari</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>dubbo-demo-consumer</artifactId> <dependencies>
<dependency>
<artifactId>dubbo-demo-provider</artifactId>
<groupId>com.nari</groupId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies> </project>

在resources目录下新建consumer.xml文件,声明注册中心为multicast,地址与provider.xml文件中保持一致,<dubbo:reference>引入接口,调用服务。添加log4j.properties日志文件。

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"> <!-- consumer's application name, used for tracing dependency relationship (not a matching criterion),
don't set it same as provider -->
<dubbo:application name="demo-consumer">
<dubbo:parameter key="qos.enable" value="true" />
<dubbo:parameter key="qos.accept.foreign.ip" value="false" />
<dubbo:parameter key="qos.port" value="33333" />
</dubbo:application>
<!-- use multicast registry center to discover service -->
<dubbo:registry address="multicast://224.5.6.7:1234" check="false"/>
<!-- generate proxy for the remote service, then demoService can be used in the same way as the
local regular interface -->
<dubbo:reference id="demoService" check="false" interface="com.nari.api.DemoService"/>
<!--<dubbo:consumer check="false" />-->
</beans>

新建consumer.java文件,context上下文获取bean服务,调用其方法

 package com.nari.consumer.main;

 import com.nari.api.DemoService;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Consumer { public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/consumer.xml"});
context.start();
DemoService demoService = (DemoService) context.getBean("demoService");
String hello = demoService.sayHello("zhangsan");
System.out.println(hello);
} }

6.至此,工程搭建完成,先启动Provider.main()方法,服务的提供方提供服务,如图,打印结果如下

再启动Consumer.main()方法,服务的消费方成功获取到服务,打印结果如下:

dubbo初学,快速体验的更多相关文章

  1. gitbook 入门教程之快速体验

    本文主要介绍三种使用 gitbook 的方式,分别是 gitbook 命令行工具,Gitbook Editor 官方编辑器和 gitbook.com 官网. 总体来说,三种途径适合各自不同的人群,找到 ...

  2. 快速体验 Laravel 自带的注册、登录功能

    快速体验 Laravel 自带的注册.登录功能 注册.登录经常是一件很伤脑筋的是,Laravel 提供了解决方案,可以直接使用它.试过之后,感觉真爽! 前提:本地已安装好了 PHP 运行环境.mysq ...

  3. 分布式_事务_01_2PC框架raincat快速体验1

    一.前言 关于2PC的理论知识请见:分布式_理论_03_2PC 这一节我们来看下github上一个优秀的2PC分布式事务开源框架的快速体验. 二.源码 源码请见: https://github.com ...

  4. 分布式事务_01_2PC框架raincat快速体验

    一.前言 关于2PC的理论知识请见:分布式_理论_03_2PC 这一节我们来看下github上一个优秀的2PC分布式事务开源框架的快速体验. 二.源码 源码请见: https://github.com ...

  5. Dev 日志 | 文章《快速体验知识图谱 OwnThink》中的技术问题

    社区小伙伴反馈在实践文章<使用图数据库 Nebula Graph 数据导入快速体验知识图谱 OwnThink>时,遇到了一些问题,Nebula Graph 将在本文对该文章中出现的问题进行 ...

  6. Django之Django快速体验

    Django快速体验 前语: 这一节内容是直接快速上手,后面的内容是对内容进行按步解释,如果不想看解析的,可以直接只看这一节的内容. 1.新建项目应用新建项目test1新建应用booktest 2.注 ...

  7. CDH5.13快速体验

    相对于易用性很差Apache Hadoop,其他商业版Hadoop的性能易用性都有更好的表现,如Cloudera.Hortonworks.MapR以及国产的星环,下面使用CDH(Cloudera Di ...

  8. 【Java】15分钟快速体验阿里Java诊断工具Arthas

    [墙裂推荐]15分钟快速体验阿里Java诊断工具Arthas : https://alibaba.github.io/arthas/arthas-tutorials?language=cn&i ...

  9. sh_02_快速体验

    sh_02_快速体验 import sh_01_九九乘法表 sh_01_九九乘法表.multiple_table()

随机推荐

  1. 2019.01.23 ural1519 Formula 1(轮廓线dp)

    传送门 轮廓线dpdpdp模板题. 题意简述:给一个放有障碍的网格图,问有多少种方法能使所有非障碍格子都在同一条哈密顿回路上面. 考虑用括号序列的写法来状压这个轮廓线. 用000表示没有插头,111表 ...

  2. 2018.10.25 bzoj4350: 括号序列再战猪猪侠(区间dp)

    传送门 区间dp好题. 首先我们并不用把右括号拿进来一起dpdpdp,而是直接用左括号来dpdpdp. 然后定义状态fi,jf_{i,j}fi,j​表示区间[l,r][l,r][l,r]的合法方案数. ...

  3. WPF中的路由事件(转)

    出处:https://www.cnblogs.com/JerryWang1991/archive/2013/03/29/2981103.html 最近因为工作需要学习WPF方面的知识,因为以前只关注的 ...

  4. 第13章:MongoDB-聚合操作--初体验

    ①MongoDB 的聚合功能 MongoDB 的聚合功能,聚合操作主要用于对数据的批量处理,往往将记录按条件分组以后,然后再进行一系列操作,例如,求最大值.最小值.平均值,求和等操作. 聚合操作还能够 ...

  5. Netty4.x 源码实战系列(一): 深入理解ServerBootstrap 与 Bootstrap

    转载自:https://www.cnblogs.com/itdriver/p/8149913.html 从Java1.4开始, Java引入了non-blocking IO,简称NIO.NIO与传统s ...

  6. Windows下Python安装: requires numpy+mkl 和ImportError: cannot import name NUMPY_MKL

    最近写了一篇关于“微软开源分布式高性能GB框架LightGBM安装使用”的文章,有小伙伴安装Python环境遇到了问题.我个人也尝试安装了一下,确实遇到了很多问题."Windows7下pyt ...

  7. 动态生成PictureBox控件,涉及:PictureBox控件和flowLayoutPanel面板

    一.概述 flowLayoutPanel面板是一系列控件的容器,有关详细的使用方法留待以后总结. 二.问题提出 问题提出:点击按钮,扫描指定文件夹并将其中的所有图片放在flowLayoutPanel面 ...

  8. Ubuntu下安装并配置TexStudio

    作者:tongqingliu 转载请注明出处:http://www.cnblogs.com/liutongqing/p/7069715.html Ubuntu下安装并配置TexStudio Ubunt ...

  9. C#-VS发布网站-准备待发布网站-摘

    通过使用“发布网站”工具部署网站项目 准备网站源文件 在vs生成发布文件 配置IIS   .NET Framework 4 其他版本 Visual Studio 2008 Visual Studio ...

  10. slice()

    提取字符串中的一部分,并返回这个新的字符串 str.slice(beginSlice[, endSlice]) 参数 beginSlice 从该索引(以 0 为基数)处开始提取原字符串中的字符.如果值 ...