smallnest / rpcx

Best microservices framework in Go, like alibaba Dubbo, but with more features, Scale easily. Try it. Test it. If you feel it's better, use it! 𝐉𝐚𝐯𝐚有𝐝𝐮𝐛𝐛𝐨, 𝐆𝐨𝐥𝐚𝐧𝐠有𝐫𝐩𝐜𝐱! build for cloud!
https://rpcx.io
Other
8.11k stars 1.17k forks source link

一致性hash, 想要自定义key #811

Closed samuelyao314 closed 1 year ago

samuelyao314 commented 1 year ago

当前,一致性hash 的key计算方式是固定的。

func (s consistentHashSelector) Select(ctx context.Context, servicePath, serviceMethod string, args interface{}) string { ss := s.servers if len(ss) == 0 { return "" }

key := genKey(servicePath, serviceMethod, args)
selected, _ := s.h.Get(key).(string)
return selected

}

业务需求是: 需要,根据玩家ID,进行一致性hash路由

err := xclient.Call(context.Background(), userid, "Mul", args, reply) if err != nil { log.Fatalf("failed to call: %v", err) }

smallnest commented 1 year ago

你这种需求很常见,但是不同用户的请求对象和key各不相同,所以你在一致性哈希的基础上,实现一个自定义的Selector: https://doc.rpcx.io/part3/selector.html#user_selector