当前位置:   article > 正文

Spring Boot 整合Dubbo + Zookeeper 实现分布式 消费者+服务者业务的调用_消费者 提供者 duboo中消费者的依赖

消费者 提供者 duboo中消费者的依赖

在这里插入图片描述

一、什么是Dubbo

Dubbo是阿里出品的分布式开源服务框架,阿里已交予Apache开源组织基金会

Apache Dubbo 是一款高性能、轻量级开源RPC服务框架

Dubbo官网

☁️基本概念

在这里插入图片描述
服务提供者(Provider):暴露服务的服务提供方,服务提供者在启动时,向注册中心注册自己提供的服务。

服务消费者(Consumer): 调用远程服务的服务消费方,服务消费者在启动时,向注册中心订阅自己所需的服务,服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。

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

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

❄️Dubbo的服务提供者与服务消费者的调用关系

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

二、什么是RPC?

RPC【Remote Procedure Call】是指远程过程调用,是一种进程间通信方式,他是一种技术的思想,而不是规范。它允许程序调用另一个地址空间(通常是共享网络的另一台机器上)的过程或函数,而不用程序员显式编码这个远程调用的细节。即程序员无论是调用本地的还是远程的函数,本质上编写的调用代码基本相同。

☎️RPC基本原理

示意图
在这里插入图片描述
详细版本
在这里插入图片描述
RPC两个核心模块:序列化和通讯

三、Dubbo的特点及好处

Apache Dubbo |ˈdʌbəʊ| 提供了六大核心能力:面向接口代理的高性能RPC调用,智能容错和负载均衡,服务自动注册和发现,高度可扩展能力,运行期流量调度,可视化的服务治理与运维。

在这里插入图片描述

Dubbo的注册中心

Dubbo的注册中心有很多种,但官方推荐使用Zookeeper,其余有RedisMulticastSimple注册中心。

  • 1
  • 2

Dubbo网络通信框架

Dubbo 默认使用 Netty 框架,也是推荐的选择,另外内容还集成有MinaGrizzly
  • 1

四、Zookeeper是什么?

顾名思义 zookeeper 就是动物园管理员,他是用来管 hadoop(大象)、Hive(蜜蜂)、pig(小 猪)的管理员, Apache Hbase 和 Apache Solr 的分布式集群都用到了 zookeeper;Zookeeper: 是一个分布式的、开源的程序协调服务,是 hadoop 项目下的一个子项目。他提供的主要功 能包括:配置管理、名字服务、分布式锁、集群管理。

五、搭建Zookeeper注册中心

♻️安装Zookeeper

访问Zookeeper官网下载

下载Zookeeper

在这里插入图片描述

下载 压缩包格式的zookeeper

将下载的文件解压缩

在这里插入图片描述

✅启动Zookeeper

从cmd窗口进入Zookeeper文件夹下的bin目录,执行zkServer.cmd启动
在这里插入图片描述
如果报错提示 zoo.cfg缺失,那就进入zookeeper的conf文件夹下将zoo_sample.cfg复制一份改名为zoo.cfg即可

再次重启则成功

六、搭建Dubbo管理控制台

✒️克隆dubbo-admin 项目

从 github打开项目 dubbo-admin
dubbo-admin
从GitHub爬取项目教程如下:

使用 Git爬取 GitEE、GitLab、GitHub项目的教程

✅运行dubbo-admin

dubbo-admin采用前后端分离的形式管理项目

爬取文件如下

在这里插入图片描述

♨️部署dubbo-admin-server

运行后端项目必须启动 zookeeper,否则启动失败

cmd窗口进入dubbo-admin-server,执行以下命令

mvn clean package
  • 1

打包部署项目

在这里等大概5分钟左右即可部署完成

部署完成后生成target目录
在这里插入图片描述
cmd窗口进入target目录执行以下命令

java -jar dubbo-admin-server-0.4.0.jar
  • 1

后端项目运行成功~

♨️部署dubbo-admin-ui

在这里需要有 node 环境

教程如下

GitHub爬取项目并部署前端工程

cmd窗口进入dubbo-admin-ui,执行以下命令下载依赖

npm install
  • 1

下载完毕后,执行启动命令

npm run dev
  • 1

启动成功
在这里插入图片描述

Ⓜ️进入Dubbo管理控制台

访问前端生成的本地地址

在这里插入图片描述
默认用户名密码均为root

输入即可登录成功
在这里插入图片描述

✈️踩坑记录

后端的地址必须和前端工程vue.config.js下的target路径一样,否则404!

在这里插入图片描述

七、SpringBoot 整合Dubbo + Zookeeper

✉️项目简介

基于SpringBoot项目整合Dubbo + Zookeeper 实现消费者消费服务提供者的服务

