您现在的位置是:首页 > 后台技术 > dubbodubbo
第五章 zookeeper
第十三双眼睛2021-06-19【dubbo】人已围观
简介对于服务提供方,它需要发布服务,而且由于应用系统的复杂性,服务的数量、类型也
不断膨胀;对于服务消费方,它最关心如何获取到它所需要的服务,而面对复杂的应用系统,
需要管理大量的服务调用
服务注册中心可以通过特定协议来完成服务对外的统一。Dubbo 提供的注册中心有如下几种类型可供选:
Multicast 注册中心:组播方式
Redis 注册中心:使用 Redis 作为注册中心
Simple 注册中心:就是一个 dubbo 服务。作为注册中心。提供查找服务的功能。
Zookeeper 注册中:使用 Zookeeper 作为注册中心
推荐使用 Zookeeper 注册中心。
注册中心是什么?
Multicast 注册中心:组播方式
Redis 注册中心:使用 Redis 作为注册中心
Simple 注册中心:就是一个 dubbo 服务。作为注册中心。提供查找服务的功能。
Zookeeper 注册中:使用 Zookeeper 作为注册中心
推荐使用 Zookeeper 注册中心。
注册中心是什么?
Zookeeper 是一个高性能的,分布式的,开放源码的分布式应用程序协调服务。简称 zk。Zookeeper 是翻译管理是动物管理员。可以理解为 windows 中的资源管理器或者注册表。他是一个树形结构。这种树形结构和标准文件系统相似。ZooKeeper 树中的每个节点被称为Znode。和文件系统的目录树一样,ZooKeeper 树中的每个节点可以拥有子节点。每个节点表示一个唯一服务资源。Zookeeper 运行需要 java 环境。
注册中心 Zookeeper 怎么玩
官网下载地址: http://zookeeper.apache.org/
Windows 平台 Zookeeper 安装,配置
将zoo_sample.cfg 复制一份 改为 zoo.cfg
将相关jar包放入服务提供者和服务消费者两个项目中
zookeeper-3.4.10.jar zkclient-0.10.jar
之前的项目只需要将配置文件进行改动即可
dubbo-provider.xml
dubbo-consumer.xml
再次运行测试类
至此,使用注册中心的项目已经完成
官网下载地址: http://zookeeper.apache.org/
Windows 平台 Zookeeper 安装,配置
下载的文件 zookeeper-3.4.10.tar. 解压后到目录就可以了,例如 d:/servers/ zookeeper-3.4.10 修改 zookeeper-3.4.10/conf/ 目录下配置文件
将zoo_sample.cfg 复制一份 改为 zoo.cfg
文件内容:
使用zookeeper作为注册中心tickTime: 心跳的时间,单位毫秒. Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每个 tickTime 时间就会发送一个心跳。表明存活状态。
dataDir: 数据目录,可以是任意目录。存储 zookeeper 的快照文件、pid 文件,默认为/tmp/zookeeper,建议在 zookeeper 安装目录下创建 data 目录,
将 dataDir 配置改为/usr/local/zookeeper-3.4.10/data
将 dataDir 配置改为/usr/local/zookeeper-3.4.10/data
clientPort: 客户端连接 zookeeper 的端口,即 zookeeper 对外的服务端口,默认为 2181
Linux 平台 Zookeeper 安装、配置 也时一样得道理
Zookeeper 的客户端图形工具
Zookeeper 图形界面的客户端:ZooInspector
点击 bin 目录下的 zkServer.cmd 进行启动
看到如下结果表示启动成功
Linux 平台 Zookeeper 安装、配置 也时一样得道理
Zookeeper 的客户端图形工具
Zookeeper 图形界面的客户端:ZooInspector
点击 bin 目录下的 zkServer.cmd 进行启动
看到如下结果表示启动成功
将相关jar包放入服务提供者和服务消费者两个项目中
zookeeper-3.4.10.jar zkclient-0.10.jar
之前的项目只需要将配置文件进行改动即可
dubbo-provider.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框架使用 要唯一 --> <dubbo:application name="06zk-provider"/> <!-- 暴露dubbo的服务 interface:接口的全限定名 ref:接口实现类的id --> <dubbo:registry address="zookeeper://localhost:2181"/> <dubbo:service interface="com.xinchen.service.MovieService" ref="movieService" /> <!-- 注册接口的实现类 --> <bean id="movieService" class="com.xinchen.serviceimpl.MovieServiceImpl"></bean> <dubbo:service interface="com.xinchen.service.WeatherService" ref="weatherService" /> <!-- 注册接口的实现类 --> <bean id="weatherService" class="com.xinchen.serviceimpl.WeatherServiceImpl"></bean> </beans> |
<?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="06zk-consumer"/> <!-- 声明要使用的服务 id:表示代理对象 interface:接口 --> <dubbo:registry address="zookeeper://localhost:2181"/> <dubbo:reference id="movieService" interface="com.xinchen.service.MovieService" /> <dubbo:reference id="weatherService" interface="com.xinchen.service.WeatherService" /> <!-- 用注入的方式使用远程对象 --> <bean id="invokeService" class="com.test.InvokeService"> <property name="weatherService" ref="weatherService"></property> <property name="movieService" ref="movieService"></property> </bean> </beans> |
至此,使用注册中心的项目已经完成
注解的项目开发
服务提供者-注解
服务提供者-注解
服务提供者在 Service 实现类上加入注解@Service, 这个注解是来自 Dubbo 框架,不是spring 框架中的注解。 spring 配置文件声明注解的所在包
@Service : com.alibaba.dubbo.config.annotation.Service
创建服务提供者项目: 07zk-provider-anno
在 Service 实现类上加入@Service
修改 spring 配置,指定注解的包名
dubbo-provider.xml
创建服务提供者项目: 07zk-provider-anno
在 Service 实现类上加入@Service
修改 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"> <!-- 注册服务名称 给dubbo框架使用 要唯一 --> <dubbo:application name="provider"/> <!-- 暴露dubbo的服务 interface:接口的全限定名 ref:接口实现类的id --> <dubbo:registry address="zookeeper://localhost:2181"/> <!-- 如果有多个包,以逗号分隔 --> <dubbo:annotation package="com.xinchen.serviceimpl"/> </beans> |
服务消费者-注解
服务消费者使用@Reference ,表示要使用的远程服务。spring 配置文件声明注解包的位置。
创建服务消费者项目:07zk-consumer-anno
在远程服务对象的引用类型上加入@Reference
修改 spring。加入扫描@Reference 注解所在的包名,加入扫描@Component注解所在的包名。去掉<dubbo:reference>标签。
运行测试类
<?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:context="http://www.springframework.org/schema/context" 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://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 注册服务的名称 --> <dubbo:application name="consumer"/> <!-- 声明要使用的服务 id:表示代理对象 interface:接口 --> <dubbo:registry address="zookeeper://localhost:2181"/> <!-- 声明dubbo框架注解的位置 --> <dubbo:annotation package="com.test"/> <!-- 声明spring的注解扫描器 --> <context:component-scan base-package="com.test"/> </beans> |
Tags:dubbo
很赞哦! ()
下一篇:第六章 负载均衡(图文)
随机图文
-
第五章 zookeeper
对于服务提供方,它需要发布服务,而且由于应用系统的复杂性,服务的数量、类型也 不断膨胀;对于服务消费方,它最关心如何获取到它所需要的服务,而面对复杂的应用系统, 需要管理大量的服务调用 -
第五章 使用接口作为独立项目(图文)
公司或者企业有很多的服务,这些服务有不同的部门,不同的人员管理,维护。例如公 司做生活服务类业务的,提供类似 58 同城的业务。其中公共服务部门负责提供天气资讯和 影视资讯两个内容。A 小组两个人负责天气资讯;B 小组三个人负责影视资讯。现在需要在 公司的网站同时提供两种资讯。你作为网站开发人员需要使用 A 和 B 两个小组不同服务内 容。使用 A 组,B 组的两个服务提供者接口。 -
第二章 分多个项目(图文)
将上节的一个项目分成两个项目,一个项目提供服务,一个项目消费服务 -
第四章 使用dubbo的第一个项目(图文)
点对点的直连项目:消费者直接访问服务提供者,没有注册中心。消费者必须指定服务 提供者的访问地址(url)。