GitHub - clockworklabs/SpacetimeDB: Multiplayer at the speed of light

GitHub - clockworklabs/SpacetimeDB: Multiplayer at the speed of light

技术背景

SpacetimeDB 是一种将数据库和服务器合二为一的关系型数据库系统。它允许通过名为“模块”的存储过程将应用程序逻辑直接上传到数据库中。与传统架构不同,客户端可直接连接到数据库并在数据库内部执行应用程序逻辑。这意味着可以使用单一语言(Rust)编写整个应用程序并将其部署为单个二进制文件,避免了使用微服务、容器、Kubernetes、Docker、VM 等复杂的基础设施。

SpacetimeDB 的设计理念类似于智能合约,但它是一个与区块链无关的数据库,并且速度比任何智能合约系统都要快得多。它针对实时应用(如游戏、聊天和协作工具)进行了优化,追求最大速度和最小延迟,而非批量处理或 OLAP 工作负载。

实现步骤

安装

常见平台安装

  • macOS 和 Linux:运行安装脚本 curl -sSf https://install.spacetimedb.com | sh,之后可使用 spacetime 命令管理版本。
  • Windows:在 PowerShell 中粘贴 iwr https://windows.spacetimedb.com -useb | iex。若使用 WSL,则遵循 Linux 安装说明。

从源代码安装

  • MacOS + Linux
    1. 安装 rustup(若已安装 cargo 和 wasm32 - unknown - unknown 目标可跳过):curl https://sh.rustup.rs -sSf | sh
    2. 克隆 SpacetimeDB:git clone https://github.com/clockworklabs/SpacetimeDB
    3. 构建并安装 CLI:
1
2
cd SpacetimeDB
cargo build --locked --release -p spacetimedb-standalone -p spacetimedb-update -p spacetimedb-cli
4. 创建目录:
1
2
3
mkdir -p ~/.local/bin
export STDB_VERSION="$(./target/release/spacetimedb-cli --version | sed -n 's/.*spacetimedb tool version \([0-9.]*\);.*/\1/p')"
mkdir -p ~/.local/share/spacetime/bin/$STDB_VERSION
5. 安装更新二进制文件:
1
2
3
cp target/release/spacetimedb-update ~/.local/bin/spacetime
cp target/release/spacetimedb-cli ~/.local/share/spacetime/bin/$STDB_VERSION
cp target/release/spacetimedb-standalone ~/.local/share/spacetime/bin/$STDB_VERSION
6. 将 `~/.local/bin` 添加到路径:`export PATH="$HOME/.local/bin:$PATH"`
7. 设置 SpacetimeDB 版本:`spacetime version use $STDB_VERSION`
  • Windows
    1. 安装 Strawberry Perl、openssl 二进制文件和 rustup for Windows。
    2. 克隆 SpacetimeDB:git clone https://github.com/clockworklabs/SpacetimeDB
    3. 构建并安装 CLI:
1
2
cd SpacetimeDB
cargo build --locked --release -p spacetimedb-standalone -p spacetimedb-update -p spacetimedb-cli
4. 创建目录:
1
2
3
$stdbDir = "$HOME\AppData\Local\SpacetimeDB"
$stdbVersion = & ".\target\release\spacetimedb-cli" --version | Select-String -Pattern 'spacetimedb tool version ([0-9.]+);' | ForEach-Object { $_.Matches.Groups[1].Value }
New-Item -ItemType Directory -Path "$stdbDir\bin\$stdbVersion" -Force | Out-Null
5. 安装更新二进制文件:
1
2
3
Copy-Item "target\release\spacetimedb-update.exe" "$stdbDir\spacetime.exe"
Copy-Item "target\release\spacetimedb-cli.exe" "$stdbDir\bin\$stdbVersion\"
Copy-Item "target\release\spacetimedb-standalone.exe" "$stdbDir\bin\$stdbVersion\"
6. 将创建的目录添加到系统路径,重启 shell。
7. 设置 SpacetimeDB 版本:`spacetime version use $stdbVersion`

使用 Docker 运行

使用以下命令启动新实例:docker run --rm --pull always -p 3000:3000 clockworklabs/spacetime start

快速开始

  1. 安装 spacetime CLI 工具。
  2. 使用 spacetime start 启动 SpacetimeDB 独立节点。
  3. 使用支持的模块语言编写并上传模块。
  4. 使用客户端库连接到数据库。

核心代码

以下是在 MacOS + Linux 上从源代码安装 SpacetimeDB 的核心代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Install rustup
curl https://sh.rustup.rs -sSf | sh
# Clone SpacetimeDB
git clone https://github.com/clockworklabs/SpacetimeDB
# Build and install the CLI
cd SpacetimeDB
cargo build --locked --release -p spacetimedb-standalone -p spacetimedb-update -p spacetimedb-cli
# Create directories
mkdir -p ~/.local/bin
export STDB_VERSION="$(./target/release/spacetimedb-cli --version | sed -n 's/.*spacetimedb tool version \([0-9.]*\);.*/\1/p')"
mkdir -p ~/.local/share/spacetime/bin/$STDB_VERSION
# Install the update binary
cp target/release/spacetimedb-update ~/.local/bin/spacetime
cp target/release/spacetimedb-cli ~/.local/share/spacetime/bin/$STDB_VERSION
cp target/release/spacetimedb-standalone ~/.local/share/spacetime/bin/$STDB_VERSION
# Add path
export PATH="$HOME/.local/bin:$PATH"
# Set version
spacetime version use $STDB_VERSION

最佳实践

  • 对于实时应用开发,如游戏、聊天和协作工具,优先考虑使用 SpacetimeDB 以获得低延迟和高速度。
  • 使用单一语言 Rust 编写整个应用程序,可简化开发和部署过程。
  • 遵循官方的安装和使用文档,确保正确配置和使用 SpacetimeDB。

常见问题

  • 从源代码安装问题:除非 master 分支有尚未发布的功能,否则建议遵循官方安装说明,避免从源代码安装带来的不必要麻烦。
  • Windows 安装问题:Windows 安装需要特定版本的 Perl(如 Strawberry Perl)和 openssl 二进制文件,确保这些依赖项正确安装。
  • 版本管理问题:若 STDB_VERSION 未设置,可使用 spacetime version list 命令列出可用版本。

GitHub - clockworklabs/SpacetimeDB: Multiplayer at the speed of light
https://119291.xyz/posts/github-clockworklabs-spacetimedb-multiplayer-at-the-speed-of-light/
作者
ww
发布于
2025年5月20日
许可协议