从零开始构建分布式服务架构:用 `Dubbo` 和注册中心实现远程调用、服务注册与发现、配置管理

本文最后更新于:13 天前

前言

  • 欢迎阅读本篇博客!在当前大规模分布式系统的开发中,构建可靠的服务架构是至关重要的一环。为了实现远程方法调用、服务注册与发现以及配置管理等功能,使用 Dubbo 和注册中心(ZooKeeperNacos)成为了主流选择

  • 本文将带领您一步步搭建一个强大的分布式服务架构,通过深入探索 Dubbo 和注册中心的使用方式,帮助您轻松实现高效的远程调用和服务管理。我们将详尽介绍如何安装、配置和集成NacosZooKeeper作为注册中心,并结合 Dubbo 框架搭建完整的微服务架构

  • 无论您是刚开始接触分布式架构,还是已经有一定经验,本文都将为您提供实用的技巧和最佳实践,以确保您的服务架构在性能、可靠性和扩展性方面都达到最佳状态

  • 一起来构建强大而灵活的分布式服务架构吧!让我们从安装配置开始,逐步探索 Dubbo 与注册中心的集成,实现高效远程调用和服务管理的愉悦体验

  • Dubbo 架构实现了什么?

image-20230824134934927

  • 像调用本地方法一样,调用远程方法(2023/08/24午)
  • 以下是本文的行文思路:
    • 详细指南:使用ZooKeeper和Nacos搭建注册中心
    • 架构实践:结合 Dubbo 实现灵活的远程方法调用
    • 分布式协作:探索服务注册与发现的最佳实践
    • 高效管理:利用注册中心进行配置管理的技巧与策略

正文

搭建注册中心

  • 我们接下来谈论的下载安装以及配置管理,都是在windows系统下进行的(2023/08/22晚)

下载安装

image-20230823205911004

启动服务

  • 在ZooKeeper的bin目录下,双击zkServer.cmd即可快速启动注册中心:

image-20230822121439364

  • 同理,在Nacos的bin目录下,双击startup.cmd即可快速启动注册中心:

image-20230822121744746

  • 或者,在bin目录下,执行以下命令(单机运行):
1
startup.cmd -m standalone

image-20230823210003483

远程方法调用

  • Dubbo 官网:[3 - 基于 Spring Boot Starter 开发微服务应用 | Apache Dubbo ](https://cn. Dubbo .apache.org/zh-cn/overview/mannual/java-sdk/quick-start/spring-boot/)
  • 我们要基于 Dubbo 实现远程调用服务,实现很简单,参考官网文档就可以,这里简单说一下调用流程:

    • 启动注册中心
    • 服务提供者 -> 服务注册
    • 消费者调用服务 -> 服务发现
    • 服务调用成功
  • 这里,我们使用官网文档给出的示例代码,简单梳理下 Dubbo 实现服务远程调用的流程:(2023/08/22晚)
  • 如上,ZooKeeper注册中心已经启动成功了
  • 服务提供者 -> 服务注册:
1
2
3
4
5
6
7
@DubboService
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello(String name) {
return "Hello " + name;
}
}

image-20230822131312100

  • 消费者调用服务 -> 服务发现:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@Component
public class Task implements CommandLineRunner {
@DubboReference
private DemoService demoService;

@Override
public void run(String... args) throws Exception {
String result = demoService.sayHello("world");
System.out.println("Receive result ======> " + result);

new Thread(()-> {
while (true) {
try {
Thread.sleep(1000);
System.out.println(new Date() + " Receive result ======> " + demoService.sayHello("world"));
} catch (InterruptedException e) {
e.printStackTrace();
Thread.currentThread().interrupt();
}
}
}).start();
}
}

image-20230822131141978

  • 服务调用成功:

image-20230822130943864

服务注册与发现

  • 在拉取到官方文档中的示例代码并成功运行之后,我们需要在自己的项目中使用 ** Dubbo +Nacos 实现远程服务调用**

  • 成功下载安装Nacos注册中心,启动运行:(2023/08/24午)

image-20230824133939409

  • 如下,在项目中引入相关依赖包:
1
2
3
4
5
<dependency>
<groupId>org.apache.Dubbo</groupId>
<artifactId>Dubbo</artifactId>
<version>3.1.8</version>
</dependency>
1
2
3
4
5
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>2.2.1</version>
</dependency>
  • 在服务提供者和消费者中,分别添加如下配置:
1
2
3
4
5
6
7
8
9
Dubbo:
application:
name: Dubbo-springboot-demo-provider
protocol:
name: Dubbo
port: -1
registry:
id: nacos-registry
address: nacos://localhost:8848
1
2
3
4
5
6
7
8
9
Dubbo:
application:
name: Dubbo-springboot-demo-client
protocol:
name: Dubbo
port: -1
registry:
id: nacos-registry
address: nacos://localhost:8848
  • 添加@DubboService注解,作为服务提供者,将该service方法注册到注册中心:
1
2
3
4
5
6
7
8
9
/**
* 内部接口服务实现类
*
* @author memory
*/
@DubboService
public class InnerInterfaceInfoServiceImpl implements InnerInterfaceInfoService {
........................
}
  • 添加@DubboReference注解,作为消费者,从注册中心拉取对应服务,并成功调用:
1
2
@DubboReference
private InnerUserService innerUserService;
1
2
3
4
5
6
7
8
9
10
// 4.1.校验accessKey
// todo 从数据库中查询, accessKey是否分配给该用户
if (accessKey == null || !accessKey.equals("memory")) {
return handleNoAuth(response);
}
// accessKey是否分配给该用户
User invokeUser = innerUserService.getInvokeUser(accessKey);
if (invokeUser == null) {
return handleNoAuth(response);
}

配置管理

  • 在服务提供者和消费者中,分别添加如下配置:(2023/08/24午)
1
2
3
4
5
6
7
8
9
Dubbo:
application:
name: Dubbo-springboot-demo-provider
protocol:
name: Dubbo
port: -1
registry:
id: nacos-registry
address: nacos://localhost:8848
1
2
3
4
5
6
7
8
9
Dubbo:
application:
name: Dubbo-springboot-demo-client
protocol:
name: Dubbo
port: -1
registry:
id: nacos-registry
address: nacos://localhost:8848

Nacos

2024年4月26日

🍖 推荐阅读:

总结


从零开始构建分布式服务架构:用 `Dubbo` 和注册中心实现远程调用、服务注册与发现、配置管理
http://example.com/2023/08/22/从零开始构建分布式服务架构:用Dubbo和注册中心实现远程调用、服务注册与发现、配置管理/
作者
Memory
发布于
2023年8月22日
更新于
2024年4月26日
许可协议