JAVA —— Dubbo


1-今日菜单

  1. 分布式系统中的相关概念
  2. dubbo概述
  3. dubbo快速入门
  4. dubbo的高级特性

2-分布式系统中的相关概念

2.1-互联网项目架构

2.1.1 传统项目和互联网项目

传统项目:学校的选课系统,图书借阅系统

在这里插入图片描述

互联网项目对用户体验要求更高,从以下几个方面来衡量

  • 美观、功能、速度、稳定性
2.1.2 互联网项目架构-特点

从微信,支付宝来看:

  1. 用户多

  2. 流量大,并发高

  3. 海量数据

  4. 易受攻击

  5. 功能繁琐

  6. 需求变更快

2.2-互联网项目架构-目标

六大目标:

  • 高性能:提供快速的访问体验。

    衡量网站的几个性能指标:

    响应时间: 指执行一个请求从开始到最后收到响应数据所花费的总体时间。

    并发数: 指系统同时能处理的请求数量。

    • 并发连接数: 指的是客户端向服务器发起请求,并建立了TCP连接。每秒钟服务器连接的总TCP数量

    • 请求数: 也称为QPS(Query Per Second)指每秒多少请求。浏览器F12查看请求数

      请求数>=并发连接数

    • 并发用户数: 单位时间内有多少用户

    吞吐量: 指单位时间内系统能处理的请求数量。

    • 一个事务是指一个客户机向服务器发送请求然后服务器做出反应的过程。
    • TPS: Transactions Per Second每秒事务数。
    • 一个页面的一次访问,只会形成一 个TPS;并发连接数>= TPS
  • 高可用:网站服务一直可以正常访问。

  • 可伸缩:通过硬件增加/减少,提高/降低处理能力。

  • 高可扩展:系统间耦合低,方便的通过新增/移除方式,增加/减少新的功能/模块。

  • 安全性:提供网站安全访问和数据加密,安全存储等策略。

  • 敏捷性:随需应变,快速响应。

2.3-集群和分布式

2.3.1 集群和分布式概念

在这里插入图片描述

  • 集群:很多“人”一起 ,干一样的事。

    软件领域:一个软件,部署在多台服务器上。

在这里插入图片描述

  • 分布式:很多“人”一起干不一样的事,分工合作,就像工厂的流水线。这些不一样的事,合起来是一件大事。

    软件领域:一个大的业务系统,拆分为小的业务模块,分别部署在不同的机器上

在这里插入图片描述

2.3.2 项目应用

1. 原始项目

在这里插入图片描述

2. 集群项目

负载均衡是高可用网络基础架构的关键组件,通常用于将工作负载分布到多个服务器来提高网站、应用、数据库或其他服务的性能和可靠性。

访问淘宝时,根据用户所在地区跳转到不同的机房,提高响应速度,降低单个地区服务器的压力。

在这里插入图片描述

3. 集群分布式项目

淘宝购物:浏览商品(商品模块)是高频操作,相对而言下订单(订单模块)是低频操作

在这里插入图片描述

2.4-架构演进

2.4.1 单体架构

在这里插入图片描述

架构说明:全部功能集中在一个项目内(All in one)。

优点: 简单,开发部署都很方便,小型项目首选

缺点:

  • 项目启动慢
  • 可靠性差
  • 可伸缩性差
  • 扩展性和可维护性差
  • 性能低
2.4.2 垂直架构

垂直架构是指将单体架构中的多个模块拆分为多个独立的项目,形成多个独立的单体架构。

在这里插入图片描述

架构说明:按照业务进行切割,形成小的单体项目。

架构优点:解决了单体架构的缺点,并且技术栈可扩展(不同的系统可以用不同的编程语言编写)。

架构缺点:项目之间功能冗余、数据冗余、耦合性强。

2.4.3 分布式架构

分布式架构是指在垂直架构的基础上,将公共业务模块抽取出来,作为独立的服务供其他调用者消费,以实现服务的共享和重用。

在这里插入图片描述

什么是RPC?

RPC全称为remote procedure call,即远程过程调用。比如两台服务器A和B,A服务器上部署一个应用,B服务器上部署一个应用,A服务器上的应用想调用B服务器上的应用提供的方法,由于两个应用不在同一台服务器,所以需要使用网络进行访问。

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

分布式架构存在的问题: 服务提供方的IP或者端口一旦产生变更,所有调用他的模块都需要变更。

在这里插入图片描述

