Skip to content

Latest commit

 

History

History
903 lines (647 loc) · 28.8 KB

65.Dubbo.md

File metadata and controls

903 lines (647 loc) · 28.8 KB

Dubbo

1、Dubbo概述

1、Dubbo概念

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

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

  • 官网:http:l/dubbo.apache.org

2、Dubbo架构

image-20220628111116317

节点角色说明:

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

运行流程:

Dubbo Architecture							init(初始化) async(异步调用) sync(同步调用)
========================================================================================

Consumer(容器)
  ||	服务提供者应运行在一个容器中
0.start--init	启动	(开启服务)	 
  ||	即提供者在容器中启动
Provider(提供者)
  ||	服务提供者应当在注册中心去注册服务
1.register--init	注册	(使服务可见)  
  ||	提供者将相关信息提供给注册中心方便提供服务
Registry(注册中心)

========================================================================================

----------------------------------------------------------------------------------------
Container(消费者)	
----------------------------------------------------------------------------------------
  ||	消费者要使用服务时向注册中心订阅服务 		 ||		接收转发的服务信息
2.subscribe--init	订阅	(请求调用服务)	   3.notify--async	通知	
  ||     接收订阅信息请求                   	  ||	注册中心返回消费者所需的相关服务消息
----------------------------------------------------------------------------------------
Registry(注册中心)
----------------------------------------------------------------------------------------

========================================================================================

Container(消费者)
  ||	通过注册中心返回的信息来调用提供者服务
4.invoke--sync	请求	(RPC过程)
  ||	接收信息提供相关服务
Provider(提供者)  

========================================================================================

Provider(提供者)
  ||
5.count--async	统计服务的调用次数和调用时间等等
  ||
Monitor(管理者)
  ||
5.count--async 统计服务的调用次数和调用时间等等
  ||
Container(消费者)

2、Dubbo快速入门

1、Zookeeper安装

Dubbo官方推荐使用Zookeeper作为Registry(注册中心)

1.1 下载安装

1、环境准备

ZooKeeper服务器是用Java创建的,它运行在JVM之上。需要安装JDK 7或更高版本。

2、上传

将下载的ZooKeeper放到/opt/ZooKeeper目录下

#上传zookeeper alt+p
put f:/setup/apache-zookeeper-3.5.6-bin.tar.gz
#打开 opt目录
cd /opt
#创建zooKeeper目录
mkdir  zooKeeper
#将zookeeper安装包移动到 /opt/zooKeeper
mv apache-zookeeper-3.5.6-bin.tar.gz /opt/zookeeper/

3、解压

将tar包解压到/opt/zookeeper目录下

tar -zxvf apache-zookeeper-3.5.6-bin.tar.gz 

1.2 配置启动

1、配置zoo.cfg

进入到conf目录拷贝一个zoo_sample.cfg并完成配置

#进入到conf目录
cd /opt/zooKeeper/apache-zookeeper-3.5.6-bin/conf/
#拷贝
cp  zoo_sample.cfg  zoo.cfg

修改zoo.cfg

#打开目录
cd /opt/zooKeeper/
#创建zooKeeper存储目录
mkdir  zkdata
#修改zoo.cfg
vim /opt/zooKeeper/apache-zooKeeper-3.5.6-bin/conf/zoo.cfg

1577548250377

修改存储目录:dataDir=/opt/zookeeper/zkdata

2、启动ZooKeeper

cd /opt/zooKeeper/apache-zookeeper-3.5.6-bin/bin/
#启动
 ./zkServer.sh  start

1577548052037

看到上图表示ZooKeeper成功启动

3、查看ZooKeeper状态

./zkServer.sh status

zookeeper启动成功。standalone代表zk没有搭建集群,现在是单节点

1577548175232

zookeeper没有启动

1577548112773

注意:连接时如果开了zookeeper但无法连接上时,记得关闭防火墙

1)查看防火墙状态:

service iptables status

2)开启防火墙:

service iptables start

3)关闭防火墙:

service iptables stop

4)重启防火墙:

service iptables restart

5)永久关闭防火墙:

chkconfig iptables off

6)永久关闭后重启:

chkconfig iptables on

2、实现步骤

  1. 创建服务提供者Provider模块
  2. 创建服务消费者Consumer模块
  3. 在服务提供者模块编写UserServicelmpl提供服务
  4. 在服务消费者中的UserController远程调用
  5. UserServicelmpl提供的服务
  6. 分别启动两个服务,测试

image-20220628133943783

1、创建服务提供者Provider模块

