Dubbo是什么?核心总结

Dubbo

——是SOA架构的具体的实现框架!

2.1 Dubbo简介

Apache Dubbo是一款高性能的Java RPC框架。官网地址:[http://dubbo.apache.org]

  • dubbo由阿里开发,2018年捐献给Apache基金会

  • RPC全称为remote procedure call,即远程过程调用, 它可以让我们调用本地方法一样来调用远程方法

2.2 Dubbo架构 (重点)

 

 节点角色说明:

 

 

调用关系说明:

  1. 服务容器负责启动,加载,运行服务提供者。

  2. 服务提供者在启动时,向注册中心注册自己提供的服务。

  3. 服务消费者在启动时,向注册中心订阅自己所需的服务。

  4. 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。

  5. 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。

  6. 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。

面试相关:

  1. 服务消费者和注册中心是基于推模式还是拉模式获取服务的。

  2. 如果服务注册中心宕机了, 会不会影响到服务的正常调用

  3. 在dubbo的各个服务组件调用中中, 什么地方用的是长连接, 什么地方用的是短连接

2.3 Dubbo入门(重点)

2.3.1 服务注册中心

注册中心负责服务地址的注册与查找,服务提供者和消费者与注册中心交互。

Dubbo支持的服务注册中心有很多,比如zookeeper、redis等等,官方推荐使用 zookeeper 注册中心。

2.3.2 zookeeper本地安装

——课上安装在linux中,为了使用方便,也可以本地安装

1)安装

将zookeeper-3.4.6.zip复制到一个没有中文,没有空格的目录,然后解压,即安装成功!

2)配置

进入到zookeeper的配置目录conf下,复制zoo_sample.cfgzoo.cfg

3)启动

进入到zookeeper的bin目录下,双击zkServer.cmd启动服务!

 

2.3.3 dubbo服务提供者

——参考课上视频资料

1)坐标

<dubbo.version>2.7.4.1</dubbo.version>
<zookeeper.version>4.0.0</zookeeper.version>

<!--Dubbo的起步依赖,版本2.7之后统一为org.apache.dubbo -->
<dependency>
    <groupId>org.apache.dubbo</groupId>
    <artifactId>dubbo</artifactId>
    <version>${dubbo.version}</version>
</dependency>
<!--ZooKeeper客户端实现 -->
<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-framework</artifactId>
    <version>${zookeeper.version}</version>
</dependency>
<!--ZooKeeper客户端实现 -->
<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-recipes</artifactId>
    <version>${zookeeper.version}</version>
</dependency>

dubbo版本2.7+,连接zookeeper客户端是curator;

dubbo版本2.6-,zookeeper客户端是zkClient。

2)配置文件

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns:context="http://www.springframework.org/schema/context"
       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
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 连接zoo,声明我的服务名,将服务进行注册 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
    <dubbo:application name="dubbo-service"/>
    <dubbo:annotation package="com.itheima.service.impl" />
    
    <!-- dubbo需要web容器(tomcat8080)进行启动;dubbo对外公布dubbo服务,占用服务端口 -->
    <dubbo:protocol port="20880"/>
</beans>

dubbo版本不同,会导致引入的xml约束文件也不相同!

2.7+版本:

xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"

http://dubbo.apache.org/schema/dubbo
http://dubbo.apache.org/schema/dubbo/dubbo.xsd

2.6-版本:

xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd

需要提供一个web.xml,加载该配置文件——利用spring初始化!

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:spring/applicationContext*.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

3)业务代码+注解

接口

public interface UserService {
    public String sayHello();
}

实现类

@org.apache.dubbo.config.annotation.Service
public class UserServiceImpl implements UserService {

    public String sayHello() {
        return "hello dubbo hello!~";
    }
}

4)启动服务

tomcat7插件

 

2.3.4 dubbo服务消费者

——参考课上视频资料

1)依赖

<!--Dubbo的起步依赖,版本2.7之后统一为rg.apache.dubb -->
<dependency>
    <groupId>org.apache.dubbo</groupId>
    <artifactId>dubbo</artifactId>
    <version>${dubbo.version}</version>
</dependency>
<!--ZooKeeper客户端实现 -->
<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-framework</artifactId>
    <version>${zookeeper.version}</version>
</dependency>
<!--ZooKeeper客户端实现 -->
<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-recipes</artifactId>
    <version>${zookeeper.version}</version>
</dependency>

2)配置文件

----连接zoo,要服务接口

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
         http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
    <dubbo:application name="dubbo-web" >
        <dubbo:parameter key="qos.port" value="33333"/>
    </dubbo:application>
    <!-- 包扫描:加载bean;识别@Reference注解,远程注入!-->
    <dubbo:annotation package="com.itheima.controller" />
</beans>

3)调用业务代码

@RestController
@RequestMapping("/user")
public class UserController {
    @Reference//远程注入
    private UserService userService;

    @RequestMapping("/sayHello")
    public String sayHello(){
        return userService.sayHello();
    }
}

4)启动调用

tomcat7:run

 

2.3.5 统一接口优化

——参考课上视频资料

——基于maven依赖某模块完成,利用依赖传递

2.4 dubbo-admin

——只作为监控服务来看待,不影响dubbo的正常服务调用!

1)安装

将提供的dubbo-admin.war放置到tomcat的webapps目录下,启动tomcat即可!如果zookeeper位置不是本地,则需要修改配置文件:

2)使用

三、Dubbo高级

——以下部分用于面试

序列化

dubbo见传递对象,该对象需要实现序列化接口

服务超时

 

多版本(重点)

负载均衡

容错

服务降级

启动检查

启动时检查,配置在服务消费者一方,用于服务消费者在启动的时候主动检查注册中心或者服务提供者是否准备好提供服务

如果配置为false,代表不检查

如果配置为true,代表检查,一旦检查到服务提供者未准备好,就会直接抛异常

建议在开发阶段设置为false,在生产环境下改为true(默认)

dubbo: consumer: check: false registry: check: false

posted @ 2022-03-10 14:03  jiuchengi  阅读(468)  评论(0编辑  收藏  举报