All Projects → h2non → gentleman-consul

h2non / gentleman-consul

Licence: MIT license
Gentleman's plugin for Consul server discovery and optional configurable retry/backoff policies

Programming Languages

go
31211 projects - #10 most used programming language

gentleman-consul Build Status GoDoc Coverage Status Go Report Card

gentleman's v2 plugin for easy service discovery using Consul.

Provides transparent retry/backoff support for resilient and reactive HTTP client capabilities.
It also allows you to use custom retry strategies, such as constant or exponential retries.

Installation

go get -u gopkg.in/h2non/gentleman-consul.v2

Versions

  • v1 - First version, uses gentleman@v1.
  • v2 - Latest version, uses gentleman@v2.

API

See godoc reference for detailed API documentation.

Examples

See examples directory for featured examples.

Simple request

package main

import (
  "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

package main

import (
  "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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].