介绍

GitHub地址:https://github.com/erlieStar/study-dubbo

微服务是最近比较火的概念,而微服务框架目前主流的有Dubbo和Spring Cloud,两者都是为了解决微服务遇到的各种问题而产生的,即遇到的问题是一样的,但是解决的策略却有所不同,所以这2个框架经常拿来比较。没用过Dubbo的小伙伴也不用担心,其实Dubbo还是比较简单的,看完本文你也能掌握一个大概,重要的不是代码,而是思想。

Dubbo实现服务调用是通过RPC的方式,即客户端和服务端共用一个接口,客户端面向接口写调用,服务端面向接口写实现,中间的网络通信交给框架去实现,想深入了解的看推荐阅读。

使用入门

服务提供者

定义服务接口

public interface DemoService {
    String sayHello(String name);
}

在服务提供方实现接口

public class DemoServiceImpl implements DemoService {
    @Override
    public String sayHello(String name) {
        return "Hello " + name;
    }
}

用 Spring 配置声明暴露服务
provider.xml

<?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="helloworld-app"  />

    <!--dubbo这个服务所要暴露的服务地址所对应的注册中心,N/A为不使用注册中心-->
    <dubbo:registry address="N/A"/>

    <!--当前服务发布所依赖的协议;webserovice、Thrift、Hessain、http-->
    <dubbo:protocol name="dubbo" port="20880"/>

    <!--服务发布的配置,需要暴露的服务接口-->
    <dubbo:service interface="com.st.DemoService"
                   ref="demoService"/>

    <!--bean的定义-->
    <bean id="demoService" class="com.st.DemoServiceImpl"/>

</beans>

加载 Spring 配置

public class Provider {

    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml");
        context.start();
        System.in.read(); // 按任意键退出
    }
}

服务消费者

consumer.xml

<?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="consumer-of-helloworld-app"/>

    <dubbo:registry address="N/A"/>

    <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
    <dubbo:reference id="demoService" interface="com.st.DemoService"
                     url="dubbo://localhost:20880/com.st.DemoService"/>

</beans>

加载Spring配置,并调用远程服务

public class Consumer {

    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml");
        context.start();
        // 获取远程服务代理
        DemoService demoService = (DemoService)context.getBean("demoService");
        // 执行远程方法
        String hello = demoService.sayHello("world");
        // Hello world
        System.out.println( hello );
    }
}

这就是典型的点对点的服务调用。当然我们为了高可用,可以在consumer.xml中配置多个服务提供者,并配置响应的负载均衡策略

配置多个服务调用者在comsumer.xml的<dubbo:reference>标签的url属性中加入多个地址,中间用分号隔开即可
配置负载均衡策略在comsumer.xml的<dubbo:reference>标签中增加loadbalance即可,值可以为如下四种类型

  1. RoundRobin LoadBalance,随机,按权重设置随机概率。
  2. RoundRobin LoadBalance,轮询,按公约后的权重设置轮询比率。
  3. LeastActive LoadBalance,最少活跃调用数,相同活跃数的随机,活跃数指调用前后计数差。
  4. ConsistentHash LoadBalance,一致性 Hash,相同参数的请求总是发到同一提供者。
<dubbo:reference id="demoService" interface="com.st.DemoService"
				 url="dubbo://192.168.11.1:20880/com.st.DemoService;
				 dubbo://192.168.11.2:20880/com.st.DemoService;
				 dubbo://192.168.11.3:20880/com.st.DemoService"
				 loadbalance="roundrobin"/>

现在整体架构是如下图(假设服务消费者为订单服务,服务提供者为用户服务):

在这里插入图片描述

这样会有什么问题呢?

  1. 当服务提供者增加节点时,需要修改配置文件
  2. 当其中一个服务提供者宕机时,服务消费者不能及时感知到,还会往宕机的服务发送请求

这个时候就得引入注册中心了

注册中心

Dubbo目前支持4种注册中心,(multicast zookeeper redis simple) 推荐使用Zookeeper注册中心,本文就讲一下用zookeeper实现服务注册和发现(敲黑板,又一种zookeeper的用处)
在这里插入图片描述

现在我们来看Dubbo官网对Dubbo的介绍图,有没有和我们上面画的很相似
在这里插入图片描述
节点角色说明

节点角色说明
Provider暴露服务的服务提供方
Consumer调用远程服务的服务消费方
Registry服务注册与发现的注册中心
Monitor统计服务的调用次数和调用时间的监控中心
Container服务运行容器

调用关系说明

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

要使用注册中心,只需要将provider.xml和consumer.xml更改为如下

<!--<dubbo:registry address="N/A"/>-->
<dubbo:registry protocol="zookeeper" address="192.168.11.129:2181"/>

如果zookeeper是一个集群,则多个地址之间用逗号分隔即可

<dubbo:registry protocol="zookeeper" address="192.168.11.129:2181,192.168.11.137:2181,192.168.11.138:2181"/>

把consumer.xml中配置的直连的方式去掉

<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<!--<dubbo:reference id="demoService" interface="com.st.DemoService"-->
                 <!--url="dubbo://localhost:20880/com.st.DemoService"/>-->


<dubbo:reference id="demoService" interface="com.st.DemoService"/>

注册信息在zookeeper中如何保存?

启动上面服务后,我们观察zookeeper的根节点多了一个dubbo节点及其他,图示如下
在这里插入图片描述
最后一个节点中192.168.1.104是小编的内网地址,你可以任务和上面配置的localhost一个效果,并且这个url中参数省略了,完整地址为。

dubbo://192.168.1.104:20880/com.st.DemoService?anyhost=true&application=helloworld-app&dubbo=2.5.3&interface=com.st.DemoService&methods=sayHello&pid=13360&side=provider&timestamp=1544257793335

大家可以想一下我为什么把最后一个节点标成绿色的。没错,最后一个节点是临时节点,而其他节点是持久节点,这样,当服务宕机时,这个节点就会自动消失,不再提供服务,服务消费者也不会再请求。如果部署多个DemoService,则providers下面就是DemoService的所有服务地址

其实一个zookeeper集群能被多个应用公用,如小编Storm集群和Dubbo配置的就是一个zookeeper集群,为什么呢?因为不同的框架会在zookeeper上建不同的节点,互不影响。如dubbo会创建一个/dubbo节点,storm会创建一个/storm节点,如图

在这里插入图片描述

参考博客

[1]http://dubbo.apache.org/zh-cn/

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