1. 运行环境
本文运行环境:windows 7 + JDK1.7+ zookeeper-3.3.6 + dubbo-admin-2.5.4 (支持JDK 1.8)+ dubbo 2.5.3。
搭建环境所需要的资源下载地址:zookeeper+dubbo+tomcat资源下载
zk+dubbo+spring本地项目Demo下载:Demo下载
2. 安装配置zookeeper
解压zookeeper-3.3.6.tar.gz到指定目录下 D:\zookeeper-3.4.8
修改zoo_sample.cfg 文件名(D:\zookeeper-3.4.8\conf) 为 zoo.cfg
配置文件简单解析
1、tickTime:这个时间是作为 Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每个 tickTime 时间就会发送一个心跳。
2、dataDir:是 Zookeeper 保存数据的目录,默认情况下,Zookeeper 将写数据的日志文件也保存在这个目录里。
3、dataLogDir:是 Zookeeper 保存日志文件的目录
4、clientPort:这个端口就是客户端连接 Zookeeper 服务器的端口,Zookeeper 会监听这个端口,接受客户端的访问请求。
在(D:\zookeeper-3.4.8)目录下创建/tmp/zookeeper文件夹
启动
进入到bin文件夹下,在上面地址栏中输入cmd进入命令行,输入zkServer.cmd,若如图显示则代表启动成功。
3. 创建maven项目
项目结构:
coustomer: 调用远程服务;
interface: 存放公共接口;
provider : 提供远程服务。
具体如图所示:
1. 创建公共接口maven工程:interface
2. 创建服务提供maven工程:provider
maven依赖:
<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>dubbo.test</groupId>
<artifactId>provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>provider</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<springframework.version>3.2.4.RELEASE</springframework.version>
<!-- Logging -->
<slf4j.version>1.7.5</slf4j.version>
<slf4j-log4j12.version>1.7.5</slf4j-log4j12.version>
<log4j.version>1.2.17</log4j.version>
</properties>
<dependencies>
<dependency>
<groupId>dubbo.test</groupId>
<artifactId>interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- Logging with SLF4J -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j-log4j12.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<!-- SPRING -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.3</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
实现公共接口
添加spring配置声明文件:applicationContext.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="app-provider" />
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService" />
<!-- 和本地bean一样实现服务 -->
<bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl" />
</beans>
启动远程服务类:App.java
public class App
{
@SuppressWarnings("resource")
public static void main( String[] args ) throws IOException
{
System.out.println( "dubbo provider start!" );
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
context.start();
System.in.read(); // 为保证服务一直开着,利用输入流的阻塞来模拟
}
}
3. 创建服务消费maven工程:coustomer
添加spring配置声明文件:applicationContext.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="app-customer" />
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference id="demoService" interface="com.alibaba.dubbo.demo.DemoService" />
</beans>
创建App.java类,启动App.java类,调用远程服务
public class App {
@SuppressWarnings("resource")
public static void main(String[] args) throws IOException {
System.out.println("dubbo costomer start!");
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
context.start();
DemoService demoService = (DemoService) context.getBean("demoService"); // 获取远程服务代理
String hello = demoService.sayHello("jackson wang"); // 执行远程方法
System.out.println(hello); // 显示调用结果
// 只有开着消费者,才能在控制台看到。为保证服务一直开着,利用输入流的阻塞来模拟
// System.in.read();
}
}
4. Dubbo管理控制台配置
1、把dubbo-admin.war放到tomcat的webapps目录下
2、启动tomcat
3、在webapps文件夹下找到dubbo-admin文件夹,进入WEB-INF文件夹,打开dubbo.properties,可以看到账户密码分别为root guest
4、在启动zookeeper后,然后再启动tomcat,访问地址:http://localhost:8080/dubbo-admin-2.5.4/
输入帐号密码后即可登录到后台
5、启动远程服务类:App.java
dubbo管理页面会看到
6、启动调用远程服务App.java
dubbo管理页面看到消费者
结束
参考链接:https://blog.csdn.net/zgsdzczh/article/details/78664561