Dubbo总结(一):Dubbo基本使用

1.1 什么是Dubbo

Dubbo是阿里巴巴公司开源的一个高性能优秀的分布式服务框架。使得应用可通过高性能的 RPC 实现服务的输出和输入功能,可以和Spring框架无缝集成。
随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进。

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

1.2 Dubbo的架构


节点角色说明

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

调用关系说明

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

1.3 使用方法

Dubbo采用全Spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载Dubbo的配置即可,Dubbo基于Spring的Schema扩展进行加载。
单一工程中spring的配置

<bean id="xxxService" class="com.xxx.XxxServiceImpl" />
<bean id="xxxAction" class="com.xxx.XxxAction">
    <property name="xxxService" ref="xxxService" />
</bean>  

在本地服务的基础上,只需做简单配置,即可完成远程化:
将上面的local.xml配置拆分成两份,将服务定义部分放在服务提供方remote-provider.xml,将服务引用部分放在服务消费方remote-consumer.xml。
并在提供方增加暴露服务配置,在消费方增加引用服务配置。
发布服务:

<!-- 和本地服务一样实现远程服务 -->
<bean id="xxxService" class="com.xxx.XxxServiceImpl" />
<!-- 增加暴露远程服务配置 -->
<dubbo:service interface="com.xxx.XxxService" ref="xxxService" />

调用服务:

<!-- 增加引用远程服务配置 -->
<dubbo:reference id="xxxService" interface="com.xxx.XxxService" />
<!-- 和本地服务一样使用远程服务 -->
<bean id="xxxAction" class="com.xxx.XxxAction">
    <property name="xxxService" ref="xxxService" />
</bean>

1.4 注册中心

1.4.1 Zookeeper介绍

官方推荐使用zookeeper注册中心。
注册中心负责服务地址的注册与查找,相当于目录服务,服务提供者和消费者只在启动时与注册中心交互,注册中心不转发请求,压力较小。使用dubbo-2.3.3以上版本,建议使用zookeeper注册中心。
Zookeeper是Apacahe Hadoop的子项目,是一个树型的目录服务,支持变更推送,适合作为Dubbo服务的注册中心,工业强度较高,可用于生产环境,并推荐使用
Zookeeper:
1.可以作为集群的管理工具使用。
2.可以集中管理配置文件。

1.4.2 Zookeeper安装

安装环境
Linux:centos6.4
Jdk:1.7以上版本
Zookeeper是java开发的可以运行在windows、linux环境。需要先安装jdk。
Zookeeper:百度网盘链接:https://pan.baidu.com/s/1tRrRwBB4rBk08ESZcKMGSA 密码:0lzp
安装步骤

  1. :安装jdk
  2. :把zookeeper的压缩包上传到linux系统。
  3. :解压缩压缩包
    tar -zxvf zookeeper-3.4.6.tar.gz
  4. :进入zookeeper-3.4.6目录,创建data文件夹。
  5. :把zoo_sample.cfg改名为zoo.cfg
    [root@localhost conf]# mv zoo_sample.cfg zoo.cfg
  6. :修改data属性:dataDir=/root/zookeeper-3.4.6/data
  7. :启动zookeeper
    [root@localhost bin]# ./zkServer.sh start

关闭:[root@localhost bin]# ./zkServer.sh stop
查看状态:[root@localhost bin]# ./zkServer.sh status
注意:需要关闭防火墙
service iptables stop
永久关闭修改配置开机不启动防火墙
chkconfig iptables off
如果不能成功启动zookeeper,需要删除data目录下的zookeeper_server.pid文件。

1.4.3 Zookeeper启动失败分析

第一步:检查是否安装了jdk,因为zookeeper的运行需要JVM环境,可以用java -version
命令来验证
第二步:检查端口是否被占用,zookeeper的默认端口是2181

       netstat -apn | grep 2181


第三步:若以上均不能解决,那么删除data目录下的zookeeper_server.pid文件

1.5 监控中心

