Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhanced for Nacos weight configuration #1406

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ static List<ProviderInfo> convertInstancesToProviders(List<Instance> allInstance
for (Instance instance : allInstances) {
String url = convertInstanceToUrl(instance);
ProviderInfo providerInfo = ProviderHelper.toProviderInfo(url);

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

providerInfos.add(providerInfo);
}
return providerInfos;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,61 @@ public void buildServiceName() {
serviceName = NacosRegistryHelper.buildServiceName(provider, RpcConstants.PROTOCOL_TYPE_REST);
assertEquals(serviceName, "com.alipay.xxx.TestService:nacos-test:" + RpcConstants.PROTOCOL_TYPE_REST);
}
}

@Test
public void testNacosWeight() {
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.99D);

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

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

@Test
public void testNacosWeightLessThan0() {
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.1D);

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

ProviderInfo providerInfo = providerInfos.get(0);
assertNotNull(providerInfo);
assertEquals(-10, 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());
}
}
Loading