commit
549c883226
@ -0,0 +1,32 @@
|
||||
# This workflow will build a Java project with Maven
|
||||
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven
|
||||
|
||||
name: Codecov
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ main ]
|
||||
pull_request:
|
||||
branches: [ main ]
|
||||
|
||||
permissions:
|
||||
contents: write
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Checkout codes
|
||||
uses: actions/checkout@v3
|
||||
- name: Set up JDK 8
|
||||
uses: actions/setup-java@v3
|
||||
with:
|
||||
distribution: 'temurin'
|
||||
java-version: 8
|
||||
- name: Test with Maven
|
||||
run: mvn -B test --file pom.xml
|
||||
- name: Upload coverage to Codecov
|
||||
uses: codecov/codecov-action@v3
|
||||
with:
|
||||
file: ${{ github.workspace }}/target/site/jacoco/jacoco.xml
|
After Width: | Height: | Size: 16 KiB |
@ -0,0 +1,186 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 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.common.metadata;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import com.tencent.cloud.common.metadata.config.MetadataLocalProperties;
|
||||
import com.tencent.cloud.common.spi.InstanceMetadataProvider;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
|
||||
/**
|
||||
* test for {@link StaticMetadataManager}
|
||||
*@author lepdou 2022-06-27
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class StaticMetadataManagerTest {
|
||||
|
||||
@Mock
|
||||
private MetadataLocalProperties metadataLocalProperties;
|
||||
|
||||
@Test
|
||||
public void testParseConfigMetadata() {
|
||||
Map<String, String> content = new HashMap<>();
|
||||
content.put("k1", "v1");
|
||||
content.put("k2", "v22");
|
||||
content.put("zone", "zone1");
|
||||
content.put("region", "region1");
|
||||
|
||||
when(metadataLocalProperties.getContent()).thenReturn(content);
|
||||
when(metadataLocalProperties.getTransitive()).thenReturn(Collections.singletonList("k1"));
|
||||
|
||||
StaticMetadataManager metadataManager = new StaticMetadataManager(metadataLocalProperties, null);
|
||||
|
||||
Map<String, String> metadata = metadataManager.getAllConfigMetadata();
|
||||
Assert.assertEquals(4, metadata.size());
|
||||
Assert.assertEquals("v1", metadata.get("k1"));
|
||||
Assert.assertEquals("v22", metadata.get("k2"));
|
||||
|
||||
Map<String, String> transitiveMetadata = metadataManager.getConfigTransitiveMetadata();
|
||||
Assert.assertEquals(1, transitiveMetadata.size());
|
||||
Assert.assertEquals("v1", transitiveMetadata.get("k1"));
|
||||
|
||||
Assert.assertEquals("zone1", metadataManager.getZone());
|
||||
Assert.assertEquals("region1", metadataManager.getRegion());
|
||||
|
||||
Map<String, String> locationInfo = metadataManager.getLocationMetadata();
|
||||
Assert.assertEquals("zone1", locationInfo.get("zone"));
|
||||
Assert.assertEquals("region1", locationInfo.get("region"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomSPIMetadata() {
|
||||
Map<String, String> content = new HashMap<>();
|
||||
content.put("k1", "v1");
|
||||
content.put("k2", "v2");
|
||||
|
||||
when(metadataLocalProperties.getContent()).thenReturn(content);
|
||||
when(metadataLocalProperties.getTransitive()).thenReturn(Collections.singletonList("k1"));
|
||||
|
||||
StaticMetadataManager metadataManager = new StaticMetadataManager(metadataLocalProperties,
|
||||
new MockedMetadataProvider());
|
||||
|
||||
Map<String, String> metadata = metadataManager.getAllCustomMetadata();
|
||||
Assert.assertEquals(3, metadata.size());
|
||||
Assert.assertEquals("v1", metadata.get("k1"));
|
||||
Assert.assertEquals("v22", metadata.get("k2"));
|
||||
Assert.assertEquals("v33", metadata.get("k3"));
|
||||
|
||||
Map<String, String> transitiveMetadata = metadataManager.getCustomSPITransitiveMetadata();
|
||||
Assert.assertEquals(1, transitiveMetadata.size());
|
||||
Assert.assertEquals("v22", metadata.get("k2"));
|
||||
|
||||
Assert.assertEquals("zone2", metadataManager.getZone());
|
||||
Assert.assertEquals("region1", metadataManager.getRegion());
|
||||
|
||||
Map<String, String> locationInfo = metadataManager.getLocationMetadata();
|
||||
Assert.assertEquals("zone2", locationInfo.get("zone"));
|
||||
Assert.assertEquals("region1", locationInfo.get("region"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMergedMetadata() {
|
||||
Map<String, String> content = new HashMap<>();
|
||||
content.put("k1", "v1");
|
||||
content.put("k2", "v2");
|
||||
content.put("zone", "zone1");
|
||||
content.put("region", "region1");
|
||||
content.put("campus", "campus1");
|
||||
|
||||
when(metadataLocalProperties.getContent()).thenReturn(content);
|
||||
when(metadataLocalProperties.getTransitive()).thenReturn(Collections.singletonList("k1"));
|
||||
|
||||
StaticMetadataManager metadataManager = new StaticMetadataManager(metadataLocalProperties,
|
||||
new MockedMetadataProvider());
|
||||
|
||||
Map<String, String> metadata = metadataManager.getMergedStaticMetadata();
|
||||
Assert.assertEquals(6, metadata.size());
|
||||
Assert.assertEquals("v1", metadata.get("k1"));
|
||||
Assert.assertEquals("v22", metadata.get("k2"));
|
||||
Assert.assertEquals("v33", metadata.get("k3"));
|
||||
Assert.assertEquals("zone2", metadata.get("zone"));
|
||||
Assert.assertEquals("region1", metadata.get("region"));
|
||||
Assert.assertEquals("campus1", metadata.get("campus"));
|
||||
|
||||
Map<String, String> transitiveMetadata = metadataManager.getMergedStaticTransitiveMetadata();
|
||||
Assert.assertEquals(2, transitiveMetadata.size());
|
||||
Assert.assertEquals("v1", metadata.get("k1"));
|
||||
Assert.assertEquals("v22", metadata.get("k2"));
|
||||
|
||||
Assert.assertEquals("zone2", metadataManager.getZone());
|
||||
Assert.assertEquals("region1", metadataManager.getRegion());
|
||||
|
||||
Assert.assertTrue(CollectionUtils.isEmpty(metadataManager.getAllEnvMetadata()));
|
||||
Assert.assertTrue(CollectionUtils.isEmpty(metadataManager.getEnvTransitiveMetadata()));
|
||||
|
||||
Map<String, String> locationInfo = metadataManager.getLocationMetadata();
|
||||
Assert.assertEquals("zone2", locationInfo.get("zone"));
|
||||
Assert.assertEquals("region1", locationInfo.get("region"));
|
||||
Assert.assertEquals("campus1", locationInfo.get("campus"));
|
||||
|
||||
}
|
||||
|
||||
static class MockedMetadataProvider implements InstanceMetadataProvider {
|
||||
|
||||
@Override
|
||||
public Map<String, String> getMetadata() {
|
||||
Map<String, String> metadata = new HashMap<>();
|
||||
metadata.put("k1", "v1");
|
||||
metadata.put("k2", "v22");
|
||||
metadata.put("k3", "v33");
|
||||
return metadata;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getTransitiveMetadataKeys() {
|
||||
Set<String> transitiveKeys = new HashSet<>();
|
||||
transitiveKeys.add("k2");
|
||||
return transitiveKeys;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getRegion() {
|
||||
return "region1";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getZone() {
|
||||
return "zone2";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCampus() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 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.context;
|
||||
|
||||
import com.tencent.cloud.common.metadata.StaticMetadataManager;
|
||||
import com.tencent.polaris.api.plugin.common.ValueContext;
|
||||
import com.tencent.polaris.api.plugin.route.LocationLevel;
|
||||
import com.tencent.polaris.client.api.SDKContext;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
/**
|
||||
* After all configurations are loaded, post-initialize SDKContext.
|
||||
*@author lepdou 2022-06-28
|
||||
*/
|
||||
public class PostInitPolarisSDKContext {
|
||||
|
||||
public PostInitPolarisSDKContext(SDKContext sdkContext, StaticMetadataManager staticMetadataManager) {
|
||||
// set instance's location info
|
||||
String region = staticMetadataManager.getRegion();
|
||||
String zone = staticMetadataManager.getZone();
|
||||
String campus = staticMetadataManager.getCampus();
|
||||
|
||||
ValueContext valueContext = sdkContext.getValueContext();
|
||||
if (StringUtils.isNotBlank(region)) {
|
||||
valueContext.setValue(LocationLevel.region.name(), region);
|
||||
}
|
||||
if (StringUtils.isNotBlank(zone)) {
|
||||
valueContext.setValue(LocationLevel.zone.name(), zone);
|
||||
}
|
||||
if (StringUtils.isNotBlank(campus)) {
|
||||
valueContext.setValue(LocationLevel.campus.name(), campus);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 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.context.config;
|
||||
|
||||
import com.tencent.cloud.common.metadata.StaticMetadataManager;
|
||||
import com.tencent.cloud.polaris.context.PostInitPolarisSDKContext;
|
||||
import com.tencent.polaris.client.api.SDKContext;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* Post-initialization operations after the application initialization phase is completed.
|
||||
*@author lepdou 2022-06-28
|
||||
*/
|
||||
@Configuration
|
||||
public class PolarisContextPostConfiguration {
|
||||
|
||||
@Bean
|
||||
public PostInitPolarisSDKContext postInitPolarisSDKContext(SDKContext sdkContext, StaticMetadataManager staticMetadataManager) {
|
||||
return new PostInitPolarisSDKContext(sdkContext, staticMetadataManager);
|
||||
}
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.tencent.cloud.polaris.context.PolarisContextAutoConfiguration
|
||||
com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration,\
|
||||
com.tencent.cloud.polaris.context.config.PolarisContextPostConfiguration
|
||||
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
|
||||
com.tencent.cloud.polaris.context.PolarisContextBootstrapAutoConfiguration
|
||||
com.tencent.cloud.polaris.context.config.PolarisContextBootstrapAutoConfiguration
|
||||
|
@ -0,0 +1,173 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 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.
|
||||
*/
|
||||
// CHECKSTYLE:OFF
|
||||
|
||||
package com.tencent.cloud.polaris.loadbalancer;
|
||||
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import com.netflix.client.config.DefaultClientConfigImpl;
|
||||
import com.netflix.client.config.IClientConfig;
|
||||
import com.netflix.loadbalancer.DummyPing;
|
||||
import com.netflix.loadbalancer.Server;
|
||||
import com.netflix.loadbalancer.ServerList;
|
||||
import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
|
||||
import com.tencent.cloud.polaris.loadbalancer.config.PolarisLoadBalancerProperties;
|
||||
import com.tencent.polaris.api.core.ConsumerAPI;
|
||||
import com.tencent.polaris.api.pojo.DefaultInstance;
|
||||
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 com.tencent.polaris.api.rpc.InstancesResponse;
|
||||
import com.tencent.polaris.router.api.core.RouterAPI;
|
||||
import com.tencent.polaris.router.api.rpc.ProcessLoadBalanceResponse;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import org.springframework.cloud.netflix.ribbon.StaticServerList;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
|
||||
/**
|
||||
* Test for {@link PolarisLoadBalancer}.
|
||||
*
|
||||
* @author lapple.lei 2022-06-28
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class PolarisLoadBalancerTest {
|
||||
private static final String CLIENT_NAME = "polaris-test-server";
|
||||
private static final String NS = "testNamespace";
|
||||
private static final String[] HOST_LIST = new String[] {
|
||||
"127.0.0.1",
|
||||
"127.0.0.2",
|
||||
"127.0.0.3",
|
||||
"127.0.0.4",
|
||||
"127.0.0.5",
|
||||
};
|
||||
|
||||
@Mock
|
||||
private RouterAPI routerAPI;
|
||||
@Mock
|
||||
private ConsumerAPI consumerAPI;
|
||||
|
||||
@Test
|
||||
public void testPolarisLoadBalancer() {
|
||||
//mock consumerAPI
|
||||
when(consumerAPI.getHealthyInstances(any())).thenReturn(this.assembleInstanceResp());
|
||||
|
||||
//mock routerAPI for rule
|
||||
when(routerAPI.processLoadBalance(any())).thenReturn(assembleProcessLoadBalanceResp());
|
||||
PolarisWeightedRule rule = new PolarisWeightedRule(routerAPI);
|
||||
|
||||
// clientConfig
|
||||
IClientConfig config = new DefaultClientConfigImpl();
|
||||
config.loadProperties(CLIENT_NAME);
|
||||
|
||||
//mock for MetadataContext
|
||||
try (MockedStatic<ApplicationContextAwareUtils> mockedCtxUtils = Mockito
|
||||
.mockStatic(ApplicationContextAwareUtils.class)) {
|
||||
mockedCtxUtils.when(() -> ApplicationContextAwareUtils.getProperties("spring.cloud.polaris.namespace"))
|
||||
.thenReturn(NS);
|
||||
mockedCtxUtils.when(() -> ApplicationContextAwareUtils.getProperties("spring.cloud.polaris.service"))
|
||||
.thenReturn("TestServer");
|
||||
|
||||
PolarisLoadBalancerProperties properties = new PolarisLoadBalancerProperties();
|
||||
ServerList<Server> emptyServerList = new StaticServerList<>();
|
||||
|
||||
PolarisLoadBalancer balancer = new PolarisLoadBalancer(config, rule, new DummyPing(), emptyServerList,
|
||||
consumerAPI, properties);
|
||||
|
||||
String host = balancer.choose(null);
|
||||
|
||||
Assert.assertNotNull(host);
|
||||
Assert.assertEquals("127.0.0.1:8080", host);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExtendDiscoveryServiceInstance() {
|
||||
//mock routerAPI for rule
|
||||
when(routerAPI.processLoadBalance(any())).thenReturn(assembleProcessLoadBalanceResp());
|
||||
PolarisWeightedRule rule = new PolarisWeightedRule(routerAPI);
|
||||
|
||||
// clientConfig
|
||||
IClientConfig config = new DefaultClientConfigImpl();
|
||||
config.loadProperties(CLIENT_NAME);
|
||||
|
||||
//mock for MetadataContext
|
||||
try (MockedStatic<ApplicationContextAwareUtils> mockedCtxUtils = Mockito
|
||||
.mockStatic(ApplicationContextAwareUtils.class)) {
|
||||
|
||||
mockedCtxUtils.when(() -> ApplicationContextAwareUtils.getProperties("spring.cloud.polaris.namespace"))
|
||||
.thenReturn(NS);
|
||||
mockedCtxUtils.when(() -> ApplicationContextAwareUtils.getProperties("spring.cloud.polaris.service"))
|
||||
.thenReturn("TestServer");
|
||||
|
||||
PolarisLoadBalancerProperties properties = new PolarisLoadBalancerProperties();
|
||||
properties.setDiscoveryType("TEST");
|
||||
ServerList<Server> staticServerList = assembleServerList();
|
||||
|
||||
PolarisLoadBalancer balancer = new PolarisLoadBalancer(config, rule, new DummyPing(), staticServerList,
|
||||
consumerAPI, properties);
|
||||
|
||||
String host = balancer.choose(null);
|
||||
Assert.assertEquals("127.0.0.1:8080", host);
|
||||
}
|
||||
}
|
||||
|
||||
private ServerList<Server> assembleServerList() {
|
||||
return new StaticServerList<>(Stream.of(HOST_LIST).map(this::convertServer).toArray(Server[]::new));
|
||||
}
|
||||
|
||||
private ProcessLoadBalanceResponse assembleProcessLoadBalanceResp() {
|
||||
ServiceInstances serviceInstances = assembleServiceInstances();
|
||||
return new ProcessLoadBalanceResponse(serviceInstances.getInstances().get(0));
|
||||
}
|
||||
|
||||
private InstancesResponse assembleInstanceResp() {
|
||||
return new InstancesResponse(assembleServiceInstances());
|
||||
}
|
||||
|
||||
private ServiceInstances assembleServiceInstances() {
|
||||
ServiceKey serviceKey = new ServiceKey(NS, CLIENT_NAME);
|
||||
|
||||
List<Instance> instances = Stream.of(HOST_LIST).map(this::convertInstance).collect(Collectors.toList());
|
||||
return new DefaultServiceInstances(serviceKey, instances);
|
||||
}
|
||||
|
||||
private Instance convertInstance(String host) {
|
||||
DefaultInstance instance = new DefaultInstance();
|
||||
instance.setHost(host);
|
||||
instance.setPort(8080);
|
||||
return instance;
|
||||
}
|
||||
|
||||
private Server convertServer(String host) {
|
||||
return new Server("http", host, 8080);
|
||||
}
|
||||
}
|
Loading…
Reference in new issue