https://github.com/h2non/gentleman-consul
Gentleman's plugin for Consul server discovery and optional configurable retry/backoff policies
https://github.com/h2non/gentleman-consul
Last synced: about 2 months ago
JSON representation
Gentleman's plugin for Consul server discovery and optional configurable retry/backoff policies
- Host: GitHub
- URL: https://github.com/h2non/gentleman-consul
- Owner: h2non
- License: mit
- Created: 2016-03-09T16:34:50.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-09-11T08:48:10.000Z (almost 8 years ago)
- Last Synced: 2024-10-18T11:25:59.253Z (8 months ago)
- Language: Go
- Homepage:
- Size: 30.3 KB
- Stars: 19
- Watchers: 3
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: History.md
- License: LICENSE
Awesome Lists containing this project
README
# [gentleman](https://github.com/h2non/gentleman)-consul [](https://travis-ci.org/h2non/gentleman-consul) [](https://godoc.org/github.com/h2non/gentleman-consul) [](https://coveralls.io/github/h2non/gentleman-consul?branch=master) [](https://goreportcard.com/report/github.com/h2non/gentleman-consul)
[gentleman](https://github.com/h2non/gentleman)'s v2 plugin for easy service discovery using [Consul](https://www.consul.io).
Provides transparent retry/backoff support for resilient and [reactive](http://www.reactivemanifesto.org) HTTP client capabilities.
It also allows you to use custom [retry strategies](#custom-retry-strategy), such as [constant](https://godoc.org/github.com/eapache/go-resiliency/retrier#ConstantBackoff) or [exponential](https://godoc.org/github.com/eapache/go-resiliency/retrier#ExponentialBackoff) retries.## Installation
```bash
go get -u gopkg.in/h2non/gentleman-consul.v2
```## Versions
- **[v1](https://github.com/h2non/gentleman-consul/tree/v1)** - First version, uses `gentleman@v1`.
- **[v2](https://github.com/h2non/gentleman-consul/tree/master)** - Latest version, uses `gentleman@v2`.## API
See [godoc reference](https://godoc.org/github.com/h2non/gentleman-consul) for detailed API documentation.
## Examples
See [examples](https://github.com/h2non/gentleman-consul/blob/master/_examples) directory for featured examples.
#### Simple request
```go
package mainimport (
"fmt""gopkg.in/h2non/gentleman.v2"
"gopkg.in/h2non/gentleman-consul.v2"
)func main() {
// Create a new client
cli := gentleman.New()// Register Consul's plugin at client level
cli.Use(consul.New(consul.NewConfig("demo.consul.io", "web")))// Create a new request based on the current client
req := cli.Request()// Set a new header field
req.SetHeader("Client", "gentleman")// Perform the request
res, err := req.Send()
if err != nil {
fmt.Printf("Request error: %s\n", err)
return
}
if !res.Ok {
fmt.Printf("Invalid server response: %d\n", res.StatusCode)
return
}// Print response info
fmt.Printf("Server URL: %s\n", res.RawRequest.URL.String())
fmt.Printf("Response status: %d\n", res.StatusCode)
fmt.Printf("Server header: %s\n", res.Header.Get("Server"))
}
```#### Custom retry strategy
```go
package mainimport (
"fmt"
"time""gopkg.in/h2non/gentleman.v2"
"gopkg.in/h2non/gentleman-consul.v1"
"gopkg.in/eapache/go-resiliency.v1/retrier"
)func main() {
// Create a new client
cli := gentleman.New()// Configure Consul plugin
config := consul.NewConfig("demo.consul.io", "web")// Use a custom retrier strategy with max 10 retry attempts
config.Retrier = retrier.New(retrier.ConstantBackoff(10, time.Duration(25*time.Millisecond)), nil)// Register Consul's plugin at client level
cli.Use(consul.New(config))// Create a new request based on the current client
req := cli.Request()// Set a new header field
req.SetHeader("Client", "gentleman")// Perform the request
res, err := req.Send()
if err != nil {
fmt.Printf("Request error: %s\n", err)
return
}
if !res.Ok {
fmt.Printf("Invalid server response: %d\n", res.StatusCode)
return
}// Print response info
fmt.Printf("Server URL: %s\n", res.RawRequest.URL.String())
fmt.Printf("Response status: %d\n", res.StatusCode)
fmt.Printf("Server header: %s\n", res.Header.Get("Server"))
}
```## License
MIT - Tomas Aparicio