电商底层接口(系统接口或SOA接口)高并发解决方案:dubbo + zookeeper

简介


dubbo是一个分布式服务框架,是阿里巴巴开发的一个解决RPC远程调用优化的核心框架,包含负载均衡算法,能提高分布式系统的性能。

zookeeper是hadoop的一个子项目,主要用来解决分布式系统的数据一致性、状态同步、服务集群管理、配置同步等一系列的问题。本文使用zookeeper作为dubbo的服务注册中心。

Dubbo适用于哪些场景?


当网站变大后,不可避免的需要拆分应用进行服务化,以提高开发效率,调优性能,节省关键竞争资源等。 

当服务越来越多时,服务的URL地址信息就会爆炸式增长,配置管理变得非常困难,F5硬件负载均衡器的单点压力也越来越大。 

当进一步发展,服务间依赖关系变得错踪复杂,甚至分不清哪个应用要在哪个应用之前启动,架构师都不能完整的描述应用的架构关系。 

接着,服务的调用量越来越大,服务的容量问题就暴露出来,这个服务需要多少机器支撑?什么时候该加机器?等等…… 

在遇到这些问题时,都可以用Dubbo来解决。


Dubbo产生背景


随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进。

电商底层接口(系统接口或SOA接口)高并发解决方案:dubbo + zookeeper_第1张图片

  • 单一应用架构
    • 当网站流量很小时,只需一个应用,将所有功能都部署在一起,以减少部署节点和成本。
    • 此时,用于简化增删改查工作量的数据访问框架(ORM) 是关键。
  • 垂直应用架构
    • 当访问量逐渐增大,单一应用增加机器带来的加速度越来越小,将应用拆成互不相干的几个应用,以提升效率。
    • 此时,用于加速前端页面开发的Web框架(MVC) 是关键。
  • 分布式服务架构
    • 当垂直应用越来越多,应用之间交互不可避免,将核心业务抽取出来,作为独立的服务,逐渐形成稳定的服务中心,使前端应用能更快速的响应多变的市场需求。
    • 此时,用于提高业务复用及整合的分布式服务框架(RPC) 是关键。
  • 流动计算架构
    • 当服务越来越多,容量的评估,小服务资源的浪费等问题逐渐显现,此时需增加一个调度中心基于访问压力实时管理集群容量,提高集群利用率。
    • 此时,用于提高机器利用率的资源调度和治理中心(SOA) 是关键。

dobbo系统架构


电商底层接口(系统接口或SOA接口)高并发解决方案:dubbo + zookeeper_第2张图片

节点角色说明:

  • Provider: 暴露服务的服务提供方。
  • Consumer: 调用远程服务的服务消费方。
  • Registry: 服务注册与发现的注册中心。
  • Monitor: 统计服务的调用次调和调用时间的监控中心。
  • Container: 服务运行容器。

上图中,蓝色的表示与业务有交互,绿色的表示只对Dubbo内部交互。上述图所描述的调用流程如下:

  • 服务容器负责启动,加载,运行服务提供者。
  • 服务提供者在启动时,向注册中心注册自己提供的服务。
  • 服务消费者在启动时,向注册中心订阅自己所需的服务。
  • 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
  • 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
  • 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。

将上面抽象的调用流程图展开,详细如图所示:

电商底层接口(系统接口或SOA接口)高并发解决方案:dubbo + zookeeper_第3张图片

(1) 连通性:

  • 注册中心负责服务地址的注册与查找,相当于目录服务,服务提供者和消费者只在启动时与注册中心交互,注册中心不转发请求,压力较小
  • 监控中心负责统计各服务调用次数,调用时间等,统计先在内存汇总后每分钟一次发送到监控中心服务器,并以报表展示
  • 服务提供者向注册中心注册其提供的服务,并汇报调用时间到监控中心,此时间不包含网络开销
  • 服务消费者向注册中心获取服务提供者地址列表,并根据负载算法直接调用提供者,同时汇报调用时间到监控中心,此时间包含网络开销
  • 注册中心,服务提供者,服务消费者三者之间均为长连接,监控中心除外
  • 注册中心通过长连接感知服务提供者的存在,服务提供者宕机,注册中心将立即推送事件通知消费者
  • 注册中心和监控中心全部宕机,不影响已运行的提供者和消费者,消费者在本地缓存了提供者列表
  • 注册中心和监控中心都是可选的,服务消费者可以直连服务提供者

(2) 健状性:

  • 监控中心宕掉不影响使用,只是丢失部分采样数据
  • 数据库宕掉后,注册中心仍能通过缓存提供服务列表查询,但不能注册新服务
  • 注册中心对等集群,任意一台宕掉后,将自动切换到另一台
  • 注册中心全部宕掉后,服务提供者和服务消费者仍能通过本地缓存通讯
  • 服务提供者无状态,任意一台宕掉后,不影响使用
  • 服务提供者全部宕掉后,服务消费者应用将无法使用,并无限次重连等待服务提供者恢复

(3) 伸缩性:

  • 注册中心为对等集群,可动态增加机器部署实例,所有客户端将自动发现新的注册中心
  • 服务提供者无状态,可动态增加机器部署实例,注册中心将推送新的服务提供者信息给消费者

(4) 升级性:

当服务集群规模进一步扩大,带动IT治理结构进一步升级,需要实现动态部署,进行流动计算,现有分布式服务架构不会带来阻力:

电商底层接口(系统接口或SOA接口)高并发解决方案:dubbo + zookeeper_第4张图片



示例:

如图:某电商SOA服务。

电商底层接口(系统接口或SOA接口)高并发解决方案:dubbo + zookeeper_第5张图片

那么Controller如何调用这些服务,首先需要将这些服务通过zookeeper注册到注册中心,然后Controller再从注册中心获取服务。

dubbo服务提供配置如下:

xml version="1.0" encoding="UTF-8"?>
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="mall-soa" logger="slf4j" />
    <dubbo:registry address="${dubbo.registry.address}" timeout="5000" file="${dubbo.registry.cache}" />
    <dubbo:provider delay="5000" timeout="30000" retries="0" token="zxngqplan" filter="tpsFilter,default,providerFilter">
        <dubbo:parameter key="tps" value="100" />
        <dubbo:parameter key="tps.interval" value="1000" />
    dubbo:provider>
    <dubbo:protocol name="dubbo" port="20880" />
    

    
    id="cartService" class="com.zxng.mall.soa.order.impl.CartServiceImpl" />
    <dubbo:service interface="com.zxng.mall.soa.order.api.CartService" ref="cartService" version="1.0.0" />
    
    id="orderService" class="com.zxng.mall.soa.order.impl.OrderServiceImpl" />
    <dubbo:service interface="com.zxng.mall.soa.order.api.OrderService" ref="orderService" version="1.0.0" />
    
    id="settlementService" class="com.zxng.mall.soa.order.impl.SettlementServiceImpl" />
    <dubbo:service interface="com.zxng.mall.soa.order.api.SettlementService" ref="settlementService" version="1.0.0" />
    
    id="salesReturnService" class="com.zxng.mall.soa.order.impl.SalesReturnServiceImpl" />
    <dubbo:service interface="com.zxng.mall.soa.order.api.SalesReturnService" ref="salesReturnService" version="1.0.0" />
    
    id="groupService" class="com.zxng.mall.soa.promotion.impl.GroupServiceImpl" />
    <dubbo:service interface="com.zxng.mall.soa.promotion.api.GroupService" ref="groupService" version="1.0.0" />
    
    id="couponService" class="com.zxng.mall.soa.promotion.impl.CouponServiceImpl" />
    <dubbo:service interface="com.zxng.mall.soa.promotion.api.CouponService" ref="couponService" version="1.0.0" />

    
    id="balancePayService" class="com.zxng.mall.soa.pay.impl.BalancePayServiceImpl" />
    <dubbo:service interface="com.zxng.mall.soa.pay.api.BalancePayService" ref="balancePayService" version="1.0.0" />
    
    id="couponPayService" class="com.zxng.mall.soa.pay.impl.CouponPayServiceImpl" />
    <dubbo:service interface="com.zxng.mall.soa.pay.api.CouponPayService" ref="couponPayService" version="1.0.0" />
    
    id="wxPubPayService" class="com.zxng.mall.soa.pay.impl.WXPubPayServiceImpl" />
    <dubbo:service interface="com.zxng.mall.soa.pay.api.WXPubPayService" ref="wxPubPayService" version="1.0.0" />
    
    id="wxAppPayService" class="com.zxng.mall.soa.pay.impl.WXAppPayServiceImpl" />
    <dubbo:service interface="com.zxng.mall.soa.pay.api.WXAppPayService" ref="wxAppPayService" version="1.0.0" />
    
    id="alipayWapPayService" class="com.zxng.mall.soa.pay.impl.AlipayWapPayServiceImpl" />
    <dubbo:service interface="com.zxng.mall.soa.pay.api.AlipayWapPayService" ref="alipayWapPayService" version="1.0.0" />
    
    id="alipayAppPayService" class="com.zxng.mall.soa.pay.impl.AlipayAppPayServiceImpl" />
    <dubbo:service interface="com.zxng.mall.soa.pay.api.AlipayAppPayService" ref="alipayAppPayService" version="1.0.0" />
    
    id="paymentService" class="com.zxng.mall.soa.pay.impl.PaymentServiceImpl" />
    <dubbo:service interface="com.zxng.mall.soa.pay.api.PaymentService" ref="paymentService" version="1.0.0" />
    
    id="webhookLogService" class="com.zxng.mall.soa.pay.impl.WebhookLogServiceImpl" />
    <dubbo:service interface="com.zxng.mall.soa.pay.api.WebhookLogService" ref="webhookLogService" version="1.0.0" />

    
    id="commodityService" class="com.zxng.mall.soa.sku.impl.CommodityServiceImpl" />
    <dubbo:service interface="com.zxng.mall.soa.sku.api.CommodityService" ref="commodityService" version="1.0.0" />
    
    id="commentService" class="com.zxng.mall.soa.sku.impl.CommentServiceImpl" />
    <dubbo:service interface="com.zxng.mall.soa.sku.api.CommentService" ref="commentService" version="1.0.0" />
    
    id="categoryService" class="com.zxng.mall.soa.sku.impl.CategoryServiceImpl" />
    <dubbo:service interface="com.zxng.mall.soa.sku.api.CategoryService" ref="categoryService" version="1.0.0" />
    
    id="merchantService" class="com.zxng.mall.soa.sku.impl.MerchantServiceImpl" />
    <dubbo:service interface="com.zxng.mall.soa.sku.api.MerchantService" ref="merchantService" version="1.0.0" />

   
    id="traceService" class="com.zxng.mall.soa.member.impl.TraceServiceImpl" />
    <dubbo:service interface="com.zxng.mall.soa.member.api.TraceService" ref="traceService" version="1.0.0" />
    
    id="collectionService" class="com.zxng.mall.soa.member.impl.CollectionServiceImpl" />
    <dubbo:service interface="com.zxng.mall.soa.member.api.CollectionService" ref="collectionService" version="1.0.0" />
    
    id="loginService" class="com.zxng.mall.soa.member.impl.LoginServiceImpl" />
    <dubbo:service interface="com.zxng.mall.soa.member.api.LoginService" ref="loginService" version="1.0.0" />
    
    id="memberService" class="com.zxng.mall.soa.member.impl.MemberServiceImpl" />
    <dubbo:service interface="com.zxng.mall.soa.member.api.MemberService" ref="memberService" version="1.0.0" />
    
    id="passportService" class="com.zxng.mall.soa.member.impl.PassportServiceImpl" />
    <dubbo:service interface="com.zxng.mall.soa.member.api.PassportService" ref="passportService" version="1.0.0" />

    
    id="expressService" class="com.zxng.mall.soa.thirdparty.impl.ExpressServiceImpl" />
    <dubbo:service interface="com.zxng.mall.soa.thirdparty.api.ExpressService" ref="expressService" version="1.0.0" />
    
    id="weixinPayApiService" class="com.zxng.mall.soa.thirdparty.impl.WeixinPayApiServiceImpl" />
    <dubbo:service interface="com.zxng.mall.soa.thirdparty.api.WeixinPayApiService" ref="weixinPayApiService" version="1.0.0" />
    
    id="alipayPayApiService" class="com.zxng.mall.soa.thirdparty.impl.AlipayPayApiServiceImpl" />
    <dubbo:service interface="com.zxng.mall.soa.thirdparty.api.AlipayPayApiService" ref="alipayPayApiService" version="1.0.0" />
    
    id="smsService" class="com.zxng.mall.soa.thirdparty.impl.SmsServiceImpl" />
    <dubbo:service interface="com.zxng.mall.soa.thirdparty.api.SmsService" ref="smsService" version="1.0.0" />
    
    id="weixinService" class="com.zxng.mall.soa.thirdparty.impl.WeixinServiceImpl" />
    <dubbo:service interface="com.zxng.mall.soa.thirdparty.api.WeixinService" ref="weixinService" version="1.0.0" />

    
    id="metaService" class="com.zxng.mall.soa.meta.impl.MetaServiceImpl" />
    <dubbo:service interface="com.zxng.mall.soa.meta.api.MetaService" ref="metaService" version="1.0.0" />
    
    id="commonService" class="com.zxng.mall.soa.meta.impl.CommonServiceImpl" />
    <dubbo:service interface="com.zxng.mall.soa.meta.api.CommonService" ref="commonService" version="1.0.0" />
    
    id="advertiseService" class="com.zxng.mall.soa.meta.impl.AdvertiseServiceImpl" />
    <dubbo:service interface="com.zxng.mall.soa.meta.api.AdvertiseService" ref="advertiseService" version="1.0.0" />
    
    id="columnService" class="com.zxng.mall.soa.meta.impl.ColumnServiceImpl" />
    <dubbo:service interface="com.zxng.mall.soa.meta.api.ColumnService" ref="columnService" version="1.0.0" />
    
    id="keywordService" class="com.zxng.mall.soa.meta.impl.KeywordServiceImpl" />
    <dubbo:service interface="com.zxng.mall.soa.meta.api.KeywordService" ref="keywordService" version="1.0.0" />
    
    id="siteConfigService" class="com.zxng.mall.soa.meta.impl.SiteConfigServiceImpl" />
    <dubbo:service interface="com.zxng.mall.soa.meta.api.SiteConfigService" ref="siteConfigService" version="1.0.0" />

    
    id="searchService" class="com.zxng.mall.soa.search.impl.SearchServiceImpl" />
    <dubbo:service interface="com.zxng.mall.soa.search.api.SearchService" ref="searchService" version="1.0.0" />

    
    id="weiXinUserService" class="com.zxng.mall.soa.weixin.impl.WeiXinUserServiceImpl" />
    <dubbo:service interface="com.zxng.mall.soa.weixin.api.WeiXinUserService" ref="weiXinUserService" version="1.0.0" />


