开发工具 | MyEclipse 10.7 |
---|---|
JDK | 1.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/
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代码的编写
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>
3.创建3个子项目分别为 myService(服务商(接口),模块提供方) myProvider(供应者,给dubbo zookeeper注册暴露接口) myConsumer
<projectxmlns="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>
2. 新建一个接口类
package com.xiaoshuai; public interface HelloService { public String speakHello(String name); }
3. 以上工作完成后,进行maven install
<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>
2. 实现接口服务者
package com.xiaoshuai.impl;
import com.xiaoshuai.HelloService;
public class HelloServiceImpl implements HelloService{
public String speakHello(String name) {
return "你好:"+name+"欢迎查阅小帅丶博客";
}
}
3. 将实现类接口类暴露给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>
4. 需要创建一个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();
}
}
}
5. 可以运行。启动该提供者服务,编写消费者。
6. 可以看到dubbo 提供者服务已经注册
<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>
2. 消费者进行订阅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>
3. 创建一个消费者方法,进行调用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);
}
}
4. 运行该方法输出一下内容
5. 接下来看dubbo中心有什么内容。
6. 看看消费者里面显示些什么内容
7. 详细消费者信息如图所示