答:ZooKeeper 是一个开源的分布式应用程序协调服务,是一个典型的分布式数据一致性解决方案。设计目的是将那些复杂且容易出错的分布式一致性服务封装起来,构成一个高效可靠的系统,并以一系列简单易用的原子操作提供给用户使用。
答:ZooKeeper 主要提供以下功能:
答:ZooKeeper 通常有三种搭建模式:
答: ZooKeeper 特性如下:
A:所有的节点都具有稳定的存储能力 B:ZooKeeper 任意节点之间都能够进行通信(消息发送 & 接收) C:为了提高性能,ZooKeeper 允许同一份数据存在一部分节点写成功,另一部分节点写失败 D:ZooKeeper 集群运行期间,只要半数以上节点存活,ZooKeeper 就能正常服务 答:C 题目解析:ZooKeeper 不允许同一份数据存在一部分节点写成功,另一部分节点写失败的情况,这不符合 ZooKeeper“一致性”的原则。
答:ZooKeeper 实现分布式锁的步骤如下:
答:ZooKeeper 实现分布式事务,类似于两阶段提交,总共分为以下 4 步:
答:在分布式环境中,有些业务逻辑只需要集群中的某一台机器进行执行,其他的机器可以共享这个结果,这样可以大大减少重复计算,提高性能,这就是主节点存在的意义。
答:Dubbo 是一款高性能、轻量级的开源 Java RPC 框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现。
答:Dubbo 特性如下:
答:Dubbo 核心组件如下:
答:Dubbo 负责均衡策略如下:
负载均衡配置如下:
服务端服务级别
\<dubbo:service interface="xxx" loadbalance="roundrobin" /\>
客户端服务级别
\<dubbo:reference interface="xxx" loadbalance="roundrobin" /\>
服务端方法级别
\<dubbo:service interface="xxx"\>
\<dubbo:method name="xxx" loadbalance="roundrobin"/\>
\</dubbo:service\>
客户端方法级别
\<dubbo:reference interface="xxx"\>
\<dubbo:method name="xxx" loadbalance="roundrobin"/\>
\</dubbo:reference\>
A:dubbo://
B:rmi://
C:redis://
D:restful://
答:D
题目解析:restful 是一种编程规范,并不是一种传输协议,也不被 Dubbo 支持。
答:推荐使用 ZooKeeper 作为注册中心,还有 Nacos、Redis、Simple 注册中心(普通的 Dubbo 服务)。
答:Dubbo 支持同一服务向多注册中心同时注册,或者不同服务分别注册到不同的注册中心上去,甚至可以同时引用注册在不同注册中心上的同名服务。
多注册中心注册:
\<?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://dubbo.apache.org/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"\>
\<dubbo:application name="world" /\>
\<!-- 多注册中心配置 --\>
\<dubbo:registry id="hangzhouRegistry" address="10.20.141.150:9090" /\>
\<dubbo:registry id="qingdaoRegistry" address="10.20.141.151:9010" default="false" /\>
\<!-- 向多个注册中心注册 --\>
\<dubbo:service interface="com.alibaba.hello.api.HelloService" version="1.0.0" ref="helloService" registry="hangzhouRegistry,qingdaoRegistry" /\>
\</beans\>
答:Dubbo 支持的主要连接方式有:组播、直连和 ZooKeeper 等注册中心。
① 组播方式,不需要启动任何中心节点,只要广播地址一样,就可以互相发现。
组播受网络结构限制,只适合小规模应用或开发阶段使用。组播地址段:224.0.0.0 ~ 239.255.255.255
配置
\<dubbo:registry address="multicast://224.5.6.7:1234" /\>
或
\<dubbo:registry protocol="multicast" address="224.5.6.7:1234" /\>
为了减少广播量,Dubbo 缺省使用单播发送提供者地址信息给消费者,如果一个机器上同时启了多个消费者进程,消费者需声明 unicast=false,否则只会有一个消费者能收到消息;当服务者和消费者运行在同一台机器上,消费者同样需要声明 unicast=false,否则消费者无法收到消息,导致 No provider available for the service 异常:
\<dubbo:registry address="multicast://224.5.6.7:1234?unicast=false" /\>
或
\<dubbo:registry protocol="multicast" address="224.5.6.7:1234"\>
\<dubbo:parameter key="unicast" value="false" /\>
\</dubbo:registry\>
② 直连方式,注册中心本身就是一个普通的 Dubbo 服务,可以减少第三方依赖,使整体通讯方式一致。
\<dubbo:registry protocol="zookeeper" address="N/A" file="./.dubbo-platform"/\>
将 Simple 注册中心暴露成 Dubbo 服务:
\<?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://dubbo.apache.org/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"\>
\<!-- 当前应用信息配置 --\>
\<dubbo:application name="simple-registry" /\>
\<!-- 暴露服务协议配置 --\>
\<dubbo:protocol port="9090" /\>
\<!-- 暴露服务配置 --\>
\<dubbo:service interface="org.apache.dubbo.registry.RegistryService" ref="registryService" registry="N/A" ondisconnect="disconnect" callbacks="1000"\>
\<dubbo:method name="subscribe"\>\<dubbo:argument index="1" callback="true" /\>\</dubbo:method\>
\<dubbo:method name="unsubscribe"\>\<dubbo:argument index="1" callback="false" /\>\</dubbo:method\>
\</dubbo:service\>
\<!-- 简单注册中心实现,可自行扩展实现集群和状态同步 --\>
\<bean id="registryService" class="org.apache.dubbo.registry.simple.SimpleRegistryService" /\>
\</beans\>
引用 Simple Registry 服务:
\<dubbo:registry address="127.0.0.1:9090" /\>
或者:
<dubbo:service interface="org.apache.dubbo.registry.RegistryService" group="simple" version="1.0.0" ... >
或者:
<dubbo:registry address="127.0.0.1:9090" group="simple" version="1.0.0" />
适用性说明:此 SimpleRegistryService 只是简单实现,不支持集群,可作为自定义注册中心的参考,但不适合直接用于生产环境。
③ ZooKeeper 注册中心,Zookeeper 是 Apacahe Hadoop 的子项目,是一个树型的目录服务,支持变更推送,适合作为 Dubbo 服务的注册中心,工业强度较高,可用于生产环境,并推荐使用。
服务提供者启动时:向 /dubbo/com.foo.BarService/providers 目录下写入自己的 URL 地址
支持以下功能:
<dubbo:registry check="false" />
时,记录失败注册和订阅请求,后台定时重试<dubbo:registry username="admin" password="1234" />
设置 zookeeper 登录信息<dubbo:registry group="dubbo" />
设置 zookeeper 的根节点,不设置将使用无根树*
号通配符 <dubbo:reference group="*" version="*" />
,可订阅服务的所有分组和所有版本的提供者Zookeeper 使用
在 provider 和 consumer 中增加 zookeeper 客户端 jar 包依赖:
\<dependency\>
\<groupId\>org.apache.zookeeper\</groupId\>
\<artifactId\>zookeeper\</artifactId\>
\<version\>3.3.3\</version\>
\</dependency\>
Dubbo 支持 zkclient 和 curator 两种 Zookeeper 客户端实现:
注意:在 2.7.x 的版本中已经移除了 zkclient 的实现,如果要使用 zkclient 客户端,需要自行拓展。
使用 zkclient 客户端
从 2.2.0 版本开始缺省为 zkclient 实现,以提升 zookeeper 客户端的健状性。zkclient 是 Datameer 开源的一个 Zookeeper 客户端实现。
缺省配置:
\<dubbo:registry ... client="zkclient" /\>
或:
dubbo.registry.client=zkclient
或:
zookeeper://10.20.153.10:2181?client=zkclient
需依赖或直接下载:
\<dependency\>
\<groupId\>com.github.sgroschupf\</groupId\>
\<artifactId\>zkclient\</artifactId\>
\<version\>0.1\</version\>
\</dependency\>
使用 curator 客户端
从 2.3.0 版本开始支持可选 curator 实现。Curator 是 Netflix 开源的一个 Zookeeper 客户端实现。
如果需要改为 curator 实现,请配置:
\<dubbo:registry ... client="curator" /\>
或:
dubbo.registry.client=curator
或:
zookeeper://10.20.153.10:2181?client=curator
需依赖或直接下载:
\<dependency\>
\<groupId\>com.netflix.curator\</groupId\>
\<artifactId\>curator-framework\</artifactId\>
\<version\>1.1.10\</version\>
\</dependency\>
Zookeeper 单机配置:
\<dubbo:registry address="zookeeper://10.20.153.10:2181" /\>
或:
\<dubbo:registry protocol="zookeeper" address="10.20.153.10:2181" /\>
Zookeeper 集群配置:
\<dubbo:registry address="zookeeper://10.20.153.10:2181?backup=10.20.153.11:2181,10.20.153.12:2181" /\>
或:
\<dubbo:registry protocol="zookeeper" address="10.20.153.10:2181,10.20.153.11:2181,10.20.153.12:2181" /\>
同一 Zookeeper,分成多组注册中心:
<dubbo:registry id="chinaRegistry" protocol="zookeeper" address="10.20.153.10:2181" group="china" />
<dubbo:registry id="intlRegistry" protocol="zookeeper" address="10.20.153.10:2181" group="intl" />
答:在应用系统服务中,当依赖服务因访问压力过大而响应变慢或失败,上游服务为了保护系统整体的可用性,临时切断对下游服务的调用。这种牺牲局部,保全整体的措施就叫做熔断。
答:可以,Dubbo 提供了声明式缓存,用于加速热门数据的访问速度,以减少用户加缓存的工作量。
Dubbo 支持的缓存类型有:
配置如下:
\<dubbo:reference interface="com.foo.BarService" cache="lru" /\>
或
\<dubbo:reference interface="com.foo.BarService"\>
\<dubbo:method name="findBar" cache="lru" /\>
\</dubbo:reference\>
答:Dubbo 集群容错模式如下。
失败自动切换,当出现失败,重试其他服务器。通常用于读操作,但重试会带来更长延迟。可通过 retries="2"
来设置重试次数(不含第一次)。
重试次数配置如下:
\<dubbo:service retries="2" /\>
或
\<dubbo:reference retries="2" /\>
或
\<dubbo:reference\>
\<dubbo:method name="findFoo" retries="2" /\>
\</dubbo:reference\>
快速失败,只发起一次调用,失败立即报错。通常用于非幂等性的写操作,比如新增记录。
失败安全,出现异常时,直接忽略。通常用于写入审计日志等操作。
失败自动恢复,后台记录失败请求,定时重发。通常用于消息通知操作。
并行调用多个服务器,只要一个成功即返回。通常用于实时性要求较高的读操作,但需要浪费更多服务资源。可通过 forks="2" 来设置最大并行数。
广播调用所有提供者,逐个调用,任意一台报错则报错。通常用于通知所有提供者更新缓存或日志等本地资源信息。
欢迎关注我的公众号,回复关键字“Java” ,将会有大礼相送!!! 祝各位面试成功!!!
%97%E5%8F%B7%E4%BA%8C%E7%BB%B4%E7%A0%81.png)
原文:https://www.cnblogs.com/dailyprogrammer/p/12272760.html