消费者为订单模块,传入用户id去查询用户模块的用户信息,实现远程RPC调用服务,分布式调用,而不是单体架构

服务提供者为用户模块,返回用户信息

♻️项目结构图

在这里插入图片描述

  1. 父工程:dubbo-boot
  2. 公共API(接口及实体类):GmallPublicInterafce
  3. 服务提供者:UserServiceProvider
  4. 服务消费者:OrderServiceConsumer

⏰效果图

在这里插入图片描述

✴️核心源码

父级工程 dubbo-boot

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>

    <!-- 整体引入springboot工程 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>org.example</groupId>
    <artifactId>dubbo-boot</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <!-- 子模块 -->
    <modules>
        <module>GmallPublicInterface</module>
        <module>UserServiceProvider</module>
        <module>OrderServiceConsumer</module>
    </modules>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

</project>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

公共API模块 GmallPublicInterface

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">
    <parent>
        <artifactId>dubbo-boot</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>GmallPublicInterface</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <!-- 引入dubbo -->
        <!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.2</version>
        </dependency>

        <!-- 注册中心使用的是zookeeper,引入操作zookeeper的客户端 -->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.12.0</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.2.0</version>
        </dependency>
    </dependencies>

</project>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

还有一些核心的接口及实体类

服务提供者模块 UserServiceProvider

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">
    <parent>
        <artifactId>dubbo-boot</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>

    <artifactId>UserServiceProvider</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>


    <dependencies>

        <dependency>
            <groupId>org.example</groupId>
            <artifactId>GmallPublicInterface</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>




    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>

        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
            <resource>
                <directory>libs/</directory>
                <targetPath>libs</targetPath>
                <includes>
                    <include>**/*.jar</include>
                </includes>
            </resource>
        </resources>
    </build>


</project>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70

application.yml

# 配置端口号
server:
  port: 8084
# 配置Dubbo
dubbo:
  application:
    name: UserServiceProvider
  registry:
    address: zookeeper://127.0.0.1:2181
  protocol:
    name: dubbo
    port: 20881
  monitor:
    protocol: registry
  consumer:
    timeout: 2000
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

UserServiceImpl

package com.wanshi.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.wanshi.bean.UserAddress;
import com.wanshi.service.UserService;

import java.util.Arrays;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

	@Override
	public List<UserAddress> getUserAddressList(String userId) {
		System.out.println("UserServiceImpl.....old...");
		// TODO Auto-generated method stub
		UserAddress address1 = new UserAddress(1, "北京市朝阳区", "1", "Bug 终结者", "010-5625321", "Y");
		UserAddress address2 = new UserAddress(2, "北京市海淀区", "1", "小王", "010-66253834", "N");
		return Arrays.asList(address1,address2);
	}

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

服务消费者模块 OrderServiceConsumer

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">
    <parent>
        <artifactId>dubbo-boot</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>OrderServiceConsumer</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.example</groupId>
            <artifactId>GmallPublicInterface</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>

        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
            <resource>
                <directory>libs/</directory>
                <targetPath>libs</targetPath>
                <includes>
                    <include>**/*.jar</include>
                </includes>
            </resource>
        </resources>
    </build>

</project>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63

application.yml

# 配置端口
server:
  port: 8085

# 配置Dubbo
dubbo:
  application:
    name: OrderServiceConsumer
  registry:
    address: zookeeper://127.0.0.1:2181
  monitor:
    protocol: registry
  consumer:
    check: false
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

OrderController

package com.wanshi.gmall.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.validation.MethodValidated;
import com.wanshi.bean.UserAddress;
import com.wanshi.service.OrderService;
import com.wanshi.service.UserService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 1.将服务提供者注册到注册中心 (暴露服务)
 *  1). 导入 dubbo 依赖 2.6.2、引入操作zookeeper的客户端
 *  2). 配置服务提供者
 *
 * 2.让服务消费者去注册中心订阅服务提供者的服务地址
 * @author whc
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Reference
    UserService userService;

    @Override
    public List<UserAddress> initOrder(String userId) {
        //1. 查询用户的收货地址
        List<UserAddress> userAddressList = userService.getUserAddressList(userId);
        System.out.println("用户id:" + userId);
        userAddressList.forEach(user -> {
            System.out.println(user.getUserAddress());
        });
        return userAddressList;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

小结

Spring Boot 项目聚合Dubbo,目前是企业很常用的技术,多多练习,SpringBoot整合Dubbo实现分布式开发不在话下,一些细节之处要注意,细心做事,方可实现目标,从细节出发,不断挑战自己,迎接一个崭新的自己!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/53972
推荐阅读
相关标签
  

闽ICP备14008679号