Git Product home page Git Product logo

Comments (6)

jizhuozhi avatar jizhuozhi commented on July 24, 2024 1

那我知道你这个问题在哪了,etcd实现在进行diff的时候是使用了DefaultDiff,而DefaultDiff只对addr进行了判断,这个是基于云环境下不可变实例的假设来做的,不过控制面动态修改权重确实是有效需求,DefaultDiff的逻辑我来修正下吧,cc @HeyJavaBean

https://github.com/kitex-contrib/registry-etcd/blob/8f24094a74c322f6fd3231d9a02de4d87047a93d/etcd_resolver.go#L107-L110

// DefaultDiff provides a natural implementation for the Diff method of the Resolver interface.
func DefaultDiff(cacheKey string, prev, next Result) (Change, bool) {
ch := Change{
Result: Result{
Cacheable: next.Cacheable,
CacheKey: cacheKey,
Instances: next.Instances,
},
}
prevMap := make(map[string]struct{}, len(prev.Instances))
for _, ins := range prev.Instances {
prevMap[ins.Address().String()] = struct{}{}
}
nextMap := make(map[string]struct{}, len(next.Instances))
for _, ins := range next.Instances {
addr := ins.Address().String()
nextMap[addr] = struct{}{}
if _, found := prevMap[addr]; !found {
ch.Added = append(ch.Added, ins)
}
}
for _, ins := range prev.Instances {
if _, found := nextMap[ins.Address().String()]; !found {
ch.Removed = append(ch.Removed, ins)
}
}
return ch, len(ch.Added)+len(ch.Removed) != 0
}

from kitex.

HeyJavaBean avatar HeyJavaBean commented on July 24, 2024

我觉得这个场景的需求不该通过强行设置负载均衡切留来实现
正常的解决方案是:你的服务1重启,在服务发现环节,服务1被剔除掉了,所以client的可用下游里面就没有服务1。当服务1完成重启后,重新注册回来,流量就正常分摊给5个下游了。
要达到上面的效果,你需要配合注册中心、健康检查这些手段来实现对应的代码。

from kitex.

hzxgo avatar hzxgo commented on July 24, 2024

@HeyJavaBean 我现在的业务就是你说的这种方式实现的。对于不需要100%保证业务无影响的情况下这样是没问题,但是要确保重启的绝对安全还是要无业务的情况下才稳妥,所以想到了先切流量再重启。

from kitex.

HeyJavaBean avatar HeyJavaBean commented on July 24, 2024

那我感觉从负载均衡可能做不到。不过既然你能知道要下线A服务(比如你还知道他的IP什么的),你可以尝试通过kitex的自定义服务发现resolver例添加逻辑,直接判断然后拦截掉A的服务发现结果?

from kitex.

jizhuozhi avatar jizhuozhi commented on July 24, 2024

不过从Picker创建的实现来看,权重为0的实例会被过滤掉,不会出现说流量分配到权重为0的实例的情况,可以给下Kitex初始化代码以及版本么

for idx, instance := range e.Instances {
weight := instance.Weight()
if weight <= 0 {
klog.Warnf("KITEX: invalid weight, weight=%d instance=%s", weight, e.Instances[idx].Address())
continue
}
weightSum += weight
instances[cnt] = instance
if cnt > 0 && instances[cnt-1].Weight() != weight {
balance = false
}
cnt++
}
instances = instances[:cnt]

from kitex.

hzxgo avatar hzxgo commented on July 24, 2024

@jizhuozhi 如下:

  1. kitex版本:kitex --version =》v0.9.1
  2. 初始化Client:
    c, err := demo.NewClient(
    variable.BuildClientName(hd.name()),
    client.WithMuxConnection(2),
    client.WithRPCTimeout(60time.Second),
    client.WithLoadBalancer(loadbalance.NewWeightedRandomBalancer()),
    client.WithConnectTimeout(10
    time.Second),
    client.WithFailureRetry(newFailurePolicy()),
    client.WithResolver(reg),
    )

操作流程:

  1. 2个微服务(A,B)正常启动后,ETCD里的weight都为10,此时2个微服务负载比例50%;
  2. 调整ETCD中A服务的weight=0,即使等待一会后,A服务还是会有流量,不会为0
  3. 调整ETCD中B服务的weight=9999999,A服务的weight不修改仍为10,此时流量基本都到A服务了,B服务基本没流量(目前我是采用这种方案在切流量)

from kitex.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.