创建一个Maven项目(dubbo-service)作为Provider模块

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.gewuyou</groupId>
    <artifactId>dubbo-service</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <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>

        <!--日志-->
        <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之后统一为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>
        <!--依赖公共的接口模块-->
        <dependency>
            <groupId>com.gewuyou</groupId>
            <artifactId>dubbo-interface</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!--tomcat插件-->
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.1</version>
                <configuration>
                    <port>9000</port>
                    <path>/</path>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
web.xml
<?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>
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">

   <!--这个包交给Dubbo扫描了,所以不需要Spring扫描-->
   <!--<context:component-scan base-package="com.gewuyou.service" />-->
   <!--dubbo的配置-->
   <!--1、配置项目的名称 必须唯一-->
   <dubbo:application name="dubbo-service"/>
   <!--2、配置注册中心的地址-->
   <dubbo:registry address="zookeeper://192.168.216.135:2181"/>
   <!--3、配置dubbo的包扫描-->
   <dubbo:annotation package="com.gewuyou.service.impl"/>
</beans>

2、创建服务消费者Consumer模块

创建一个Maven项目(dubbo-web)作为Consumer模块

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.gewuyou</groupId>
    <artifactId>dubbo-web</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>


    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <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>

        <!--日志-->
        <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之后统一为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>

        <!--&lt;!&ndash;依赖service模块&ndash;&gt;-->
        <dependency>
            <groupId>com.gewuyou</groupId>
            <artifactId>dubbo-interface</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <!--tomcat插件-->
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.1</version>
                <configuration>
                    <port>80</port>
                    <path>/</path>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
web.xml
<?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">
   <!-- 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>
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: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.gewuyou.controller"/>

        <!--dubbo的配置-->
        <!--1、配置项目的名称 必须唯一-->
        <dubbo:application name="dubbo-web">
                <dubbo:parameter key="qos.port" value="33333"/>
        </dubbo:application>
        <!--2、配置注册中心的地址-->
        <dubbo:registry address="zookeeper://192.168.216.135:2181"/>
        <!--3、配置dubbo的包扫描-->
        <dubbo:annotation package="com.gewuyou.controller"/>
</beans>

3、在服务提供者模块编写UserServicelmpl提供服务

package com.gewuyou.service.impl;

import com.gewuyou.service.UserService;
import org.apache.dubbo.config.annotation.Service;


// @Service 将该类的对象创建出来,放到Spring的IOC容器中 bean的定义
@Service //将这个类提供的(方法)服务对外发布,将访问的地址 ip 端口 ,路径注册到注册中心
public class UserServiceImpl implements UserService {
    @Override
    public String sayHello() {
        return "hello dubbo!";
    }
}

注:Dubbo2.77版本之后@Service被弃用,新注解为@DubboService

4、在服务消费者中的UserController远程调用

package com.gewuyou.controller;

import com.gewuyou.service.UserService;
import org.apache.dubbo.config.annotation.Reference;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/users")
public class UserController {
    /*
        1、从zookeeper注册中心获取userService的访问url
        2、进行远程调用RPC
        3、将结果封装为一个代理对象。给变量赋值
     */

    @Reference//远程注入
    private UserService userService;

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

注:Dubbo2.77版本之后@Reference被弃用,新注解为@DubboReference

5、UserServicelmpl提供的服务

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

6、使用公共接口减少接口冗余

创建个公共模块将二者同时需要的接口或者代码进行整合,减少代码冗余

7、分别启动两个服务,测试

image-20220628162805806

image-20220628162825597

3、Duboo高级特性

1、Dubbo-admin管理平台

