您现在的位置是:首页 > 后台技术 > dubbodubbo

第五章 zookeeper

第十三双眼睛2021-06-19【dubbo】人已围观

简介对于服务提供方,它需要发布服务,而且由于应用系统的复杂性,服务的数量、类型也
不断膨胀;对于服务消费方,它最关心如何获取到它所需要的服务,而面对复杂的应用系统,
需要管理大量的服务调用

服务注册中心可以通过特定协议来完成服务对外的统一。Dubbo 提供的注册中心有如下几种类型可供选:
Multicast 注册中心:组播方式
Redis 注册中心:使用 Redis 作为注册中心
Simple 注册中心:就是一个 dubbo 服务。作为注册中心。提供查找服务的功能。
Zookeeper 注册中:使用 Zookeeper 作为注册中心
推荐使用 Zookeeper 注册中心。
注册中心是什么?
Zookeeper 是一个高性能的,分布式的,开放源码的分布式应用程序协调服务。简称 zk。Zookeeper 是翻译管理是动物管理员。可以理解为 windows 中的资源管理器或者注册表。他是一个树形结构。这种树形结构和标准文件系统相似。ZooKeeper 树中的每个节点被称为Znode。和文件系统的目录树一样,ZooKeeper 树中的每个节点可以拥有子节点。每个节点表示一个唯一服务资源。Zookeeper 运行需要 java 环境。
注册中心 Zookeeper 怎么玩
官网下载地址: 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
文件内容:
tickTime: 心跳的时间,单位毫秒. Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每个 tickTime 时间就会发送一个心跳。表明存活状态。
dataDir: 数据目录,可以是任意目录。存储 zookeeper 的快照文件、pid 文件,默认为/tmp/zookeeper,建议在 zookeeper 安装目录下创建 data 目录,
将 dataDir 配置改为/usr/local/zookeeper-3.4.10/data
clientPort: 客户端连接 zookeeper 的端口,即 zookeeper 对外的服务端口,默认为 2181
Linux 平台 Zookeeper 安装、配置 也时一样得道理
Zookeeper 的客户端图形工具
Zookeeper 图形界面的客户端:ZooInspector
点击 bin 目录下的 zkServer.cmd 进行启动
看到如下结果表示启动成功
使用zookeeper作为注册中心
将相关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>
dubbo-consumer.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="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
<?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

很赞哦! ()

文章评论

    共有条评论来说两句吧...

    用户名:

    验证码:

本站推荐

站点信息

  • 网站名称:JavaStudy
  • 建站时间:2019-1-14
  • 网站程序:帝国CMS7.5
  • 文章统计242篇文章
  • 标签管理标签云
  • 统计数据百度统计
  • 微信公众号:扫描二维码,关注我们