• Dubbo配置基础V1.0.0


    Dubbo配置文件详解

    一、应用架构演变

    • 单一服务架构
    • 垂直服务架构
    • 分布式服务架构
    • 流动计算架构

    二、Dubbo介绍

    • Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案。
    • 核心内容:
      • 透明化的远程方法调用,就像调用本地方法一样调用远程方法,只需简单配置,没有任何API侵入。
      • 软负载均衡及容错机制,可在内网替代F5等硬件负载均衡器,降低成本,减少单点。
      • 服务自动注册与发现,不再需要写死服务提供方地址,注册中心基于接口名查询服务提供者的IP地址,并且能够平滑添加或删除服务提供者。
    • Dubbo主要由服务提供者,服务消费者,注册中心,监控中心几个节点角色组成。

    三、Dubbo主要配置

    3.1、指定服务在dubbo中的名称

    <dubbo:application name="producer"/>
    
    • 1

    3.2、指定dubbo注册中心地址

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

    3.3、指定通信规则

    <dubbo:protocol name="dubbo" port="20882" />
    
    • 1

    3.4、暴露服务

    <dubbo:service interface="com.dgc.learn.service.UserService" ref="userServiceImpl" />
    
    • 1

    3.5、引用服务

    <dubbo:reference interface="com.dgc.learn.service.UserService" id="userService" />
    
    • 1

    四、Dubbo常用配置

    4.1、超时配置

    • 由于网络原因,可能会出现不确定的中间状态(超时),为防止因超时引起的阻塞,需设置超时时间

    • 消费端

      • 全局超时配置:
      <dubbo:consumer timeout="5000" />
      
      • 1
      • 指定接口以及特定方法超时配置:
      <dubbo:reference interface="com.foo.BarService" timeout="2000">
      		<dubbo:method name="sayHello" timeout="3000" />
      </dubbo:reference>
      
      • 1
      • 2
      • 3
    • 服务端

      • 全局超时配置:
      <dubbo:provider timeout="5000"/>
      
      • 1
      • 指定接口以及特定方法超时配置:
      <dubbo:provider interface="com.foo.BarService" timeout="2000">
      	<dubbo:method name="sayHello" timeout="3000" />
      </dubbo:provider>
      
      • 1
      • 2
      • 3

    4.2、失败重试

    • 当出现失败,会自动重试其它服务器,重试次数(不含第一次)

      • 服务端:
      <dubbo:service retries="2"/>
      
      • 1
      • 消费端:
      <dubbo:reference retries="2"/>
      
      • 1
    • 注意:两端配置其一即可!

    4.3、版本号

    • 方便接口升级,不影响老版本接口,对接口增加版本号

    • 服务端:

      <dubbo:service version="1.0.0"/>
      
      • 1
    • 消费端:

      <dubbo:reference version="1.0.0"/>
      
      • 1
    • 注意:两者之间只要version号相同时才可以调用!!!

    4.4、负载均衡

    • dubbo负载均衡策略
    <dubbo:service  interface="..."  loadbalance="roundrobin"/>
    
    • 1
    • 参数:loadbalance 可选值
      • Random 随机调用(默认策略)
      • RoundRobin 轮循,按公约后的权重设置轮循比率
      • LeastActive 最小活跃调用数,对于响应快的优先调用
      • ConsistentHash 一致性HASH:相同参数的请求总发送到同一提供者,当某一台提供者挂时,原本发往该提供者的请求,基于虚拟节点,平摊到其它提供者

    4.5、启动服务检查

    • 启动时会检查所依赖的服务是否正常,如果不正常,则启动报错,默认为true.当出现相互依赖时,当设置为false
    <dubbo:reference cheack="true">
    
    • 1
    • 在consumer上配置表示如果启动时没有服务提供者,会报错
    <dubbo:consumer cheack="true">
    
    • 1
    • 在registry上配置表示如果启动时连接不上注册中心会报错
    <dubbo:registry cheack="true">
    
    • 1

    4.6、多协议

    • dubbo可支持的协议
      • dubbo RMI hessian webservice http thrift

    4.7、多注册中心支持

    • 配置多个注册中心
    <dubbo:registry id="one" protocol="zookeeper" address="127.0.0.1:2181"/>
    <dubbo:registry id="two" protocol="zookeeper" address="127.0.0.2:2181"/>
    
    • 1
    • 2
    • 为服务指定注册中心
    <dubbo:service interface="com.dgc.learn.service.UserService" ref="userServiceImpl" registry="one"/>
    
    • 1

    4.8、异步调用

    • 配置接口异步返回
    <dubbo:service async="true">
    
    • 1
    • 但是hessian协议,使用async异步回调会报错!!!

    4.9、只订阅/只注册

    • 不向注册中心注册,只订阅
    <dubbo:registry register="false">
    
    • 1
    • 不向注册中心订阅,只注册
    <dubbo registry subscribe="false">
    
    • 1

    五、集群容错

    • Failover Cluster 失败切换(缺省):当出现失败,重试其它服务器。通常用于读操作,可通过retries="num"来设置重试次数。
    • Failfast Cluster 快速失败:只发起一次调用,失败立即报错。通常用于非幂等性的写操作,比如新增记录。
    • Failsafe Cluster 失败安全:出现异常时,直接忽略。通常用于写入审计日志等操作。
    • Failback Cluster 失败恢复:后台记录失败请求,定时重发。通常用于消息通知操作。
    • Forking Cluster 并行调用:只要一个成功即返回。通常用于实时性要求较高的读操作,可通过forks="2"来设置最大并行数。
    • Broadcast Cluster 广播调用:逐个调用,任意一台报错则报错。通常用于通知所有提供者更新缓存或日志等本地资源信息。

    六、配置优先级

    • dubbo所有配置的优先级顺序
      • refrence method > service method > refrence > service > consumer > provider
    • 核心是:局部覆盖外部,消费端覆盖提供端

    七、配置模板

    • 服务端模板:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
           xmlns="http://www.springframework.org/schema/beans"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
              http://www.springframework.org/schema/beans/spring-beans.xsd
              http://dubbo.apache.org/schema/dubbo
              http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
     	<dubbo:application name="提供端服务名称" default="true"/>
        <dubbo:registry id="register" address="注册地址1"  file="注册接口缓存的服务器目录"/>
        <dubbo:registry id="auth" address="注册地址2" file="注册接口缓存的服务器目录"/>
        <dubbo:protocol name="通信规则名称" port="通信端口" threads="线程数量"  payload="接口传输流量上限"/>
        <!--配置链接不到注册中心报错-->
        <dubbo:registry cheack="true">
    
        <!-- 提供服务 
    		retries:重试次数
    		timeout:超时时间
    		registry:注册地址
    		async:是否支持结果异步
    		version:接口版本
    	-->
        <dubbo:service interface="cn.com.dgc.service.WebServiceBiz"
                       ref="webServiceBiz" retries="0" timeout="60000" 				                        registry="register" async="false"
                       version="1.0.0"
                       />
    </beans>
    
    • 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
    • 消费端模板:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
           xmlns="http://www.springframework.org/schema/beans"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
              http://www.springframework.org/schema/beans/spring-beans.xsd
              http://dubbo.apache.org/schema/dubbo
              http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
        <dubbo:application name="消费端服务名称" default="true"/>
        <dubbo:registry id="register" address="注册地址1"/>
        <dubbo:registry id="logger" address="注册地址2"/>
        <dubbo:protocol  name="通信规则名称"/>
    	<!--消费接口-->	
        <dubbo:reference registry="register"interface="cn.com.dgc.service.WebServiceBiz"
                         id="webServiceBiz" check="false" timeout="60000"/>
    </beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15





  • 相关阅读:
    uni-app app端.m3u8类型流的播放
    Java基础面试题(背诵篇)
    若依微服务上传图片文件代理配置
    如何抓取 GitHub:实用教程 2024
    javascript中的事件总结
    数据库总结之高级篇
    JavaSE进阶之(十)Map 子接口之 ConcurrentHashMap
    一款强大的红队信息收集和资产收集工具(Kscan)
    一篇文章教你Pytest快速入门和基础讲解,一定要看
    Andorid复习
  • 原文地址:https://blog.csdn.net/qq_42445425/article/details/125526407