2 在项目中应用Dubbo与ZooKeeper
2017-06-16 本文已影响183人
笑Skr人啊
a):准备工作
1:首先,要熟悉如何创建Maven项目,请参照 MyEclipse配置Maven 和 创建Maven项目
2:下载Dubbo
3:下载ZooKeeper
b): 项目结构介绍
三个项目的目录结构,生产者(provider),接口(interface),消费者(consumer)
其中接口只是提供了接口的方法,无需其他配置。而生产者和消费者需要dubbo.xml配置文件来配置。
![](https://img.haomeiwen.com/i3879083/7363cd44faf89901.png)
-
接口(interface)
![](https://img.haomeiwen.com/i3879083/d97553c7514414e3.png)
** SayHelloWorldService **
package com.gp6.inter;
public interface SayHelloWorldService {
public void sayHelloWorld();
}
-
提供者product
![](https://img.haomeiwen.com/i3879083/7e28d669f19624fa.png)
** 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.gp6</groupId>
<artifactId>provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build/>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.9</version>
</dependency>
<dependency>
<groupId>com.gp6</groupId>
<artifactId>interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
</dependencies>
</project>
** dubbo.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">
<bean id="helloService" class="com.gp6.provider.SayHelloWorldServiceImpl"></bean>
<!-- 提供方应用名称-->
<dubbo:application name="dubbo_product"></dubbo:application>
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" check="false" subscribe="false" register="">
</dubbo:registry>
<!-- 要暴露的服务接口 -->
<dubbo:service interface="com.gp6.inter.SayHelloWorldService" ref="helloService" />
<dubbo:protocol accesslog="true" name="dubbo" port="20880" />
</beans>
** SayHelloWorldServiceImpl **
package com.gp6.provider;
import com.gp6.inter.SayHelloWorldService;
public class SayHelloWorldServiceImpl implements SayHelloWorldService {
public void sayHelloWorld() {
System.out.println("Say Hello World");
}
}
** ProviderTest **
package com.gp6.provider;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ProviderTest {
public static void main(String[] args){
//加载dubbo.xml
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[] { "classpath*:dubbo.xml" });
//执行不退出程序的操作
while(true);
}
}
-
消费者customer
![](https://img.haomeiwen.com/i3879083/4befd8f6761a0521.png)
** 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.gp6</groupId>
<artifactId>consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.gp6</groupId>
<artifactId>interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
</dependencies>
<build>
<finalName>
consumer
</finalName>
</build>
</project>
** dubbo.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="dubbo_customer"></dubbo:application>
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" check="false">
</dubbo:registry>
<!-- 要引用的服务 -->
<dubbo:reference interface="com.gp6.inter.SayHelloWorldService" id="helloService" >
</dubbo:reference>
</beans>
** ConsumerTest **
package com.gp6.consumer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.gp6.inter.SayHelloWorldService;
public class ConsumerTest {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[] { "classpath*:dubbo.xml" });
SayHelloWorldService sayHelloWorldService = (SayHelloWorldService) applicationContext.getBean("helloService");
//使用接口中的方法
sayHelloWorldService.sayHelloWorld();
while(true);
}
}
-
三个项目所需的jar包,Maven会自动下载
** 生产者provider **
![](https://img.haomeiwen.com/i3879083/ee83f76c3ff455e4.png)
![](https://img.haomeiwen.com/i3879083/f74c6dc6a18c7d3e.png)
-
运行ZooKeeper
将下载的ZooKeeper解压,打开命令行窗口,输入命令
![](https://img.haomeiwen.com/i3879083/c16521a5d312ffcb.png)
-
运行Dubbo
将下载的Dubbo解压,放到Tomcat下的webapps下,启动Tomcat
![](https://img.haomeiwen.com/i3879083/6d6461bc9be85932.png)
![](https://img.haomeiwen.com/i3879083/858e8ddbc50d8937.png)
-
打开dubbo注册中心
其实和运行其他javaweb项目一样 :locallhost:8080/ 加项目名称即可访问。localhost:8080/dubbo-admin/或127.0.0.1:8080/dubbo-admin
此时会提示输入账号密码,
![](https://img.haomeiwen.com/i3879083/3c089083a00b3d7e.png)
账号密码配置地址:E:\WorkSpace\Study\Apache_Tomcat_6.0\webapps\dubbo-admin\WEB-INF\dubbo.properties
![](https://img.haomeiwen.com/i3879083/a3a7b729b7030880.png)
-
运行生产者测试类(ProviderTest)
![](https://img.haomeiwen.com/i3879083/59a12d4b8806774b.png)
-
运行消费者测试类(ConsumerTest)
![](https://img.haomeiwen.com/i3879083/4b1122adaf8c2870.png)
-
查看注册中心的应用共有一个提供者,一个消费者,名称和dubbo.xml的配置一样
![](https://img.haomeiwen.com/i3879083/c4f9bc66b446e912.png)