  • dubbo-admin管理平台,是图形化的服务管理页面
  • 从注册中心中获取到所有的提供者/消费者进行配置管理
  • 路由规则、动态配置、服务降级、访问控制、权重调整、
  • 负载均衡等管理功能
  • dubbo-admin是一个前后端分离的项目。前端使用vue
  • 后端使用springboot
  • 安装dubbo-admin其实就是部署该项目

一、dubbo-admin安装

1、环境准备

dubbo-admin 是一个前后端分离的项目。前端使用vue,后端使用springboot,安装 dubbo-admin 其实就是部署该项目。我们将dubbo-admin安装到开发环境上。要保证开发环境有jdk,maven,nodejs

安装node**(如果当前机器已经安装请忽略)**

因为前端工程是用vue开发的,所以需要安装node.js,node.js中自带了npm,后面我们会通过npm启动

下载地址

https://nodejs.org/en/

1578298201398

2、下载 Dubbo-Admin

进入github,搜索dubbo-admin

https://github.com/apache/dubbo-admin

下载:

1578297063167

3、把下载的zip包解压到指定文件夹
(解压到哪个文件夹随意)

1578297477356

4、修改配置文件

解压后我们进入…\dubbo-admin-develop\dubbo-admin-server\src\main\resources目录,找到 application.properties 配置文件 进行配置修改

1578297603008

修改zookeeper地址

1578297758655

# centers in dubbo2.7
admin.registry.address=zookeeper://192.168.216.135:2181
admin.config-center=zookeeper://192.168.216.135:2181
admin.metadata-report.address=zookeeper://192.168.216.135:2181

admin.registry.address注册中心 admin.config-center 配置中心 admin.metadata-report.address元数据中心

5、打包项目

在 dubbo-admin-develop 目录执行打包命令

mvn  clean package

1578300464726

6、启动后端

切换到目录

dubbo-Admin-develop\dubbo-admin-distribution\target>

执行下面的命令启动 dubbo-admin,dubbo-admin后台由SpringBoot构建。

java -jar .\dubbo-admin-0.1.jar

1578300551892

7、前台后端

dubbo-admin-ui 目录下执行命令

npm run dev

1578300677041

8、访问

浏览器输入。用户名密码都是root

http://localhost:8081/

1578300774258

二、dubbo-admin简单使用

注意:Dubbo Admin【服务Mock】【服务统计】将在后续版本发布....

在上面的步骤中,我们已经进入了Dubbo-Admin的主界面,在【快速入门】章节中,我们定义了服务生产者、和服务消费者,下面我们从Dubbo-Admin管理界面找到这个两个服务

1、点击服务查询

2、查询结果

1578301528363

A:输入的查询条件com.itheima.service.UserService

B:搜索类型,主要分为【按服务名】【按IP地址】【按应用】三种类型查询

C:搜索结果

3.1.4 dubo-admin查看详情

我们查看com.itheima.service.UserService (服务提供者)的具体详细信息,包含【元数据信息】

1)点击详情

从【详情】界面查看,主要分为3个区域

A区域:主要包含服务端 基础信息比如服务名称、应用名称等

B区域:主要包含了生产者、消费者一些基本信息

C区域:是元数据信息,注意看上面的图,元数据信息是空的

我们需要打开我们的生产者配置文件加入下面配置

    <!-- 元数据配置 -->
    <dubbo:metadata-report address="zookeeper://192.168.216.135:2181" />

重新启动生产者,再次打开Dubbo-Admin

这样我们的元数据信息就出来了

1578301892712

2、Dubbo常用高级配置

1、序列化

image-20220628185121407

2、地址缓存

image-202206281926365623、超时与重试

超时

正常的请求过程,完成任务的线程会被回收或者销毁

image-20220628212208081

消费者请求阻塞,等待,导致调用不成功(提供者查询时间过长或者出错),使线程无法归还或者销毁

image-20220628212353694

当在访问高峰期时这种情况会导致线程堆积,使得服务器资源消耗严重,造成雪崩

image-20220628212750812

dubbo使用超时机制来尝试解决这个问题

image-20220628213007215

配置Dubbo的超时时间

使用Dubbo的@Service注解来设置超时时间与允许重试次数(提供方)
@Service(timeout = 3000,retries = 0) //当前服务3s超时
当然Dubbo的@Reference注解也能设置设置超时时间与允许重试次数(消费方)
@@Reference(timeout = 3000,retries = 0)

注:当两方同时都配置了超时时间时,消费方配置会覆盖生产方但建议在生产方设置超时时间,因为生产方能够大致判断请求时长据此设置超时

