Multiplayer at the speed of light
技术背景
SpacetimeDB 是一个将数据库和服务器功能合二为一的关系型数据库系统。传统的应用架构中,客户端与数据库之间需要部署一个 Web 或游戏服务器,而 SpacetimeDB 允许客户端直接连接到数据库,并通过名为“模块”的存储过程在数据库内部执行应用逻辑。它与智能合约的概念类似,但 SpacetimeDB 是一个数据库,与区块链无关,且速度比任何智能合约系统都要快得多。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
- 安装 rustup(若已安装
cargo
和 wasm32-unknown-unknown
目标则可跳过):curl https://sh.rustup.rs -sSf | sh
。 - 克隆 SpacetimeDB 仓库:
git clone https://github.com/clockworklabs/SpacetimeDB
。 - 构建并安装 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
- 安装 Strawberry Perl、openssl 二进制文件和 rustup for Windows。
- 克隆 SpacetimeDB 仓库:
git clone https://github.com/clockworklabs/SpacetimeDB
。 - 构建并安装 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
使用以下命令启动一个新的 SpacetimeDB 实例:
1
| docker run --rm --pull always -p 3000:3000 clockworklabs/spacetime start
|
快速上手
- 安装
spacetime
CLI 工具。 - 使用
spacetime start
启动一个 SpacetimeDB 独立节点。 - 使用支持的模块语言编写并上传一个模块。
- 使用客户端库连接到数据库。
核心代码
由于文档未给出具体的业务核心代码示例,以下为安装相关代码示例:
MacOS + Linux 安装
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| curl https://sh.rustup.rs -sSf | sh
git clone https://github.com/clockworklabs/SpacetimeDB
cd SpacetimeDB cargo build --locked --release -p spacetimedb-standalone -p spacetimedb-update -p spacetimedb-cli
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
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
export PATH="$HOME/.local/bin:$PATH"
spacetime version use $STDB_VERSION
|
Windows 安装
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| git clone https://github.com/clockworklabs/SpacetimeDB
cd SpacetimeDB cargo build --locked --release -p spacetimedb-standalone -p spacetimedb-update -p spacetimedb-cli
$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
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\"
spacetime version use $stdbVersion
|
最佳实践
- 对于实时应用开发,如游戏、聊天等,优先考虑使用 SpacetimeDB 以获得低延迟和高速度的体验。
- 尽量使用支持的语言编写模块,如 Rust 和 C#,利用其性能优势。
- 在安装时,优先使用官方提供的安装脚本,除非需要使用未发布的特性,再考虑从源代码安装。
常见问题
- 安装问题:若在从源代码安装时遇到依赖问题,如缺少
perl
或 openssl
,按照文档提示安装相应的软件。 - 版本问题:若
STDB_VERSION
未正确设置,可以使用 spacetime version list
命令列出可用版本,然后重新设置。 - 路径问题:在 Windows 上添加路径时,建议添加到系统路径,以确保所有应用都能访问 SpacetimeDB。添加后需要重启 shell 使路径生效。