一、运行环境
本文运行环境:windows 7 + JDK1.8 + zookeeper-3.3.6 + dubbo-admin-2.5.4 (支持JDK 1.8)+ dubbo 2.5.3
所需工具可至资源页下载 --》点我下载
Demo下载
本文采用Dubbo与Zookeeper、Spring框架的整合。
主要是以下几个步骤:
1. 安装Zookeeper,启动;
2. 创建MAVEN项目,构建Dubbo+Zookeeper+Spring实现的简单Demo;
3. 安装Dubbo-admin,实现监控。
在(D:\zookeeper-3.4.8\)目录下创建/tmp/zookeeper文件夹
启动
进入到bin文件夹下,在上面地址栏中输入cmd进入命令行,输入zkServer.cmd,若如图显示则代表启动成功。
三、创建MAVEN项目
(编译环境使用Intellij JDK版本为1.8)
项目结构:
dubbo-api : 存放公共接口;
dubbo-consumer : 调用远程服务;
dubbo-provider : 提供远程服务。
具体如图所示:
1、新建MAVEN项目 DubboDemo
导入所需要的jar包依赖。
4.0.0
DubboDemo
DubboDemo
pom
1.0-SNAPSHOT
dubbo-api
dubbo-consumer
dubbo-provider
DubboDemo Maven Webapp
http://maven.apache.org
1.8
1.8
UTF-8
UTF-8
4.2.5.RELEASE
3.2.8
5.1.29
1.7.18
1.2.17
junit
junit
3.8.1
test
jstl
jstl
1.2
javax
javaee-api
7.0
junit
junit
4.11
test
org.springframework
spring-core
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-oxm
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-aop
${spring.version}
org.springframework
spring-test
${spring.version}
org.mybatis
mybatis
${mybatis.version}
org.mybatis
mybatis-spring
1.2.2
mysql
mysql-connector-java
${mysql-driver.version}
commons-dbcp
commons-dbcp
1.2.2
com.alibaba
fastjson
1.2.22
log4j
log4j
${log4j.version}
org.slf4j
slf4j-api
${slf4j.version}
org.slf4j
slf4j-log4j12
${slf4j.version}
org.codehaus.jackson
jackson-mapper-asl
1.9.13
com.fasterxml.jackson.core
jackson-core
2.8.0
com.fasterxml.jackson.core
jackson-databind
2.8.0
commons-fileupload
commons-fileupload
1.3.1
commons-io
commons-io
2.4
commons-codec
commons-codec
1.9
org.quartz-scheduler
quartz
2.2.1
org.apache.shiro
shiro-core
1.3.2
org.apache.shiro
shiro-web
1.3.2
org.apache.shiro
shiro-spring
1.3.2
org.apache.shiro
shiro-ehcache
1.3.2
org.apache.zookeeper
zookeeper
3.4.9
com.alibaba
dubbo
2.5.3
org.springframework
spring
com.101tec
zkclient
0.10
DubboDemo
dubbo-api
1.0-SNAPSHOT
DubboDemo
2、在此项目下新建MAVEN项目:dubbo-api
package com.zh.dubbo.demo;
import java.util.List;
public interface DemoService {
List getPermissions(Long id);
}
(若不能创建java文件,则在java文件夹点击右键,选择Mark Directory As -----Source Root即可
)
实现公共接口
package com.zh.dubbo.demo.impl;
import com.zh.dubbo.demo.DemoService;
import java.util.ArrayList;
import java.util.List;
public class DemoServiceImpl implements DemoService {
public List getPermissions(Long id) {
List demo = new ArrayList();
demo.add(String.format("Permission_%d", id - 1));
demo.add(String.format("Permission_%d", id));
demo.add(String.format("Permission_%d", id + 1));
return demo;
}
}
添加Spring配置声明文件 Provider.xml 此文件放在target/classes文件夹下
package com.zh.dubbo.demo.impl;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
public class Provider {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml");
System.out.println(context.getDisplayName() + ": here");
context.start();
System.out.println("服务已经启动...");
System.in.read();
}
}
4、
创建dubbo-consumer的MAVEN项目
添加Spring配置声明文件 consumer.xml 此文件放在target/classes文件夹下
创建Consumer类,
启动Consumer,调用远程服务
package com.zh.dubbo.consumer;
import com.zh.dubbo.demo.DemoService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Consumer {
public static void main(String[] args) {
//测试常规服务
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("consumer.xml");
context.start();
System.out.println("consumer start");
DemoService demoService = context.getBean(DemoService.class);
System.out.println("consumer");
System.out.println(demoService.getPermissions(1L));
}
}
四、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/
--结束--