生产方超时
严重: Servlet.service() for servlet [springmvc] in context with path [] threw exception [Request processing failed; nested exception is org.apache.dubbo.rpc.RpcException: Failed to invoke the method getUserById in the service com.gewuyou.service.UserService. Tried 1 times of the providers [192.168.216.1:20880] (1/1) from the registry 192.168.216.135:2181 on the consumer 192.168.216.1 using the dubbo version 2.7.4.1. Last error is: Invoke remote method timeout. method: getUserById, provider: dubbo://192.168.216.1:20880/com.gewuyou.service.UserService?anyhost=true&application=dubbo-web&bean.name=ServiceBean:com.gewuyou.service.UserService&check=false&deprecated=false&dubbo=2.0.2&dynamic=true&generic=false&interface=com.gewuyou.service.UserService&lazy=false&methods=getUserById,sayHello&pid=25312&qos.port=33333&register.ip=192.168.216.1&release=2.7.4.1&remote.application=dubbo-service&retries=0&revision=1.0-SNAPSHOT&side=consumer&sticky=false&timeout=3000&timestamp=1656423687198, cause: org.apache.dubbo.remoting.TimeoutException: Waiting server-side response timeout by scan timer. start time: 2022-06-28 21:41:41.793, end time: 2022-06-28 21:41:44.828, client elapsed: 38 ms, server elapsed: 2997 ms, timeout: 3000 ms, request: Request [id=0, version=2.0.2, twoway=true, event=false, broken=false, data=null], channel: /192.168.216.1:2553 -> /192.168.216.1:20880] with root cause
=========================================================================================严重:servlet [springmvc] 在路径 [] 的上下文中的 Servlet.service() 引发异常 [请求处理失败;嵌套异常是org.apache.dubbo.rpc.RpcException:调用com.gewuyou.service.UserService服务中getUserById方法失败。使用 dubbo 版本 2.7.4.1 在消费者 192.168.216.1 上尝试了 1 次提供者 [192.168.216.1:20880] (11) 来自注册中心 192.168.216.135:2181。最后一个错误是:调用远程方法超时。方法:getUserById,提供者:dubbo:192.168.216.1:20880com.gewuyou.service.UserService?anyhost=true&application=dubbo-web&bean.name=ServiceBean:com.gewuyou.service.UserService&check=false&deprecated=false&dubbo=2.0.2&dynamic=true&generic= false&interface=com.gewuyou.service.UserService&lazy=false&methods=getUserById,sayHello&pid=25312&qos.port=33333&register.ip=192.168.216.1&release=2.7.4.1&remote.application=dubbo-service&retries=0&revision=1.0-SNAPSHOT&side=consumer&sticky=false&timeout= 3000&timestamp=1656423687198,原因:org.apache.dubbo.remoting.TimeoutException:等待服务器端响应超时扫描定时器。开始时间:2022-06-28 21:41:41.793,结束时间:2022-06-28 21:41:44.828,客户端经过:38 毫秒,服务器经过:2997 毫秒,超时:3000 毫秒,请求:请求 [id =0, version=2.0.2, twoway=true, event=false, broken=false, data=null], channel: 192.168.216.1:2553 -> 192.168.216.1:20880] 根本原因
重试

image-20220628215303534

4、多版本

image-20220628220517117

使用Dubbo的@Service注解中的version值来设置当前服务版本
@Service(version = "v1.6") //服务版本v1.6
@Service(version = "v2.0") //服务版本v2.0
使用Dubbo的@Reference注解中的version值来选择消费者调用的服务版本
@Reference(version = "v2.0")//使用v2.0服务版本

5、负载均衡

负载均衡的四种策略

策略一:Random

image-20220628221625406

三者权重均为100,每个接收请求概率的概率趋近于 1/3(100/300) ,所以当权重值越高其接收概率也就越高

设置策略与权重
使用Dubbo的@Reference注解中的loadbalance值来设置策略
@Reference(loadbalance = "random")//默认使用
使用Dubbo的@Service注解中的weight值来设置权重
@Service(weight = "100") //权重值为100
策略二:RoundRobin

image-20220628222842857

挨个来,如果提供者没有权重或者权重相等,则按顺序调用例如1->2->3 1->2->3(无随机)

当某个权重值更高时,如上图,1->2->3 ->2,2 会被再次轮到,因为三者的访问次数比应当为1:2:1

设置策略
使用Dubbo的@Reference注解中的loadbalance值来设置策略
@Reference(loadbalance = "roundrobin")
策略三:LeastActive

image-20220628223536635

请求将根据各个提供者的活跃数量在寻找最小活跃数量的一个进行调用,当时间一样时随机调用

设置策略
使用Dubbo的@Reference注解中的loadbalance值来设置策略
@Reference(loadbalance = "leastactive")
策略四:ConsistentHash

image-20220628223932128

根据哈希算法求出传入的请求参数,相同的参数总是发送到同一提供者

设置策略
使用Dubbo的@Reference注解中的loadbalance值来设置策略
@Reference(loadbalance = "consistenthash")

6、集群容错

image-20220628224318348

7、服务降级

image-20220628225534448

使用Dubbo的@Reference注解中的mock值来设置服务降级方式
@Reference(mock = "force:return null")//不再调用UserService服务(即不再调用提供者提供的服务)
@Reference(mock = "fail:return null")//仍旧调用提供者提供的服务,但失败后不抛异常,返回null值