2.4.4 SOA架构

SOA: (Service- Oriented Architecture,面向服务的架构):是一个组件模型,它将应用程序的不同功能单元(称为服务)进行拆分,并通过这些服务之间定义良好的接口和契约联系起来。举例:课表

ESB: (Enterparise Servce Bus):企业服务总线,服务中介。主要是提供了一个服务于服务之间的交互。ESB包含的功能如:负载均衡,流量控制,加密处理,服务的监控,异常处理,监控告急等等。

在这里插入图片描述

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

2.4.5 微服务架构
  • 微服务架构是在SOA上做的升华,微服务架构强调的一个重点是“业务需要彻底的组件化和服务化”,原有的单个
    业务系统会拆分为多个可以独立开发、设计、运行的小应用。这些小应用之间通过服务完成交互和集成。
  • 微服务架构= 80%的SOA服务架构思想+ 100%的组件化架构思想+ 80%的领域建模思想

在这里插入图片描述

架构说明:

  • 将系统服务层完全独立出来,抽取为一个一个的微服务。
  • 抽取的粒度更细,遵循单一原则。
  • 如下是一个电子商城使用微服务架构后拆分出来的各个模块

在这里插入图片描述

特点:

  • 服务实现组件化:开发者可以自由选择开发技术,每个微服务可以使用不同语言进行开发
  • 服务之间交互一般使用HTTP REST风格开发
  • 去中心化:每个微服务有自己单独的数据库来保存业务数据
  • 自动化部署:把应用拆分成为一 个个独立的单个服务,方便自动化部署、测试、运维
2.4.6 架构演进

在这里插入图片描述

3-dubbo 概述

3.1 什么是RPC

RPC全称为remote procedure call,即远程过程调用。比如两台服务器A和B,A服务器上部署一个应用,B服务器上部署一个应用,A服务器上的应用想调用B服务器上的应用提供的方法,由于两个应用不在同一台服务器,所以需要使用网络进行访问。

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

3.2 Dubbo概念

●Dubbo是阿里巴巴公司开源的一个高性能、轻量级的Java RPC框架。

●致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案。

●官网: http://dubbo.apache.org/zh-cn/

在这里插入图片描述

节点角色说明:

●Container: 服务运行容器;学校

●Provider: 暴露服务的服务提供方;自习室

●Consumer: 调用远程服务的服务消费方;学员

●Registry: 服务注册与发现的注册中心;电子黑板

●Monitor:统计服务的调用次数和调用时间的监控中心;统计每天自习室人数

3.3 注册中心

Dubbo不能单独使用,需要配套的服务注册与发现的注册中心,常用的注册中心是Zookeeper,Nacos

3.4 支持的协议

  • Dubbo协议
  • HTTP协议
  • Redis协议
  • WebService协议
  • RMI协议
  • Hessian协议
  • Thrift协议
  • Memcached协议

4-dubbo快速入门

4.1 Zookeeper安装

参考资料中的zookeeper安装.md

4.2 dubbo入门总览

Dubbo作为一个RPC框架,其最核心的功能就是要实现跨网络的远程调用。

本小节就是要创建两个应用,一个作为服务的提供方,一个作为服务的消费方。

通过Dubbo来实现服务消费方远程调用服务提供方的方法。

在这里插入图片描述

4.2 案例基础制作

4.2.1 Service层开发
  1. 创建maven工程dubbo-service,在pom.xml文件中导入如下坐标

    <properties>
        <spring.version>5.1.9.RELEASE</spring.version>
        <dubbo.version>2.7.4.1</dubbo.version>
        <zookeeper.version>4.0.0</zookeeper.version>
    </properties>
    
    <dependencies>
        <!-- servlet3.0规范的坐标 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <!--spring的坐标-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        
        <!--springmvc的坐标-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
    
    </dependencies>
    
  2. 新建UserService和UserServiceImpl

    public interface UserService {
        String sayHello();
    }
    
    @Service//将该类的对象创建出来,放到Spring的IOC容器中  bean定义
    public class UserServiceImpl implements UserService {
    
        public String sayHello() {
            return "hello dubbo hello!~";
        }
    }
    
  3. 新建Spring配置文件:resources\spring\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: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">
    
    
       <context:component-scan base-package="com.xxx.service" />
    
    </beans>
    
  4. 新建日志配置文件:resources\log4j.properties

    # DEBUG < INFO < WARN < ERROR < FATAL
    # Global logging configuration
    log4j.rootLogger=info, stdout,file
    # My logging configuration...
    #log4j.logger.com.tocersoft.school=DEBUG
    #log4j.logger.net.sf.hibernate.cache=debug
    ## Console output...
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%5p %d %C: %m%n
    
    log4j.appender.file=org.apache.log4j.FileAppender
    log4j.appender.file.File=../logs/iask.log
    log4j.appender.file.layout=org.apache.log4j.PatternLayout
    log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss}  %l  %m%n
    
