实践Dubbo

Dubbo is a high-performance, java based open source RPC framework.

前言

背景

Dubbo Architecture Roadmap

架构

Dubbo Architecture

配置

配置关系

Dubbo Config

  1. Application:应用,可以是消费者或提供者

  2. Provider:提供者,用于提供Service的默认配置,可以开放多个Protocol,可以发布到多个Registry,可以对接多个Monitor,归属某个Application

  3. Consumer:消费者,用于提供Reference的默认配置,可以从多个Registry查找Service,可以对接多个Monitor,归属某个Application

  4. Module:模块

  5. Registry:注册中心,提供服务注册和发现,比如zookeeper、nacos等

  6. Protocol:RPC协议,比如dubbo、http等

  7. Monitor:监控中心

  8. Service:对应于Interface的实现,可以包括多个method,可以开放多个Protocol,可以发布到多个Registry,可以对接多个Monitor,归属某个Provider

  9. Reference:对应于Service的引用,可以从多个Registry查找Service,可以对接多个Monitor,归属某个Consumer

  10. Method:隶属于Service,一个具体的功能方法

  11. Argument:参数

优先级

Overrides and Priorities

设计

启动时检查

集群容错

fault tolerance

负载均衡

线程模型

Dubbo Thread Model

线程池

RejectedExecutionHandler为继承AbortPolicy的AbortPolicyWithReport

fixed
属性 默认值 描述
threads 200 固定线程数量,corethreads=threads
queues 0 队列长度

alive固定为0

cached
属性 默认值 描述
corethreads 0 核心线程数
threads Integer.MAX_VALUE 最大线程数
queues 0 队列长度
alive 60*1000 冗余线程可空闲时间,单位毫秒
limited
属性 默认值 描述
corethreads 0 核心线程数
threads 200 最大线程数
queues 0 队列长度

alive固定为Long.MAX_VALUE,可以理解为线程不会被回收

eager

当corethreads用完时,优先创建线程,而不是放入队列

属性 默认值 描述
corethreads 0 核心线程数
threads Integer.MAX_VALUE 最大线程数
queues 0 队列长度
alive 60*1000 冗余线程可空闲时间,单位毫秒

服务端

New I/O server boss #1 ([id: 0x4086d8fb, /0:0:0:0:0:0:0:0:20880])

New I/O client worker #1-1

New I/O client worker #1-2

DubboServerHandler-192.168.1.193:20880-thread-1

DubboServerHandler-192.168.1.193:20880-thread-2

客户端

New I/O client worker #1-1

DubboClientHandler-172.19.177.79:20880-thread-1

DubboClientHandler-172.19.177.79:20880-thread-2

直连提供者

Dubbo Directly

路由规则

条件路由

支持以服务或应用为粒度配置路由规则

标签路由

以Provider应用为粒度配置路由规则

生态系统

脚手架

快速生成基于 Spring Boot 的 Dubbo 项目:

Dubbo Initializr

多语言

Dubbo支持以下语言:

JavaNode.jsPythonPHP

API

Dubbo支持通过多种API方式启动:

Spring XMLSpring AnnotationPlain JavaSpring Boot

Registry

Dubbo支持以下注册中心:

ZookeeperRedisSimpleMulticastEtcd3

Cluster

Dubbo支持以下容错机制:

Fail overFail safeFail fastFail backForkingBroadcast

Load balance

Dubbo支持以下负载均衡策略:

RandomLeast ActiveRound RobinConsistent hash

Protocol

Dubbo支持以下协议:

DubboRMIHessianHTTPWebServiceThriftNative ThriftMemcachedRedisRestJsonRPCXmlRPCJmsRpc

Transport

Dubbo支持以下网络传输扩展:

Netty3Netty4GrizzlyJettyMinaP2PZookeeper

Serialization

Dubbo支持以下序列化机制:

Hessian2JavaJSONFstKryoNative HessianAvro

QA

注册地址

需要配置zookeeper服务器的ip和主机名映射,Linux服务器下修改/etc/hosts

java.net.UnknownHostException Name or service not known

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
2022-06-20 16:17:41,139 ERROR [main] com.xxl.rpc.util.IpUtil [IpUtil.java : 108] service-2.novalocal: service-2.novalocal: Name or service not known
java.net.UnknownHostException: service-2.novalocal: service-2.novalocal: Name or service not known
        at java.net.InetAddress.getLocalHost(InetAddress.java:1505)
        at com.xxl.rpc.util.IpUtil.getLocalAddress0(IpUtil.java:102)
        at com.xxl.rpc.util.IpUtil.getLocalAddress(IpUtil.java:161)
        at com.xxl.rpc.util.IpUtil.getIp(IpUtil.java:172)
        at com.xxl.job.core.executor.XxlJobExecutor.start(XxlJobExecutor.java:83)
        at com.xxl.job.core.executor.impl.XxlJobSpringExecutor.afterPropertiesSet(XxlJobSpringExecutor.java:42)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1855)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1792)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:879)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141)
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747)
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215)
        at com.xxl.job.executor.XxlJobExecutorApplication.main(XxlJobExecutorApplication.java:14)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:51)
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:52)
Caused by: java.net.UnknownHostException: service-2.novalocal: Name or service not known
        at java.net.Inet4AddressImpl.lookupAllHostAddr(Native Method)
        at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:928)
        at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1323)
        at java.net.InetAddress.getLocalHost(InetAddress.java:1500)
        ... 31 common frames omitted

ping hostname不通导致,在/etc/hosts添加ip和hostname的映射即可解决。