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

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

一、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地址

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

1.3.2 启动、停止Zookeeper

进入Zookeeper的bin目录,启动服务命令 ./zkServer.sh start
停止服务命令 ./zkServer.sh stop
查看服务状态: ./zkServer.sh status
客户端连接
./zkCli.sh

二、使用Dubbo

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

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

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

  • 将服务提供者注册到Zookeeper注册中心
<?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: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">
<!-- 当前应用名称,用于注册中心计算应用间依赖关系,注意:消费者和提供者应用名不要一样 -->
<dubbo:application name="dubbodemo_provider" />
<!-- 连接服务注册中心zookeeper ip为zookeeper所在服务器的ip地址-->
<dubbo:registry address="zookeeper://192.168.134.129:2181"/>
<!-- 指定通信规则 协议和port -->
<dubbo:protocol name="dubbo" port="20881"></dubbo:protocol>
<!-- 扫描指定包,暴露服务 -->
服务实现类上使用的Service注解是Dubbo提供的,用于对外发布服务
<!-- 扫描指定包,加入@Service注解的类会被发布为服务 -->
<dubbo:annotation package="com.mk.service.impl" />
// 暴露服务
<dubbo:service interface="Service的全限定类型" ref="服务实现的id名"></dubbo:service>
//服务实现
<bean id="xxx" class=""全限定类名></bean>
</beans>

2.3 服务提供者Service

package com.mk.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.mk.service.HelloService;
@Service
public class HelloServiceImpl implements HelloService {
public String sayHello(String name) {
return "hello " + name;
}
}

2.4 消费者Consumer

<?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: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">
<!-- 当前应用名称,用于注册中心计算应用间依赖关系,注意:消费者和提供者应用名不要一样 -->
<dubbo:application name="dubbodemo-consumer" />
<!-- 连接服务注册中心zookeeper ip为zookeeper所在服务器的ip地址-->
<dubbo:registry address="zookeeper://192.168.134.129:2181"/>
<!-- 扫描的方式暴露接口 -->
<dubbo:annotation package="com.mk.controller" />
//声明需要调用的远程服务接口,生成远程服务代理
<dubbo:reference interface="服务提供者的全限定接口类名" id="xxx"></dubbo:reference>
</beans>

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

import com.alibaba.dubbo.config.annotation.Reference;
import com.mk.service.HelloService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/demo")
public class HelloController {
@Reference
private HelloService helloService;
@RequestMapping("/hello")
@ResponseBody
public String getName(String name){
//远程调用
String result = helloService.sayHello(name);
System.out.println(result);
return result;
}
}

相关文章