[Dubbo实战]dubbo + zookeeper + spring 实战 (转)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Dubbo实战]dubbo + zookeeper + spring 实战 (转)相关的知识,希望对你有一定的参考价值。
这里最熟悉的就是spring了,项目中应用很多。dubbo是一个实现分布式的框架,zookeeper是注册中心。给我的感觉就像多对多关系的两者表,zookeeper相当于第三张表维护关系。下面通过一个小程序加深认识。
一、安装zookeeper
去官网下载zookeeper, 然后解压到目录中,我解压到了E:\zookeeper-3.3.6,在启动zookeeper之前,首先找到conf文件夹下的 zoo_sample.cfg,重新命名为zoo.cfg,网上说zookeeper启动的时候这个文件会是默认的配置文件。接下来到bin目录下,双击 启动zkServer.cmd,启动成功了如下:
建的项目是maven项目,所以jar包的依赖都通过maven引用的,项目通过spring容器管理。
二、Server
项目结构图:
1.接口
- package com.mor.server.dubbo.service;
- /**
- * 服务端接口
- * @author zx
- * @date 2015年8月17日 下午3:19:12
- */
- public interface DemoServer {
- String sayHello(String str);
- }
2.实现
- package com.mor.server.dubbo.service;
- import java.util.Date;
- /**
- * 服务端接口实现类
- * @author zx
- * @date 2015年8月17日 下午3:18:52
- */
- public class DemoServerImpl implements DemoServer {
- public String sayHello(String str) {
- str = "Hello " + str + " 2:" + new Date();
- System.err.println("server:" + str);
- return str;
- }
- }
3.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>
- <groupId>com.mor.maven</groupId>
- <artifactId>dubboserver</artifactId>
- <version>0.0.1</version>
- <packaging>jar</packaging>
- <name>dubboserver</name>
- <url>http://maven.apache.org</url>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <spring.version>3.1.4.RELEASE</spring.version>
- <slf4j.version>1.6.6</slf4j.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>3.8.1</version>
- <scope>test</scope>
- </dependency>
- <!-- Spring -->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-aop</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-asm</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-core</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-beans</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-expression</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <!-- spring end -->
- <!-- log -->
- <dependency>
- <groupId>log4j</groupId>
- <artifactId>log4j</artifactId>
- <version>1.2.16</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-api</artifactId>
- <version>${slf4j.version}</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-log4j12</artifactId>
- <version>${slf4j.version}</version>
- </dependency>
- <!-- dubbo -->
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>dubbo</artifactId>
- <version>2.5.3</version>
- </dependency>
- <!-- zkclient -->
- <dependency>
- <groupId>com.github.sgroschupf</groupId>
- <artifactId>zkclient</artifactId>
- <version>0.1</version>
- </dependency>
- <!-- zookeeper -->
- <dependency>
- <groupId>org.apache.zookeeper</groupId>
- <artifactId>zookeeper</artifactId>
- <version>3.3.6</version>
- </dependency>
- </dependencies>
- <build>
- <finalName>dubbo-demo</finalName>
- <plugins>
- <!-- 非多个资源配置 start-->
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>2.1</version>
- <configuration>
- <source>1.5</source>
- <target>1.5</target>
- <encoding>UTF-8</encoding>
- <failOnError>false</failOnError>
- </configuration>
- </plugin>
- <!-- 非多个资源配置 end-->
- </plugins>
- </build>
- </project>
通过maven引用需要的jar包
4.spring配置文件
先引入dubbo的标签
- <?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-world-app" />
- <dubbo:registry protocol="zookeeper" address="192.168.24.140:2181" />
- <dubbo:protocol name="dubbo" port="20880" />
- <dubbo:service interface="com.mor.server.dubbo.service.DemoServer" ref="demoService" /> <!-- 和本地bean一样实现服务 -->
- <bean id="demoService" class="com.mor.server.dubbo.service.DemoServerImpl" />
- </beans>
5.执行入口
- package com.mor.main;
- import java.io.IOException;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- /**
- * 服务器的执行入口
- * @author zx
- * @date 2015年8月17日 下午3:17:33
- */
- public class Main {
- public static void main(String[] args) throws IOException {
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationProvider.xml" });
- context.start();
- System.out.println("按任意键退出");
- System.in.read();
- }
- }
三、Client
项目结构图:
1.接口同服务端
2.调用接口
- package com.mor.server.dubbo.service;
- import java.util.Date;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class ChatAction {
- public void SayHello(){
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationConsumer.xml" });
- context.start();
- DemoServer demoServer = (DemoServer) context.getBean("demoService");
- System.out.println("client:"+demoServer.sayHello("zx"+"1:"+new Date())+"3:"+new Date());
- }
- }
3.pom文件引用的jar都相同,只是修改一下基本的配置就可以了。
4.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: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
- ">
- <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 192.9.145.19:2181,192.9.145.19:2182,192.9.145.19:2183-->
- <dubbo:application name="consumer-of-helloworld-app" /> <!-- 使用multicast广播注册中心暴露发现服务地址 -->
- <dubbo:registry protocol="zookeeper" address="192.168.24.140:2181,,192.168.24.140:2182,192.168.24.140:2183" /> <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
- <dubbo:reference id="demoService" interface="com.mor.server.dubbo.service.DemoServer" />
- </beans>
5.执行入口
- package com.mor.client.dubbo.main;
- import com.mor.server.dubbo.service.ChatAction;
- /**
- * 客户端的执行入口
- * @author zx
- * @date 2015年8月17日 下午3:18:00
- */
- public class Main {
- public static void main(String[] args) throws InterruptedException {
- int i=0;
- while(i++<100){
- ChatAction act = new ChatAction();
- act.SayHello();
- Thread.sleep(3000);
- }
- }
- }
四、测试
先启动zookeeper,再依次启动服务器和客户端。
服务器启动成功如下:
客户端访问成功如下:
五、总结
运用dubbo能实现分布式,dubbo也是面向服务的架构。zookeeper做为注册中心,拿到服务器端暴露的接口,客户端也向zookeepe去注册,客户端需要什么服务注册中心就提供给客户端。这样客户端和服务端很好的解耦了。
以上是关于[Dubbo实战]dubbo + zookeeper + spring 实战 (转)的主要内容,如果未能解决你的问题,请参考以下文章
Dubbo实战 Dubbo+Zookeeper+Spring整合应用篇-Dubbo基于Zookeeper实现分布式服务
Dubbo实战 Dubbo+Zookeeper+Spring整合应用篇-Dubbo基于Zookeeper实现分布式服务
Dubbo实战 Dubbo+Zookeeper+Spring整合应用篇-Dubbo基于Zookeeper实现分布式服务
[dubbo实战] dubbo+zookeeper伪集群搭建