zookeeper常常在微服务系统中扮演核心的角色,我们在使用zookeeper时也遇到了一些容易犯的坑。比如某个时间突然发现rpc server流量没了,上去服务看了下zk连接还在,进一步发现临时节点没了...跟了下rpc server的日志看到,见截图:
参考下代码注释:
`
const (
StateUnknown State = -1
StateDisconnected State = 0
StateConnecting State = 1
StateAuthFailed State = 4
StateConnectedReadOnly State = 5
StateSaslAuthenticated State = 6
StateExpired State = -112
StateConnected = State(100)
StateHasSession = State(101)
)
const (
FlagEphemeral = 1
FlagSequence = 2
)
`
可以看到,client断开连接后重连,session过期后也重新创建了。但是,server创建的临时节点消失了。原来,zookeeper的临时节点、watcher等都是和session绑定的!而一旦session过期,zookeeper server就会清除和session有关的这些状态和数据。client要做的就是重建连接,重新创建临时节点,重新watch。另外watch本身也有要注意的,收到事件通知后,需要重新注册watcher。
网友评论