From e67ea542b414de26eae6a759e85ea7f5dff35b6c Mon Sep 17 00:00:00 2001 From: evenliu Date: Wed, 27 Mar 2024 16:55:21 +0800 Subject: [PATCH 1/2] fix test case error (#1407) Co-authored-by: liujianjun.ljj --- .../sofatracer}/SofaTracerSubscriberTest.java | 19 +++++-------------- .../test/resources/sofa-rpc/rpc-config.json | 3 ++- 2 files changed, 7 insertions(+), 15 deletions(-) rename {tracer/tracer-opentracing/src/test/java/com/alipay/sofa/rpc/event => test/test-integration/src/test/java/com/alipay/sofa/rpc/tracer/sofatracer}/SofaTracerSubscriberTest.java (91%) diff --git a/tracer/tracer-opentracing/src/test/java/com/alipay/sofa/rpc/event/SofaTracerSubscriberTest.java b/test/test-integration/src/test/java/com/alipay/sofa/rpc/tracer/sofatracer/SofaTracerSubscriberTest.java similarity index 91% rename from tracer/tracer-opentracing/src/test/java/com/alipay/sofa/rpc/event/SofaTracerSubscriberTest.java rename to test/test-integration/src/test/java/com/alipay/sofa/rpc/tracer/sofatracer/SofaTracerSubscriberTest.java index 4c46faa73..5f9b78b84 100644 --- a/tracer/tracer-opentracing/src/test/java/com/alipay/sofa/rpc/event/SofaTracerSubscriberTest.java +++ b/test/test-integration/src/test/java/com/alipay/sofa/rpc/tracer/sofatracer/SofaTracerSubscriberTest.java @@ -14,19 +14,20 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alipay.sofa.rpc.event; +package com.alipay.sofa.rpc.tracer.sofatracer; import com.alipay.common.tracer.core.holder.SofaTraceContextHolder; import com.alipay.common.tracer.core.span.SofaTracerSpan; import com.alipay.sofa.rpc.common.RemotingConstants; -import com.alipay.sofa.rpc.common.RpcOptions; import com.alipay.sofa.rpc.context.RpcInternalContext; import com.alipay.sofa.rpc.core.request.SofaRequest; +import com.alipay.sofa.rpc.event.ClientBeforeSendEvent; +import com.alipay.sofa.rpc.event.ClientStartInvokeEvent; +import com.alipay.sofa.rpc.event.EventBus; +import com.alipay.sofa.rpc.event.ServerReceiveEvent; import com.alipay.sofa.rpc.tracer.sofatracer.log.tags.RpcSpanTags; import io.opentracing.tag.Tags; -import org.junit.AfterClass; import org.junit.Assert; -import org.junit.BeforeClass; import org.junit.Test; import java.util.Map; @@ -41,11 +42,6 @@ */ public class SofaTracerSubscriberTest { - @BeforeClass - public static void beforeClass() { - System.getProperties().put(RpcOptions.DEFAULT_TRACER, "sofaTracer"); - } - @Test public void testClientSendAndServerReceiveTracerEvent() { SofaRequest sofaRequest = new SofaRequest(); @@ -85,9 +81,4 @@ public void testClientSendAndServerReceiveTracerEvent() { SofaTraceContextHolder.getSofaTraceContext().clear(); } - @AfterClass - public static void afterClass() { - System.getProperties().remove(RpcOptions.DEFAULT_TRACER); - } - } \ No newline at end of file diff --git a/test/test-integration/src/test/resources/sofa-rpc/rpc-config.json b/test/test-integration/src/test/resources/sofa-rpc/rpc-config.json index 5b573549a..786432c70 100644 --- a/test/test-integration/src/test/resources/sofa-rpc/rpc-config.json +++ b/test/test-integration/src/test/resources/sofa-rpc/rpc-config.json @@ -1,4 +1,5 @@ { "rpc.config.order": 999, - "logger.impl": "com.alipay.sofa.rpc.log.SLF4JLoggerImpl" + "logger.impl": "com.alipay.sofa.rpc.log.SLF4JLoggerImpl", + "default.tracer": "sofaTracer" } \ No newline at end of file From a97d72c10622759b23b8a0292a4b073eb5ed68cb Mon Sep 17 00:00:00 2001 From: Wang Chengming <634749869@qq.com> Date: Thu, 28 Mar 2024 11:31:13 +0800 Subject: [PATCH 2/2] Enhanced for Nacos weight configuration (#1406) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 呈铭 --- .../registry/nacos/NacosRegistryHelper.java | 7 +++ .../nacos/NacosRegistryHelperTest.java | 59 ++++++++++++++++++- 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/registry/registry-nacos/src/main/java/com/alipay/sofa/rpc/registry/nacos/NacosRegistryHelper.java b/registry/registry-nacos/src/main/java/com/alipay/sofa/rpc/registry/nacos/NacosRegistryHelper.java index 2951edb8e..4fcb9f8a3 100644 --- a/registry/registry-nacos/src/main/java/com/alipay/sofa/rpc/registry/nacos/NacosRegistryHelper.java +++ b/registry/registry-nacos/src/main/java/com/alipay/sofa/rpc/registry/nacos/NacosRegistryHelper.java @@ -123,6 +123,13 @@ static List convertInstancesToProviders(List 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)); + providerInfos.add(providerInfo); } return providerInfos; diff --git a/registry/registry-nacos/src/test/java/com/alipay/sofa/rpc/registry/nacos/NacosRegistryHelperTest.java b/registry/registry-nacos/src/test/java/com/alipay/sofa/rpc/registry/nacos/NacosRegistryHelperTest.java index 8a38cbf0e..385ee3dab 100644 --- a/registry/registry-nacos/src/test/java/com/alipay/sofa/rpc/registry/nacos/NacosRegistryHelperTest.java +++ b/registry/registry-nacos/src/test/java/com/alipay/sofa/rpc/registry/nacos/NacosRegistryHelperTest.java @@ -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); } -} \ No newline at end of file + + @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 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 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 providerInfos = NacosRegistryHelper + .convertInstancesToProviders(Lists.newArrayList(instance)); + assertNotNull(providerInfos); + assertEquals(1, providerInfos.size()); + + ProviderInfo providerInfo = providerInfos.get(0); + assertNotNull(providerInfo); + assertEquals(0, providerInfo.getWeight()); + } +}