1、基本环境搭建
1.1、软件及源码
zookeeper安装配置:https://www.jianshu.com/p/de90172ea680
node.js及vue安装配置:https://www.jianshu.com/p/7834fc55e9e8
dubbo源码:https://github.com/apache/incubator-dubbo
dubbo管理后台源码:https://github.com/apache/incubator-dubbo-ops
1.2、环境配置
1.2.1、zookeeper配置
解压zookeeper压缩包,会有如下目录:

双击运行bin目录下的zkServer.cmd启动脚本,启动成功会显示如下信息:

若执行脚本出现闪退,则启动异常,请打开windows的cmd窗口,在此窗口中执行zkServer.cmd命令,并查看异常原因。
若2181端口被占用,请先关闭占用此端口的应用。
zookeeper服务端启动完成后,启动bin目录下的zkCli.cmd客户端脚本。启动成功并输入:【ls /】命令会有以下输出:

1.2.2、dubbo源码编译
dubbo源码目录结构如下:

利用idea等IDE工具集成的maven进行编译打包,并本地install;

1.2.3、dubbo管理后台
(1)管理后台结构介绍
dubbo后台管理的目录结构如下:

dubbo最新版使用了前后端分离:
dubbo-admin-backend:基于spring-boot的后端业务逻辑代码;
dubbo-admin-frontend:基于vue的前台页面资源代码;
(1)后端编译配置启动
后端代码目录如下:

后端主要配置zookeeper,即配置注册中心,application.properties中配置如下:

运行DubboAdminApplication的main函数,启动后端服务:

启动成功将有如下输出:

(1)前端配置启动
前端代码目录如下:

config/index.js中有如下配置:

前台服务器启动之后有如下输出:

打开浏览器,输入:http://localhost:8081

2、服务提供者及调用者
项目目录结构如下:

maven中dubbo版本依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--此版本为本地编译版本,若demo无法运行请替换为已发布版本-->
<!--经测试,最新版本无法识别注册中心-->
<!--<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.0-SNAPSHOT</version>
</dependency>-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.5</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.0.1</version>
</dependency>
</dependencies>
2.1、服务提供者
2.1.1、dubbo-rpc-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://dubbo.apache.org/schema/dubbo"
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">
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="privider-api-test" />
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry id="zookeeperRegistry" protocol="zookeeper" address="zookeeper://127.0.0.1:2181" />
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service id="dubboTestApi" interface="dubbo.demo.api.DubboTestApi" ref="dubboTestApiImpl" />
<!-- 和本地bean一样实现服务 -->
<bean id="dubboTestApiImpl" class="dubbo.demo.api.DubboTestApiImpl" />
</beans>
使用zookeeper作为注册中心,协议dubbo协议,暴露端口为20880;
2.1.2、服务启动实现
public class ProviderMain {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("config/rpc/dubbo-rpc-provider.xml");
context.start();
System.in.read(); // 按任意键退出
}
}
2.1.3、启动服务
运行main函数,启动服务,输出如下:

管理后台服务提供者有如下信息:

2.2、服务调用者
2.2.1、dubbo-rpc-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://dubbo.apache.org/schema/dubbo"
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">
<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
<dubbo:application name="consumer-api-test" />
<!-- 使用zookeeper注册中心暴露发现服务地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference id="dubboTestApi" interface="dubbo.demo.api.DubboTestApi" />
</beans>
2.2.2、服务启动实现
public class ConsumerMain {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("config/rpc/dubbo-rpc-consumer.xml");
context.start();
DubboTestApi testApi = (DubboTestApi) context.getBean("dubboTestApi"); // 获取远程服务代理
String ret = testApi.echoTest("this is my test!");
System.out.println(ret); // 显示调用结果
System.in.read();
}
}
2.2.3、调用服务
服务输出:

管理后台服务消费者有如下信息:

demo源码:https://github.com/zhaozhou11/dubbo-demo.git
参考文档:http://dubbo.apache.org/zh-cn/docs/user/quick-start.html
网友评论