|
|
@ -1,30 +1,36 @@ |
|
|
|
Consul API client |
|
|
|
# Consul API Client |
|
|
|
================= |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
This package provides the `api` package which attempts to |
|
|
|
This package provides the `api` package which provides programmatic access to the full Consul API. |
|
|
|
provide programmatic access to the full Consul API. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Currently, all of the Consul APIs included in version 0.6.0 are supported. |
|
|
|
The full documentation is available on [Godoc](https://godoc.org/github.com/hashicorp/consul/api). |
|
|
|
|
|
|
|
|
|
|
|
Documentation |
|
|
|
## Usage |
|
|
|
============= |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The full documentation is available on [Godoc](https://godoc.org/github.com/hashicorp/consul/api) |
|
|
|
Below is an example of using the Consul client. To run the example, you must first |
|
|
|
|
|
|
|
[install Consul](https://developer.hashicorp.com/consul/downloads) and |
|
|
|
|
|
|
|
[Go](https://go.dev/doc/install). |
|
|
|
|
|
|
|
|
|
|
|
Usage |
|
|
|
To run the client API, create a new Go module. |
|
|
|
===== |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Below is an example of using the Consul client: |
|
|
|
```shell |
|
|
|
|
|
|
|
go mod init consul-demo |
|
|
|
|
|
|
|
``` |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Copy the example code into a file called `main.go` in the directory where the module is defined. |
|
|
|
|
|
|
|
As seen in the example, the Consul API is often imported with the alias `capi`. |
|
|
|
|
|
|
|
|
|
|
|
```go |
|
|
|
```go |
|
|
|
package main |
|
|
|
package main |
|
|
|
|
|
|
|
|
|
|
|
import "github.com/hashicorp/consul/api" |
|
|
|
import ( |
|
|
|
import "fmt" |
|
|
|
"fmt" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
capi "github.com/hashicorp/consul/api" |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
func main() { |
|
|
|
func main() { |
|
|
|
// Get a new client |
|
|
|
// Get a new client |
|
|
|
client, err := api.NewClient(api.DefaultConfig()) |
|
|
|
client, err := capi.NewClient(capi.DefaultConfig()) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
panic(err) |
|
|
|
panic(err) |
|
|
|
} |
|
|
|
} |
|
|
@ -33,7 +39,7 @@ func main() { |
|
|
|
kv := client.KV() |
|
|
|
kv := client.KV() |
|
|
|
|
|
|
|
|
|
|
|
// PUT a new KV pair |
|
|
|
// PUT a new KV pair |
|
|
|
p := &api.KVPair{Key: "REDIS_MAXCLIENTS", Value: []byte("1000")} |
|
|
|
p := &capi.KVPair{Key: "REDIS_MAXCLIENTS", Value: []byte("1000")} |
|
|
|
_, err = kv.Put(p, nil) |
|
|
|
_, err = kv.Put(p, nil) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
panic(err) |
|
|
|
panic(err) |
|
|
@ -48,19 +54,23 @@ func main() { |
|
|
|
} |
|
|
|
} |
|
|
|
``` |
|
|
|
``` |
|
|
|
|
|
|
|
|
|
|
|
To run this example, start a Consul server: |
|
|
|
Install the Consul API dependency with `go mod tidy`. |
|
|
|
|
|
|
|
|
|
|
|
```bash |
|
|
|
In a separate terminal window, start a local Consul server. |
|
|
|
consul agent -dev |
|
|
|
|
|
|
|
|
|
|
|
```shell |
|
|
|
|
|
|
|
consul agent -dev -node machine |
|
|
|
``` |
|
|
|
``` |
|
|
|
|
|
|
|
|
|
|
|
Copy the code above into a file such as `main.go`. |
|
|
|
Run the example. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```shell |
|
|
|
|
|
|
|
go run . |
|
|
|
|
|
|
|
``` |
|
|
|
|
|
|
|
|
|
|
|
Install and run. You'll see a key (`REDIS_MAXCLIENTS`) and value (`1000`) printed. |
|
|
|
You should get the following result printed to the terminal. |
|
|
|
|
|
|
|
|
|
|
|
```bash |
|
|
|
```shell |
|
|
|
$ go get |
|
|
|
|
|
|
|
$ go run main.go |
|
|
|
|
|
|
|
KV: REDIS_MAXCLIENTS 1000 |
|
|
|
KV: REDIS_MAXCLIENTS 1000 |
|
|
|
``` |
|
|
|
``` |
|
|
|
|
|
|
|
|
|
|
|