从注册中心获取服务配置如下:

xml version="1.0" encoding="UTF-8"?>
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="mall-web" logger="slf4j"/>
    <dubbo:registry address="${dubbo.registry.address}" timeout="10000" file="${dubbo.registry.cache}" />
    <dubbo:consumer check="false" filter="consumerFilter" />
    

    <dubbo:reference id="cartService" interface="com.zxng.mall.soa.order.api.CartService" version="1.0.0" />
    <dubbo:reference id="orderService" interface="com.zxng.mall.soa.order.api.OrderService" version="1.0.0" />
    <dubbo:reference id="salesReturnService" interface="com.zxng.mall.soa.order.api.SalesReturnService" version="1.0.0" />
    <dubbo:reference id="groupService" interface="com.zxng.mall.soa.promotion.api.GroupService" version="1.0.0" />
    <dubbo:reference id="couponService" interface="com.zxng.mall.soa.promotion.api.CouponService" version="1.0.0" />

    <dubbo:reference id="commodityService" interface="com.zxng.mall.soa.sku.api.CommodityService" version="1.0.0" />
    <dubbo:reference id="commentService" interface="com.zxng.mall.soa.sku.api.CommentService" version="1.0.0" />
    <dubbo:reference id="categoryService" interface="com.zxng.mall.soa.sku.api.CategoryService" version="1.0.0" />
    <dubbo:reference id="merchantService" interface="com.zxng.mall.soa.sku.api.MerchantService" version="1.0.0" />

    <dubbo:reference id="memberService" interface="com.zxng.mall.soa.member.api.MemberService" version="1.0.0" />
    <dubbo:reference id="traceService" interface="com.zxng.mall.soa.member.api.TraceService" version="1.0.0" />
    <dubbo:reference id="collectionService" interface="com.zxng.mall.soa.member.api.CollectionService" version="1.0.0" />
    <dubbo:reference id="loginService" interface="com.zxng.mall.soa.member.api.LoginService" version="1.0.0" />
    <dubbo:reference id="passportService" interface="com.zxng.mall.soa.member.api.PassportService" version="1.0.0" />

    <dubbo:reference id="commonService" interface="com.zxng.mall.soa.meta.api.CommonService" version="1.0.0" />
    <dubbo:reference id="metaService" interface="com.zxng.mall.soa.meta.api.MetaService" version="1.0.0" />
    <dubbo:reference id="advertiseService" interface="com.zxng.mall.soa.meta.api.AdvertiseService" version="1.0.0" />
    <dubbo:reference id="columnService" interface="com.zxng.mall.soa.meta.api.ColumnService" version="1.0.0" />
    <dubbo:reference id="keywordService" interface="com.zxng.mall.soa.meta.api.KeywordService" version="1.0.0" />
    <dubbo:reference id="siteConfigService" interface="com.zxng.mall.soa.meta.api.SiteConfigService" version="1.0.0" />

    <dubbo:reference id="expressService" interface="com.zxng.mall.soa.thirdparty.api.ExpressService" version="1.0.0" />
    <dubbo:reference id="weixinService" interface="com.zxng.mall.soa.thirdparty.api.WeixinService" version="1.0.0" />

    <dubbo:reference id="searchService" interface="com.zxng.mall.soa.search.api.SearchService" version="1.0.0" />

    <dubbo:reference id="alipayAppPayService" interface="com.zxng.mall.soa.pay.api.AlipayAppPayService" version="1.0.0" />
    <dubbo:reference id="alipayWapPayService" interface="com.zxng.mall.soa.pay.api.AlipayWapPayService" version="1.0.0" />
    <dubbo:reference id="wxPubPayService" interface="com.zxng.mall.soa.pay.api.WXPubPayService" version="1.0.0" />
    <dubbo:reference id="wxAppPayService" interface="com.zxng.mall.soa.pay.api.WXAppPayService" version="1.0.0" />

    <dubbo:reference id="weiXinUserService" interface="com.zxng.mall.soa.weixin.api.WeiXinUserService" version="1.0.0" />


你可能感兴趣的:(互联网技术)