Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to use the tcpServer command in nsqlookupd

Shulou Source: shulou.com Published: 2022-06-01 02:55:06 09月26日 Update

这篇文章主要介绍nsqlookupd中tcpServer命令怎么用,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

一、tcpServer

1、case "PING": 健康检查

更新 client.peerInfo.lastUpdate 为 time.Now().UnixNano()

回复 "OK"

2、case "IDENTIFY": 身份认证,client 注册(ip,port等)

此命令,每个 client 只发送一次。

server 继续从 conn 读取1个 int32,这个数字表明 body 长度

err = binary.Read(reader, binary.BigEndian, &bodyLen)

继续从 conn 读取上面获取长度的字节流,原始报文是 json 格式

peerInfo := PeerInfo{id: client.RemoteAddr().String()}

err = json.Unmarshal(body, &peerInfo)

解码成 PeerInfo;同时 验证字段是否完整,设置 lateUpdate

下一步就是要把 producer 的信息保存起来,这块涉及一个非常重要的 struct,在 NSQLookupd 对其有一个指针。

type NSQLookupd struct {

// ............

DB *RegistrationDB

}

type RegistrationDB struct {

sync.RWMutex // 锁用来协调多个协程的访问

registrationMap map[Registration]Producers // producer slice

}

// 下面这句是非常重要的注册 添加生产者,注意到 Registration 只有第一个字段有值

// 添加时会做判断,根据 peerInfo.Id,也就是 client 的 ip 地址

p.context.nsqlookupd.DB.AddProducer(Registration{"client", "", ""}, &Producer{peerInfo: client.peerInfo})

注意其中锁的使用,sync.RWMutex 默认初始值就可以直接使用

r.Lock()

defer r.Unlock() // 方法结束时,确保释放锁

最后一步把 nsqlookupd 主机的信息编码,发送给 client

3、case "REGISTER": topic,channel 注册

client 首先要 IDENTITY,即身份认证

// channel 注册

key := Registration{"channel", topic, channel}

p.context.nsqlookupd.DB.AddProducer(key, &Producer{peerInfo: client.peerInfo})

// topic 注册

key := Registration{"topic", topic, ""}

p.context.nsqlookupd.DB.AddProducer(key, &Producer{peerInfo: client.peerInfo})

注意 key 和 client 认证时候的不同

给 client 返回 "OK"

4、case "UNREGISTER": 注销 topic, channel

这个地方有个小细节,不明白作者为什么要创建新的 Producer slice

cleaned := make(Producers, 0)

for _, producer := range producers {

if producer.peerInfo.id != id {

cleaned = append(cleaned, producer)

} else {

removed = true

}

}

// Note: this leaves keys in the DB even if they have empty lists

r.registrationMap[k] = cleaned

以上是"nsqlookupd中tcpServer命令怎么用"这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!

Tags: Commands authentication important information content fields articles identity authentication length different original health host that is value author interest only at the same time Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology macOS Shulou Tech Info Redmi MariaDB