环境
开发工具 Eclipse
JDK 1.8+
容器 Tomcat 8(运行dubbo)
zookeeper版本 zookeeper-3.4.6
dubbo dubbo-admin-2.5.6
dubbo-admin-2.5.6下载地址:http://dubbo.io/
zookeeper下载地址:http://www-eu.apache.org/dist/zookeeper/zookeeper-3.4.6/
Dubbo+Zookeeper 的下载安装配置启动
部署与引入
1.dubbo-admin-2.5.6下载完成后
解压缩进入dubbo-admin目录:执行mvn install,进行打包:
放在webapps文件夹下面。先把默认的tomcat的ROOT项目备份后移除。将dubbo-admin-2.5.6.war改成ROOT.war 备用。
然后修改dubbo配置:
2.zookeeper下载后解压安装即可。windows安装完成后如下图
进入到conf里面,会看到zoo_sample.cfg文件。复制一个修改为zoo.cfg,修改相关配置内容
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/tmp/zookeeper
# the port at which the clients will connect
#这块是设置zookeeper的端口。默认2181
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
到bin文件夹下面。启动zookeeper,windows 点击zkServer.cmd即可,启动成功如下图
3.可以启动tomcat了。这样直接访问 就能看到dubbo的页面。要不然dubbo启动会报错。第一次入门的话建议就用默认的端口配置即可。默认用户 root 密码 root
登录成功以后看到如下页面
以上就是dubbo+zookeeper的配置和启动。下面开始Java代码的编写
整合Dubbo+Zookeeper+SpringMVC
创建SDK(接口项目)tfedu-base-usersdk。
定义接口ClassBusinessService:
创建服务方
1、创建maven项目tfedu-base-user
2.pom.xml文件添加:
<!-- dubbo引入 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.6</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--dubbo注册中心-->
<!-- <dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency> -->
<!--zookeeper客户端-->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
<!-- <groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.5</version> -->
</dependency>
<!-- 自定义的SDK项目 -->
<dependency>
<groupId>net.tfedu.sdk</groupId>
<artifactId>tfedu-base-usersdk</artifactId>
<version>1.0.0</version>
</dependency>
3、创建接口SDK实现:
4.dubbo接口发布:
(1)、创建dubbo-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="Jhd_Security" owner="allen.xu" organization="MyJhd"/>
<!-- 使用multicast广播注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://192.168.111.111:2181" timeout="500000" id="myjhd_id" />
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="net.tfedu.sdk.user.ClassBusinessService" ref="classBusinessService"
version="1.0.0"
cluster="failfast"
executes="10"
timeout="500000"
registry="myjhd_id" />
<!-- 和本地bean一样实现服务 -->
<bean id="classBusinessService" class="net.tfedu.base.user.impl.ClassBusinessServiceImpl" />
</beans>
(2)、spring-context.xml引入:
<import resource="dubbo-provider.xml" />
(3)、需要创建一个Java类写个方法 去 加载provider.xml 注册到dubbo + zookeeper:
a、可以直接通过war启动;
b、也可以通过启动程序
启动成功后
4、创建消费方:
1、创建maven项目tfedu-base-consumer
2、pom.xml文件引入与服务提供方一致;
3、接口发布:
<?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="consumer-of-sayHello-app" owner="myname" organization="MyJhd"/>
<!-- 使用multicast广播注册中心暴露发现服务地址 -->
<dubbo:registry address="zookeeper://192.168.111.111:2181" timeout="500000" id="myjhd_id"/>
<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference id="classBusinessService" interface="net.tfedu.sdk.user.ClassBusinessService"
version="1.0.0"
cluster="failfast"
timeout="500000"
registry="myjhd_id" />
</beans>
4、启动消费方
@ContextConfiguration(locations = { "classpath:config/dubbo-consumer.xml" })
public class ConsumerControllerTest {
@Autowired
private ClassBusinessService classBusinessService;
@Test
public void getClassStudent() {
System.out.println("消费dubbo服务:方式一....................");
System.out.println(classBusinessService.getClassStudent(1).toString());
System.out.println("消费dubbo服务:方式二....................");
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"classpath:config/dubbo-consumer.xml");
context.start();
ClassBusinessService helloService = (ClassBusinessService) context.getBean("classBusinessService");
List<Long> result = helloService.getClassStudent(6L);
System.out.println(result);
try {
Thread.sleep(100000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}