Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Spring+Dubbo+Zookeeper简单框架与使用

Spring+Dubbo+Zookeeper简单框架与使用

作者头像
田维常
发布于 2019-07-16 14:33:45
发布于 2019-07-16 14:33:45
69500
代码可运行
举报
运行总次数:0
代码可运行

一、实例搭建

1、搭建框架前先下载Zookeeper(http://mirrors.cnnic.cn/apache/zookeeper/zookeeper-3.3.6/zookeeper-3.3.6.tar.gz)

2、解压Zookeeper到指定文件目录,在bin目录下双击zkServer.cmd(Windows),启动Zookeeper服务,正常应该是如下图所示,错误则看第三步

3、若启动失败,则在conf目录下,新建zoo.cfg配置文件

配置如下,主要修改路径地址(参考:http://blog.csdn.net/morning99/article/details/40426133)

# 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 anacknowledgement syncLimit=5 # the directory where the snapshot isstored. //镜像数据位置 dataDir=F:\Work\Zookeeper\data #日志位置 dataLogDir=F:\Work\Zookeeper\logs # the port at which the clients willconnect 客户端连接的端口 clientPort=2181

参数详解:

1.tickTime:CS通信心跳数

Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每个 tickTime 时间就会发送一个心跳。tickTime以毫秒为单位。

2.initLimit:LF初始通信时限

集群中的follower服务器(F)与leader服务器(L)之间初始连接时能容忍的最多心跳数(tickTime的数量)。

3.syncLimit:LF同步通信时限 集群中的follower服务器与leader服务器之间请求和应答之间能容忍的最多心跳数(tickTime的数量)。

4.dataDir:数据文件目录 Zookeeper保存数据的目录,默认情况下,Zookeeper将写数据的日志文件也保存在这个目录里。

5.dataLogDir:日志文件目录 Zookeeper保存日志文件的目录。

6.clientPort:客户端连接端口 客户端连接 Zookeeper 服务器的端口,Zookeeper 会监听这个端口,接受客户端的访问请求。

7.服务器名称与地址:集群信息(服务器编号,服务器地址,LF通信端口,选举端口) 这个配置项的书写格式比较特殊,规则如下:

server.N=YYY:A:B eg: server.0=233.34.9.144:2008:6008 server.1=233.34.9.145:2008:6008

Zookeeper配置参数详解:http://blog.csdn.net/poechant/article/details/6650249

3、配置pom.xml(Provider与Consumer配置一致)

<dependencies> <!-- junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>servlet-api</artifactId> <version>6.0.45</version> </dependency> <!-- dubbo --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> </dependency> <!-- spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> <version>2.5.6</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.3.3.RELEASE</version> </dependency> <!-- zookeeper --> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.5.2-alpha</version> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> </dependencies>

Provider方:

结构如下图(和Consumer方类似)

4、具体类的编写(和Consumer方一致)

在model下新建一个User类,但是由于使用Dubbo,所以一定要实现序列化Serializable类

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
public class User implements Serializable{
	private static final long serialVersionUID = -1009733312893309388L;
	
	private String name;
	private String sex;
	private Integer age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
}

然后在service下新建一个DemoService接口(和Consumer方一致),impl下新建DemoServiceImpl实现接口

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
public interface DemoService {
	String sayHello(String name);  
    public List<User> getUsers();
}

public class DemoServiceImpl implements DemoService {

	public String sayHello(String name) {
		return "Hello " + name;
	}

	public List<User> getUsers() {
		List<User> list = new ArrayList<User>();  
        User u1 = new User();  
        u1.setName("jack");  
        u1.setAge(20);  
        u1.setSex("女");  
          
        User u2 = new User();  
        u2.setName("tom");  
        u2.setAge(21);  
        u2.setSex("男");  
          
        User u3 = new User();  
        u3.setName("rose");  
        u3.setAge(19);  
        u3.setSex("男");  
          
        list.add(u1);  
        list.add(u2);  
        list.add(u3);  
        return list;
	}
}

然后provider下新建一个Provider类,实现在Zookeeper中注册

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
public class Provider {

	public static void main(String[] args) {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"application.xml"});  
        context.start();  
        try {
			System.in.read();// 为保证服务一直开着,利用输入流的阻塞来模拟   
		} catch (IOException e) {
			e.printStackTrace();
		} 
	}
}

5、application.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 "> <!-- 具体的实现bean --> <bean id="demoService" class="com.zd.dubbo.service.impl.DemoServiceImpl" /> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name="xixi_provider" /> <!-- 使用zookeeper注册中心暴露服务地址 --> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 声明需要暴露的服务接口 --> <dubbo:service interface="com.zd.dubbo.service.DemoService" ref="demoService" /> </beans>

Consumer方:

目录结构

我理解的是Provider方在Zookeeper注册,暴露服务地址以及DemoService接口,然后Consumer方就可以调用其暴露出来的接口,具体实现由Provider完成,Consumer方只需要拥有与Provider方一致的接口,调用接口方法就实现远程调用。

