这里最熟悉的就是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.接口

  1. package com.mor.server.dubbo.service;
  2. /**
  3. * 服务端接口
  4. * @author zx
  5. * @date 2015年8月17日 下午3:19:12
  6. */
  7. public interface DemoServer {
  8. String sayHello(String str);
  9. }

2.实现

  1. package com.mor.server.dubbo.service;
  2. import java.util.Date;
  3. /**
  4. * 服务端接口实现类
  5. * @author zx
  6. * @date 2015年8月17日 下午3:18:52
  7. */
  8. public class DemoServerImpl implements DemoServer {
  9. public String sayHello(String str) {
  10. str = "Hello " + str + "  2:" + new Date();
  11. System.err.println("server:" + str);
  12. return str;
  13. }
  14. }

3.pom文件

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.mor.maven</groupId>
  5. <artifactId>dubboserver</artifactId>
  6. <version>0.0.1</version>
  7. <packaging>jar</packaging>
  8. <name>dubboserver</name>
  9. <url>http://maven.apache.org</url>
  10. <properties>
  11. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  12. <spring.version>3.1.4.RELEASE</spring.version>
  13. <slf4j.version>1.6.6</slf4j.version>
  14. </properties>
  15. <dependencies>
  16. <dependency>
  17. <groupId>junit</groupId>
  18. <artifactId>junit</artifactId>
  19. <version>3.8.1</version>
  20. <scope>test</scope>
  21. </dependency>
  22. <!-- Spring -->
  23. <dependency>
  24. <groupId>org.springframework</groupId>
  25. <artifactId>spring-aop</artifactId>
  26. <version>${spring.version}</version>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework</groupId>
  30. <artifactId>spring-asm</artifactId>
  31. <version>${spring.version}</version>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.springframework</groupId>
  35. <artifactId>spring-core</artifactId>
  36. <version>${spring.version}</version>
  37. </dependency>
  38. <dependency>
  39. <groupId>org.springframework</groupId>
  40. <artifactId>spring-beans</artifactId>
  41. <version>${spring.version}</version>
  42. </dependency>
  43. <dependency>
  44. <groupId>org.springframework</groupId>
  45. <artifactId>spring-context</artifactId>
  46. <version>${spring.version}</version>
  47. </dependency>
  48. <dependency>
  49. <groupId>org.springframework</groupId>
  50. <artifactId>spring-expression</artifactId>
  51. <version>${spring.version}</version>
  52. </dependency>
  53. <!-- spring end -->
  54. <!-- log -->
  55. <dependency>
  56. <groupId>log4j</groupId>
  57. <artifactId>log4j</artifactId>
  58. <version>1.2.16</version>
  59. </dependency>
  60. <dependency>
  61. <groupId>org.slf4j</groupId>
  62. <artifactId>slf4j-api</artifactId>
  63. <version>${slf4j.version}</version>
  64. </dependency>
  65. <dependency>
  66. <groupId>org.slf4j</groupId>
  67. <artifactId>slf4j-log4j12</artifactId>
  68. <version>${slf4j.version}</version>
  69. </dependency>
  70. <!-- dubbo -->
  71. <dependency>
  72. <groupId>com.alibaba</groupId>
  73. <artifactId>dubbo</artifactId>
  74. <version>2.5.3</version>
  75. </dependency>
  76. <!-- zkclient  -->
  77. <dependency>
  78. <groupId>com.github.sgroschupf</groupId>
  79. <artifactId>zkclient</artifactId>
  80. <version>0.1</version>
  81. </dependency>
  82. <!--  zookeeper -->
  83. <dependency>
  84. <groupId>org.apache.zookeeper</groupId>
  85. <artifactId>zookeeper</artifactId>
  86. <version>3.3.6</version>
  87. </dependency>
  88. </dependencies>
  89. <build>
  90. <finalName>dubbo-demo</finalName>
  91. <plugins>
  92. <!-- 非多个资源配置 start-->
  93. <plugin>
  94. <groupId>org.apache.maven.plugins</groupId>
  95. <artifactId>maven-compiler-plugin</artifactId>
  96. <version>2.1</version>
  97. <configuration>
  98. <source>1.5</source>
  99. <target>1.5</target>
  100. <encoding>UTF-8</encoding>
  101. <failOnError>false</failOnError>
  102. </configuration>
  103. </plugin>
  104. <!-- 非多个资源配置 end-->
  105. </plugins>
  106. </build>
  107. </project>

