现在很流行的Dubbo很多朋友都听说过吧,最近我也在看这方面的东西,分享先我的心得笔记。

先说说我们团队要做的项目框架,很简单重在实现基于zookeeper的dubbo注册。

框架:springmvc+spring+mybatis+shiro+zookeeper+dubbo

项目分三层,model存放数据,view页面展示、controller下面具体逻辑实现。通过dubbo消费方和供应方注册,供应方给消费方暴露接口,供消费方调用。 
工程部署需要配置文件有:

 

spring-provider-dubbo.xml(提供方配置文件)

 

<span style="font-size:14px;"><?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 服务提供者应用名称 -->
  <dubbo:application name="demo-dubbo-provider-app" />
  <!--dubbo 注册中心-->
  <dubbo:registry address="zookeeper://192.168.1.125:2181" />
  <!--服务提供者 端口-->
  <dubbo:protocol name="dubbo" port="30001" />
  <!--dubbo提供服务-->
  <dubbo:service interface="com.swrac.www.testdubbo.IGoodsManager" ref="goodsService" />
  <!--spring bean 对象-->
  <bean id="goodsService" class="com.swrac.www.testdubbo.impl.GoodsManager" /> 
</beans></span>

 

spring-consumer-dubbo.xml(消费方配置文件)
{-- 
<-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 --> 

<-- 使用zookeeper注册中心暴露服务地址 --> 

<-- 生成远程服务代理,可以像使用本地bean一样使用demoService --> 
<dubbo:reference id="demoService" interface="com.unj.dubbotest.provider.DemoService" /> 
--}

例如我的配置:

 

<span style="font-size:14px;"><?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="demo-dubbo-consumer-app" />
   <dubbo:registry address="zookeeper://192.168.1.125:2181" />
	<!-- 和本地bean一样实现服务 -->
	<bean id="goodsService" class="com.swrac.www.testdubbo.impl.GoodsManager" /> 
</beans></span>

 

dubbo.properties 
{-- 
<--基于ZooKeeper的Dubbo注册中心直接部署tomcat,修改WEB-INF下文件--> 
dubbo.registry.address=zookeeper://127.0.0.1:2181 
dubbo.admin.root.password=root 
dubbo.admin.guest.password=guest 
--}

本人的配置如下:

 

<span style="font-size:14px;">dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.admin.root.password=root
dubbo.admin.guest.password=guest</span>

 

zoo_sample.cfg 
{-- 
zookeeper/conf/下,修改zoo_sample.cfg为zoo.cfg,启动bin/下zkServer.cmd 
--}

本人的配置如下:

 

# 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=D:\\Java\\zookeeper-3.4.5\\data
dataLogDir=D:\\Java\\zookeeper-3.4.5\\log
# the port at which the clients will connect
clientPort=2181
#
# 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

 

因为引入dubbo,摒弃了原有Web Service项目的wdls暴露,由于项目依赖关系严重,项目使用maven构建,通过Maven pom.xml三维坐标引入jar包,调用dubbo暴露接口开发。

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