主要贴出与Provider不同的代码,其他与其类似或一致的就不贴了。

1、consumer下新建Consumer类

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
public class Consumer {

	public static void main(String[] args) {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(  
                new String[] { "application.xml" });  
        context.start();  
  
        DemoService demoService = (DemoService) context.getBean("demoService"); //  
        String hello = demoService.sayHello("tom"); //调用sayHello方法
        System.out.println(hello); 
        
        //获取用户列表
        List<User> list = demoService.getUsers();  
        if (list != null && list.size() > 0) {  
            for (int i = 0; i < list.size(); i++) {  
                System.out.println(list.get(i));  
            }  
        }  
        try {
			System.in.read();
		} catch (IOException e) {
			e.printStackTrace();
		}  
	}
}

2、application.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="hehe_consumer" /> <!-- 使用zookeeper注册中心暴露服务地址 --> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <!-- 生成远程服务代理,可以像使用本地bean一样使用demoService --> <dubbo:reference id="demoService" interface="com.zd.dubbo.service.DemoService" /> </beans>

然后先启动Provider再启动Consumer,结果如下图:

二、常见问题

1、Dubbo采用Spring配置方式,加入Schema即可,如下

但是可能报错:

Multiple annotations found at this line: - cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'dubbo:application'. - schema_reference.4: Failed to read schema document 'http://code.alibabatech.com/schema/dubbo/dubbo.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.

解决方案:

在maven下载的dubbo.jar(路径:C:\Users\Administrator\.m2\repository\com\alibaba\dubbo\2.5.3)解压文件中可以找到dubbo.xsd(搜索查找即可)

然后Window-->Preferences-->XML-->XML Catalog-->Add-->Catalog Entry

由于Uri Location的路径中不能包含 .,所以我将其重新拷贝到另一个地方了,一定要修改Key,配置如下:

然后右键项目,选择Validate!

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-05-30,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Java后端技术栈 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
阿里 RPC 框架 DUBBO 初体验
最近研究了一下阿里开源的分布式RPC框架dubbo,楼主写了一个 demo,体验了一下dubbo的功能。
haifeiWu
2018/09/11
9400
阿里 RPC 框架 DUBBO 初体验
Dubbo(五) Dubbo入门demo——helloworld
前言 前面我已经介绍了dubbo的一些基本工具和知识,让大家简单的了解了下RPC框架和Dubbo。接下来就是重点了,Dubbo的helloworld项目。 一、搭建项目 首先我们新建三个maven项目
生活创客
2018/01/30
2.4K0
Dubbo(五) Dubbo入门demo——helloworld
Dubbo+zookeeper实现分布式服务框架
Dubbo也是一套微服务框架,他与SpringCloud的区别就是,他支持多种协议,而SpringCloud只支持Http协议。如果没有分布式,那么他是不存在的。
慕容千语
2019/06/12
9660
Dubbo的两种常规启动方式
然后打成jar给dubbo-user-provider和dubbo-user-consumer使用
田维常
2019/12/25
1.4K0
Dubbo的两种常规启动方式
Dubbo与Zookeeper、SpringMVC整合和使用(入门级)
后续会补充完善SpringMVC部分 项目码云GIT地址:https://gitee.com/xshuai/dubbo/ 开发工具 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-e
小帅丶
2018/02/08
1.9K0
Dubbo与Zookeeper、SpringMVC整合和使用(入门级)
Dubbo02【搭建provider和consumer】
  本文来给大家介绍下基于Spring配置的方式来搭建dubbo中的服务提供端和消费端