4.2.2 Controller层开发
  1. 创建maven工程(打包方式为war)dubbo-web,pom.xml配置和上面服务提供者相同,只需要添加tomcat插件

    <packaging>war</packaging>
    
    
    <properties>
        <spring.version>5.1.9.RELEASE</spring.version>
        <dubbo.version>2.7.4.1</dubbo.version>
        <zookeeper.version>4.0.0</zookeeper.version>
    </properties>
    
    <dependencies>
        <!-- servlet3.0规范的坐标 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <!--spring的坐标-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--springmvc的坐标-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
    
    </dependencies>
    
    
    <build>
        <plugins>
            <!--tomcat插件-->
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.1</version>
                <configuration>
                    <port>8000</port>
                    <path>/</path>
                </configuration>
            </plugin>
        </plugins>
    </build>
    
  2. 配置SpringMVC:resources\spring\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: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">
    
        <mvc:annotation-driven/>
        <context:component-scan base-package="com.xxx.controller"/>
    </beans>
    
  3. 配置web.xml:webapp\WEB-INF\web.xml,配置监听器保证Tomcat启动时加载Spring配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns="http://java.sun.com/xml/ns/javaee"
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
             version="2.5">
    
    	<!-- 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>
    		 
    	<!-- Springmvc -->	 
        <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:spring/springmvc.xml</param-value>
            </init-param>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>springmvc</servlet-name>
            <url-pattern>*.do</url-pattern>
        </servlet-mapping>
    
    </web-app>
    

    注意:DispatcherServlet的第二种拦截配置方式:*.do

    表示只拦截URL以.do结尾的,比如http://localhost:8000/user/sayHello.do

  4. 编写UserController

    @RestController
    @RequestMapping("/user")
    public class UserController {
    
        //注入Service
        @Autowired//本地注入
        private UserService userService;
    
        @RequestMapping("/sayHello")
        public String sayHello(){
            return userService.sayHello();
        }
    
    }
    
    
  5. 对dubbo-service执行mvn install

  6. 对dubbo-web执行tomcat7:run

  7. 访问http://localhost:8000/user/sayHello.do

4.3服务提供者

在这里插入图片描述

  1. 添加Dubbo、Zookeeper和日志的引用

    <!--日志-->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.21</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.21</version>
    </dependency>
    
    <!--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>
    
    
  2. 在dubbo-service工程中修改src/main/resources/applicationContext.xml ,确保引入dubbo命名空间

    <?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://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">
    
    
        <!--dubbo的配置-->
        <!--1.配置项目的名称,唯一-->
        <!-- 当前应用名称,用于注册中心计算应用间依赖关系,注意:消费者和提供者应用名不要一样 -->
        <dubbo:application name="dubbo-service"/>
        <!--2.配置注册中心的地址-->
        <!-- 连接服务注册中心zookeeper ip为zookeeper所在服务器的ip地址-->
        <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
        <!--3.配置dubbo包扫描-->
        <dubbo:annotation package="com.xxx.service.impl" />
        
    </beans>
    
    

    如果要在Dubbo Admin中进行测试,需要添加元数据配置

    <!--开启Dubbo-admin的元数据显示-->
    <!-- 元数据配置 -->
    <dubbo:metadata-report address="zookeeper://127.0.0.1:2181" />
    
    
  3. 修改UserServiceImpl中的注解

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

    import org.apache.dubbo.config.annotation.Service;
    
    
    @Service//将这个类提供的方法(服务)对外发布。将访问的地址 ip,端口,路径注册到注册中心中
    public class UserServiceImpl implements UserService {
    
        public String sayHello() {
            return "hello dubbo hello!~";
        }
    }
    
    
  4. 将模块改为web工程 -> 修改dubbo-service的pom.xml:打包为war,添加tomcat7插件

    <packaging>war</packaging>
    
    
    <build>
        <plugins>
            <!--tomcat插件-->
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.1</version>
                <configuration>
                    <port>8002</port>
                    <path>/</path>
                </configuration>
            </plugin>
        </plugins>
    </build>
    
    
  5. 在src\main目录下添加webapp\WEB-INF\web.xml,保留Spring提供的监听器

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns="http://java.sun.com/xml/ns/javaee"
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
             version="2.5">
    
       <!-- 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>
    
    </web-app>
    
    
  6. 启动测试:tomcat7:run,启动后自动注册UserService服务到Zookeeper中

