Spring Boot 整合Dubbo + Zookeeper 实现分布式 消费者与服务者的业务调用

最后

毕竟工作也这么久了 ,除了途虎一轮,也七七八八面试了不少大厂,像阿里、饿了么、美团、滴滴这些面试过程就不一一写在这篇文章上了。我会整理一份详细的面试过程及大家想知道的一些问题细节

美团面试经验

美团面试
字节面试经验
字节面试
菜鸟面试经验
菜鸟面试
蚂蚁金服面试经验
蚂蚁金服
唯品会面试经验
唯品会

因篇幅有限,图文无法详细发出

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

dubbo-admin

从GitHub爬取项目教程如下:

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

✅运行dubbo-admin


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

爬取文件如下

在这里插入图片描述

♨️部署dubbo-admin-server

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

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


mvn clean package



打包部署项目

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

部署完成后生成target目录

在这里插入图片描述

cmd窗口进入target目录执行以下命令


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



后端项目运行成功~

♨️部署dubbo-admin-ui

在这里需要有 node 环境

教程如下

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

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


npm install



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


npm run dev



启动成功

在这里插入图片描述

Ⓜ️进入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>



🚃公共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>



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

🚃服务提供者模块 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>



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



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);

	}



}





🚃服务消费者模块 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>



# Kafka实战笔记

> **关于这份笔记,为了不影响大家的阅读体验,我只能在文章中展示部分的章节内容和核心截图**

![image.png](https://img-blog.csdnimg.cn/img_convert/2cc85608d32d17d0310e38553cbf07da.webp?x-oss-process=image/format,png)


*   **Kafka入门**
*   **为什么选择Kafka**
*   **Karka的安装、管理和配置**

![image.png](https://img-blog.csdnimg.cn/img_convert/09f9343baea30b89edb8f1253f7b813f.webp?x-oss-process=image/format,png)


*   **Kafka的集群**
*   **第一个Kafka程序**
*   ![image.png](https://img-blog.csdnimg.cn/img_convert/56720cf082b0aa6a7cd907884921df91.webp?x-oss-process=image/format,png)


afka的生产者

![image.png](https://img-blog.csdnimg.cn/img_convert/37e6816a12e51cfc2d5d377dcbecc351.webp?x-oss-process=image/format,png)

*   **Kafka的消费者**
*   **深入理解Kafka**
*   **可靠的数据传递**

![image.png](https://img-blog.csdnimg.cn/img_convert/5559791fcaf7563ba6c6365e8c2f695f.webp?x-oss-process=image/format,png)


![image.png](https://img-blog.csdnimg.cn/img_convert/1bda7817d9d754d718afa2247dde034f.webp?x-oss-process=image/format,png)


*   **Spring和Kalka的整合**
*   **Sprinboot和Kafka的整合**
*   **Kafka实战之削峰填谷**
*   **数据管道和流式处理(了解即可)**

![image.png](https://img-blog.csdnimg.cn/img_convert/f6131584380c287f1c9651417d4e7c93.webp?x-oss-process=image/format,png)


*   **Kafka实战之削峰填谷**

![image.png](https://img-blog.csdnimg.cn/img_convert/337d207a899941b1dc357d9684f19843.webp?x-oss-process=image/format,png)

> **本文已被[CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】](https://bbs.csdn.net/forums/4f45ff00ff254613a03fab5e56a57acb)收录**

**[需要这份系统化的资料的朋友,可以点击这里获取](https://bbs.csdn.net/forums/4f45ff00ff254613a03fab5e56a57acb)**

-1715464417607)]


*   **Kafka入门**
*   **为什么选择Kafka**
*   **Karka的安装、管理和配置**

[外链图片转存中...(img-SlTdTzZN-1715464417608)]


*   **Kafka的集群**
*   **第一个Kafka程序**
*   [外链图片转存中...(img-C5BEhGsM-1715464417608)]


afka的生产者

[外链图片转存中...(img-v2Ih2tD4-1715464417608)]

*   **Kafka的消费者**
*   **深入理解Kafka**
*   **可靠的数据传递**

[外链图片转存中...(img-jRZqhzxc-1715464417609)]


[外链图片转存中...(img-14D9sycd-1715464417609)]


*   **Spring和Kalka的整合**
*   **Sprinboot和Kafka的整合**
*   **Kafka实战之削峰填谷**
*   **数据管道和流式处理(了解即可)**

[外链图片转存中...(img-DTUnGM1c-1715464417609)]


*   **Kafka实战之削峰填谷**

[外链图片转存中...(img-sEEkxw0c-1715464417610)]

> **本文已被[CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】](https://bbs.csdn.net/forums/4f45ff00ff254613a03fab5e56a57acb)收录**

**[需要这份系统化的资料的朋友,可以点击这里获取](https://bbs.csdn.net/forums/4f45ff00ff254613a03fab5e56a57acb)**

  • 30
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Dubbo是一款高性能、轻量级的Java RPC框架,可以实现不同进程间的远程调用。在Spring Boot中使用Dubbo可以大大简化分布式系统的开发和部署。 下面是Dubbo整合Spring Boot的详细全过程: 1. 创建一个Spring Boot项目 首先需要创建一个Spring Boot项目,可以使用Spring Initializr快速创建。在创建项目时,需要添加DubboZookeeper的依赖。 2. 配置Dubbo 在src/main/resources目录下创建一个名为dubbo.properties的文件,用于配置Dubbo。在配置文件中,需要指定Dubbo要扫描的包、注册中心的地址等信息。 示例配置文件: ```properties # Dubbo应用名称 dubbo.application.name=dubbo-demo # Dubbo注册中心地址 dubbo.registry.address=zookeeper://127.0.0.1:2181 # Dubbo扫描的包 dubbo.scan.basePackages=com.example.demo.service.impl ``` 3. 编写Dubbo服务 在创建Dubbo服务时,需要使用@DubboService注解将服务暴露出来。示例代码: ```java package com.example.demo.service.impl; import com.alibaba.dubbo.config.annotation.DubboService; import com.example.demo.service.HelloService; @DubboService public class HelloServiceImpl implements HelloService { @Override public String sayHello(String name) { return "Hello, " + name; } } ``` 4. 编写Dubbo消费者 Dubbo消费者需要使用@DubboReference注解引用Dubbo服务。示例代码: ```java package com.example.demo.controller; import com.alibaba.dubbo.config.annotation.DubboReference; import com.example.demo.service.HelloService; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @DubboReference private HelloService helloService; @GetMapping("/hello/{name}") public String sayHello(@PathVariable String name) { return helloService.sayHello(name); } } ``` 5. 配置Zookeeper Dubbo需要使用Zookeeper作为注册中心。可以在官网下载Zookeeper,并按照官方文档进行安装和配置。 6. 启动Dubbo服务 使用Spring Boot的启动方式启动Dubbo服务。 7. 测试Dubbo服务 使用浏览器或者其他工具访问Dubbo服务,测试是否能够正常调用。 以上就是Dubbo整合Spring Boot的详细全过程。在实际开发中,需要根据项目需求进行配置和调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值