Skip to content

Commit

Permalink
Enhanced for Nacos weight configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
呈铭 committed Mar 26, 2024
1 parent 2e9309a commit 31bd1fb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,11 @@ static List<ProviderInfo> convertInstancesToProviders(List<Instance> allInstance
String url = convertInstanceToUrl(instance);
ProviderInfo providerInfo = ProviderHelper.toProviderInfo(url);

double nacosWeight = instance.getWeight();
// Nacos的默认权重为1.0
// 当Nacos默认权重传入1.0,根据代码逻辑计算结果为100,与sofa-rpc默认权重一致
// 因为是接口级别,如果不同的服务具有不同的权重也不会出现被覆盖或者冲突的情况
if (nacosWeight >= 0.0) {
long weight = Math.round(providerInfo.getWeight() * nacosWeight);
providerInfo.setWeight((int) weight);
}
long weight = Math.round(providerInfo.getWeight() * instance.getWeight());
providerInfo.setWeight((int) weight);

providerInfos.add(providerInfo);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,23 @@ public void testNacosWeightLessThan0() {
assertNotNull(providerInfo);
assertEquals(100, providerInfo.getWeight());
}

@Test
public void testNacosWeightWith0() {
Instance instance = new Instance();
instance.setClusterName(NacosRegistryHelper.DEFAULT_CLUSTER);
instance.setIp("1.1.1.1");
instance.setPort(12200);
instance.setServiceName("com.alipay.xxx.TestService");
instance.setWeight(0.0D);

List<ProviderInfo> providerInfos = NacosRegistryHelper
.convertInstancesToProviders(Lists.newArrayList(instance));
assertNotNull(providerInfos);
assertEquals(1, providerInfos.size());

ProviderInfo providerInfo = providerInfos.get(0);
assertNotNull(providerInfo);
assertEquals(0, providerInfo.getWeight());
}
}

0 comments on commit 31bd1fb

Please sign in to comment.