4.4服务消费者

  1. 将UserService接口提取到dubbo-interface模块中

在这里插入图片描述

  1. dubbo-service和dubbo-web的pom.xml中引入dubbo-interface

    <!--依赖公共的接口模块-->
    <dependency>
        <groupId>com.xxx</groupId>
        <artifactId>dubbo-interface</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    
    
  2. 添加Dubbo、Zookeeper和日志的引用

    <!--日志-->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.21</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.21</version>
    </dependency>
    
    <!--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>
    
    
  3. 在dubbo-web工程中添加Dubbo配置:src/main/resources/spring/springmvc.xml

    <!--dubbo的配置-->
    <!--1.配置项目的名称,唯一-->
    <!-- 当前应用名称,用于注册中心计算应用间依赖关系,注意:消费者和提供者应用名不要一样 -->
    <dubbo:application name="dubbo-web" />
    <!--2.配置注册中心的地址-->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
    <!--3.配置dubbo包扫描-->
    <!-- 包扫描的方式 引用服务 扫描@Reference -->
    <dubbo:annotation package="com.xxx.controller" />
    
    
  4. 修改UserController

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

    import org.apache.dubbo.config.annotation.Reference;
    
    
    @RestController
    @RequestMapping("/user")
    public class UserController {
    
        /*
            1. 从zookeeper注册中心获取userService的访问url
            2. 进行远程调用RPC
            3. 将结果封装为一个代理对象。给变量赋值
         */
    
        @Reference//远程注入
        private UserService userService;
    
    
        @RequestMapping("/sayHello")
        public String sayHello(){
            return userService.sayHello();
        }
    
    }
    
    
  5. 启动dubbo-service和dubbo-web测试:tomcat7:run http://localhost:8000/user/sayHello.do

  6. 最终执行过程如下

在这里插入图片描述

5-dubbo高级特性

5.1-dubbo-admin安装

在这里插入图片描述

dubbo- admin

​ ●dubbo-admin管理平台,是图形化的服务管理页面

​ ●从注册中心中获取到所有的提供者 /消费者进行配置管理

​ ●路由规则、动态配置、服务降级、访问控制、权重调整、负载均衡等管理功能

​ ●dubbo- admin是一个前后端分离的项目。前端使用vue,后端使用springboot

​ ●安装dubbo-admin其实就是部署该项目

具体安装参见:dubbo-admin.md

5.2-dubbo-admin使用

具体使用参见:dubbo-admin.md

5.3-序列化

  1. dubbo 内部已经封装序列化和反序列化的过程
  2. 我们只需要在定义pojo类时实现Serializable接口即可
  3. 一般会定义一 个公共的pojo模块,让生产者和消费者都依赖该模块。

在这里插入图片描述

易错点:User对象未实现serializable接口

错误信息:

在这里插入图片描述

解决办法:

public User implements Serializable {
    private String name;
    private int age;
}

5.4-地址缓存

注册中心挂了,服务是否可以正常访问?

  1. 可以,因为dubbo服务消费者在第一次调用时,会将服务提供方地址缓存到本地,以后再调用则不会访问注册中心。
  2. 当服务提供者地址发生变化时,注册中心会通知服务消费者。

在这里插入图片描述

5.5-超时

在这里插入图片描述

  • 服务消费者在调用服务提供者的时候发生了阻塞、等待的情形,这个时候,服务消费者会直等待下去。

  • 在某个峰值时刻,大量的请求都在同时请求服务消费者,会造成线程的大量堆积,势必会造成雪崩。

  • 支付宝用户查询余额

  • dubbo利用超时机制来解决这个问题:设置一个超时时间, 在这个时间段内,无法完成服务则自动断开连接。

  • 使用timeout属性配置超时时间,默认值1000,单位毫秒

    //timeout 超时时间 单位毫秒  retries 重试次数
    @Service(timeout = 3000,retries=0)
    
    

    注意:超时时间建议配置在服务提供方

