Dubbo+Zookeeper创建服务提供者与消费者

x33g5p2x  于2021-10-07 转载在 Zookeeper  
字(5.0k)|赞(0)|评价(0)|浏览(678)

一、Apache Dubbo

1.1 Dubbo简介

  • Apache Dubbo是一款高性能的Java RPC框架。其前身是阿里巴巴公司开源的、轻量级的开源Java RPC
    框架,可以和Spring框架无缝集成,2018年阿里巴巴把这个框架捐献给了apache基金会

1.2 RPC是什么?

  • RPC全称为remote procedure call,即远程过程调用。比如两台服务器A和B,A服务器上部署一个应
    用,B服务器上部署一个应用,A服务器上的应用想调用B服务器上的应用提供的方法,由于两个应用不
    在一个内存空间,不能直接调用,所以需要通过网络来表达调用的语义和传达调用的数据。
    需要注意的是RPC并不是一个具体的技术,而是指整个网络远程调用过程。
    RPC是一个泛化的概念,严格来说一切远程过程调用手段都属于RPC范畴。各种开发语言都有自己的
    RPC框架。Java中的RPC框架比较多,广泛使用的有RMI、Hessian、Dubbo等。
  • Dubbo提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发 现。

1.3 Zookeeper

  • Zookeeper 是 Apache Hadoop 的子项目,是一个树型的目录服务,支持变更推送,适合作为 Dubbo
    服务的注册中心,工业强度较高,可用于生产环境,并推荐使用 。

1.3.1 安装Zookeeper

下载Zookeeper地址

  • 安装步骤
  1. 第一步:安装 jdk(略) 第二步:把 zookeeper 的压缩包(zookeeper-3.4.6.tar.gz)上传到 linux
  2. 第三步:解压缩压缩包 tar -zxvf zookeeper-3.4.6.tar.gz -C /usr 第四步:进入zookeeper-3.4.6
  3. 录,创建data目录 mkdir data 第五步:进入conf目录 ,把zoo_sample.cfg 改名为zoo.cfg cd conf
  4. mv zoo_sample.cfg zoo.cfg 第六步:打开zoo.cfg文件, 修改data属性:dataDir=/usr/zookeeper3.4.6/data

1.3.2 启动、停止Zookeeper

  1. 进入Zookeeperbin目录,启动服务命令 ./zkServer.sh start
  2. 停止服务命令 ./zkServer.sh stop
  3. 查看服务状态: ./zkServer.sh status
  4. 客户端连接
  5. ./zkCli.sh

二、使用Dubbo

2.1 Dubbo使用Zookeeper的版本(注意版本)

  1. 1. dubbo2.6以前的版本需要引入 zkclient 操作 zookeeper
  2. 2. dubbo2.6及往后的版本需要引入 curator 操作 zookeeper

2.2 在src/main/resources下创建provider.xml

  • 将服务提供者注册到Zookeeper注册中心
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  3. <!-- 当前应用名称,用于注册中心计算应用间依赖关系,注意:消费者和提供者应用名不要一样 -->
  4. <dubbo:application name="dubbodemo_provider" />
  5. <!-- 连接服务注册中心zookeeper ip为zookeeper所在服务器的ip地址-->
  6. <dubbo:registry address="zookeeper://192.168.134.129:2181"/>
  7. <!-- 指定通信规则 协议和port -->
  8. <dubbo:protocol name="dubbo" port="20881"></dubbo:protocol>
  9. <!-- 扫描指定包,暴露服务 -->
  10. 服务实现类上使用的Service注解是Dubbo提供的,用于对外发布服务
  11. <!-- 扫描指定包,加入@Service注解的类会被发布为服务 -->
  12. <dubbo:annotation package="com.mk.service.impl" />
  13. // 暴露服务
  14. <dubbo:service interface="Service的全限定类型" ref="服务实现的id名"></dubbo:service>
  15. //服务实现
  16. <bean id="xxx" class=""全限定类名></bean>
  17. </beans>

2.3 服务提供者Service

  1. package com.mk.service.impl;
  2. import com.alibaba.dubbo.config.annotation.Service;
  3. import com.mk.service.HelloService;
  4. @Service
  5. public class HelloServiceImpl implements HelloService {
  6. public String sayHello(String name) {
  7. return "hello " + name;
  8. }
  9. }

2.4 消费者Consumer

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  3. <!-- 当前应用名称,用于注册中心计算应用间依赖关系,注意:消费者和提供者应用名不要一样 -->
  4. <dubbo:application name="dubbodemo-consumer" />
  5. <!-- 连接服务注册中心zookeeper ip为zookeeper所在服务器的ip地址-->
  6. <dubbo:registry address="zookeeper://192.168.134.129:2181"/>
  7. <!-- 扫描的方式暴露接口 -->
  8. <dubbo:annotation package="com.mk.controller" />
  9. //声明需要调用的远程服务接口,生成远程服务代理
  10. <dubbo:reference interface="服务提供者的全限定接口类名" id="xxx"></dubbo:reference>
  11. </beans>

Controller中注入HelloService使用的是Dubbo提供的@Reference注解
将服务提供者工程中的HelloService接口复制到当前工程

  1. import com.alibaba.dubbo.config.annotation.Reference;
  2. import com.mk.service.HelloService;
  3. import org.springframework.stereotype.Controller;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.ResponseBody;
  6. @Controller
  7. @RequestMapping("/demo")
  8. public class HelloController {
  9. @Reference
  10. private HelloService helloService;
  11. @RequestMapping("/hello")
  12. @ResponseBody
  13. public String getName(String name){
  14. //远程调用
  15. String result = helloService.sayHello(name);
  16. System.out.println(result);
  17. return result;
  18. }
  19. }

相关文章