通过maven引用需要的jar包
4.spring配置文件

先引入dubbo的标签

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://code.alibabatech.com/schema/dubbo
  8. http://code.alibabatech.com/schema/dubbo/dubbo.xsd
  9. ">
  10. <dubbo:application name="hello-world-app" />
  11. <dubbo:registry  protocol="zookeeper"  address="192.168.24.140:2181"  />
  12. <dubbo:protocol name="dubbo" port="20880" />
  13. <dubbo:service interface="com.mor.server.dubbo.service.DemoServer" ref="demoService" />       <!-- 和本地bean一样实现服务 -->
  14. <bean id="demoService" class="com.mor.server.dubbo.service.DemoServerImpl" />
  15. </beans>

5.执行入口

  1. package com.mor.main;
  2. import java.io.IOException;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. /**
  5. * 服务器的执行入口
  6. * @author zx
  7. * @date 2015年8月17日 下午3:17:33
  8. */
  9. public class Main {
  10. public static void main(String[] args) throws IOException {
  11. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationProvider.xml" });
  12. context.start();
  13. System.out.println("按任意键退出");
  14. System.in.read();
  15. }
  16. }

三、Client

项目结构图:

1.接口同服务端

2.调用接口

  1. package com.mor.server.dubbo.service;
  2. import java.util.Date;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class ChatAction {
  5. public void SayHello(){
  6. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationConsumer.xml" });
  7. context.start();
  8. DemoServer demoServer = (DemoServer) context.getBean("demoService");
  9. System.out.println("client:"+demoServer.sayHello("zx"+"1:"+new Date())+"3:"+new Date());
  10. }
  11. }

3.pom文件引用的jar都相同,只是修改一下基本的配置就可以了。

4.spring配置文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://code.alibabatech.com/schema/dubbo
  8. http://code.alibabatech.com/schema/dubbo/dubbo.xsd
  9. ">
  10. <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样  192.9.145.19:2181,192.9.145.19:2182,192.9.145.19:2183-->
  11. <dubbo:application name="consumer-of-helloworld-app" />       <!-- 使用multicast广播注册中心暴露发现服务地址 -->
  12. <dubbo:registry  protocol="zookeeper"  address="192.168.24.140:2181,,192.168.24.140:2182,192.168.24.140:2183" />         <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
  13. <dubbo:reference id="demoService" interface="com.mor.server.dubbo.service.DemoServer" />
  14. </beans>

5.执行入口

  1. package com.mor.client.dubbo.main;
  2. import com.mor.server.dubbo.service.ChatAction;
  3. /**
  4. * 客户端的执行入口
  5. * @author zx
  6. * @date 2015年8月17日 下午3:18:00
  7. */
  8. public class Main {
  9. public static void main(String[] args) throws InterruptedException {
  10. int i=0;
  11. while(i++<100){
  12. ChatAction act = new ChatAction();
  13. act.SayHello();
  14. Thread.sleep(3000);
  15. }
  16. }
  17. }


四、测试

先启动zookeeper,再依次启动服务器和客户端。

服务器启动成功如下:

客户端访问成功如下:

五、总结

运用dubbo能实现分布式,dubbo也是面向服务的架构。zookeeper做为注册中心,拿到服务器端暴露的接口,客户端也向zookeepe去注册,客户端需要什么服务注册中心就提供给客户端。这样客户端和服务端很好的解耦了。

