您的位置:首页 > 编程语言 > Java开发

dubbo、zookeeper、Spring整合实例

2015-06-12 14:57 537 查看
在window中实现,记录一下,防止备忘!简单的配置!,只是防止自己忘记


1.到官网下载zookeeper,修改zoo_sample.cfg为zoo.cfg ,打开增加dataDir=D:\\zookeeper-3.4.6\\datadataLogDir=D:\\zookeeper-3.4.6\\log 配置。

2.启动zookeeper,启动的时候 zkServer 不添加start参数。(原来在liunx下启动添加,故在windows下添加,一直报错,后来发现没有start参数)。

3.配置tomcat,把dubbo-admin添加到tomcat下,修改配置文件。dubbo.properties。把注册地址修改为zookeeper的注册中心地址。。

4.启动tomcat,打开dubbo-admin应用。输入用户名密码,默认都是root,进入dubbo首页。

5.新建web项目server端和client端,把dubbo下的jar拷到lib下,然后在添加一个aspectjweaver-1.5.3.jar的jar包。

server端配置spring.xml,增加一个dubbo.cfg.xml,并添加到spring.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:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" 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://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-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-3.1.xsd" default-lazy-init="false">
<!-- 提供方应用名称信息,这个相当于起一个名字,我们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:service interface="com.TestRegistryService" ref="testRegistryService" register=""/>
<!-- 接口实现类 -->
<bean id="testRegistryService" class="com.impl.DemoServerImpl" />
</beans>
6.部署启动项目。在dubbo中可以看到,说明部署成功!

7。编写客户端,我就简单的测试一下。。。也是配置一个xml文件,然后添加到spring配置文件中。

<?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:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" 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://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-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-3.1.xsd" default-lazy-init="false">
<!-- 提供方应用名称信息,这个相当于起一个名字,我们dubbo管理页面比较清晰是哪个应用暴露出来的 -->
<dubbo:application name="dubbo_provider"></dubbo:application>
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://10.103.9.20:2181"  check="false" file="false" ></dubbo:registry>
<!-- 要暴露的服务接口 -->
<dubbo:reference id="testRegistryService" interface="com.TestRegistryService" check="false"></dubbo:reference>

</beans>

8.客户端简单测试一下,如图:

ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");

TestRegistryService hello=(TestRegistryService) context.getBean("testRegistryService");

System.out.println(hello.hello("nihao"));


至此,配置成功

注意:我在客户端 配置时有如下错误,删除红色的配置,原因没有看。。。。

<dubbo:registry address="zookeeper://10.103.9.20:2181" check="true" subscribe="false" file="false" ></dubbo:registry>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: