github.imxd.top/hashicorp/consul@v1.4.5/api/README.md (about)

     1  Consul API client
     2  =================
     3  
     4  This package provides the `api` package which attempts to
     5  provide programmatic access to the full Consul API.
     6  
     7  Currently, all of the Consul APIs included in version 0.6.0 are supported.
     8  
     9  Documentation
    10  =============
    11  
    12  The full documentation is available on [Godoc](https://godoc.org/github.com/hashicorp/consul/api)
    13  
    14  Usage
    15  =====
    16  
    17  Below is an example of using the Consul client:
    18  
    19  ```go
    20  package main
    21  
    22  import "github.com/hashicorp/consul/api"
    23  import "fmt"
    24  
    25  func main() {
    26  	// Get a new client
    27  	client, err := api.NewClient(api.DefaultConfig())
    28  	if err != nil {
    29  		panic(err)
    30  	}
    31  
    32  	// Get a handle to the KV API
    33  	kv := client.KV()
    34  
    35  	// PUT a new KV pair
    36  	p := &api.KVPair{Key: "REDIS_MAXCLIENTS", Value: []byte("1000")}
    37  	_, err = kv.Put(p, nil)
    38  	if err != nil {
    39  		panic(err)
    40  	}
    41  
    42  	// Lookup the pair
    43  	pair, _, err := kv.Get("REDIS_MAXCLIENTS", nil)
    44  	if err != nil {
    45  		panic(err)
    46  	}
    47  	fmt.Printf("KV: %v %s\n", pair.Key, pair.Value)
    48  }
    49  ```
    50  
    51  To run this example, start a Consul server:
    52  
    53  ```bash
    54  consul agent -dev
    55  ```
    56  
    57  Copy the code above into a file such as `main.go`.
    58  
    59  Install and run. You'll see a key (`REDIS_MAXCLIENTS`) and value (`1000`) printed.
    60  
    61  ```bash
    62  $ go get
    63  $ go run main.go
    64  KV: REDIS_MAXCLIENTS 1000
    65  ```
    66  
    67  After running the code, you can also view the values in the Consul UI on your local machine at http://localhost:8500/ui/dc1/kv