参考我前面的一篇文章(点击这里),前面引入一个在Etcd集群环境下的关键性问题:
三个Etcd节点组成Clustering应该访问那个(进行操作请求)???
(1)针对读取操作三个任意一个都可以,即使它不是leader
(2)针对写入操作,好像只能通过连接leader来进行写入。
我有一个由三个节点组成的集群(127.0.0.1:4001、127.0.0.1:4002以及127.0.0.1:4003),有一个连接到集群开启定时器定时注册服务(实际上是定时创建带TTL的Node)的程序,如下所示:
- string sysFlag = "CBIP";
- IRegistryCenterClient rCenter = RegistryCenterClientFactory.GetRegistryCenterClient();
- ServiceInfo sInfo1 = new ServiceInfo();
- sInfo1.serviceName = "HelloService";
- sInfo1.serviceIP = "127.0.0.111";
- sInfo1.servicePort = 1888;
- rCenter.RegisterService(sInfo1);
- while (true)
- {
- Console.WriteLine(rCenter.GetConfigItem(sysFlag, "configA"));
- Console.WriteLine(rCenter.GetConfigItem(sysFlag, "configB"));
- Thread.Sleep(200);
- }
我连接到的是集群中的127.0.0.1:4001节点,开始的时候集群的leader是127.0.0.1:4001,但是随着时间推移leader会产生变化,可能会变成127.0.0.1:4002或者127.0.0.1:4003,我发现一个结论: 只要leader是127.0.0.1:4001,服务就能够成功注册(成功写入集群),只要leader不是127.0.0.1:4001,就会注册失败!而循环中读取配置项会一直有效,不会随着leader的变化失效。
问题: 为什么我按照这个教程启动的三个节点的集群,随时时间推移,leader会变来变去???
然后我把这个问题在Etcd的GitHub上向作者提问:
jiq408694711 commented 13 days agoHello:
I have built a cluster of three machines according to this document:https://github.com/coreos/etcd/blob/master/Documentation/clustering.md .
then i connect to 127.0.0.1:4001 by etcetera and register a service address(actually write service info to etcd node with ttl) by using .NET timer:
private void KeepAliveTimer(object source, System.Timers.ElapsedEventArgs e)
{
try
{
if (source != null && source is ServiceInfo)
{
ServiceInfo serviceInfo = source as ServiceInfo;
string serviceDir = "service/" + serviceInfo.serviceName;
client.CreateDir(serviceDir, 0, false);
string key = serviceDir + "/" + serviceInfo.serviceIP;
string value = JsonSerializer.GetJsonByObject(serviceInfo);
client.Set(key, value, nodeTTL);
logger.Info("[KeepAliveTimer]register success:[key]" + key + ",[value]" + value);
}
}
catch (Exception ee)
{
logger.Info("[KeepAliveTimer]register fail:" + ee.Message);
}
}
when i run my code, i find that the output sometimes is "register success", sometimes is "register fail", The reseaon is that the leader of cluster always change by itself, The registeration will fail if the leader is not "127.0.0.1:4001", The registeration will success if the leader is "127.0.0.1:4001".
I want to know :
(1) why the leader always keep changing???
(2) If i want to write to etcd correctly, which active peer i should connect ???
if the correct active peer is leader, how can i get the leader???
Thank you!!!
然后作者回答了:
unihorn commented 9 days ago(1) It is a common case for etcd to change leader, though it should not be so frequent.
(2) if you connect to non leader, it will send back a 307 redirect http response, which indicates where the leader is
然后我又问了:
jiq408694711 commented 8 days agothanks
why the etcd can not redirect my http request to leader automatically? i just need to connect to any peer of the clustering whitout concerning which is the leader
@jiq408694711 for now, honouring the redirect is the responsibility of the client, and many implementations will follow it automatically.
In future, etcd will have a proxy mode which will provide the behaviour you're after.
意思是请求重定向是client端的责任。。。许多实现方案都会自动做这个!!!看来我用的这个C#的etcd客户端没有考虑这个呀!!!
作者还说将来etcd会使用代理模式来弥补这一点。
etcd还比较新,现在还在不断开发中,1.0版本都还没有出来,让我们拭目以待吧!
目前我就先暂时不使用集群了,先用着一个节点吧。。。呵呵