go-dqlite

libdqlite 的 Go 绑定。dqlite 是一个用 C 语言开发的可嵌入的,支持流复制的数据库引擎,具备高可用性和自动故障转移功能。「Go bindings for libdqlite. dqlite is a C library that implements an embeddable and replicated SQL database engine with high availability and automatic failover.」

Github星跟蹤圖

go-dqlite Build Status Coverage Status Go Report Card GoDoc

This repository provides the go-dqlite Go package, containing bindings for the
dqlite C library and a pure-Go
client for the dqlite wire protocol.

Usage

The best way to understand how to use the go-dqlite package is probably by
looking at the source code of the demo
program
and
use it as example.

In general your application will use code such as:

dir := "/path/to/data/directory"
address := "1.2.3.4:666" // Unique node address
cluster := []string{...} // Optional list of existing nodes, when starting a new node
app, err := app.New(dir, app.WithAddress(address), app.WithCluster(cluster))
if err != nil {
        // ...
}

db, err := app.Open(context.Background(), "my-database")
if err != nil {
        // ...
}

// db is a *sql.DB object
if _, err := db.Exec("CREATE TABLE my_table (n INT)"); err != nil
        // ...
}

Build

In order to use the go-dqlite package in your application, you'll need to have
the dqlite C library installed on your
system, along with its dependencies. You then need to pass the -tags
argument to the Go tools when building or testing your packages, for example:

go build -tags libsqlite3
go test -tags libsqlite3

Documentation

The documentation for this package can be found on Godoc.

Demo

To see dqlite in action, either install the Debian package from the PPA:

sudo add-apt-repository -y ppa:dqlite/stable
sudo apt install dqlite libdqlite-dev

or build the dqlite C library and its dependencies from source, as described
here, and then run:

go install -tags libsqlite3 ./cmd/dqlite-demo

from the top-level directory of this repository.

This builds a demo dqlite application, which exposes a simple key/value store
over an HTTP API.

Once the dqlite-demo binary is installed (normally under ~/go/bin),
start three nodes of the demo application:

dqlite-demo --api 127.0.0.1:8001 --db 127.0.0.1:9001 &
dqlite-demo --api 127.0.0.1:8002 --db 127.0.0.1:9002 --join 127.0.0.1:9001 &
dqlite-demo --api 127.0.0.1:8003 --db 127.0.0.1:9003 --join 127.0.0.1:9001 &

The --api flag tells the demo program where to expose its HTTP API.

The --db flag tells the demo program to use the given address for internal
database replication.

The --join flag is optional and should be used only for additional nodes after
the first one. It informs them about the existing cluster, so they can
automatically join it.

Now we can start using the cluster. Let's insert a key pair:

curl -X PUT -d my-key http://127.0.0.1:8001/my-value

and then retrive it from the database:

curl http://127.0.0.1:8001/my-value

Currently the first node is the leader. If we stop it and then try to query the
key again curl will fail, but we can simply change the endpoint to another node
and things will work since an automatic failover has taken place:

kill -TERM %1; curl http://127.0.0.1:8002/my-value

Shell

A basic SQLite-like dqlite shell can be built with:

go install -tags libsqlite3 ./cmd/dqlite

You can test it with the dqlite-demo with:

dqlite -s 127.0.0.1:9001

It supports normal SQL queries plus the special .cluster and .leader
commands to inspect the cluster members and the current leader.

主要指標

概覽
名稱與所有者canonical/go-dqlite
主編程語言Go
編程語言Emacs Lisp (語言數: 3)
平台Linux
許可證Apache License 2.0
所有者活动
創建於2018-07-30 14:58:37
推送於2025-06-06 12:54:38
最后一次提交2025-02-13 11:38:43
發布數44
最新版本名稱v3.0.1 (發布於 )
第一版名稱v0.2.0 (發布於 )
用户参与
星數458
關注者數18
派生數77
提交數1.1k
已啟用問題?
問題數90
打開的問題數20
拉請求數229
打開的拉請求數15
關閉的拉請求數44
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?