• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

go-dqlite: Go bindings for libdqlite

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

go-dqlite

开源软件地址:

https://gitee.com/mirrors/go-dqlite

开源软件介绍:

go-dqlite Build Status Coverage Status Go Report Card GoDoc

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

Usage

The best way to understand how to use the go-dqlite package is probably bylooking at the source code of the demoprogram anduse 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 addresscluster := []string{...} // Optional list of existing nodes, when starting a new nodeapp, 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 objectif _, 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 havethe dqlite C library installed on yoursystem, along with its dependencies. You then need to pass the -tagsargument to the Go tools when building or testing your packages, for example:

export CGO_LDFLAGS_ALLOW="-Wl,-z,now"go build -tags libsqlite3go 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/devsudo apt install dqlite-tools libdqlite-dev

or build the dqlite C library and its dependencies from source, as describedhere, 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 storeover an HTTP API.

Once the dqlite-demo binary is installed (normally under ~/go/bin or/usr/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 internaldatabase replication.

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

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

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

and then retrive it from the database:

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

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

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

Shell

A basic SQLite-like dqlite shell is available in the dqlite-tools package orcan 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 .leadercommands to inspect the cluster members and the current leader.


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap