diff --git a/CHANGELOG.md b/CHANGELOG.md index d61f2244d..799dbaff5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,3 +25,4 @@ - [Standardize the name of method to create bean.](https://github.com/Tencent/spring-cloud-tencent/pull/779) - [set reporter.enabled default to true](https://github.com/Tencent/spring-cloud-tencent/pull/784) - [fix:fix nacos enabled condition bugs.](https://github.com/Tencent/spring-cloud-tencent/pull/793) +- [fix:fix instance's weight hard code to 100 bug.](https://github.com/Tencent/spring-cloud-tencent/pull/802) diff --git a/spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/LoadBalancerUtils.java b/spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/LoadBalancerUtils.java index 820c7a0b3..91078a05e 100644 --- a/spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/LoadBalancerUtils.java +++ b/spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/LoadBalancerUtils.java @@ -47,7 +47,6 @@ import org.springframework.util.CollectionUtils; public final class LoadBalancerUtils { private static final Logger LOGGER = LoggerFactory.getLogger(LoadBalancerUtils.class); - private static final int DEFAULT_WEIGHT = 100; private static final int WAIT_TIME = 3; private LoadBalancerUtils() { @@ -104,7 +103,6 @@ public final class LoadBalancerUtils { instance.setId(serviceInstance.getInstanceId()); instance.setHost(serviceInstance.getHost()); instance.setPort(serviceInstance.getPort()); - instance.setWeight(DEFAULT_WEIGHT); instance.setMetadata(serviceInstance.getMetadata()); if (serviceInstance instanceof PolarisServiceInstance) { @@ -112,6 +110,8 @@ public final class LoadBalancerUtils { instance.setRegion(polarisServiceInstance.getPolarisInstance().getRegion()); instance.setZone(polarisServiceInstance.getPolarisInstance().getZone()); instance.setCampus(polarisServiceInstance.getPolarisInstance().getCampus()); + instance.setWeight(polarisServiceInstance.getPolarisInstance().getWeight()); + } return instance; diff --git a/spring-cloud-tencent-polaris-loadbalancer/src/test/java/com/tencent/cloud/polaris/loadbalancer/LoadBalancerUtilsTest.java b/spring-cloud-tencent-polaris-loadbalancer/src/test/java/com/tencent/cloud/polaris/loadbalancer/LoadBalancerUtilsTest.java index b4aea4c00..130eec813 100644 --- a/spring-cloud-tencent-polaris-loadbalancer/src/test/java/com/tencent/cloud/polaris/loadbalancer/LoadBalancerUtilsTest.java +++ b/spring-cloud-tencent-polaris-loadbalancer/src/test/java/com/tencent/cloud/polaris/loadbalancer/LoadBalancerUtilsTest.java @@ -23,7 +23,9 @@ import java.util.List; import com.tencent.cloud.common.metadata.MetadataContext; import com.tencent.cloud.common.metadata.MetadataContextHolder; +import com.tencent.cloud.common.pojo.PolarisServiceInstance; import com.tencent.cloud.common.util.ApplicationContextAwareUtils; +import com.tencent.polaris.api.pojo.DefaultInstance; import com.tencent.polaris.api.pojo.Instance; import com.tencent.polaris.api.pojo.ServiceInstances; import org.junit.AfterClass; @@ -36,7 +38,6 @@ import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; import reactor.core.publisher.Flux; -import org.springframework.cloud.client.DefaultServiceInstance; import org.springframework.cloud.client.ServiceInstance; import static org.mockito.ArgumentMatchers.anyString; @@ -49,7 +50,7 @@ import static org.mockito.ArgumentMatchers.anyString; @RunWith(MockitoJUnitRunner.class) public class LoadBalancerUtilsTest { - private static final String TEST_NAMESPACE_AND_SERVICE = "testNamespaceAndService"; + private static final String testNamespaceAndService = "testNamespaceAndService"; private static MockedStatic mockedApplicationContextAwareUtils; private static MockedStatic mockedMetadataContextHolder; @@ -57,7 +58,7 @@ public class LoadBalancerUtilsTest { public static void beforeClass() { mockedApplicationContextAwareUtils = Mockito.mockStatic(ApplicationContextAwareUtils.class); mockedApplicationContextAwareUtils.when(() -> ApplicationContextAwareUtils.getProperties(anyString())) - .thenReturn(TEST_NAMESPACE_AND_SERVICE); + .thenReturn(testNamespaceAndService); MetadataContext metadataContext = Mockito.mock(MetadataContext.class); mockedMetadataContextHolder = Mockito.mockStatic(MetadataContextHolder.class); @@ -80,11 +81,17 @@ public class LoadBalancerUtilsTest { @Test public void testTransferNotEmptyInstances() { int instanceSize = 100; + int weight = 50; List instances = new ArrayList<>(); for (int i = 0; i < instanceSize; i++) { - instances.add(new DefaultServiceInstance("ins" + i, TEST_NAMESPACE_AND_SERVICE, "127.0.0." + i, - 8080, false)); + DefaultInstance instance = new DefaultInstance(); + instance.setService(testNamespaceAndService); + instance.setId("ins" + i); + instance.setPort(8080); + instance.setHost("127.0.0." + i); + instance.setWeight(weight); + instances.add(new PolarisServiceInstance(instance)); } ServiceInstances serviceInstances = LoadBalancerUtils.transferServersToServiceInstances(Flux.just(instances)); @@ -95,12 +102,12 @@ public class LoadBalancerUtilsTest { List polarisInstances = serviceInstances.getInstances(); for (int i = 0; i < instanceSize; i++) { Instance instance = polarisInstances.get(i); - Assert.assertEquals(TEST_NAMESPACE_AND_SERVICE, instance.getNamespace()); - Assert.assertEquals(TEST_NAMESPACE_AND_SERVICE, instance.getService()); + Assert.assertEquals(testNamespaceAndService, instance.getNamespace()); + Assert.assertEquals(testNamespaceAndService, instance.getService()); Assert.assertEquals("ins" + i, instance.getId()); Assert.assertEquals("127.0.0." + i, instance.getHost()); Assert.assertEquals(8080, instance.getPort()); - Assert.assertEquals(100, instance.getWeight()); + Assert.assertEquals(weight, instance.getWeight()); } } }