Comments (2)
问题1,多个grpc服务后端话,每个后端是一个conn,然后通过注册其支持的接口,
Line 42 in 9238d81
问题2,参见代码注释的链接https://github.com/grpc/grpc/blob/master/doc/naming.md#name-syntax
from grpc-lb.
我大概知道是怎么回事了,
调用 grpc.DialContext(ctx, r.Scheme()+"://authority/"+"serviceName",...)时
会根据第二个参数解析出一个resolver.Target{}
Scheme=r.Scheme()
Authority="authority"
Endpoint="serviceName"
根据Scheme将拿到我们注册的Builder.
还有一个问题,grpc每次调用我们的Build方法,就会进行watch一个服务,但是我看grpc是根据schema来区分不同的Resolver,但是我要求可以查询每个不同的服务,请问如果schema相同,Endpoint(即服务)不同会重复调用Build方法吗。
这是我的代码,进行了一些修改,服务名从resolver.Target中拿
const schema = "wethedevelop/resolver"
// resolver is the implementaion of grpc.resolve.Builder
type Resolver struct {
endpoints string
cli *clientv3.Client
cc resolver.ClientConn
}
// NewResolver return resolver builder
// target example: "http://127.0.0.1:2379,http://127.0.0.1:12379,http://127.0.0.1:22379"
// service is service name
func NewResolver(endpoints string) resolver.Builder {
return &Resolver{endpoints: endpoints}
}
// Scheme return etcdv3 schema
func (r *Resolver) Scheme() string {
return schema
}
// ResolveNow
func (r *Resolver) ResolveNow(rn resolver.ResolveNowOptions) {
}
// Close
func (r *Resolver) Close() {
}
// Build to resolver.Resolver
func (r *Resolver) Build(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOptions) (resolver.Resolver, error) {
var err error
r.cli, err = clientv3.New(clientv3.Config{
Endpoints: strings.Split(r.endpoints, ","),
})
if err != nil {
return nil, fmt.Errorf("gateway: create clientv3 client failed: %v", err)
}
r.cc = cc
go r.watch(fmt.Sprintf("/%s/%s/", schema, target.Endpoint))
return r, nil
}
func (r *Resolver) watch(prefix string) {
addrDict := make(map[string]resolver.Address)
update := func() {
addrList := make([]resolver.Address, 0, len(addrDict))
for _, v := range addrDict {
addrList = append(addrList, v)
}
r.cc.UpdateState(resolver.State{Addresses: addrList})
}
resp, err := r.cli.Get(context.Background(), prefix, clientv3.WithPrefix())
if err == nil {
for i := range resp.Kvs {
addrDict[string(resp.Kvs[i].Value)] = resolver.Address{Addr: string(resp.Kvs[i].Value)}
}
}
update()
rch := r.cli.Watch(context.Background(), prefix, clientv3.WithPrefix(), clientv3.WithPrevKV())
for n := range rch {
for _, ev := range n.Events {
switch ev.Type {
case mvccpb.PUT:
addrDict[string(ev.Kv.Key)] = resolver.Address{Addr: string(ev.Kv.Value)}
case mvccpb.DELETE:
delete(addrDict, string(ev.PrevKv.Key))
}
}
update()
}
}
from grpc-lb.
Related Issues (12)
- 服务下线,其实并未从客户端将命名解析删除 HOT 1
- cli.go里面cancel的作用 HOT 1
- 不读取返回的消息是否不妥
- 话说现在包是怎么了。。。 HOT 1
- context deadline exceeded HOT 4
- register.go 里 unregister 不会造成死锁吗? HOT 1
- com.midea/jr/grpclb 这个库是哪里来的呢? HOT 3
- 请教一个别的问题 HOT 3
- 代码来源问题 HOT 1
- 部分Deprecated,还有更新计划吗? HOT 1
- KeepAlive方法性能问题 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from grpc-lb.