Dubbo.docx
- 文档编号:27265177
- 上传时间:2023-06-28
- 格式:DOCX
- 页数:23
- 大小:96.19KB
Dubbo.docx
《Dubbo.docx》由会员分享,可在线阅读,更多相关《Dubbo.docx(23页珍藏版)》请在冰豆网上搜索。
Dubbo
1.Dubbo
1.1.什么是dubbo
随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进。
∙单一应用架构
∙当网站流量很小时,只需一个应用,将所有功能都部署在一起,以减少部署节点和成本。
∙此时,用于简化增删改查工作量的数据访问框架(ORM)是关键。
∙垂直应用架构
∙当访问量逐渐增大,单一应用增加机器带来的加速度越来越小,将应用拆成互不相干的几个应用,以提升效率。
∙此时,用于加速前端页面开发的Web框架(MVC)是关键。
∙分布式服务架构
∙当垂直应用越来越多,应用之间交互不可避免,将核心业务抽取出来,作为独立的服务,逐渐形成稳定的服务中心,使前端应用能更快速的响应多变的市场需求。
∙此时,用于提高业务复用及整合的分布式服务框架(RPC)是关键。
∙流动计算架构
∙当服务越来越多,容量的评估,小服务资源的浪费等问题逐渐显现,此时需增加一个调度中心基于访问压力实时管理集群容量,提高集群利用率。
∙此时,用于提高机器利用率的资源调度和治理中心(SOA)是关键。
Dubbo就是资源调度和治理中心的管理工具。
1.2.Dubbo的架构
节点角色说明:
∙Provider:
暴露服务的服务提供方。
∙Consumer:
调用远程服务的服务消费方。
∙Registry:
服务注册与发现的注册中心。
∙Monitor:
统计服务的调用次调和调用时间的监控中心。
∙Container:
服务运行容器。
调用关系说明:
∙0.服务容器负责启动,加载,运行服务提供者。
∙1.服务提供者在启动时,向注册中心注册自己提供的服务。
∙2.服务消费者在启动时,向注册中心订阅自己所需的服务。
∙3.注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
∙4.服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
∙5.服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。
1.3.使用方法
1.3.1.Spring配置
Dubbo采用全Spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载Dubbo的配置即可,Dubbo基于Spring的Schema扩展进行加载。
单一工程中spring的配置
远程服务:
在本地服务的基础上,只需做简单配置,即可完成远程化:
将上面的local.xml配置拆分成两份,将服务定义部分放在服务提供方remote-provider.xml,将服务引用部分放在服务消费方remote-consumer.xml。
并在提供方增加暴露服务配置 service>,在消费方增加引用服务配置 reference>。 发布服务: --和本地服务一样实现远程服务--> --增加暴露远程服务配置--> serviceinterface="com.xxx.XxxService"ref="xxxService"/> 调用服务: --增加引用远程服务配置--> referenceid="xxxService"interface="com.xxx.XxxService"/> --和本地服务一样使用远程服务--> 1.3.2.注册中心 注册中心负责服务地址的注册与查找,相当于目录服务,服务提供者和消费者只在启动时与注册中心交互,注册中心不转发请求,压力较小。 使用dubbo-2.3.3以上版本,建议使用zookeeper注册中心。 Zookeeper是ApacaheHadoop的子项目,是一个树型的目录服务,支持变更推送,适合作为Dubbo服务的注册中心,工业强度较高,可用于生产环境,并推荐使用 Zookeeper的安装: 第一步: 安装jdk 第二步: 解压缩zookeeper压缩包 第三步: 将conf文件夹下zoo_sample.cfg复制一份,改名为zoo.cfg 第四步: 修改配置dataDir属性,指定一个真实目录 第五步: 启动zookeeper: bin/zkServer.shstart 关闭zookeeper: bin/zkServer.shstop 查看zookeeper状态: bin/zkServer.shstatus 2.框架整合 2.1.添加dubbo的依赖 加入dubbo相关的jar包。 服务层、表现层都添加。 --dubbo相关--> --排除依赖--> 2.2.整合思路 1、Dao层: mybatis整合spring,通过spring管理SqlSessionFactory、mapper代理对象。 需要mybatis和spring的整合包。 整合内容 对应工程 Pojo Taotao-mangaer-pojo Mapper映射文件 Taotao-mangaer-dao Mapper接口 Taotao-mangaer-dao sqlmapConfig.xml Taotao-manager-service applicationContext-dao.xml Taotao-manager-service 2、Service层: 所有的service实现类都放到spring容器中管理。 由spring创建数据库连接池,并有spring管理实务。 发布dubbo服务 整合内容 对应工程 Service接口 Taotao-mangaer-interface service实现类 Taotao-mangaer-service applicationContext-service.xml Taotao-manager-service applicationContext-trans.xml Taotao-manager-service 3、表现层: Springmvc框架,由springmvc管理controller。 引用dubbo服务 整合内容 对应工程 Springmvc.xml(扫描controller、引用dubbo服务) Taotao-manager-web Controller Taotao-manager-web 2.3.Dao整合 2.3.1.创建SqlMapConfig.xml配置文件 xmlversion="1.0"encoding="UTF-8"? > DOCTYPEconfiguration PUBLIC"-//mybatis.org//DTDConfig3.0//EN" "http: //mybatis.org/dtd/mybatis-3-config.dtd"> 2.3.2.Spring整合mybatis 创建applicationContext-dao.xml //www.springframework.org/schema/beans" xmlns: context="http: //www.springframework.org/schema/context"xmlns: p="http: //www.springframework.org/schema/p" xmlns: aop="http: //www.springframework.org/schema/aop"xmlns: tx="http: //www.springframework.org/schema/tx" xmlns: xsi="http: //www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation="http: //www.springframework.org/schema/beanshttp: //www.springframework.org/schema/beans/spring-beans-4.2.xsd http: //www.springframework.org/schema/contexthttp: //www.springframework.org/schema/context/spring-context-4.2.xsd http: //www.springframework.org/schema/aophttp: //www.springframework.org/schema/aop/spring-aop-4.2.xsdhttp: //www.springframework.org/schema/txhttp: //www.springframework.org/schema/tx/spring-tx-4.2.xsd http: //www.springframework.org/schema/utilhttp: //www.springframework.org/schema/util/spring-util-4.2.xsd"> --数据库连接池--> --加载配置文件--> property-placeholderlocation="classpath: properties/*.properties"/> --数据库连接池--> destroy-method="close"> --让spring管理sqlsessionfactory使用mybatis和spring整合包中的--> --数据库连接池--> --加载mybatis的全局配置文件--> mybatis/SqlMapConfig.xml"/> db.properties jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc: mysql: //localhost: 3306/taotao? characterEncoding=utf-8 jdbc.username=root jdbc.password=root 备注: Druid是目前最好的数据库连接池,在功能、性能、扩展性方面,都超过其他数据库连接池,包括DBCP、C3P0、BoneCP、Proxool、JBossDataSource。 Druid已经在阿里巴巴部署了超过600个应用,经过多年多生产环境大规模部署的严苛考验。 2.4.Service整合 2.4.1.管理Service xmlversion="1.0"encoding="UTF-8"? > //www.springframework.org/schema/beans" xmlns: context="http: //www.springframework.org/schema/context"xmlns: p="http: //www.springframework.org/schema/p" xmlns: aop="http: //www.springframework.org/schema/aop"xmlns: tx="http: //www.springframework.org/schema/tx" xmlns: dubbo="xmlns: xsi="http: //www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation="http: //www.springframework.org/schema/beanshttp: //www.springframework.org/schema/beans/spring-beans-4.2.xsd http: //www.springframework.org/schema/contexthttp: //www.springframework.org/schema/context/spring-context-4.2.xsd http: //www.springframework.org/schema/aophttp: //www.springframework.org/schema/aop/spring-aop-4.2.xsdhttp: //www.springframework.org/schema/txhttp: //www.springframework.org/schema/tx/spring-tx-4.2.xsd http: //www.springframework.org/schema/utilhttp: //www.springframework.org/schema/util/spring-util-4.2.xsd"> component-scanbase-package="com.taotao.service"> component-scan> --使用dubbo发布服务--> --提供方应用信息,用于计算依赖关系--> applicationname="taotao-manager"/> registryprotocol="zookeeper" address="192.168.25.154: 2181,192.168.25.154: 2182,192.168.25.154: 2183"/> --用dubbo协议在20880端口暴露服务--> protocolname="dubbo"port="20880"/> --声明需要暴露的服务接口--> serviceinterface="com.taotao.service.ItemService"ref="itemServiceImpl"/> 2.4.2.事务管理 创建applicationContext-trans.xml //www.springframework.org/schema/beans" xmlns: context="http: //www.springframework.org/schema/context"xmlns: p="http: //www.springframework.org/schema/p" xmlns: aop="http: //www.springframework.org/schema/aop"xmlns: tx="http: //www.springframework.org/schema/tx" xmlns: xsi="http: //www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation="http: //www.springframework.org/schema/beanshttp: //www.springframework.org/schema/beans/spring-beans-4.2.xsd http: //www.springframework.org/schema/contexthttp: //www.springframework.org/schema/context/spring-context-4.2.xsd http: //www.springframework.org/schema/aophttp: //www.springframework.org/schema/aop/spring-aop-4.2.xsdhttp: //www.springframework.org/schema/txhttp: //www.springframework.org/schema/tx/spring-tx-4.2.xsd http: //www.springframework.org/schema/utilhttp: //www.springframework.org/schema/util/spring-util-4.2.xsd"> --事务管理器--> class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> --数据源--> --通知--> adviceid="txAdvice"transaction-manager="transactionManager"> attributes> --传播行为--> methodname="save*"propagation="REQUIRED"/> methodname="insert*"propagation="REQUIRED"/> methodname="add*"propagation="REQUIRED"/> methodname="create*"propagation="REQUIRED"/> methodname="delete*"propagation="REQUIRED"/> methodname="update*"propagation="REQUIRED"/> methodname="find*"propagation="SUPPORTS"read-only="true"/> methodname="select*"propagation="SUPPORTS"read-only="true"/> methodname="get*"propagation="SUPPORTS"read-only="true"/> attributes> advice> --切面--> config> advisoradvice-ref="txAdvice" pointcut="execution(*com.taotao.service.*.*(..))"/> config> 2.4.3.Web.xml xmlversion="1.0"encoding="UTF-8"? > xsi="http: //www.w3.org/2
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Dubbo