前言
-
欢迎阅读本篇博客!在当前大规模分布式系统的开发中,构建可靠的服务架构是至关重要的一环。为了实现远程方法调用、服务注册与发现以及配置管理等功能,使用Dubbo和注册中心(ZooKeeper、Nacos)成为了主流选择
-
本文将带领您一步步搭建一个强大的分布式服务架构,通过深入探索Dubbo和注册中心的使用方式,帮助您轻松实现高效的远程调用和服务管理。我们将详尽介绍如何安装、配置和集成Nacos和ZooKeeper作为注册中心,并结合Dubbo框架搭建完整的微服务架构
-
无论您是刚开始接触分布式架构,还是已经有一定经验,本文都将为您提供实用的技巧和最佳实践,以确保您的服务架构在性能、可靠性和扩展性方面都达到最佳状态
-
一起来构建强大而灵活的分布式服务架构吧!让我们从安装配置开始,逐步探索Dubbo与注册中心的集成,实现高效远程调用和服务管理的愉悦体验
-
Dubbo架构实现了什么?
-
像调用本地方法一样,调用远程方法(2023/08/24午)
-
以下是本文的行文思路:
-
详细指南:使用ZooKeeper和Nacos搭建注册中心
-
架构实践:结合Dubbo实现灵活的远程方法调用
-
分布式协作:探索服务注册与发现的最佳实践
-
高效管理:利用注册中心进行配置管理的技巧与策略
-
正文
搭建注册中心
-
我们接下来谈论的下载安装以及配置管理,都是在windows系统下进行的(2023/08/22晚)
下载安装
-
ZooKeeper:Apache ZooKeeper
-
Nacos:github.com
-
这里我们提供了ZeeKeeper和Nacos的下载方式,还有其他的下载方式,这里就不展示了
-
ZooKeeper的安压缩包下载后,直接解压就行,详细过程可以参考这篇博客👇:
-
Nacos的下载则更加简单,直接拉取GitHub源代码就可以,参考官方文档👇:
启动服务
-
在ZooKeeper的bin目录下,双击zkServer.cmd即可快速启动注册中心:
-
同理,在Nacos的bin目录下,双击startup.cmd即可快速启动注册中心:
-
或者,在bin目录下,执行以下命令(单机运行):
startup.cmd -m standalone
远程方法调用
-
Dubbo官网:3 - 基于 Spring Boot Starter 开发微服务应用 | Apache Dubbo
-
我们要基于Dubbo实现远程调用服务,实现很简单,参考官网文档就可以,这里简单说一下调用流程:
-
启动注册中心
-
服务提供者 -> 服务注册
-
消费者调用服务 -> 服务发现
-
服务调用成功
-
-
这里,我们使用官网文档给出的示例代码,简单梳理下Dubbo实现服务远程调用的流程:(2023/08/22晚)
-
如上,ZooKeeper注册中心已经启动成功了
-
服务提供者 -> 服务注册:
@DubboService
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello(String name) {
return "Hello " + name;
}
}
-
消费者调用服务 -> 服务发现:
-
服务调用成功:
@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();
}
}
服务注册与发现
-
在拉取到官方文档中的示例代码并成功运行之后,我们需要在自己的项目中使用 Dubbo+Nacos 实现远程服务调用
-
成功下载安装Nacos注册中心,启动运行:(2023/08/24午)
-
如下,在项目中引入相关依赖包:
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>3.1.8</version>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>2.2.1</version>
</dependency>
-
在服务提供者和消费者中,分别添加如下配置:
dubbo: application: name: dubbo-springboot-demo-provider protocol: name: dubbo port: -1 registry: id: nacos-registry address: nacos://localhost:8848
dubbo: application: name: dubbo-springboot-demo-client protocol: name: dubbo port: -1 registry: id: nacos-registry address: nacos://localhost:8848
-
添加@DubboService注解,作为服务提供者,将该service方法注册到注册中心:
/**
* 内部接口服务实现类
*
* @author memory
*/
@DubboService
public class InnerInterfaceInfoServiceImpl implements InnerInterfaceInfoService {
........................
}
-
添加@DubboReference注解,作为消费者,从注册中心拉取对应服务,并成功调用:
@DubboReference
private InnerUserService innerUserService;
// 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午)
dubbo: application: name: dubbo-springboot-demo-provider protocol: name: dubbo port: -1 registry: id: nacos-registry address: nacos://localhost:8848
dubbo: application: name: dubbo-springboot-demo-client protocol: name: dubbo port: -1 registry: id: nacos-registry address: nacos://localhost:8848