跳至主要內容

本地运行 SubQuery

SubQuery Team大约 5 分钟

本地运行 SubQuery

本指南通过如何在您的基础设施上运行本地的 SubQuery 节点,其中包括索引器和查询服务。 不用担心在运行自己的 SubQuery 基础架构中所出现的问题。 SubQuery provides a Managed Serviceopen in new window to the community for free. 按照我们所发布的指南 查看您如何将项目部署到 SubQuery 项目open in new window

使用 Docker

其中一种解决方案是运行Docker 容器,它是由Docker-component.yml文件所定义的。 对于刚刚初始化的新项目,您无需在此处进行任何更改。

在项目目录下运行以下命令:

docker-compose pull && docker-compose up

第一次下载所需软件包可能需要一些时间(@subql/nodeopen in new window, @subql/quiryopen in new window, and Postgress) ,但很快你就会看到一个运行中的 SubQuery 节点。 :::

运行 Indexer (subql/node)

需求:

SubQuery 节点需要一个加载的过程,它能够从 SubQuery 项目中提取基于子区块链的数据,并将其保存到 Postgres 数据库。

If you are running your project locally using subql-node or subql-node-<network>, make sure you enable the pg_extension btree_gist

You can run the following SQL query:

CREATE EXTENSION IF NOT EXISTS btree_gist;

安装

# NPM
npm install -g @subql/node

Please note that we **DO NOT** encourage the use of `yarn global` due to its poor dependency management which may lead to an errors down the line. :::

安装完毕后,您可以使用以下命令来启动节点:

subql-node <command>

Key Commands

The following commands will assist you to complete the configuration of a SubQuery node and begin indexing. 要了解更多信息,您可以运行 --help

指向本地项目路径

subql-node -f your-project-path

Connect to database

export DB_USER=postgres
export DB_PASS=postgres
export DB_DATABASE=postgres
export DB_HOST=localhost
export DB_PORT=5432
subql-node -f your-project-path

根据您的 Postgres 数据库的配置(例如不同的数据库密码),请同时确保索引器 (subql/node) 和查询服务 (subql/query ) 可以建立到它的连接。

指定一个配置文件

subql-node -c your-project-config.yml

This will point the query node to a manifest file which can be in YAML or JSON format.

Change the block fetching batch size

subql-node -f your-project-path --batch-size 200

Result:
[IndexerManager] fetch block [203, 402]
[IndexerManager] fetch block [403, 602]

索引器首次对链进行索引时,获取单个区块将显著降低性能。 增加批量大小以调整获取的区块数将减少整体处理时间。 当前的默认批量大小为 100。

在本地模式下运行

subql-node -f your-project-path --local

For debugging purposes, users can run the node in local mode. 切换到本地模型将在默认架构 public 中创建 Postgres 表。

如果不使用本地模式,将创建具有初始 subquery_ 和相应项目表的新 Postgres 模式。

检查您的节点健康状况

有两个端口可用来检查和监视所运行的 SubQuery 节点的健康状况。

  • 健康检查端点,返回一个简单的 200 个响应。
  • 元数据端点,包括您正在运行的 SubQuery 节点的附加分析。

将此附加到您的 SubQuery 节点的基本 URL。 例如:http://localhost:3000/meta 将会返回

{
    "currentProcessingHeight": 1000699,
    "currentProcessingTimestamp": 1631517883547,
    "targetHeight": 6807295,
    "bestHeight": 6807298,
    "indexerNodeVersion": "0.19.1",
    "lastProcessedHeight": 1000699,
    "lastProcessedTimestamp": 1631517883555,
    "uptime": 41.151789063,
    "polkadotSdkVersion": "5.4.1",
    "apiConnected": true,
    "injectedApiConnected": true,
    "usingDictionary": false,
    "chain": "Polkadot",
    "specName": "polkadot",
    "genesisHash": "0x91b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3",
    "blockTime": 6000
}

http://localhost:3000/health 如果成功,将返回 HTTP 200。

如果索引器不正常,将返回 500 错误。 这通常可以在节点启动时看到。

{
    "status": 500,
    "error": "Indexer is not healthy"
}

如果使用了不正确的 URL,将返回 404 not found 错误。

{
"statusCode": 404,
"message": "Cannot GET /healthy",
"error": "Not Found"
}

调试您的项目

使用 节点检查器open in new window 运行以下命令。

node --inspect-brk <path to subql-node> -f <path to subQuery project>

例如:

node --inspect-brk /usr/local/bin/subql-node -f ~/Code/subQuery/projects/subql-helloworld/
Debugger listening on ws://127.0.0.1:9229/56156753-c07d-4bbe-af2d-2c7ff4bcc5ad
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.

然后打开 Chrome 开发工具,进入 Source>Filesystem,将项目添加到工作区并开始调试。 查看更多信息如何调试 SubQuery 项目.

运行 Query 服务(subql/query)

安装

# NPM
npm install -g @subql/query

::: 危险 请注意,我们鼓励使用 yarn global,因为它糟糕的依赖管理可能会导致错误。 :::

运行 Query 服务

export DB_HOST=localhost
subql-query --name <project_name> --playground

确保项目名称与初始化项目时的项目名称相同。 另外,请检查环境变量是否正确。

成功运行 subql 查询服务后,打开浏览器并转到http://localhost:3000. 您应该看到在 Explorer 中显示的 GraphQL 播放地和准备查询的模式。 您应该看到在 Explorer 中显示的 GraphQL 播放器和准备查询的模式。