[Dubbo实战]dubbo + zookeeper + spring 实战 (转)的更多相关文章

  1. 【Dubbo实战】 Dubbo+Zookeeper+Spring整合应用篇-Dubbo基于Zookeeper实现分布式服务(转)

    Dubbo与Zookeeper.Spring整合使用 Dubbo采用全Spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载Dubbo的配置即可,Dubbo基于Spri ...

  2. [dubbo实战] dubbo+zookeeper伪集群搭建

    zookeeper作为注册中心,服务器和客户端都要访问,如果有大量的并发,肯定会有等待.所以可以通过zookeeper集群解决. 一.为什么需要zookeeper呢? 大部分分布式应用需要一个主控.协 ...

  3. [dubbo实战] dubbo+zookeeper伪集群搭建 (转)

    zookeeper作为注册中心,服务器和客户端都要访问,如果有大量的并发,肯定会有等待.所以可以通过zookeeper集群解决. 一.为什么需要zookeeper呢? 大部分分布式应用需要一 个主控. ...

  4. Dubbo教程:入门到实战

    Dubbox简介 Dubbox 是一个分布式服务框架,其前身是阿里巴巴开源项目Dubbo ,被国内电商及互联网项目中使用,后期阿里巴巴停止了该项目的维护,当当网便在Dubbo基础上进行优化,并继续维护 ...

  5. dubbo+zookeeper+spring+springMVC+mybatis的使用

    读前声明:由于本人水平有限,有错误或者描述不恰当的地方请指出来,勿喷!第一次写博客. 源码下载链接:http://files.cnblogs.com/files/la-tiao-jun-blog/du ...

  6. Dubbo+Zookeeper+Spring整合应用篇-Dubbo基于Zookeeper实现分布式服务(转)

    Dubbo与Zookeeper.Spring整合使用 Dubbo采用全Spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载Dubbo的配置即可,Dubbo基于Spri ...

  7. 分布式服务治理框架Dubbo的前世今生及应用实战

    Dubbo的出现背景 Dubbo从开源到现在,已经出现了接近10年时间,在国内各大企业被广泛应用. 它到底有什么魔力值得大家去追捧呢?本篇文章给大家做一个详细的说明. 大规模服务化对于服务治理的要求 ...

  8. dubbo+zookeeper+spring实例

    互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这种情况下诞生的.现在核心业务抽取出来,作为独立的服务,使 ...

  9. 【实战】ZooKeeper 实战

    1. 前言 这篇文章简单给演示一下 ZooKeeper 常见命令的使用以及 ZooKeeper Java客户端 Curator 的基本使用.介绍到的内容都是最基本的操作,能满足日常工作的基本需要. 如 ...

随机推荐

  1. Spring MVC 返回类型为字符串时, 返回中文变成"?"处理

    Spring controller 如下 @Controller public class SimpleController { @ResponseBody @RequestMapping(value ...

  2. nfd指令的详细说明

    在eterm上执行NFD:SHAPEK/CA*OW指令,返回如下: LN CXR OW RT FBC/TC RBD MIN/MAX TRVDATE R 01 CA 450.00 U U 00D/00D ...

  3. 奇怪吸引子---Arneodo

    奇怪吸引子是混沌学的重要组成理论,用于演化过程的终极状态,具有如下特征:终极性.稳定性.吸引性.吸引子是一个数学概念,描写运动的收敛类型.它是指这样的一个集合,当时间趋于无穷大时,在任何一个有界集上出 ...

  4. scrapy框架系列 (3) Item Pipline

    item pipeline 当Item在Spider中被收集之后,它将会被传递到Item Pipeline,这些Item Pipeline组件按定义的顺序处理Item. 每个Item Pipeline ...

  5. CoffeeScript?TypeScript?还是JavaScript

    请注意本文只是我的偏见,我努力地理解借助CoffeeScript或TypeScript之类的编译器写JavaScript代码的理由.静态编译.强类型语言和框架,我有着这些流行的.丰富的背景.我的上一份 ...

  6. 【流处理】Kafka Stream-Spark Streaming-Storm流式计算框架比较选型

    Kafka Stream-Spark Streaming-Storm流式计算框架比较选型 elasticsearch-head Elasticsearch-sql client NLPchina/el ...

  7. OAuth2 Demo PHP

    OAuth2 Demo PHP 此应用程序的目的是演示OAuth2.0客户端和服务器之间的工作流.如果这是你第一次来这里,试图尝试的现场演示让OAuth2.0流更好的感觉. experimenting ...

  8. 【Python】使用geopy由地址找经纬度等信息

    代码: from geopy.geocoders import Nominatim geolocator = Nominatim() location = geolocator.geocode(&qu ...

  9. Array、ArrayList、List、IEnumerable、for、foreach应用

    一.Array 类 (System) 声明数组(本身也是一种变量,要先声明再使用) 1.声明数组的语法,数组大小由长度绝定: 数据类型 [] 数组名: 如: string[] student; //字 ...

  10. Invalid volume failure config value: 1

    原因: hdfs-site.xml中的配置为: <property> <name>dfs.datanode.failed.volumes.tolerated</name& ...