# 分布式系统基本概念

## 什么是分布式系统

分布式系统是由一组独立的计算机组成的系统，这些计算机通过网络相互通信和协调，对外表现为一个统一的整体。对于用户来说，使用分布式系统就像在使用一台强大的计算机。

## 分布式系统的特点

### 1. 透明性

* **访问透明**：用户无需关心资源的位置
* **位置透明**：资源的位置对用户不可见
* **并发透明**：多个进程可以并发操作共享资源而不互相干扰
* **复制透明**：系统的冗余副本对用户透明
* **故障透明**：系统能够隐藏组件的故障和恢复

### 2. 开放性

分布式系统应该是开放的，支持不同厂商的硬件和软件协同工作。

### 3. 可扩展性

系统应该能够通过增加计算机来扩展性能和容量。

### 4. 容错性

分布式系统应该能够容忍部分组件的故障，继续提供服务。

## 分布式系统的挑战

### 1. 网络延迟与分区

网络通信不可避免地引入延迟，网络分区可能导致节点间无法通信。

### 2. 时钟同步

不同节点的时钟可能不一致，影响事件排序和事务处理。

### 3. 数据一致性

如何在多个节点间保持数据的一致性是一个核心挑战。

### 4. 故障检测

在分布式系统中，很难准确判断一个节点是故障还是只是响应缓慢。

### 5. 并发控制

多个节点并发访问共享资源时需要有效的协调机制。

## 分布式系统的通信方式

### 1. RPC（远程过程调用）

RPC 允许程序像调用本地方法一样调用远程服务上的方法。

```java
// 传统本地调用
UserService service = new UserService();
User user = service.getUser(1);

// RPC远程调用
UserServiceStub stub = new UserServiceStub("http://user-service");
User user = stub.getUser(1);
```

### 2. 消息队列

通过消息队列实现异步通信，提高系统的解耦性和可扩展性。

### 3. REST API

基于 HTTP 协议的轻量级通信方式。

## 分布式架构模式

### 1. 客户端-服务器模式

最基础的分布式架构，客户端请求服务器处理数据。

### 2. 多层架构

* 表现层（UI）
* 业务逻辑层（Service）
* 数据访问层（DAO）
* 数据存储层（Database）

### 3. 微服务架构

将大型应用拆分为多个小型、独立部署的服务。

### 4. P2P 架构

所有节点平等，每个节点既是客户端又是服务器。

## CAP 定理简介

CAP 定理指出，在分布式系统中，以下三个特性最多只能同时满足两个：

* **C (Consistency)**：一致性，所有节点在同一时间看到的数据相同
* **A (Availability)**：可用性，每个请求都能得到响应
* **P (Partition Tolerance)**：分区容错性，系统在网络分区时仍能运行

## 总结

分布式系统虽然带来了性能提升和高可用性，但也引入了复杂性。理解分布式系统的基本概念和挑战是设计可靠分布式架构的基础。在后续章节中，我们将深入学习 CAP 理论、一致性协议、分布式事务等核心内容。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://qiangrens-organization.gitbook.io/qkd90/8-fen-bu-shi-li-lun/fen-bu-shi-xi-tong-ji-ben-gai-nian.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
