diff --git a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/RouterUtils.java b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/RouterUtils.java deleted file mode 100644 index b9920f6fc..000000000 --- a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/RouterUtils.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * Tencent is pleased to support the open source community by making spring-cloud-tencent available. - * - * Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -package com.tencent.cloud.polaris.router; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.function.Function; -import java.util.stream.Collectors; - -import com.tencent.cloud.common.constant.MetadataConstant; -import com.tencent.cloud.common.metadata.MetadataContext; -import com.tencent.cloud.common.metadata.MetadataContextHolder; -import com.tencent.cloud.rpc.enhancement.transformer.InstanceTransformer; -import com.tencent.polaris.api.pojo.DefaultServiceInstances; -import com.tencent.polaris.api.pojo.Instance; -import com.tencent.polaris.api.pojo.ServiceInstances; -import com.tencent.polaris.api.pojo.ServiceKey; -import org.reactivestreams.Publisher; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import reactor.core.publisher.Flux; - -import org.springframework.cloud.client.ServiceInstance; -import org.springframework.util.CollectionUtils; - -/** - * load balancer utils. - * - * @author lepdou 2022-05-17 - */ -public final class RouterUtils { - private static final Logger LOGGER = LoggerFactory.getLogger(RouterUtils.class); - - private static final int WAIT_TIME = 3; - - private RouterUtils() { - } - - /** - * transfer servers to ServiceInstances. - * - * @param servers servers - * @param instanceTransformer instanceTransformer - * @return ServiceInstances - */ - public static ServiceInstances transferServersToServiceInstances(Flux> servers, InstanceTransformer instanceTransformer) { - List instanceList = Collections.synchronizedList(new ArrayList<>()); - servers.flatMap((Function, Publisher>) serviceInstances -> - Flux.fromIterable(serviceInstances.stream() - .map(instanceTransformer::transform) - .collect(Collectors.toList()))).subscribe(instance -> instanceList.add((Instance) instance)); - - String serviceName = ""; - Map serviceMetadata = new HashMap<>(); - if (!CollectionUtils.isEmpty(instanceList)) { - serviceName = instanceList.get(0).getService(); - serviceMetadata = instanceList.get(0).getServiceMetadata(); - } - - String namespace = MetadataContextHolder.get().getContext(MetadataContext.FRAGMENT_APPLICATION_NONE, - MetadataConstant.POLARIS_TARGET_NAMESPACE, MetadataContext.LOCAL_NAMESPACE); - - ServiceKey serviceKey = new ServiceKey(namespace, serviceName); - - return new DefaultServiceInstances(serviceKey, instanceList, serviceMetadata); - } -} diff --git a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/RouterUtilsTest.java b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/RouterUtilsTest.java deleted file mode 100644 index d8e6466ba..000000000 --- a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/RouterUtilsTest.java +++ /dev/null @@ -1,111 +0,0 @@ -/* - * Tencent is pleased to support the open source community by making spring-cloud-tencent available. - * - * Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -package com.tencent.cloud.polaris.router; - -import java.util.ArrayList; -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.cloud.rpc.enhancement.transformer.PolarisInstanceTransformer; -import com.tencent.polaris.api.pojo.DefaultInstance; -import com.tencent.polaris.api.pojo.Instance; -import com.tencent.polaris.api.pojo.ServiceInstances; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.MockedStatic; -import org.mockito.Mockito; -import org.mockito.junit.jupiter.MockitoExtension; -import reactor.core.publisher.Flux; - -import org.springframework.cloud.client.ServiceInstance; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.mockStatic; - -/** - * Test for ${@link RouterUtils}. - * - * @author lepdou 2022-07-04 - */ -@ExtendWith(MockitoExtension.class) -public class RouterUtilsTest { - - private static final String testNamespaceAndService = "testNamespaceAndService"; - - @BeforeAll - static void beforeAll() { - MetadataContext.LOCAL_NAMESPACE = testNamespaceAndService; - } - - @Test - public void testTransferEmptyInstances() { - ServiceInstances serviceInstances = RouterUtils.transferServersToServiceInstances(Flux.empty(), new PolarisInstanceTransformer()); - assertThat(serviceInstances.getInstances()).isNotNull(); - assertThat(serviceInstances.getInstances()).isEmpty(); - } - - @Test - public void testTransferNotEmptyInstances() { - try ( - MockedStatic mockedApplicationContextAwareUtils = mockStatic(ApplicationContextAwareUtils.class); - MockedStatic mockedMetadataContextHolder = mockStatic(MetadataContextHolder.class) - ) { - mockedApplicationContextAwareUtils.when(() -> ApplicationContextAwareUtils.getProperties(anyString())) - .thenReturn(testNamespaceAndService); - MetadataContext metadataContext = Mockito.mock(MetadataContext.class); - mockedMetadataContextHolder.when(MetadataContextHolder::get).thenReturn(metadataContext); - Mockito.when(metadataContext.getContext(anyString(), anyString(), anyString())) - .thenReturn(testNamespaceAndService); - - int instanceSize = 100; - int weight = 50; - - List instances = new ArrayList<>(); - for (int i = 0; i < instanceSize; i++) { - 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 = RouterUtils.transferServersToServiceInstances(Flux.just(instances), new PolarisInstanceTransformer()); - - assertThat(serviceInstances.getInstances()).isNotNull(); - assertThat(serviceInstances.getInstances().size()).isEqualTo(instanceSize); - - List polarisInstances = serviceInstances.getInstances(); - for (int i = 0; i < instanceSize; i++) { - Instance instance = polarisInstances.get(i); - assertThat(instance.getNamespace()).isEqualTo(testNamespaceAndService); - assertThat(instance.getService()).isEqualTo(testNamespaceAndService); - assertThat(instance.getId()).isEqualTo("ins" + i); - assertThat(instance.getHost()).isEqualTo("127.0.0." + i); - assertThat(instance.getPort()).isEqualTo(8080); - assertThat(instance.getWeight()).isEqualTo(weight); - } - } - } -}