链接:https://pan.baidu.com/s/1s5-7FtK0jBb0nXO6oolSlA 密码:2mm7

  1. 将dubbo-admin.war上传到Linux 放入tomcat/webapp下
  2. 启动tomcat
  3. 访问http://192.168.25.167:8080/dubbo-admin/
    用户名:root
    密码:root

注意
1.如果监控中心和注册中心在同一台服务器上,可以不需要任何配置。
2.如果不在同一台服务器,需要修改配置文件:
/root/apache-tomcat-7.0.47/webapps/dubbo-admin/WEB-INF/dubbo.properties

1.6 实例 基于Maven

1.6.1 基本案例

1.在pom.xml文件中添加dubbo依赖

<dependency>
     <groupId>com.alibaba</groupId>
     <artifactId>dubbo</artifactId>
<version>2.5.5</version>
</dependency>

2.定义service interface

 package com.alibaba.dubbo.demo;
public interface DemoService {
       String sayHello(String name);
}

3.实现接口

package com.alibaba.dubbo.demo.provider;
import com.alibaba.dubbo.demo.DemoService;
public class DemoServiceImpl implements DemoService {
       public String sayHello(String name) {
          return "Hello " + name;
      }
}

4.配置服务并发布

<?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://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="demo-provider"/>
    <dubbo:registry address="multicast://224.5.6.7:1234"/>
    <dubbo:protocol name="dubbo" port="20880"/>
    <dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService"/>
    <bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl"/></beans>

5.配置服务用户

<?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://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="demo-consumer"/>
    <dubbo:registry address="multicast://224.5.6.7:1234"/>
    <dubbo:reference id="demoService" interface="com.alibaba.dubbo.demo.DemoService"/></beans>

6.调用服务并运行

1.6.2 整合到SSM框架中

1.添加相关依赖

 <!-- dubbo相关 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.jboss.netty</groupId>
                    <artifactId>netty</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
        </dependency>
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
        </dependency><!-- dubbo相关 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.jboss.netty</groupId>
                    <artifactId>netty</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
        </dependency>
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
        </dependency>

2.配置服务提供方

 <!-- 使用dubbo发布服务 -->
    <!-- 提供方应用信息,用于计算依赖关系 -->
      <!-- 提供方应用名称,相当于起一个名字,方便管理  -->
    <dubbo:application name="e3-manager" />
      <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry protocol="zookeeper"
        address="192.168.25.154:2181,192.168.25.154:2182,192.168.25.154:2183" />
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" />
    <!-- 声明需要暴露的服务接口 -->
       <dubbo:service interface="cn.e3mall.service.ItemService" ref="itemServiceImpl" />

说明
dubbo:registry 标签一些属性的说明:
1)register是否向此注册中心注册服务,如果设为false,将只订阅,不注册。
2)check注册中心不存在时,是否报错。
3)subscribe是否向此注册中心订阅服务,如果设为false,将只注册,不订阅。
4)timeout注册中心请求超时时间(毫秒)。
5)address可以Zookeeper集群配置,地址可以多个以逗号隔开等。
dubbo:service标签的一些属性说明:
1)interface服务接口的路径
2)ref引用对应的实现类的Bean的ID
3)registry向指定注册中心注册,在多个注册中心时使用,值为的id属性,多个注册中心ID用逗号分隔,如果不想将该服务注册到任何registry,可将值设为N/A
4)register 默认true ,该协议的服务是否注册到注册中心。
3.配置服务使用方

<!-- 引用dubbo服务 -->
    <dubbo:application name="e3-manager-web"/>
<dubbo:registry protocol="zookeeper"    address="192.168.25.154:2181,192.168.25.154:2182,192.168.25.154:2183"/> 
    <dubbo:reference interface="cn.e3mall.service.ItemService" id="itemService" />

说明
dubbo:reference 的一些属性的说明:
1)interface调用的服务接口
2)check 启动时检查提供者是否存在,true报错,false忽略
3)registry 从指定注册中心注册获取服务列表,在多个注册中心时使用,值为的id属性,多个注册中心ID用逗号分隔
4)loadbalance 负载均衡策略,可选值:random,roundrobin,leastactive,分别表示:随机,轮循,最少活跃调用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值