使用dubbo+zookeeper创建一个小应用
2016-04-05 22:01
651 查看
dubbo和zookeeper都是分布式服务框架,其中dubbo是阿里巴巴研发的开源框架,dubbo主要分为服务提供方,服务消费方,注册中心,监控中心,服务运行容器。下图是dubbo官网的一个结构图:
这里zookeeper在这里主要是用来作为dubbo的注册中心,dubbo还可以使用Multicast注册中心,Zookeeper注册中心,Redis注册中心,Simple注册中心。下面来看实例:
创建一个Java Project作为dubbo的 provider。命名为DubboProvider
1、引入一下jar包:
2、新建一个IhelloService接口
3、新建一个IhelloService的实现类HelloServiceImpl.java
然后在新建一个Java Project项目DubboConsumer,并引入一下jar包:
5、在DubboConsumer中新建一个接口IhelloService.java
6、新建consumer.xml
7、新建一个测试类TestConsumer
http://www.apache.org/dist/zookeeper/
9、先启动zookeeper注册中心
/zookeeper-3.4.8\bin目录下的zkServer.cmd(window环境)或者zkServer.sh(Linux环境)。然后在运行TestProvider.java在运行TestConsumer就能看到输出结果。
这里zookeeper在这里主要是用来作为dubbo的注册中心,dubbo还可以使用Multicast注册中心,Zookeeper注册中心,Redis注册中心,Simple注册中心。下面来看实例:
创建一个Java Project作为dubbo的 provider。命名为DubboProvider
1、引入一下jar包:
2、新建一个IhelloService接口
package com.service; /** * @description * @date:(2016-3-29 下午10:21:03) * @author Administrator * @version v1.0 * @since v1.0 * * Modified history * * Modified date: * Modifier user: * description: * * */ public interface IHelloService { String sayHello(String name); }
3、新建一个IhelloService的实现类HelloServiceImpl.java
package com.service.impl; import com.service.IHelloService; /** * @description * @date:(2016-3-29 下午10:21:26) * @author Administrator * @version v1.0 * @since v1.0 * * Modified history * * Modified date: * Modifier user: * description: * * */ public class HelloServiceImpl implements IHelloService{ @Override public String sayHello(String name) { System.out.println("生产者中的方法执行..."); return "hello "+name; } }3、新建一个provider.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:application name="hello-world-app" /> <!-- 使用multicast广播注册中心暴露服务地址 --> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 声明需要暴露的服务接口 --> <dubbo:service interface="com.service.IHelloService" ref="helloService" /> <!-- 和本地bean一样实现服务 --> <bean id="helloService" class="com.service.impl.HelloServiceImpl" /> </beans>4、新建一个服务发布者启动类
package com.test; import java.io.IOException; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * @description * @date:(2016-3-29 下午10:41:24) * @author Administrator * @version v1.0 * @since v1.0 * * Modified history * * Modified date: * Modifier user: * description: * * */ public class TestProvider { public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("provider.xml"); applicationContext.start(); System.in.read(); // 为保证服务一直开着,利用输入流的阻塞来模拟 } }
然后在新建一个Java Project项目DubboConsumer,并引入一下jar包:
5、在DubboConsumer中新建一个接口IhelloService.java
package com.service; /** * @description * @date:(2016-3-29 下午10:21:03) * @author Administrator * @version v1.0 * @since v1.0 * * Modified history * * Modified date: * Modifier user: * description: * * */ public interface IHelloService { String sayHello(String name); }
6、新建consumer.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:application name="consumer-of-helloworld-app" /> <!-- 使用multicast广播注册中心暴露发现服务地址 --> <dubbo:registry address="zookeeper://127.0.0.1:2181"/> <!-- 用dubbo协议在20880端口暴露服务 --> <!-- <dubbo:protocol name="dubbo" port="20880" /> --> <!-- 生成远程服务代理,可以和本地bean一样使用demoService --> <dubbo:reference id="helloService" interface="com.service.IHelloService" /> </beans>
7、新建一个测试类TestConsumer
package com.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.service.IHelloService; /** * @description * @date:(2016-3-29 下午10:41:03) * @author Administrator * @version v1.0 * @since v1.0 * * Modified history * * Modified date: * Modifier user: * description: * * */ public class TestConsumer { public static void main(String[] args) { ApplicationContext applicationContext=new ClassPathXmlApplicationContext("consumer.xml"); IHelloService helloService=(IHelloService)applicationContext.getBean("helloService"); String result=helloService.sayHello("Tom"); System.out.println(result); } }8、下载zookeeper
http://www.apache.org/dist/zookeeper/
9、先启动zookeeper注册中心
/zookeeper-3.4.8\bin目录下的zkServer.cmd(window环境)或者zkServer.sh(Linux环境)。然后在运行TestProvider.java在运行TestConsumer就能看到输出结果。
相关文章推荐
- 为什么我要成为最佳(一)
- C++虚函数表详细解释及实例分析
- 数据库
- session 学习
- 一个程序员的孤独
- 拓扑排序·二:有向无环图,求总的病毒感染数
- HDOJ 1267 下沙的沙子有几粒?(递推)
- hdu3591 The trouble of Xiaoqian(多重背包 + 完全背包)
- 安卓欢迎界面的编写
- ACM_模板_KMP算法
- 网页元素的滑动事件(四)
- 视图控制器中navigationItem中backBarButtonItem设置问题
- Qt回忆录之配置开发环境
- 配置gitlab gerrit jenkins
- iOS关键字
- Activity生命周期的通俗理解
- C++ 输入一个0-6的整数,转化成星期输出
- 仙剑---相爱
- 阿里资深工程师分享支付宝热补丁技术—— AndFix原理
- Java入门:基础算法之从字符串中找到重复的字符