Home

Awesome

GRPC consul resolver

Feature rich and easy-to-use resolver which return endpoints for service from the Hashicorp Consul and watch for the changes.

This library is production ready and will always save backward-compatibility

Quick Start

For using resolving endpoints from your Hashicorp Consul just import this library with import _ /github.com/mbobakov/grpc-consul-resolver and pass valid connection string to the grpc.Dial.

For full example see this section

Connection string

consul://[user:password@]127.0.0.127:8555/my-service?[healthy=]&[wait=]&[near=]&[insecure=]&[limit=]&[tag=]&[token=]

Parameters:

NameFormatDescription
tagstringSelect endpoints only with this tag
healthytrue/falseReturn only endpoints which pass all health-checks. Default: false
waitas in time.ParseDurationWait time for watch changes. Due this time period endpoints will be force refreshed. Default: inherits agent property
insecuretrue/falseAllow insecure communication with Consul. Default: true
nearstringSort endpoints by response duration. Can be efficient combine with limit parameter default: "_agent"
limitintLimit number of endpoints for the service. Default: no limit
timeoutas in time.ParseDurationHttp-client timeout. Default: 60s
max-backoffas in time.ParseDurationMax backoff time for reconnect to consul. Reconnects will start from 10ms to max-backoff exponentialy with factor 2. Default: 1s
tokenstringConsul token
dcstringConsul datacenter to choose. Optional
allow-staletrue/falseAllow stale results from the agent. https://www.consul.io/api/features/consistency.html#stale
require-consistenttrue/falseRequireConsistent forces the read to be fully consistent. This is more expensive but prevents ever performing a stale read.

Example

package main

import (
	"time"
	"log"

	_ "github.com/mbobakov/grpc-consul-resolver" // It's important

	"google.golang.org/grpc"
)

func main() {
    conn, err := grpc.Dial(
        "consul://127.0.0.1:8500/whoami?wait=14s&tag=manual",
        grpc.WithInsecure(),
        grpc.WithDefaultServiceConfig(`{"loadBalancingPolicy": "round_robin"}`),
    )
    if err != nil {
        log.Fatal(err)
    }
    defer conn.Close()
    ...
}

License

MIT-LICENSE. See LICENSE or the LICENSE file provided in the repository for details.