通过dubbo暴露接口调用方法,及基于zookeeper的dubbo涉及配置文件

来源:互联网 发布:爱思苹果助手 for mac 编辑:程序博客网 时间:2024/05/21 10:29

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

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

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

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


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

[plain] view plain copy
 print?
  1. <span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.   xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"  
  5.   xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.     http://www.springframework.org/schema/beans/spring-beans.xsd  
  7.     http://code.alibabatech.com/schema/dubbo   
  8.     http://code.alibabatech.com/schema/dubbo/dubbo.xsd  
  9.     ">   
  10.   <!--dubbo 服务提供者应用名称 -->  
  11.   <dubbo:application name="demo-dubbo-provider-app" />  
  12.   <!--dubbo 注册中心-->  
  13.   <dubbo:registry address="zookeeper://192.168.1.125:2181" />  
  14.   <!--服务提供者 端口-->  
  15.   <dubbo:protocol name="dubbo" port="30001" />  
  16.   <!--dubbo提供服务-->  
  17.   <dubbo:service interface="com.swrac.www.testdubbo.IGoodsManager" ref="goodsService" />  
  18.   <!--spring bean 对象-->  
  19.   <bean id="goodsService" class="com.swrac.www.testdubbo.impl.GoodsManager" />   
  20. </beans></span>  

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

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

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

例如我的配置:

[plain] view plain copy
 print?
  1. <span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.   xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"  
  5.   xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.     http://www.springframework.org/schema/beans/spring-beans.xsd  
  7.     http://code.alibabatech.com/schema/dubbo   
  8.     http://code.alibabatech.com/schema/dubbo/dubbo.xsd  
  9.     ">   
  10.    <dubbo:application name="demo-dubbo-consumer-app" />  
  11.    <dubbo:registry address="zookeeper://192.168.1.125:2181" />  
  12.     <!-- 和本地bean一样实现服务 -->  
  13.     <bean id="goodsService" class="com.swrac.www.testdubbo.impl.GoodsManager" />   
  14. </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 
--} 

本人的配置如下:

[plain] view plain copy
 print?
  1. <span style="font-size:14px;">dubbo.registry.address=zookeeper://127.0.0.1:2181  
  2. dubbo.admin.root.password=root  
  3. dubbo.admin.guest.password=guest</span>  

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

本人的配置如下:

[plain] view plain copy
 print?
  1. # The number of milliseconds of each tick  
  2. tickTime=2000  
  3. # The number of ticks that the initial   
  4. # synchronization phase can take  
  5. initLimit=10  
  6. # The number of ticks that can pass between   
  7. # sending a request and getting an acknowledgement  
  8. syncLimit=5  
  9. # the directory where the snapshot is stored.  
  10. # do not use /tmp for storage, /tmp here is just   
  11. # example sakes.  
  12. dataDir=D:\\Java\\zookeeper-3.4.5\\data  
  13. dataLogDir=D:\\Java\\zookeeper-3.4.5\\log  
  14. # the port at which the clients will connect  
  15. clientPort=2181  
  16. #  
  17. # Be sure to read the maintenance section of the   
  18. # administrator guide before turning on autopurge.  
  19. #  
  20. # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance  
  21. #  
  22. # The number of snapshots to retain in dataDir  
  23. # autopurge.snapRetainCount=3  
  24. # Purge task interval in hours  
  25. # Set to "0" to disable auto purge feature  
  26. # autopurge.purgeInterval=1  

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

原创粉丝点击