[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与Zookeeper.Spring整合使用 Dubbo采用全Spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载Dubbo的配置即可,Dubbo基于Spri ...
- [dubbo实战] dubbo+zookeeper伪集群搭建
zookeeper作为注册中心,服务器和客户端都要访问,如果有大量的并发,肯定会有等待.所以可以通过zookeeper集群解决. 一.为什么需要zookeeper呢? 大部分分布式应用需要一个主控.协 ...
- [dubbo实战] dubbo+zookeeper伪集群搭建 (转)
zookeeper作为注册中心,服务器和客户端都要访问,如果有大量的并发,肯定会有等待.所以可以通过zookeeper集群解决. 一.为什么需要zookeeper呢? 大部分分布式应用需要一 个主控. ...
- Dubbo教程:入门到实战
Dubbox简介 Dubbox 是一个分布式服务框架,其前身是阿里巴巴开源项目Dubbo ,被国内电商及互联网项目中使用,后期阿里巴巴停止了该项目的维护,当当网便在Dubbo基础上进行优化,并继续维护 ...
- dubbo+zookeeper+spring+springMVC+mybatis的使用
读前声明:由于本人水平有限,有错误或者描述不恰当的地方请指出来,勿喷!第一次写博客. 源码下载链接:http://files.cnblogs.com/files/la-tiao-jun-blog/du ...
- Dubbo+Zookeeper+Spring整合应用篇-Dubbo基于Zookeeper实现分布式服务(转)
Dubbo与Zookeeper.Spring整合使用 Dubbo采用全Spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载Dubbo的配置即可,Dubbo基于Spri ...
- 分布式服务治理框架Dubbo的前世今生及应用实战
Dubbo的出现背景 Dubbo从开源到现在,已经出现了接近10年时间,在国内各大企业被广泛应用. 它到底有什么魔力值得大家去追捧呢?本篇文章给大家做一个详细的说明. 大规模服务化对于服务治理的要求 ...
- dubbo+zookeeper+spring实例
互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这种情况下诞生的.现在核心业务抽取出来,作为独立的服务,使 ...
- 【实战】ZooKeeper 实战
1. 前言 这篇文章简单给演示一下 ZooKeeper 常见命令的使用以及 ZooKeeper Java客户端 Curator 的基本使用.介绍到的内容都是最基本的操作,能满足日常工作的基本需要. 如 ...
随机推荐
- java含有静态代码块新建的时候报错java.lang.ExceptionInInitializerError
问题描述 最近在写一些单元测试用例,为了避免连接外界服务,所有选择mock了数据库Dao层,计划将数据库所需要的数据存在List中,在类加载的时候初始化List并且填充数据.代码如下: public ...
- 大型Web 网站 Asp.net Session过期你怎么办
在 WEB 系统中, 我们一般会用session来保存一些简单但是却很重要的信息.比如Asp.net中经常会用Session来保存用户登录信息,比如UserID.为了解决 WEB场大家采用了把sess ...
- 探索Javascript异步编程
异步编程带来的问题在客户端Javascript中并不明显,但随着服务器端Javascript越来越广的被使用,大量的异步IO操作使得该问题变得明显.许多不同的方法都可以解决这个问题,本文讨论了一些方法 ...
- Android -- ADT变化&aar&Lint
Switch Case switch case 常用的使用方法: switch(v.getId()){ case R.id.btn1: doClick1(); break; } 在ADT中的改变 在正 ...
- Kafka:ZK+Kafka+Spark Streaming集群环境搭建(十三)kafka+spark streaming打包好的程序提交时提示虚拟内存不足(Container is running beyond virtual memory limits. Current usage: 119.5 MB of 1 GB physical memory used; 2.2 GB of 2.1 G)
异常问题:Container is running beyond virtual memory limits. Current usage: 119.5 MB of 1 GB physical mem ...
- GIF添加3D加速
由于浏览器内核对Gif格式的图片会产生卡的情况,所以我们需要告诉浏览器,开启一下加速,方法很简单,就是利用css3的特性,强制告诉浏览器,这是个元素,需要3D转换,请务必开启加速效果 方法1 给gif ...
- KafkaOffsetMonitor 安装
KafkaOffsetMonitor 安装 1,下载KafkaOffsetMonitor-assembly-0.2.0.jar 2,启动 步骤1:启动ZK(DN1-DN3节点) zkServer. ...
- C# .Net计算函数执行的时间
C#计算函数执行的时间 protected void StopwatchTest() { System.Diagnostics.Stopwatch stopwatch = new System.Dia ...
- winf
真的,先亮注册码!!(直接复制即可) 注册码: <第1组> 用户名:大眼仔~旭(Anan) 注册码:01000000007002140V <第2组> 用户名:大眼仔~旭(Ana ...
- Qt通过ODBC连接SQL Server2008实践总结
Qt连接数据库的方式很多,这里说明一种最常用也是最实用的方式,因为这种方式在Windows上开发程序使用起来非常方便,并且也是远程连接数据库所需要用到的方式. 前提工作: 在Win7下安装了SQL S ...