Dubbo(分布式RPC框架)——入门

目录

1、软件架构的演进过程

1.1、单体架构

1.2、垂直架构

1.3、SOA架构

1.4、微服务架构

2、Apache Dubbo概述

2.1、Dubbo简介

2.2、Dubbo架构图

3、服务注册中心Zookeeper

3.1、Zookeeper介绍

3.2、Zookeeper安装

4、Dubbo快速入门

4.1、服务提供方开发

4.2、配置web.xml文件

4.3、创建服务接口

4.4、applicationContext.xml配置文件

4.5、服务发现者开发


1、软件架构的演进过程

1.1、单体架构

架构说明:

全部功能集中在一个项目内(All in one)。

架构优点:

架构简单,前期开发成本低、开发周期短,适合小型项目。

架构缺点:

全部功能集成在一个工程中,对于大型项目不易开发、扩展和维护。

技术栈受限,只能使用一种语言开发。

系统性能扩展只能通过扩展集群节点,成本高。

1.2、垂直架构

架构说明:

按照业务进行切割,形成小的单体项目。

架构优点:

技术栈可扩展(不同的系统可以用不同的编程语言编写)。

架构缺点:

功能集中在一个项目中,不利于开发、扩展、维护。

系统扩张只能通过集群的方式。

项目之间功能冗余、数据冗余、耦合性强。

1.3、SOA架构

SOA全称为Service-Oriented Architecture,即面向服务的架构。它可以根据需求通过网络对松散耦合的粗粒度应用组件(服务)进行分布式部署、组合和使用。一个服务通常以独立的形式存在于操作系统进程中。

站在功能的角度,把业务逻辑抽象成可复用的服务,通过服务的编排实现业务的快速再生,目的:把原先固有的业务功能转变为通用的业务服务,实现业务逻辑的快速复用。

架构说明:

将重复功能或模块抽取成组件的形式,对外提供服务,在项目与服务之间使用ESB(企业服务总线)的形式作为通信的桥梁。

架构优点:

重复功能或模块抽取为服务,提高开发效率。

可重用性高。

可维护性高。

架构缺点:

各系统之间业务不同,很难确认功能或模块是重复的。

抽取服务的粒度大。

系统和服务之间耦合度高。

1.4、微服务架构

架构说明:

将系统服务层完全独立出来,抽取为一个一个的微服务。

抽取的粒度更细,遵循单一原则。

采用轻量级框架协议传输。

架构优点:

服务拆分粒度更细,有利于提高开发效率。

可以针对不同服务制定对应的优化方案。

适用于互联网时代,产品迭代周期更短。

架构缺点:

粒度太细导致服务太多,维护成本高。

分布式系统开发的技术成本高,对团队的挑战大。

2、Apache Dubbo概述

2.1、Dubbo简介

Apache Dubbo是一款高性能的Java RPC框架。其前身是阿里巴巴公司开源的一个高性能、轻量级的开源Java RPC框架,可以和Spring框架无缝集成。

什么是RPC?

RPC全称为remote procedure call,即远程过程调用。比如两台服务器A和B,A服务器上部署一个应用,B服务器上部署一个应用,A服务器上的应用想调用B服务器上的应用提供的方法,由于两个应用不在一个内存空间,不能直接调用,所以需要通过网络来表达调用的语义和传达调用的数据。

需要注意的是RPC并不是一个具体的技术,而是指整个网络远程调用过程。

RPC是一个泛化的概念,严格来说一切远程过程调用手段都属于RPC范畴。各种开发语言都有自己的RPC框架。Java中的RPC框架比较多,广泛使用的有RMI、Hessian、Dubbo等。

Dubbo提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现。

2.2、Dubbo架构图

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

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

调用关系说明:

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

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

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

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

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

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

3、服务注册中心Zookeeper

3.1、Zookeeper介绍

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

流程说明:

  • 服务提供者(Provider)启动时: 向 /dubbo/com.foo.BarService/providers 目录下写入自己的 URL 地址

  • 服务消费者(Consumer)启动时: 订阅 /dubbo/com.foo.BarService/providers 目录下的提供者 URL 地址。并向 /dubbo/com.foo.BarService/consumers 目录下写入自己的 URL 地址

  • 监控中心(Monitor)启动时: 订阅 /dubbo/com.foo.BarService 目录下的所有提供者和消费者 URL 地址

3.2、Zookeeper安装

链接:https://pan.baidu.com/s/1ewbnN4PN99cL0oeqeOONMg 
提取码:py22

解压:打开zookeeper\apache-zookeeper-3.5.5-bin\conf,复制zoo_sample.cfg 并粘贴到当前目录下,命名zoo.cfg。

编辑zoo.cfg.修改如下配置:

#指定数据文件存放的目录

dataDir= E:/zookeeper/data    

#指定日志存放的目录

dataLogDir= E:/zookeeper/log

进入zookeeper\apache-zookeeper-3.5.5-bin\bin双击zkServer.cmd即可运行zookeeper

4、Dubbo快速入门

4.1、服务提供方开发

(1)创建maven-web工程,项目名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.8.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>

4.2、配置web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
		  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
           version="3.0">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

</web-app>

4.3、创建服务接口

public interface HelloService {
    public String hello(String name);
}
@Service//dubbo的注解  服务发布  基于的接口类对象
public class HelloServiceImpl implements HelloService {
    @Override
    public String hello(String name) {
        System.out.println("端口号为8082的provider被调用了");
        return "hello " + name;
    }
}

注意:@Service一定要导入dubbo包下的

import com.alibaba.dubbo.config.annotation.Service;

4.4、applicationContext.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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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://www.springframework.org/schema/aop
 	http://www.springframework.org/schema/aop/spring-aop.xsd
 	http://www.springframework.org/schema/context
 	http://www.springframework.org/schema/context/spring-context.xsd
 	http://www.springframework.org/schema/tx
 	http://www.springframework.org/schema/tx/spring-tx.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">

    <!--配置应用名-->
    <dubbo:application name="dubbo_provider"/>
    <!--配置注册中心地址-->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
    <!--配置协议和端口-->
    <dubbo:protocol name="dubbo" port="20880"/>
    <!--包扫描:让dubbo注解起作用-->
    <dubbo:annotation package="cn.itssl"/>
    
</beans>

4.5、服务发现者开发

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

(2)配置web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
		  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
           version="3.0">
    <servlet>
        <servlet-name>s1</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>s1</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

(3)将服务提供者工程中的HelloService接口复制到当前工程

(4)编写Controller

注意:@Reference也必须导dubbo包下的

@RestController
public class HelloController {
    @Reference//服务发现  
    private HelloService helloService;

    //解决中文乱码
    @GetMapping(value = "/hello/{name}", produces = "text/html;charset=utf-8")
    public String hello(@PathVariable String name) {
        return helloService.hello(name);
    }
}

创建配置文件springmvc.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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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://www.springframework.org/schema/aop
 	http://www.springframework.org/schema/aop/spring-aop.xsd
 	http://www.springframework.org/schema/context
 	http://www.springframework.org/schema/context/spring-context.xsd
 	http://www.springframework.org/schema/tx
 	http://www.springframework.org/schema/tx/spring-tx.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">

    <context:component-scan base-package="cn.itssl"/>
    <mvc:annotation-driven/>
    <dubbo:application name="dubbo_consumer"/>
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
    <dubbo:annotation package="cn.itssl"/>
  
</beans>

猜你喜欢

转载自blog.csdn.net/select_myname/article/details/128336063