Dubbo框架学习笔记

时间:2021-10-23
本文章向大家介绍Dubbo框架学习笔记,主要包括Dubbo框架学习笔记使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Dubbo介绍

Dubbo是什么?

Dubbo 是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,可以和 Spring 框架无缝集成。
Dubbo是一款高性能、轻量级的开源 Java RPC 框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现。

RPC是什么

RPC(Remote Procedure Call)是指远程过程调用,是一种进程间通信方式,它是一种技术的思想,而不是规范。即允许服务器A通过网络调用服务器B上的过程或函数

Dubbo架构

Dubbo架构图(Dubbo官方提供)如下:

节点角色说明:

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

虚线都是异步访问,实线都是同步访问 蓝色虚线:在启动时完成的功能 红色虚线(实线)都是程序运行过程中执行的功能

调用关系说明

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

服务注册中心Zookeeper

通过前面的Dubbo架构图可以看到,Registry(服务注册中心)在其中起着至关重要的作用。Dubbo官
方推荐使用Zookeeper作为服务注册中心。

Zookeeper的介绍

Zookeeper 是一个分布式的,开放源码的分布式应用程序协调服务,是 Google 的 Chubby 一个开源的实现,是 Hadoop 和 Hbase 的重要组件。它是一个为分布式应用提供一致性服务的软件,提供的功能包括:配置维护、名字服务、分布式同步、组服务等。

Linux 安装 Zookeeper

下载地址:http://archive.apache.org/dist/zookeeper/
安装步骤:

  • 第一步:安装 jdk(略)
  • 第二步:把 Zookeeper 的压缩包(zookeeper-3.4.6.tar.gz)上传到 linux 系统
  • 第三步:解压缩压缩包 tar -zxvf zookeeper-3.4.6.tar.gz -C /usr/local/
  • 第四步:进入zookeeper-3.4.6目录,创建data目录 mkdir data
  • 第五步:进入conf目录 ,把 zoo_sample.cfg 改名为 zoo.cfg
cd conf
mv zoo_sample.cfg zoo.cfg
  • 第六步:打开zoo.cfg文件, 修改data属性:dataDir=/usr/local/zookeeper 3.4.6/data

Dubbo快速入门

服务提供方开发

  1. 创建maven工程(打包方式为war)dubbo_provider,在 pom.xml 文件中导入如下坐标
 <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <spring.version>5.0.5.RELEASE</spring.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- dubbo相关 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.7</version>
        </dependency>
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>
        <dependency>
            <groupId>javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.12.1.GA</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.47</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <configuration>
                    <!-- 指定端口 -->
                    <port>8081</port>
                    <!-- 请求路径 -->
                    <path>/</path>
                </configuration>
            </plugin>
        </plugins>
    </build>
  1. 配置 web.xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:dubbo-provider.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>
  1. 创建服务接口
public interface TestService {
    String hello(String name);
}
  1. 创建服务实现类
@Service
public class TestServiceImpl implements TestService {
    @Override
    public String hello(String name) {
        return "Hello," + name;
    }
}

注意:服务实现类上使用的Service注解是Dubbo提供的,用于对外发布服务

  1. 在 src/main/resources 下创建 dubbo-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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
        xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        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://code.alibabatech.com/schema/dubbo
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 当前应用名称,用于注册中心计算应用间依赖关系,注意:消费者和提供者应用名不要一样 -->
    <dubbo:application name="dubbo_provider" />
    <!-- 连接服务注册中心zookeeper ip为zookeeper所在服务器的ip地址-->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
    <!-- 扫描指定包,加入@Service注解的类会被发布为服务 -->
    <dubbo:annotation package="com.fgba.service.impl" />
    <!-- 注册 协议和port -->
    <dubbo:protocol name="dubbo" port="20880" />
</beans>
  1. 启动服务

    tomcat7:run

服务消费方开发

  1. 创建 maven 工程(打包方式为 war)dubbo_consumer,pom.xml 配置和上面服务提供者相同,只需要将 Tomcat 插件的端口号改为8082即可

  2. 配置web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

   <servlet>
       <servlet-name>springmvc</servlet-name>
       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
       <!-- 指定加载的配置文件 ,通过参数contextConfigLocation加载 -->
       <init-param>
           <param-name>contextConfigLocation</param-name>
           <param-value>classpath:dubbo-consumer.xml</param-value>
       </init-param>
   </servlet>

    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

  1. 将服务提供者工程中的 TestService 接口复制到当前工程

  2. 编写 Controller

@Controller
@RequestMapping("/test")
public class TestController {
    @Reference
    private TestService testService;

    @RequestMapping("/hello")
    @ResponseBody
    public String getName(String name){
        //远程调用
        String result = testService.hello(name);
        System.out.println(result);
        return result;
    }
}

注意:Controller中注入TestService使用的是Dubbo提供的@Reference注解

  1. 在 src/main/resources 下创建 dubbo-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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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://code.alibabatech.com/schema/dubbo
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 当前应用名称,用于注册中心计算应用间依赖关系,注意:消费者和提供者应用名不要一样 -->
    <dubbo:application name="dubbo-consumer" />
    <!-- 连接服务注册中心zookeeper ip为zookeeper所在服务器的ip地址-->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
    <!-- 扫描的方式暴露接口 -->
    <dubbo:annotation package="com.fgba.controller" />
    <!--  扫描@Controller注解  -->
    <context:component-scan base-package="com.fgba.controller" />

</beans>
  1. 运行测试

    tomcat7:run 启动

    在浏览器输入http://localhost:8082/test/hello?name=Jack,查看浏览器输出结果

Dubbo管理控制台

我们在开发时,需要知道Zookeeper注册中心都注册了哪些服务,有哪些消费者来消费这些服务。我们 可以通过部署一个管理中心来实现。其实管理中心就是一个web应用,部署到tomcat即可

安装步骤

  1. 将 dubbo-admin-2.6.0.war 文件复制到 tomcat 的 webapps 目录下

  2. 启动 tomcat,此 war 文件会自动解压

  3. 修改 WEB-INF 下的 dubbo.properties 文件,注意 dubbo.registry.address 对应的值需要对应当前使用的 Zookeeper 的 ip 地址和端口号

    dubbo.registry.address=zookeeper://localhost:2181
    dubbo.admin.root.password=root
    dubbo.admin.guest.password=guest 
    
  4. 重启tomcat

使用步骤

  1. 访问 http://localhost:8080/dubbo-admin-2.6.0/ ,输入用户名(root)和密码(root)
  1. 启动服务提供者工程和服务消费者工程,可以在查看到对应的信息

总结

Dubbo 是一个服务框架,其作用是将不同服务器上的服务进行整合,所以一般应用于分布式环境下。它在分布式环境下通过网络完成各个模块之间的调用,通过注册中心来管理服务提供方和服务消费方。

原文地址:https://www.cnblogs.com/fgba/p/15449233.html