演示效果:

  1. 模拟查询用户超时:UserServiceImpl

    @Service(timeout = 3000, retries = 0)//当前服务3秒超时
    public class UserServiceImpl implements UserService {
    
        public String sayHello() {
            return "hello dubbo hello!~";
        }
    
        public User findUserById(int id) {
            //查询User对象
            User user = new User(1, "zhangsan", "123");
    
            try {
                //数据库查询很慢,查了5秒
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return user;
        }
    }
    
    
  2. 查看是否3秒超时抛出异常:UserController

    @RestController
    @RequestMapping("/user")
    public class UserController {
    
        @Reference //远程注入
        private UserService userService;
    
        @RequestMapping("/sayHello")
        public String sayHello() {
            return userService.sayHello();
        }
    
        /**
         * 根据id查询用户信息
         *
         * @param id
         * @return
         */
        int i = 1;
    
        @RequestMapping("/find")
        public User find(int id) {
            new Thread(new Runnable() {
                public void run() {
                    while (true) {
                        System.out.println(i++);
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }).start();
    
            return userService.findUserById(id);
        }
    
    }
    
    
  3. 访问http://localhost:8000/user/find.do?id=1
    在这里插入图片描述

5.6-重试

在这里插入图片描述

  1. 设置了超时时间,在这个时间段内,无法完成服务访问,则自动断开连接。

  2. 如果出现网络抖动,则这一次请求就会失败。

  3. Dubbo提供重试机制来避免类似问题的发生。

  4. 通过retries属性来设置重试次数。默认为2次

    //timeout 超时时间 单位毫秒  retries 重试次数
    @Service(timeout = 3000, retries=3)
    
    

5.7-多版本

在这里插入图片描述

灰度发布: 当出现新功能时,会让一部分用户先使用新功能,用户反馈没问题时,再将所有用户迁移到新功能。

dubbo中使用version属性来设置和调用同一个接口的不同版本

生产者配置

@Service(version="v2.0")
public class UserServiceImp12 implements UserService {...}

消费者配置

@Reference(version = "v2.0")//远程注入
private UserService userService;

5.8-负载均衡(了解)

Dubbo提供4种负载均衡策略 :

Random: 按权重随机,默认值。按权重设置随机概率。
在这里插入图片描述

RoundRobin: 按权重轮询。

LeastActive: 最少活跃调用数,相同活跃数的随机。

在这里插入图片描述

ConsistentHash: 一 致性Hash,相同参数的请求总是发到同一提供者。

在这里插入图片描述

服务提供者配置

@Service(weight = 100)
public class UserServiceImp12 implements UserService {...}

application.xml 配置parameter key

在这里插入图片描述

消费者配置

//@Reference(loadbalance = "roundrobin")
//@Reference(loadbalance = "leastactive")
//@Reference(loadbalance = "consistenthash")
@Reference(loadbalance = "random")//默认 按权重随机
private UserService userService;

5.9-集群容错(了解)

Dubbo提供6种集群容错模式:
Failfast Cluster : 失败重试,默认容错模式。当出现失败时,重试其它服务器,默认重试2次,使用retries配置。
Failfast Cluster : 快速失败,发起一次调用,失败立即报错。通常用于写操作。
Failsafe Cluster: 失败安全,出现异常时,直接忽略。返回一个空结果。
Failback Cluster: 失败自动恢复,后台记录失败请求,定时重发。
Forking Cluster : 并行调用多个服务器,只要一个成功即返回。
Broadcast Cluster: 广播调用所有提供者,逐个调用,任意一台报错则报错。

在这里插入图片描述

消费者配置

@Reference(cluster = "failover")//远程注入
private UserService userService;

5.10-服务降级

服务降级:当服务器压力剧增的情况下,根据实际业务情况及流量,对一些服务和页面有策略的不处理或换种简单的方式处理,从而释放服务器资源以保证核心交易正常运作或高效运作

服务降级方式:
mock= force:return null:表示消费方对该服务的方法调用都直接返回null值,不发起远程调用。用来屏蔽不重要服务不可用时对调用方的影响。

mock=fail:return null:表示消费方对该服务的方法调用在失败后,再返回null值,不抛异常。用来容忍不重要服务不稳定时对调用方的影响

在这里插入图片描述

消费方配置

//远程注入
@Reference(mock =“ force:return null")//不再调用userService的服务
private UserService userService;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

改变世界的李

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值