dubbo+ZooKeeper框架基础配置


先说为什么要用分布式架构:个人理解是从一个繁杂的整体式项目拆分成一个一个互相独立的项目,分别部署到不同的机器,减轻开发的复杂度,提高服务的高可用。


个人搭建的demo基于SSM框架,分成三个Module,API、Provider、Consumer,其中 API打包方式是jar ,提供给Provider和Consumer使用。


安装Dubbo控制台

首先安装dubbo-admin控制台,在github上搜alibaba/dubbo源码自己编译war包。

这个是我自己编译2.54版本的dubbo-admin:链接:http://pan.baidu.com/s/1pLHmZ95 密码:oo6n

放到Tomcat下进行编译,启动后打开网页。

dubbo-admin


安装ZooKeeper

这个去ZooKeeper官网下载自己喜欢的版本(个人下载当时最新版)

网上也有很多启动服务的教程,记得修改conf目录下的zoo.cfg文件的配置。


dubbo配置文件

使用dubbo作为分布式框架,Zookeeper作为注册中心

Provider是服务提供者,dubbo配置文件为dubbo-service.xml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?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:p="http://www.springframework.org/schema/p"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


<context:component-scan base-package="cn.sevenyuan.demo"/>

<!-- 往ZooKeeper发布自己提供的服务 -->
<dubbo:application name="provider"/>
<dubbo:registry protocol="zookeeper" address="zookeeper://localhost:2181" check="true"/>
<dubbo:protocol name="dubbo" port="20802" />

<!-- 此处是自己设置的服务 -->
<dubbo:service timeout="15000" interface="cn.sevenyuan.demo.service.IBookService" ref="bookService" />


</beans>

Consumer是服务消费者,dubbo配置文件为dubbo-reference.xml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?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:p="http://www.springframework.org/schema/p"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<context:component-scan base-package="cn.sevenyuan.demo"/>

<dubbo:application name="consumer-test"/>
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://localhost" username="root" password="root"/>
<dubbo:protocol name="dubbo" host="localhost" port="20880" />

<dubbo:registry protocol="zookeeper" address="zookeeper://localhost:2181" />

<dubbo:reference timeout="3000" interface="cn.sevenyuan.demo.service.IBookService" id="bookService" check="false"/>

</beans>

配置好自己的服务之后,启动服务提供者和服务消费者,看dubbo控制面板就能看到在dubbo注册和消费的服务