Service Discovery Extension
kitex-contrib has provided multiple service discovery extensions: DNS, ETCD, ZooKeeper, Eureka, Consul, Nacos, Polaris.
If you want to adopt other service discovery protocol, you can implement the Resolver
interface, and clients can inject it by WithResolver
Option.
Interface Definition
The interface is defined in pkg/discovery/discovery.go
and is defined as follows:
type Resolver interface {
Target(ctx context.Context, target rpcinfo.EndpointInfo) string
Resolve(ctx context.Context, key string) (Result, error)
Diff(key string, prev, next Result) (Change, bool)
Name() string
}
type Result struct {
Cacheable bool // if can be cached
CacheKey string // the unique key of cached result
Instances []Instance // the result of service discovery
}
// the diff result
type Change struct {
Result Result
Added []Instance
Updated []Instance
Removed []Instance
}
Resolver
interface detail:
Resolve
: as the core method ofResolver
, it obtains the service discovery result from target keyTarget
: it resolves the unique target endpoint that from the downstream endpoints provided byResolve
, and the result will be used as the unique key of the cacheDiff
: it is used to compare the discovery results with the last time. The differences in results are used to notify other components, such as loadbalancer and circuitbreaker, etcName
: it is used to specify a unique name forResolver
, and will use it to cache and reuseResolver
Usage Example
You need to implement the the Resolver
interface, and using it by Option:
import (
"xx/kitex/client"
)
func main() {
opt := client.WithResolver(YOUR_RESOLVER)
// new client
xxx.NewClient("destServiceName", opt)
}
Attention
To improve performance, Kitex reusing Resolver
, so the Resolver
method implementation must be concurrent security.
Last modified
April 19, 2023
: chore: fix link error (#615) (e17d4ec)