本来负责的支付项目中有一个调用第三方短信平台的接口,因为短信平台要求短信发送有IP白名单限制,于是老大决定将项目中短信发送这一块提取出来,项目中使用dubbo,和zookeeper相关配置。
参照dubbo官网,附上dubbo官网上quickstart部分:http://dubbo.io/User+Guide-zh.htm
其实很早之前就想看dubbo和zookeeper,可是看到dubbo官网上那么多架构就很烦躁,所以知难而退,本次即将在项目中使用,所以扎下心来看官网上的快速启动部分。也百度了很多相关配置,终于搞定了dubbo的入门部分
用到dubbo,首先安装zookeeper,作为调度中心。下面介绍zookeeper的安装。
1. 解压zookeeper-3.4.9.tar.gz到指定目录,进入zookeeper-3.4.9\conf目录并复制zoo_sample.cfg文件改名为zoo.cfg
因为zookeeper启动时默认找zoo.cfg这个文件,修改zoo.cfg文件内容如下:
# The number of milliseconds of each tick <span style="color:#ff0000;">tickTime=2000 </span> # The number of ticks that the initial # synchronization phase can take <span style="color:#ff0000;">initLimit=10 </span> # The number of ticks that can pass between # sending a request and getting an acknowledgement <span style="color:#ff0000;">syncLimit=5 </span> # the directory where the snapshot is stored. # do not use /tmp for storage, /tmp here is just # example sakes. dataDir=E:\Develop\DevelopmentKit\zookeeper-3.4.9\data # the port at which the clients will connect 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
到此zookeeper安装完毕,进入zookeeper-3.4.6\bin目录,执行zkServer.cmd或者zkServer.sh脚本就可以
启动zookeeper了,例如在Windows下进入cmd命令行,E:\Develop\DevelopmentKit\zookeeper-3.4.9\zookeeper-3.4.
9\bin>zkServe可。
zookeeper启动成功
2.1 参照官网的demo部分,将将要暴露出的接口放在了一个新建的maven项目中,注意,打的是jar包,一开始我打了war包总是报一个com.service的错,上面是L加你的接口名
接口项目lib结构如左
/** * 提取出来作为其他项目通用发送短信service,使用dubbo沟通 * * @author Lujx * @date 创建时间:2016年10月31日 */ public interface SMSDubboService { public Map<String, Object> sendSMS(String phone, String cont, HttpServletRequest request); public void sayHello(); }
2.2 配置dubbo_provider项目
2.2.1 pom文件中引用接口项目
<dependency> <groupId>aoorey</groupId> <artifactId>dubboTest</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>
2.2.2 接口实现类
<span style="color:#ff0000;">@Service("sMSDubboService")</span>
<span style="color:#cc0000;">//这个地方应该service注解应该写service名字</span> @Transactional public class SMSDubboServiceImpl implements SMSDubboService { @Autowired NoticeSMSLogService smsLogService; @Override public void sayHello() { System.out.println("测试dubbo联通"); }2.2.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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <context:annotation-config /> <!-- 提供方应用名称信息,这个相当于起一个名字,我们dubbo管理页面比较清晰是哪个应用暴露出来的 --> <dubbo:application name="dubbo_provider"></dubbo:application> <!--这个地方的注册中心指向的是我本地安装的zookeeper,如果不用也可以像官网一样采用广播方式 --> <dubbo:registry address="zookeeper://127.0.0.1:2181" check="false" subscribe="false"></dubbo:registry> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880" host="127.0.0.1" /> <!-- 提供服务方配置 ref中的部分代表指向暴露接口的实现类,接口实现类可以不写,也可以写作为一个bean配置在xml里面--> <dubbo:service interface="com.aoorey.service.SMSDubboService" ref="sMSDubboService" />
<context:property-placeholder /> lt;/beans>
至此,生产者项目已经准备完毕。
2.3 dubbo_consumer项目准备
2.3.1 pom文件导入接口项目
<dependency> <groupId>aoorey</groupId> <artifactId>dubboTest</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>2.3.2 配置文件准备
<!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name="consumer-userService" /> <!-- 使用multicast广播注册中心暴露服务地址 --> <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> --> <!-- 使用zookeeper注册中心暴露服务地址 --> lt;dubbo:registry address="zookeeper://127.0.0.1:2181" /> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 生成远程服务代理,可以和本地bean一样使用实现类 --> <dubbo:reference id="smsService" interface="com.aoorey.service.SMSDubboService" />2.3.3 调用生产者暴露出来的接口及实现类
@Controller @RequestMapping public class TestController { @Resource SMSDubboService sMSDubboService; @RequestMapping(value = "/dubboTest") public void dubboTest() { sMSDubboService.sayHello(); }
}
1.先启动zookeeper
2.启动dubbo-provider项目,我项目是springBoot项目,因此在application类中
@Configuration @ImportResource({ "classpath:dubboContext.xml" }) class XmlResource { } }3.启动dubbo-consumer项目
注意:1)在启动两个项目时,都必须先将两个配置文件添加进spring管理中
2)在dubbo配置文件中,dubbo.xsd约束可能报错,解决方式是是windows-preferences-xml catalog添加约束指向本地下载好的dubbo.xsd,同时将key改为下载好的xsd一样的版本
最后结果
dubbo官方还同时提供了一个dubbo admin管理器,供开发者检测服务的管理
下载dubbo -admin放到tomcat中apache-tomcat-6.0.35\webapps\ROOT目录下,将原有的root下面的文件删除
修改WEB-INF目录下的dubbo.properties文件:
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.admin.root.password=root
dubbo.admin.guest.password=guest
启动tomcat
访问http://127.0.0.1:8080/governance/applications/
登录的用户名和密码都是root,不是root/guest