介绍就不过多的说明。可以参考http://blog.csdn.net/congcong68/article/details/41113239博客里面写的相关介绍。
后续会补充完善SpringMVC部分
微信扫一扫,支持一下我的个人微信小程序
项目码云GIT地址:https://gitee.com/xshuai/dubbo/
*开发工具MyEclipse 10.7JDK1.7容器*Tomcat 8(运行dubbo)zookeeper版本zookeeper-3.4.6*dubbo*dubbo-admin-2.5.3
dubbo-admin-2.5.3下载地址:http://pan.baidu.com/s/1bozCMzP
zookeeper下载地址:http://www-eu.apache.org/dist/zookeeper/zookeeper-3.4.6/
Dubbo+Zookeeper 的下载安装配置启动
1.dubbo-admin-2.5.3下载完成后。放在webapps文件夹下面。先把默认的tomcat的ROOT项目备份后移除。将dubbo-admin-2.5.3.war改成ROOT.war 备用
2.zookeeper下载后解压安装即可。windows安装完成后如下图
进入到conf里面,会看到zoo_sample.cfg文件。复制一个修改为zoo.cfg,修改相关配置内容
# 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=/tmp/zookeeper
# the port at which the clients will connect
#这块是设置zookeeper的端口。默认2181
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
到bin文件夹下面。启动zookeeper,windows 点击zkServer.cmd即可,启动成功如下图
3.可以启动tomcat了。这样直接访问 就能看到dubbo的页面。要不然dubbo启动会报错。第一次入门的话建议就用默认的端口配置即可。默认用户 root 密码 root
登录成功以后看到如下页面
以上就是dubbo+zookeeper的配置和启动。下面开始Java代码的编写
整合Dubbo+Zookeeper+SpringMVC
1.创建myDubbo项目为主项目,结构图如下 myC P S后续才创建,刚开始是没有的。
POM.XML配置如下
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com</groupId>
<artifactId>mydubbo</artifactId>
注意这里。打包为POM,分布式系统的第一步
<packaging>pom</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>mydubbo</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>3.2.4.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--dubbo注册中心-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
<!--zookeeper客户端-->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
<build>
<finalName>mydubbo</finalName>
</build>
创建子项目后自动会加载。刚开始是没有的
<modules>
<module>myService</module>
<module>myProvider</module>
<module>myConsumer</module>
</modules>
</project>
2.创建服务提供商子项目,MyEclipse操作如下图示意 对mydubbo项目鼠标右键new或者maven插件直接创建maven module项目,填写子项目名称。有的如果不选种Create a simple project 自定义创建会报错。选择则不会。根据实际情况来确定是否选择
3.创建3个子项目分别为 myService(服务商(接口),模块提供方) myProvider(供应者,给dubbo zookeeper注册暴露接口) myConsumer
myService 项目相关配置
pom配置
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <artifactId>mydubbo</artifactId> <groupId>com</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <name>myService</name> <artifactId>myService</artifactId> <packaging>jar</packaging></project>
新建一个接口类
package com.xiaoshuai;public interface HelloService { public String speakHello(String name);}
以上工作完成后,进行maven install
myProvider项目相关配置
- pom配置
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <artifactId>mydubbo</artifactId> <groupId>com</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>myProvider</artifactId>引用接口服务项目 子项目maven install 为jar <dependencies> <dependency> <groupId>com</groupId> <artifactId>myService</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies></project>
- 实现接口服务者
package com.xiaoshuai.impl;import com.xiaoshuai.HelloService;public class HelloServiceImpl implements HelloService{ public String speakHello(String name) { return "你好:"+name+"欢迎查阅小帅丶博客"; } }
将实现类接口类暴露给dubbo+zookeeper 在myProvider创建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: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="hello-provider" /> <!-- 使用multicast广播注册中心暴露服务地址 --> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 声明需要暴露的服务接口 --> <dubbo:service interface="com.xiaoshuai.HelloService" ref="helloService" /> <!-- 和本地bean一样实现服务 --> <bean id="helloService" class="com.xiaoshuai.impl.HelloServiceImpl" /> </beans>
需要创建一个Java类写个方法 去 加载provider.xml 注册到dubbo + zookeeper
package com.xiaoshuai.impl;import org.springframework.context.support.ClassPathXmlApplicationContext;public class ProviderServer { public static void main(String[] args){ try { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml"); context.start(); System.in.read(); } catch (Exception e) { e.printStackTrace(); } }}
可以运行。启动该提供者服务,编写消费者。
可以看到dubbo 提供者服务已经注册
myConsumer 项目相关配置
pom配置
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <artifactId>mydubbo</artifactId> <groupId>com</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>myConsumer</artifactId> <dependencies> <dependency> <groupId>com</groupId> <artifactId>myService</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies></project>
消费者进行订阅provider的服务,也就是需要去zookeeper读取加载服务,那就需要创建一个consumer.xml去加载zookeeper
<?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="hello-consumer" /> <!-- 使用multicast广播注册中心暴露发现服务地址 --> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <!-- 生成远程服务代理,可以和本地bean一样使用demoService --> <dubbo:reference id="helloService" interface="com.xiaoshuai.HelloService" /></beans>
创建一个消费者方法,进行调用ConsumerClient
package com.xiaoshuai;import org.springframework.context.support.ClassPathXmlApplicationContext;public class ConsumerClient { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml"); HelloService helloService = (HelloService) context.getBean("helloService"); String result = helloService.speakHello("xiaoshuai"); System.out.println(result); }}
运行该方法输出一下内容
接下来看dubbo中心有什么内容。
看看消费者里面显示些什么内容
详细消费者信息如图所示
- 以上一个简单的分布式的Demo就已经完结
- 新手入门。有问题的地方可以一起交流哦。
作者:小帅帅丶
原文地址:https://my.oschina.net/xshuai/blog/891281
喜欢 0
版权归原作者 tivnn 所有, 如有侵权,请联系我们删除。