首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Dubbo+zookeeper实现分布式服务框架

Dubbo+zookeeper实现分布式服务框架

作者头像
慕容千语
发布2019-06-12 20:40:54
9170
发布2019-06-12 20:40:54
举报
什么是Dubbo??

Dubbo也是一套微服务框架,他与SpringCloud的区别就是,他支持多种协议,而SpringCloud只支持Http协议。如果没有分布式,那么他是不存在的。

Dubbo底层架构图 Dubbo底层

首先Provider生成服务将服务注册到zookeeper(具体实现下面有代码),然后zookeeper接收到过后底层会触发zookeeper监听事件(不懂请看前一节),然后告诉Consumer可以消费了,但是Provider关闭过后zookeeper不会删除节点,因为是存储的持久化节点,不是临时节点。然后会有一个专门的模块来监听服务的调用,统计模块调用次数和反馈信息。

Dubbo有哪些作用

①:Dubbo有服务治理的能力:透明化的远程方法调用,就像调用本地方法一样调用远程方法,只需简单 配置,没有任何API侵入。 ②:集群容错:软负载均衡及容错机制,可在内网替代F5等硬件负鞭均衡器,降低成本,减少单点。 ③:自动发现:服务自动注册与发现,不再需要写死服务提供方地址,注册中心基于接口名查询服务提供者的IP地址,并且能够平滑添加或删除服务提供者。 另外:Dubbo采用全Spring 配置方式,功明化接入应用,对应用没有任何API侵只需用Spring加载Dubbo的配置即可,Dubbo基FSpring的Schema扩入,展进行加载。

搭建模拟环境

首先我们搭建一个Dubbo服务类(该类主要是写接口供生产者和消费者引用并继承。主要原因是为了避免消费者和生产者都去写一次相同的接口类)

创建项目名称:dubbo-common

在per.lx.service包下新建UserService接口

public interface UserService {

String getUser(Integer id);

}

如果需要更多的服务的话,都在dubbo-common包下进行新建,然后在生产者中去实现它的内部方法并注册到zookeeper

下面创建服务提供者和消费者,都需要引入相同的依赖pom.xml:

<dependencies>

<dependency>

<groupId>com.alibaba</groupId>

<artifactId>dubbo</artifactId>

<version>2.5.6</version>

</dependency>

<!-- 添加zk客户端依赖 -->

<dependency>

<groupId>com.github.sgroschupf</groupId>

<artifactId>zkclient</artifactId>

<version>0.1</version>

</dependency>

<!-- 添加对dubbo-common的依赖,使用UserService -->

<dependency>

<groupId>per.lx</groupId>

<artifactId>dubbo-common</artifactId>

<version>0.0.1-SNAPSHOT</version>

</dependency>

</dependencies>

创建项目名称服务提供者:dubbo-producer

新建一个UserServiceImpl去实现dubbo-common中的UserService接口

public class UserServiceImpl implements UserService{

@Override

public String getUser(Integer id) {

System.out.println("进入到getUser服务实现内 id:"+id);

if(id==1){

return "Hello,LLL丶禾羊,id:"+id;

}

if(id==2){

return "Hello,OSC,id:"+id;

}

if(id==3){

return "Hello,开源中国!,id:"+id;

}

return null;

}

}

并且在resources下面新建配置文件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:application name="provider" />

<!-- 使用zookeeper注册中心暴露服务地址 -->

<dubbo:registry address="zookeeper://127.0.0.1:2181" />

<!-- 用dubbo协议在29014端口暴露服务 -->

<dubbo:protocol name="dubbo" port="29014" />

<!-- 声明需要暴露的服务接口 -->

<dubbo:service interface="per.lx.service.UserService" ref="userService" />

<!-- 具体的实现bean -->

<bean id="userService" class="per.lx.service.impl.UserServiceImpl" />

</beans>

最后新建一个启动类StartProducer

public class StartProducer {

public static void main(String[] args) throws IOException {

ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("provider.xml");

app.start();

System.out.println("服务发布成功...");

System.in.read();//让程序阻塞。

}

}

这里直接启动主函数,可以直接在控制台看到服务发布成功。之后打开zookeeper查看工具可以查看到服务信息(如下图,工具是ZooInspector.jar,开源的,百度可下载)。

写完生产者,消费者更加简单。

新建消费者项目dubbo-consumer

在resources中新建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="consumer" />

<!-- 使用multicast广播注册中心暴露发现服务地址 -->

<dubbo:registry protocol="zookeeper" address="zookeeper://127.0.0.1:2181" />

<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->

<dubbo:reference id="userService" interface="per.lx.service.UserService" />

</beans>

然后直接新建启动类StartConsumer

public class StartConsumer {

public static void main(String[] args) throws IOException{

ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("consumer.xml");

app.start();

UserService userService = (UserService) app.getBean("userService");

String name = userService.getUser(1);

System.out.println("消费端从生产者获取到 name:" + name);

System.in.read();//让程序阻塞。

}

}

启动之后可以发现从控制台打印输出图中内容,表示发布成功。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.10.31 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 什么是Dubbo??
  • Dubbo有哪些作用
  • 搭建模拟环境
相关产品与服务
微服务引擎 TSE
微服务引擎(Tencent Cloud Service Engine)提供开箱即用的云上全场景微服务解决方案。支持开源增强的云原生注册配置中心(Zookeeper、Nacos 和 Apollo),北极星网格(腾讯自研并开源的 PolarisMesh)、云原生 API 网关(Kong)以及微服务应用托管的弹性微服务平台。微服务引擎完全兼容开源版本的使用方式,在功能、可用性和可运维性等多个方面进行增强。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档