用户4919348
2019/04/02
6550
Dubbo02【搭建provider和consumer】
Dubbo(Dubbo与Zookeeper、SpringMVC整合)
Zookeeper作为Dubbo服务的注册中心,Dubbo原先基于数据库的注册中心,没采用Zookeeper,Zookeeper一个分布式的服务框架,是树型的目录服务的数据存储,能做到集群管理数据 ,这里能很好的作为Dubbo服务的注册中心,Dubbo能与Zookeeper做到集群部署,当提供者出现断电等异常停机时,Zookeeper注册中心能自动删除提供者信息,当提供者重启时,能自动恢复注册数据,以及订阅请求。我们先在linux上安装Zookeeper,我们安装最简单的单点,集群比较麻烦。
用户1257215
2018/07/27
3.1K0
Dubbo(Dubbo与Zookeeper、SpringMVC整合)
Dubbo源码分析:小白入门篇
主要也是现在Spring CLoud Alibaba实在是太火了,很多小伙伴都想好好搞搞。
田维常
2021/11/10
2520
Dubbo源码分析:小白入门篇
Spring Boot 整合Dubbo开发实战
Dubbo是Alibaba开源的分布式服务框架,它最大的特点是按照分层的方式来架构,使用这种方式可以使各个层之间解耦合(或者最大限度地松耦合)。从服务模型的角度来看,Dubbo采用的是一种非常简单的模型,要么是提供方提供服务,要么是消费方消费服务,所以基于这一点可以抽象出服务提供方(Provider)和服务消费方(Consumer)两个角色。关于注册中心、协议支持、服务监控等内容,详见后面描述。
架构探险之道
2019/07/25
4990
dubbo框架搭建
Dubbo offers three key functionalities, which include interface based remote call, fault tolerance & load balancing, and automatic service registration & discovery.
week
2018/08/24
7590
dubbo框架搭建
Dubbo分布式服务框架入门(附工程)
要想了解Dubbo是什么,我们不防先了解它有什么用。 使用场景:比如我想开发一个网上商城项目,这个网上商城呢,比较复杂,分为pc端web管理后台,微信端销售公众号,那么我们分成四个项目,pc端网站,微信端网站,还有一个后台服务项目,接口服务项目。
全栈程序员站长
2022/06/29
2420
Dubbo分布式服务框架入门(附工程)
一个简单的案例带你入门Dubbo分布式框架
相信有很多小伙伴都知道,dubbo是一个分布式、高性能、透明化的RPC服务框架,提供服务自动注册、自动发现等高效服务治理方案,dubbo的中文文档也是非常全的,中文文档可以参考这里dubbo.io。由于官网的介绍比较简洁,我这里打算通过Maven多模块工程再给小伙伴们演示一下用法。 ---- 环境:IntelliJ IDEA2017.1 ---- 关于如何在IntelliJ IDEA中创建Maven多模块项目,小伙伴们可以参考之前的博客 IntelliJ IDEA中创建Web聚合项目(Maven多模块项目)
江南一点雨
2018/04/02
6540
一个简单的案例带你入门Dubbo分布式框架
dubbo起步
之前在项目中使用过dubbo,但很久没有再用,以致都忘了它的用法,今天看到当当网开源的一个项目dubbox, 觉得挺实用的。这里先将dubbo的概念及用法记录下来以备忘。 快速启动 如果是在一个进程内不同的组件调用,一般在spring里是如下配置的: <bean id=“xxxService” class=“com.xxx.XxxServiceImpl” /> <!-- xxxAction注入xxxService --> <bean id=“xxxAction” class=“com.xxx.XxxAct
jeremyxu
2018/05/10
9510
Dubbo与Zookeeper,SpringMVC整合和使用
互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这种情况下诞生的。将核心业务抽取出来,作为独立的服务,使前端应用能更快速和稳定的响应。
字母哥博客
2020/09/23
4470
Dubbo与Zookeeper,SpringMVC整合和使用
搭建简单的Dubbo生产者与消费者
Dubbo是一款高性能、轻量级的开源Java RPC框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现。
Melody132
2020/03/10
1K0
dubbo系列(一)——dubbo简介和dubbo+spring+zookeeper配置整合
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
逝兮诚
2019/10/30
6330
dubbo系列(一)——dubbo简介和dubbo+spring+zookeeper配置整合
手把手教你dubbo怎么用?
Dubbo采用全Spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载Dubbo的配置即可,Dubbo基于Spring的Schema扩展进行加载。如果不想使用Spring配置,而希望通过API的方式进行调用(不推荐) 下面我们就来看看spring配置方式的写法: 服务提供者: 1. 下载zookeeper注册中心,下载地址:http://www.apache.org/dyn/closer.cgi/zookeeper/  下载后解压即可,进入D:\apach-zookeeper
MonroeCode
2018/02/09
6430
手把手教你dubbo怎么用?
『互联网架构』软件架构-掌握dubbo常规应用(上)(40)
1.Provider(提供者)绑定指定端口并启动服务(20880)。2.提供者连接注册中心,并发本机IP、端口、应用信息和提供服务信息发送至注册中心存储。3.Consumer(消费者),连接注册中心 ,并发送应用信息、所求服务信息至注册中心。4.注册中心根据消费者所求服务信息匹配对应的提供者列表发送至Consumer 应用缓存。配置文件里面的interface来匹配。5.Consumer 在发起远程调用时基于缓存的消费者列表择其一发起调用。缓存,是虚线,性能提升,注册中心挂了不影响。6.Provider 状态变更会实时通知注册中心、在由注册中心实时推送至Consumer。7.一定要记住,真正的调用跟注册中心没关系,而是cosumber直接调用的provider。
IT架构圈
2019/05/10
4620
『互联网架构』软件架构-掌握dubbo常规应用(上)(40)
dubbo 实现简易分布式服务
用户7630333
2023/12/07
1680
dubbo 实现简易分布式服务
Spring-boot:5分钟整合Dubbo构建分布式服务
概述:   Dubbo是Alibaba开源的分布式服务框架,它最大的特点是按照分层的方式来架构,使用这种方式可以使各个层之间解耦合(或者最大限度地松耦合)。从服务模型的角度来看,Dubbo采用的是一种
九灵
2018/03/09
1.8K0
Spring-boot:5分钟整合Dubbo构建分布式服务
相关推荐
阿里 RPC 框架 DUBBO 初体验
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验