commit
9b1eddb6e4
@ -1,3 +1,7 @@
|
|||||||
# Change Log
|
# Change Log
|
||||||
---
|
---
|
||||||
|
[UT: Add config module unit test](https://github.com/Tencent/spring-cloud-tencent/pull/229)
|
||||||
|
[Feature: Add config change listener feature support](https://github.com/Tencent/spring-cloud-tencent/pull/220)
|
||||||
|
[Feature: Support spring cloud gateway routers](https://github.com/Tencent/spring-cloud-tencent/pull/230)
|
||||||
|
[Feature: Add instance metadata spi for registration](https://github.com/Tencent/spring-cloud-tencent/pull/244)
|
||||||
|
[Bugfix: fix guava version conflict bug & fix router strong dependency on LoadBalancerClientFilter](https://github.com/Tencent/spring-cloud-tencent/pull/236)
|
||||||
|
@ -0,0 +1,37 @@
|
|||||||
|
/*
|
||||||
|
* 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.config;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether to connect to a remote server, suitable for local development mode.
|
||||||
|
*
|
||||||
|
* @author lepdou 2022-06-11
|
||||||
|
*/
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Target({ ElementType.TYPE, ElementType.METHOD })
|
||||||
|
@ConditionalOnProperty(value = "spring.cloud.polaris.config.connect-remote-server", matchIfMissing = true)
|
||||||
|
public @interface ConditionalOnConnectRemoteServerEnabled {
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,170 @@
|
|||||||
|
/*
|
||||||
|
* 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.config.adapter;
|
||||||
|
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import com.tencent.polaris.configuration.api.core.ConfigFileChangeListener;
|
||||||
|
import com.tencent.polaris.configuration.api.core.ConfigKVFile;
|
||||||
|
import com.tencent.polaris.configuration.api.core.ConfigKVFileChangeEvent;
|
||||||
|
import com.tencent.polaris.configuration.api.core.ConfigKVFileChangeListener;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mock config kv file for test.
|
||||||
|
*@author lepdou 2022-06-11
|
||||||
|
*/
|
||||||
|
public class MockedConfigKVFile implements ConfigKVFile {
|
||||||
|
|
||||||
|
private final Map<String, Object> properties;
|
||||||
|
private final List<ConfigKVFileChangeListener> listeners = new ArrayList<>();
|
||||||
|
|
||||||
|
public MockedConfigKVFile(Map<String, Object> properties) {
|
||||||
|
this.properties = properties;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getProperty(String s, String s1) {
|
||||||
|
return String.valueOf(properties.get(s));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Integer getIntProperty(String s, Integer integer) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Long getLongProperty(String s, Long aLong) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Short getShortProperty(String s, Short aShort) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Float getFloatProperty(String s, Float aFloat) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Double getDoubleProperty(String s, Double aDouble) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Byte getByteProperty(String s, Byte aByte) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Boolean getBooleanProperty(String s, Boolean aBoolean) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String[] getArrayProperty(String s, String s1, String[] strings) {
|
||||||
|
return new String[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T extends Enum<T>> T getEnumProperty(String s, Class<T> aClass, T t) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T> T getJsonProperty(String s, Class<T> aClass, T t) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T> T getJsonProperty(String s, Type type, T t) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<String> getPropertyNames() {
|
||||||
|
return properties.keySet();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addChangeListener(ConfigKVFileChangeListener configKVFileChangeListener) {
|
||||||
|
listeners.add(configKVFileChangeListener);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void removeChangeListener(ConfigKVFileChangeListener configKVFileChangeListener) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getContent() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T> T asJson(Class<T> aClass, T t) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T> T asJson(Type type, T t) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean hasContent() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addChangeListener(ConfigFileChangeListener configFileChangeListener) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void removeChangeListener(ConfigFileChangeListener configFileChangeListener) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void fireChangeListener(ConfigKVFileChangeEvent event) {
|
||||||
|
for (ConfigKVFileChangeListener listener : listeners) {
|
||||||
|
listener.onChange(event);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getNamespace() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getFileGroup() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getFileName() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,188 @@
|
|||||||
|
/*
|
||||||
|
* 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.config.adapter;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import com.google.common.collect.Lists;
|
||||||
|
import com.tencent.cloud.polaris.config.config.ConfigFileGroup;
|
||||||
|
import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
|
||||||
|
import com.tencent.cloud.polaris.context.PolarisContextProperties;
|
||||||
|
import com.tencent.polaris.configuration.api.core.ConfigFileService;
|
||||||
|
import com.tencent.polaris.configuration.api.core.ConfigKVFile;
|
||||||
|
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.core.env.Environment;
|
||||||
|
import org.springframework.core.env.PropertySource;
|
||||||
|
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test for {@link PolarisConfigFileLocator}
|
||||||
|
*@author lepdou 2022-06-11
|
||||||
|
*/
|
||||||
|
@RunWith(MockitoJUnitRunner.class)
|
||||||
|
public class PolarisConfigFileLocatorTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private PolarisConfigProperties polarisConfigProperties;
|
||||||
|
@Mock
|
||||||
|
private PolarisContextProperties polarisContextProperties;
|
||||||
|
@Mock
|
||||||
|
private ConfigFileService configFileService;
|
||||||
|
@Mock
|
||||||
|
private PolarisPropertySourceManager polarisPropertySourceManager;
|
||||||
|
@Mock
|
||||||
|
private Environment environment;
|
||||||
|
|
||||||
|
private final String testNamespace = "testNamespace";
|
||||||
|
private final String testServiceName = "testServiceName";
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testLoadApplicationPropertiesFile() {
|
||||||
|
PolarisConfigFileLocator locator = new PolarisConfigFileLocator(polarisConfigProperties, polarisContextProperties,
|
||||||
|
configFileService, polarisPropertySourceManager, environment);
|
||||||
|
|
||||||
|
when(polarisContextProperties.getNamespace()).thenReturn(testNamespace);
|
||||||
|
when(polarisContextProperties.getService()).thenReturn(testServiceName);
|
||||||
|
|
||||||
|
// application.properties
|
||||||
|
Map<String, Object> applicationProperties = new HashMap<>();
|
||||||
|
applicationProperties.put("k1", "v1");
|
||||||
|
applicationProperties.put("k2", "v2");
|
||||||
|
applicationProperties.put("k3", "v3");
|
||||||
|
ConfigKVFile propertiesFile = new MockedConfigKVFile(applicationProperties);
|
||||||
|
when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "application.properties"))
|
||||||
|
.thenReturn(propertiesFile);
|
||||||
|
|
||||||
|
Map<String, Object> emptyMap = new HashMap<>();
|
||||||
|
ConfigKVFile emptyConfigFile = new MockedConfigKVFile(emptyMap);
|
||||||
|
when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "application.yml")).thenReturn(emptyConfigFile);
|
||||||
|
when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "bootstrap.properties")).thenReturn(emptyConfigFile);
|
||||||
|
when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "bootstrap.yml")).thenReturn(emptyConfigFile);
|
||||||
|
|
||||||
|
when(polarisConfigProperties.getGroups()).thenReturn(null);
|
||||||
|
when(environment.getActiveProfiles()).thenReturn(new String[] {});
|
||||||
|
|
||||||
|
PropertySource<?> propertySource = locator.locate(environment);
|
||||||
|
|
||||||
|
Assert.assertEquals("v1", propertySource.getProperty("k1"));
|
||||||
|
Assert.assertEquals("v2", propertySource.getProperty("k2"));
|
||||||
|
Assert.assertEquals("v3", propertySource.getProperty("k3"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testActiveProfileFilesPriorityBiggerThanDefault() {
|
||||||
|
PolarisConfigFileLocator locator = new PolarisConfigFileLocator(polarisConfigProperties, polarisContextProperties,
|
||||||
|
configFileService, polarisPropertySourceManager, environment);
|
||||||
|
|
||||||
|
when(polarisContextProperties.getNamespace()).thenReturn(testNamespace);
|
||||||
|
when(polarisContextProperties.getService()).thenReturn(testServiceName);
|
||||||
|
|
||||||
|
// application.properties
|
||||||
|
Map<String, Object> applicationProperties = new HashMap<>();
|
||||||
|
applicationProperties.put("k1", "v1");
|
||||||
|
applicationProperties.put("k2", "v2");
|
||||||
|
applicationProperties.put("k3", "v3");
|
||||||
|
ConfigKVFile propertiesFile = new MockedConfigKVFile(applicationProperties);
|
||||||
|
when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "application.properties"))
|
||||||
|
.thenReturn(propertiesFile);
|
||||||
|
|
||||||
|
// application-dev.properties
|
||||||
|
Map<String, Object> devProperties = new HashMap<>();
|
||||||
|
devProperties.put("k1", "v11");
|
||||||
|
ConfigKVFile devFile = new MockedConfigKVFile(devProperties);
|
||||||
|
when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "application-dev.properties"))
|
||||||
|
.thenReturn(devFile);
|
||||||
|
|
||||||
|
Map<String, Object> emptyMap = new HashMap<>();
|
||||||
|
ConfigKVFile emptyConfigFile = new MockedConfigKVFile(emptyMap);
|
||||||
|
when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "application.yml")).thenReturn(emptyConfigFile);
|
||||||
|
when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "application-dev.yml")).thenReturn(emptyConfigFile);
|
||||||
|
when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "bootstrap.properties")).thenReturn(emptyConfigFile);
|
||||||
|
when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "bootstrap-dev.properties")).thenReturn(emptyConfigFile);
|
||||||
|
when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "bootstrap.yml")).thenReturn(emptyConfigFile);
|
||||||
|
when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "bootstrap-dev.yml")).thenReturn(emptyConfigFile);
|
||||||
|
|
||||||
|
when(polarisConfigProperties.getGroups()).thenReturn(null);
|
||||||
|
when(environment.getActiveProfiles()).thenReturn(new String[] {"dev"});
|
||||||
|
|
||||||
|
PropertySource<?> propertySource = locator.locate(environment);
|
||||||
|
|
||||||
|
Assert.assertEquals("v11", propertySource.getProperty("k1"));
|
||||||
|
Assert.assertEquals("v2", propertySource.getProperty("k2"));
|
||||||
|
Assert.assertEquals("v3", propertySource.getProperty("k3"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetCustomFiles() {
|
||||||
|
PolarisConfigFileLocator locator = new PolarisConfigFileLocator(polarisConfigProperties, polarisContextProperties,
|
||||||
|
configFileService, polarisPropertySourceManager, environment);
|
||||||
|
|
||||||
|
when(polarisContextProperties.getNamespace()).thenReturn(testNamespace);
|
||||||
|
when(polarisContextProperties.getService()).thenReturn(testServiceName);
|
||||||
|
|
||||||
|
Map<String, Object> emptyMap = new HashMap<>();
|
||||||
|
ConfigKVFile emptyConfigFile = new MockedConfigKVFile(emptyMap);
|
||||||
|
|
||||||
|
when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "application.properties")).thenReturn(emptyConfigFile);
|
||||||
|
when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "application.yml")).thenReturn(emptyConfigFile);
|
||||||
|
when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "bootstrap.properties")).thenReturn(emptyConfigFile);
|
||||||
|
when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "bootstrap.yml")).thenReturn(emptyConfigFile);
|
||||||
|
|
||||||
|
List<ConfigFileGroup> customFiles = new LinkedList<>();
|
||||||
|
ConfigFileGroup configFileGroup = new ConfigFileGroup();
|
||||||
|
String customGroup = "group1";
|
||||||
|
configFileGroup.setName(customGroup);
|
||||||
|
String customFile1 = "file1.properties";
|
||||||
|
String customFile2 = "file2.properties";
|
||||||
|
configFileGroup.setFiles(Lists.newArrayList(customFile1, customFile2));
|
||||||
|
customFiles.add(configFileGroup);
|
||||||
|
|
||||||
|
when(polarisConfigProperties.getGroups()).thenReturn(customFiles);
|
||||||
|
when(environment.getActiveProfiles()).thenReturn(new String[] {});
|
||||||
|
|
||||||
|
// file1.properties
|
||||||
|
Map<String, Object> file1Map = new HashMap<>();
|
||||||
|
file1Map.put("k1", "v1");
|
||||||
|
file1Map.put("k2", "v2");
|
||||||
|
ConfigKVFile file1 = new MockedConfigKVFile(file1Map);
|
||||||
|
when(configFileService.getConfigPropertiesFile(testNamespace, customGroup, customFile1)).thenReturn(file1);
|
||||||
|
|
||||||
|
// file2.properties
|
||||||
|
Map<String, Object> file2Map = new HashMap<>();
|
||||||
|
file2Map.put("k1", "v11");
|
||||||
|
file2Map.put("k3", "v3");
|
||||||
|
ConfigKVFile file2 = new MockedConfigKVFile(file2Map);
|
||||||
|
when(configFileService.getConfigPropertiesFile(testNamespace, customGroup, customFile2)).thenReturn(file2);
|
||||||
|
|
||||||
|
PropertySource<?> propertySource = locator.locate(environment);
|
||||||
|
|
||||||
|
Assert.assertEquals("v1", propertySource.getProperty("k1"));
|
||||||
|
Assert.assertEquals("v2", propertySource.getProperty("k2"));
|
||||||
|
Assert.assertEquals("v3", propertySource.getProperty("k3"));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,122 @@
|
|||||||
|
/*
|
||||||
|
* 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.config.adapter;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import com.google.common.collect.Lists;
|
||||||
|
import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
|
||||||
|
import com.tencent.polaris.configuration.api.core.ChangeType;
|
||||||
|
import com.tencent.polaris.configuration.api.core.ConfigKVFileChangeEvent;
|
||||||
|
import com.tencent.polaris.configuration.api.core.ConfigPropertyChangeInfo;
|
||||||
|
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.cloud.context.refresh.ContextRefresher;
|
||||||
|
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test for {@link PolarisPropertySourceAutoRefresher}
|
||||||
|
*@author lepdou 2022-06-11
|
||||||
|
*/
|
||||||
|
@RunWith(MockitoJUnitRunner.class)
|
||||||
|
public class PolarisPropertiesSourceAutoRefresherTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private PolarisConfigProperties polarisConfigProperties;
|
||||||
|
@Mock
|
||||||
|
private PolarisPropertySourceManager polarisPropertySourceManager;
|
||||||
|
@Mock
|
||||||
|
private ContextRefresher contextRefresher;
|
||||||
|
|
||||||
|
private final String testNamespace = "testNamespace";
|
||||||
|
private final String testServiceName = "testServiceName";
|
||||||
|
private final String testFileName = "application.properties";
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testConfigFileChanged() {
|
||||||
|
PolarisPropertySourceAutoRefresher refresher = new PolarisPropertySourceAutoRefresher(polarisConfigProperties,
|
||||||
|
polarisPropertySourceManager, contextRefresher);
|
||||||
|
|
||||||
|
when(polarisConfigProperties.isAutoRefresh()).thenReturn(true);
|
||||||
|
|
||||||
|
Map<String, Object> content = new HashMap<>();
|
||||||
|
content.put("k1", "v1");
|
||||||
|
content.put("k2", "v2");
|
||||||
|
content.put("k3", "v3");
|
||||||
|
MockedConfigKVFile file = new MockedConfigKVFile(content);
|
||||||
|
PolarisPropertySource polarisPropertySource = new PolarisPropertySource(testNamespace, testServiceName, testFileName,
|
||||||
|
file, content);
|
||||||
|
|
||||||
|
when(polarisPropertySourceManager.getAllPropertySources()).thenReturn(Lists.newArrayList(polarisPropertySource));
|
||||||
|
|
||||||
|
ConfigPropertyChangeInfo changeInfo = new ConfigPropertyChangeInfo("k1", "v1", "v11", ChangeType.MODIFIED);
|
||||||
|
ConfigPropertyChangeInfo changeInfo2 = new ConfigPropertyChangeInfo("k4", null, "v4", ChangeType.ADDED);
|
||||||
|
ConfigPropertyChangeInfo changeInfo3 = new ConfigPropertyChangeInfo("k2", "v2", null, ChangeType.DELETED);
|
||||||
|
Map<String, ConfigPropertyChangeInfo> changeInfos = new HashMap<>();
|
||||||
|
changeInfos.put("k1", changeInfo);
|
||||||
|
changeInfos.put("k2", changeInfo3);
|
||||||
|
changeInfos.put("k4", changeInfo2);
|
||||||
|
|
||||||
|
ConfigKVFileChangeEvent event = new ConfigKVFileChangeEvent(changeInfos);
|
||||||
|
refresher.onApplicationEvent(null);
|
||||||
|
|
||||||
|
file.fireChangeListener(event);
|
||||||
|
|
||||||
|
Assert.assertEquals("v11", polarisPropertySource.getProperty("k1"));
|
||||||
|
Assert.assertEquals("v3", polarisPropertySource.getProperty("k3"));
|
||||||
|
Assert.assertNull(polarisPropertySource.getProperty("k2"));
|
||||||
|
Assert.assertEquals("v4", polarisPropertySource.getProperty("k4"));
|
||||||
|
verify(contextRefresher).refresh();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNewConfigFile() {
|
||||||
|
PolarisPropertySourceAutoRefresher refresher = new PolarisPropertySourceAutoRefresher(polarisConfigProperties,
|
||||||
|
polarisPropertySourceManager, contextRefresher);
|
||||||
|
|
||||||
|
when(polarisConfigProperties.isAutoRefresh()).thenReturn(true);
|
||||||
|
|
||||||
|
Map<String, Object> emptyContent = new HashMap<>();
|
||||||
|
MockedConfigKVFile file = new MockedConfigKVFile(emptyContent);
|
||||||
|
PolarisPropertySource polarisPropertySource = new PolarisPropertySource(testNamespace, testServiceName, testFileName,
|
||||||
|
file, emptyContent);
|
||||||
|
|
||||||
|
when(polarisPropertySourceManager.getAllPropertySources()).thenReturn(Lists.newArrayList(polarisPropertySource));
|
||||||
|
|
||||||
|
ConfigPropertyChangeInfo changeInfo = new ConfigPropertyChangeInfo("k1", null, "v1", ChangeType.ADDED);
|
||||||
|
Map<String, ConfigPropertyChangeInfo> changeInfos = new HashMap<>();
|
||||||
|
changeInfos.put("k1", changeInfo);
|
||||||
|
|
||||||
|
ConfigKVFileChangeEvent event = new ConfigKVFileChangeEvent(changeInfos);
|
||||||
|
refresher.onApplicationEvent(null);
|
||||||
|
|
||||||
|
file.fireChangeListener(event);
|
||||||
|
|
||||||
|
Assert.assertEquals("v1", polarisPropertySource.getProperty("k1"));
|
||||||
|
verify(contextRefresher).refresh();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,124 @@
|
|||||||
|
/*
|
||||||
|
* 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.config.listener;
|
||||||
|
|
||||||
|
import com.google.common.collect.Sets;
|
||||||
|
import com.tencent.cloud.polaris.config.annotation.PolarisConfigKVFileChangeListener;
|
||||||
|
import com.tencent.polaris.configuration.api.core.ConfigPropertyChangeInfo;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
|
||||||
|
import org.springframework.context.ApplicationEventPublisher;
|
||||||
|
import org.springframework.context.ConfigurableApplicationContext;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.DEFINED_PORT;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Integration testing for change listener.
|
||||||
|
*@author lepdou 2022-06-11
|
||||||
|
*/
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest(webEnvironment = DEFINED_PORT,
|
||||||
|
classes = ConfigChangeListenerTest.TestApplication.class,
|
||||||
|
properties = {"server.port=8081",
|
||||||
|
"spring.config.location = classpath:application-test.yml"})
|
||||||
|
public class ConfigChangeListenerTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ApplicationEventPublisher applicationEventPublisher;
|
||||||
|
@Autowired
|
||||||
|
private ConfigurableApplicationContext applicationContext;
|
||||||
|
@Autowired
|
||||||
|
private TestApplication.TestConfig testConfig;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test() throws InterruptedException {
|
||||||
|
//before change
|
||||||
|
Assert.assertEquals(1000, testConfig.getTimeout());
|
||||||
|
|
||||||
|
//submit change event
|
||||||
|
System.setProperty("timeout", "2000");
|
||||||
|
EnvironmentChangeEvent event = new EnvironmentChangeEvent(applicationContext,
|
||||||
|
Sets.newHashSet("timeout"));
|
||||||
|
|
||||||
|
applicationEventPublisher.publishEvent(event);
|
||||||
|
|
||||||
|
//after change
|
||||||
|
Assert.assertEquals(2, testConfig.getChangeCnt());
|
||||||
|
Assert.assertEquals(2000, testConfig.getTimeout());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
protected static class TestApplication {
|
||||||
|
|
||||||
|
@Component
|
||||||
|
protected static class TestConfig {
|
||||||
|
|
||||||
|
@Value("${timeout:1000}")
|
||||||
|
private int timeout;
|
||||||
|
|
||||||
|
private int changeCnt;
|
||||||
|
|
||||||
|
public int getTimeout() {
|
||||||
|
return timeout;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTimeout(int timeout) {
|
||||||
|
this.timeout = timeout;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getChangeCnt() {
|
||||||
|
return changeCnt;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PolarisConfigKVFileChangeListener(interestedKeys = {"timeout"})
|
||||||
|
public void configChangedListener(ConfigChangeEvent event) {
|
||||||
|
ConfigPropertyChangeInfo changeInfo = event.getChange("timeout");
|
||||||
|
timeout = Integer.parseInt(changeInfo.getNewValue());
|
||||||
|
changeCnt++;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PolarisConfigKVFileChangeListener(interestedKeyPrefixes = {"timeout"})
|
||||||
|
public void configChangedListener2(ConfigChangeEvent event) {
|
||||||
|
ConfigPropertyChangeInfo changeInfo = event.getChange("timeout");
|
||||||
|
timeout = Integer.parseInt(changeInfo.getNewValue());
|
||||||
|
changeCnt++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Component
|
||||||
|
protected static class EventPublisher implements ApplicationEventPublisher {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void publishEvent(Object o) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
spring:
|
||||||
|
application:
|
||||||
|
name: test
|
||||||
|
cloud:
|
||||||
|
polaris:
|
||||||
|
address: grpc://127.0.0.1:8091
|
||||||
|
namespace: default
|
||||||
|
config:
|
||||||
|
connect-remote-server: false
|
@ -0,0 +1,66 @@
|
|||||||
|
/*
|
||||||
|
* 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.router.beanprocessor;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import com.tencent.cloud.common.metadata.config.MetadataLocalProperties;
|
||||||
|
import com.tencent.cloud.common.util.BeanFactoryUtils;
|
||||||
|
import com.tencent.cloud.polaris.router.RouterRuleLabelResolver;
|
||||||
|
import com.tencent.cloud.polaris.router.scg.PolarisLoadBalancerClientFilter;
|
||||||
|
import com.tencent.cloud.polaris.router.spi.RouterLabelResolver;
|
||||||
|
|
||||||
|
import org.springframework.beans.BeansException;
|
||||||
|
import org.springframework.beans.factory.BeanFactory;
|
||||||
|
import org.springframework.beans.factory.BeanFactoryAware;
|
||||||
|
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||||
|
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
|
||||||
|
import org.springframework.cloud.gateway.config.LoadBalancerProperties;
|
||||||
|
import org.springframework.cloud.gateway.filter.LoadBalancerClientFilter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Replace LoadBalancerClientFilter with PolarisLoadBalancerClientFilter.
|
||||||
|
*@author lepdou 2022-06-15
|
||||||
|
*/
|
||||||
|
public class LoadBalancerClientFilterBeanPostProcessor implements BeanPostProcessor, BeanFactoryAware {
|
||||||
|
|
||||||
|
private BeanFactory factory;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
|
||||||
|
this.factory = beanFactory;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
|
||||||
|
if (bean instanceof LoadBalancerClientFilter) {
|
||||||
|
// Support spring cloud gateway router.
|
||||||
|
// Replaces the default LoadBalancerClientFilter implementation and returns a custom PolarisLoadBalancerClientFilter
|
||||||
|
LoadBalancerClient loadBalancerClient = this.factory.getBean(LoadBalancerClient.class);
|
||||||
|
LoadBalancerProperties loadBalancerProperties = this.factory.getBean(LoadBalancerProperties.class);
|
||||||
|
List<RouterLabelResolver> routerLabelResolvers = BeanFactoryUtils.getBeans(factory, RouterLabelResolver.class);
|
||||||
|
MetadataLocalProperties metadataLocalProperties = this.factory.getBean(MetadataLocalProperties.class);
|
||||||
|
RouterRuleLabelResolver routerRuleLabelResolver = this.factory.getBean(RouterRuleLabelResolver.class);
|
||||||
|
|
||||||
|
return new PolarisLoadBalancerClientFilter(loadBalancerClient, loadBalancerProperties,
|
||||||
|
metadataLocalProperties, routerRuleLabelResolver, routerLabelResolvers);
|
||||||
|
}
|
||||||
|
return bean;
|
||||||
|
}
|
||||||
|
}
|
8
spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/resttemplate/PolarisLoadBalancerBeanPostProcessor.java → spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/beanprocessor/LoadBalancerInterceptorBeanPostProcessor.java
8
spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/resttemplate/PolarisLoadBalancerBeanPostProcessor.java → spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/beanprocessor/LoadBalancerInterceptorBeanPostProcessor.java
@ -0,0 +1,58 @@
|
|||||||
|
/*
|
||||||
|
* 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.router.config;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import com.tencent.cloud.common.metadata.config.MetadataLocalProperties;
|
||||||
|
import com.tencent.cloud.polaris.router.RouterRuleLabelResolver;
|
||||||
|
import com.tencent.cloud.polaris.router.feign.PolarisCachingSpringLoadBalanceFactory;
|
||||||
|
import com.tencent.cloud.polaris.router.feign.RouterLabelFeignInterceptor;
|
||||||
|
import com.tencent.cloud.polaris.router.spi.RouterLabelResolver;
|
||||||
|
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||||
|
import org.springframework.cloud.netflix.ribbon.RibbonClients;
|
||||||
|
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.lang.Nullable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* configuration for feign singleton components.
|
||||||
|
* Feign-related components need to be loaded only in the feign environment.
|
||||||
|
*@author lepdou 2022-06-10
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
@ConditionalOnClass(name = {"org.springframework.cloud.openfeign.ribbon.FeignLoadBalancer"})
|
||||||
|
@RibbonClients(defaultConfiguration = {FeignLoadBalancerConfiguration.class})
|
||||||
|
public class FeignAutoConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public RouterLabelFeignInterceptor routerLabelInterceptor(@Nullable List<RouterLabelResolver> routerLabelResolvers,
|
||||||
|
MetadataLocalProperties metadataLocalProperties,
|
||||||
|
RouterRuleLabelResolver routerRuleLabelResolver) {
|
||||||
|
return new RouterLabelFeignInterceptor(routerLabelResolvers, metadataLocalProperties, routerRuleLabelResolver);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public PolarisCachingSpringLoadBalanceFactory polarisCachingSpringLoadBalanceFactory(SpringClientFactory factory) {
|
||||||
|
return new PolarisCachingSpringLoadBalanceFactory(factory);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,144 @@
|
|||||||
|
/*
|
||||||
|
* 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.router.scg;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import com.tencent.cloud.common.metadata.MetadataContext;
|
||||||
|
import com.tencent.cloud.common.metadata.MetadataContextHolder;
|
||||||
|
import com.tencent.cloud.common.metadata.config.MetadataLocalProperties;
|
||||||
|
import com.tencent.cloud.common.util.ExpressionLabelUtils;
|
||||||
|
import com.tencent.cloud.polaris.router.PolarisRouterContext;
|
||||||
|
import com.tencent.cloud.polaris.router.RouterRuleLabelResolver;
|
||||||
|
import com.tencent.cloud.polaris.router.spi.RouterLabelResolver;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import org.springframework.cloud.client.ServiceInstance;
|
||||||
|
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
|
||||||
|
import org.springframework.cloud.gateway.config.LoadBalancerProperties;
|
||||||
|
import org.springframework.cloud.gateway.filter.LoadBalancerClientFilter;
|
||||||
|
import org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient;
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
|
import org.springframework.util.CollectionUtils;
|
||||||
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
|
|
||||||
|
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Replaces the default LoadBalancerClientFilter implementation.
|
||||||
|
*@author lepdou 2022-06-10
|
||||||
|
*/
|
||||||
|
public class PolarisLoadBalancerClientFilter extends LoadBalancerClientFilter {
|
||||||
|
private final static Logger LOGGER = LoggerFactory.getLogger(PolarisLoadBalancerClientFilter.class);
|
||||||
|
|
||||||
|
private final MetadataLocalProperties metadataLocalProperties;
|
||||||
|
private final RouterRuleLabelResolver routerRuleLabelResolver;
|
||||||
|
private final List<RouterLabelResolver> routerLabelResolvers;
|
||||||
|
|
||||||
|
private final boolean isRibbonLoadBalanceClient;
|
||||||
|
|
||||||
|
public PolarisLoadBalancerClientFilter(LoadBalancerClient loadBalancer, LoadBalancerProperties properties,
|
||||||
|
MetadataLocalProperties metadataLocalProperties,
|
||||||
|
RouterRuleLabelResolver routerRuleLabelResolver,
|
||||||
|
List<RouterLabelResolver> routerLabelResolvers) {
|
||||||
|
super(loadBalancer, properties);
|
||||||
|
this.metadataLocalProperties = metadataLocalProperties;
|
||||||
|
this.routerRuleLabelResolver = routerRuleLabelResolver;
|
||||||
|
|
||||||
|
if (!CollectionUtils.isEmpty(routerLabelResolvers)) {
|
||||||
|
routerLabelResolvers.sort(Comparator.comparingInt(Ordered::getOrder));
|
||||||
|
this.routerLabelResolvers = routerLabelResolvers;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
this.routerLabelResolvers = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.isRibbonLoadBalanceClient = loadBalancer instanceof RibbonLoadBalancerClient;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected ServiceInstance choose(ServerWebExchange exchange) {
|
||||||
|
String peerServiceName = ((URI) exchange.getAttribute(GATEWAY_REQUEST_URL_ATTR)).getHost();
|
||||||
|
|
||||||
|
if (isRibbonLoadBalanceClient) {
|
||||||
|
// Pass routing context to ribbon load balancer
|
||||||
|
PolarisRouterContext routerContext = genRouterContext(exchange, peerServiceName);
|
||||||
|
return ((RibbonLoadBalancerClient) loadBalancer).choose(peerServiceName, routerContext);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return loadBalancer.choose(peerServiceName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
PolarisRouterContext genRouterContext(ServerWebExchange exchange, String peerServiceName) {
|
||||||
|
// local service labels
|
||||||
|
Map<String, String> labels = new HashMap<>(metadataLocalProperties.getContent());
|
||||||
|
|
||||||
|
// labels from rule expression
|
||||||
|
Map<String, String> ruleExpressionLabels = getExpressionLabels(exchange, peerServiceName);
|
||||||
|
if (!CollectionUtils.isEmpty(ruleExpressionLabels)) {
|
||||||
|
labels.putAll(ruleExpressionLabels);
|
||||||
|
}
|
||||||
|
|
||||||
|
// labels from request
|
||||||
|
if (!CollectionUtils.isEmpty(routerLabelResolvers)) {
|
||||||
|
routerLabelResolvers.forEach(resolver -> {
|
||||||
|
try {
|
||||||
|
Map<String, String> customResolvedLabels = resolver.resolve(exchange);
|
||||||
|
if (!CollectionUtils.isEmpty(customResolvedLabels)) {
|
||||||
|
labels.putAll(customResolvedLabels);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Throwable t) {
|
||||||
|
LOGGER.error("[SCT][Router] revoke RouterLabelResolver occur some exception. ", t);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// labels from downstream
|
||||||
|
Map<String, String> transitiveLabels = MetadataContextHolder.get()
|
||||||
|
.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
|
||||||
|
labels.putAll(transitiveLabels);
|
||||||
|
|
||||||
|
PolarisRouterContext routerContext = new PolarisRouterContext();
|
||||||
|
|
||||||
|
routerContext.setLabels(PolarisRouterContext.RULE_ROUTER_LABELS, labels);
|
||||||
|
routerContext.setLabels(PolarisRouterContext.TRANSITIVE_LABELS, transitiveLabels);
|
||||||
|
|
||||||
|
return routerContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String, String> getExpressionLabels(ServerWebExchange exchange, String peerServiceName) {
|
||||||
|
Set<String> labelKeys = routerRuleLabelResolver.getExpressionLabelKeys(MetadataContext.LOCAL_NAMESPACE,
|
||||||
|
MetadataContext.LOCAL_SERVICE, peerServiceName);
|
||||||
|
|
||||||
|
if (CollectionUtils.isEmpty(labelKeys)) {
|
||||||
|
return Collections.emptyMap();
|
||||||
|
}
|
||||||
|
|
||||||
|
return ExpressionLabelUtils.resolve(exchange, labelKeys);
|
||||||
|
}
|
||||||
|
}
|
@ -1,2 +1,3 @@
|
|||||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||||
com.tencent.cloud.polaris.router.config.RouterAutoConfiguration
|
com.tencent.cloud.polaris.router.config.RouterAutoConfiguration,\
|
||||||
|
com.tencent.cloud.polaris.router.config.FeignAutoConfiguration
|
||||||
|
@ -0,0 +1,192 @@
|
|||||||
|
/*
|
||||||
|
* 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.router.scg;
|
||||||
|
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import com.google.common.collect.Lists;
|
||||||
|
import com.google.common.collect.Sets;
|
||||||
|
import com.tencent.cloud.common.metadata.MetadataContext;
|
||||||
|
import com.tencent.cloud.common.metadata.MetadataContextHolder;
|
||||||
|
import com.tencent.cloud.common.metadata.config.MetadataLocalProperties;
|
||||||
|
import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
|
||||||
|
import com.tencent.cloud.polaris.router.PolarisRouterContext;
|
||||||
|
import com.tencent.cloud.polaris.router.RouterRuleLabelResolver;
|
||||||
|
import com.tencent.cloud.polaris.router.spi.RouterLabelResolver;
|
||||||
|
import org.junit.AfterClass;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
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.client.loadbalancer.LoadBalancerClient;
|
||||||
|
import org.springframework.cloud.gateway.config.LoadBalancerProperties;
|
||||||
|
import org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient;
|
||||||
|
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
|
||||||
|
import org.springframework.mock.web.server.MockServerWebExchange;
|
||||||
|
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyString;
|
||||||
|
import static org.mockito.Mockito.times;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test for ${@link PolarisLoadBalancerClientFilter}
|
||||||
|
*@author lepdou 2022-06-13
|
||||||
|
*/
|
||||||
|
@RunWith(MockitoJUnitRunner.class)
|
||||||
|
public class PolarisLoadBalancerClientFilterTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private MetadataLocalProperties metadataLocalProperties;
|
||||||
|
@Mock
|
||||||
|
private RouterRuleLabelResolver routerRuleLabelResolver;
|
||||||
|
@Mock
|
||||||
|
private RouterLabelResolver routerLabelResolver;
|
||||||
|
@Mock
|
||||||
|
private LoadBalancerClient loadBalancerClient;
|
||||||
|
@Mock
|
||||||
|
private LoadBalancerProperties loadBalancerProperties;
|
||||||
|
|
||||||
|
private static final String callerService = "callerService";
|
||||||
|
private static final String calleeService = "calleeService";
|
||||||
|
|
||||||
|
private static MockedStatic<ApplicationContextAwareUtils> mockedApplicationContextAwareUtils;
|
||||||
|
private static MockedStatic<MetadataContextHolder> mockedMetadataContextHolder;
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
public static void beforeClass() {
|
||||||
|
mockedApplicationContextAwareUtils = Mockito.mockStatic(ApplicationContextAwareUtils.class);
|
||||||
|
mockedApplicationContextAwareUtils.when(() -> ApplicationContextAwareUtils.getProperties(anyString()))
|
||||||
|
.thenReturn(callerService);
|
||||||
|
|
||||||
|
MetadataContext metadataContext = Mockito.mock(MetadataContext.class);
|
||||||
|
|
||||||
|
// mock transitive metadata
|
||||||
|
Map<String, String> transitiveLabels = new HashMap<>();
|
||||||
|
transitiveLabels.put("t1", "v1");
|
||||||
|
transitiveLabels.put("t2", "v2");
|
||||||
|
when(metadataContext.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE)).thenReturn(transitiveLabels);
|
||||||
|
|
||||||
|
mockedMetadataContextHolder = Mockito.mockStatic(MetadataContextHolder.class);
|
||||||
|
mockedMetadataContextHolder.when(MetadataContextHolder::get).thenReturn(metadataContext);
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterClass
|
||||||
|
public static void afterClass() {
|
||||||
|
mockedApplicationContextAwareUtils.close();
|
||||||
|
mockedMetadataContextHolder.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGenRouterContext() {
|
||||||
|
PolarisLoadBalancerClientFilter polarisLoadBalancerClientFilter = new PolarisLoadBalancerClientFilter(
|
||||||
|
loadBalancerClient, loadBalancerProperties, metadataLocalProperties, routerRuleLabelResolver,
|
||||||
|
Lists.newArrayList(routerLabelResolver));
|
||||||
|
|
||||||
|
Map<String, String> localMetadata = new HashMap<>();
|
||||||
|
localMetadata.put("env", "blue");
|
||||||
|
when(metadataLocalProperties.getContent()).thenReturn(localMetadata);
|
||||||
|
|
||||||
|
Set<String> expressionLabelKeys = Sets.newHashSet("${http.header.k1}", "${http.query.userid}");
|
||||||
|
when(routerRuleLabelResolver.getExpressionLabelKeys(anyString(), anyString(), anyString())).thenReturn(expressionLabelKeys);
|
||||||
|
|
||||||
|
MockServerHttpRequest request = MockServerHttpRequest.get("/" + calleeService + "/users")
|
||||||
|
.header("k1", "v1")
|
||||||
|
.queryParam("userid", "zhangsan")
|
||||||
|
.build();
|
||||||
|
MockServerWebExchange webExchange = new MockServerWebExchange.Builder(request).build();
|
||||||
|
|
||||||
|
Map<String, String> customMetadata = new HashMap<>();
|
||||||
|
customMetadata.put("k2", "v2");
|
||||||
|
when(routerLabelResolver.resolve(webExchange)).thenReturn(customMetadata);
|
||||||
|
|
||||||
|
PolarisRouterContext routerContext = polarisLoadBalancerClientFilter.genRouterContext(webExchange, calleeService);
|
||||||
|
|
||||||
|
Map<String, String> routerLabels = routerContext.getLabels(PolarisRouterContext.RULE_ROUTER_LABELS);
|
||||||
|
Assert.assertEquals("v1", routerLabels.get("${http.header.k1}"));
|
||||||
|
Assert.assertEquals("zhangsan", routerLabels.get("${http.query.userid}"));
|
||||||
|
Assert.assertEquals("blue", routerLabels.get("env"));
|
||||||
|
Assert.assertEquals("v1", routerLabels.get("t1"));
|
||||||
|
Assert.assertEquals("v2", routerLabels.get("t2"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testChooseInstanceWithoutRibbon() {
|
||||||
|
PolarisLoadBalancerClientFilter polarisLoadBalancerClientFilter = new PolarisLoadBalancerClientFilter(
|
||||||
|
loadBalancerClient, loadBalancerProperties, metadataLocalProperties, routerRuleLabelResolver,
|
||||||
|
Lists.newArrayList(routerLabelResolver));
|
||||||
|
|
||||||
|
String url = "/" + calleeService + "/users";
|
||||||
|
MockServerHttpRequest request = MockServerHttpRequest.get(url)
|
||||||
|
.header("k1", "v1")
|
||||||
|
.queryParam("userid", "zhangsan")
|
||||||
|
.build();
|
||||||
|
MockServerWebExchange webExchange = new MockServerWebExchange.Builder(request).build();
|
||||||
|
webExchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, URI.create("http://" + calleeService + "/users"));
|
||||||
|
|
||||||
|
polarisLoadBalancerClientFilter.choose(webExchange);
|
||||||
|
|
||||||
|
verify(loadBalancerClient).choose(calleeService);
|
||||||
|
verify(metadataLocalProperties, times(0)).getContent();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testChooseInstanceWithRibbon() {
|
||||||
|
RibbonLoadBalancerClient ribbonLoadBalancerClient = Mockito.mock(RibbonLoadBalancerClient.class);
|
||||||
|
|
||||||
|
PolarisLoadBalancerClientFilter polarisLoadBalancerClientFilter = new PolarisLoadBalancerClientFilter(
|
||||||
|
ribbonLoadBalancerClient, loadBalancerProperties, metadataLocalProperties, routerRuleLabelResolver,
|
||||||
|
Lists.newArrayList(routerLabelResolver));
|
||||||
|
|
||||||
|
Map<String, String> localMetadata = new HashMap<>();
|
||||||
|
localMetadata.put("env", "blue");
|
||||||
|
when(metadataLocalProperties.getContent()).thenReturn(localMetadata);
|
||||||
|
|
||||||
|
Set<String> expressionLabelKeys = Sets.newHashSet("${http.header.k1}", "${http.query.userid}");
|
||||||
|
when(routerRuleLabelResolver.getExpressionLabelKeys(anyString(), anyString(), anyString())).thenReturn(expressionLabelKeys);
|
||||||
|
|
||||||
|
String url = "/" + calleeService + "/users";
|
||||||
|
MockServerHttpRequest request = MockServerHttpRequest.get(url)
|
||||||
|
.header("k1", "v1")
|
||||||
|
.queryParam("userid", "zhangsan")
|
||||||
|
.build();
|
||||||
|
MockServerWebExchange webExchange = new MockServerWebExchange.Builder(request).build();
|
||||||
|
webExchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, URI.create("http://" + calleeService + "/users"));
|
||||||
|
|
||||||
|
Map<String, String> customMetadata = new HashMap<>();
|
||||||
|
customMetadata.put("k2", "v2");
|
||||||
|
when(routerLabelResolver.resolve(webExchange)).thenReturn(customMetadata);
|
||||||
|
|
||||||
|
polarisLoadBalancerClientFilter.choose(webExchange);
|
||||||
|
|
||||||
|
verify(ribbonLoadBalancerClient).choose(anyString(), any());
|
||||||
|
verify(metadataLocalProperties, times(1)).getContent();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,56 @@
|
|||||||
|
/*
|
||||||
|
* 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.discovery.service.callee;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import com.tencent.cloud.polaris.context.spi.InstanceMetadataProvider;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* custom metadata for instance.
|
||||||
|
*@author lepdou 2022-06-16
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class CustomMetadata implements InstanceMetadataProvider {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<String, String> getMetadata() {
|
||||||
|
Map<String, String> metadata = new HashMap<>();
|
||||||
|
metadata.put("k1", "v1");
|
||||||
|
return metadata;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getRegion() {
|
||||||
|
return "shanghai";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getZone() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getCampus() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>polaris-gateway-example</artifactId>
|
||||||
|
<groupId>com.tencent.cloud</groupId>
|
||||||
|
<version>${revision}</version>
|
||||||
|
<relativePath>../pom.xml</relativePath>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<artifactId>gateway-callee-service2</artifactId>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.tencent.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-tencent-polaris-discovery</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
@ -0,0 +1,35 @@
|
|||||||
|
/*
|
||||||
|
* 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.gateway.example.callee;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gateway callee application.
|
||||||
|
*
|
||||||
|
* @author Haotian Zhang
|
||||||
|
*/
|
||||||
|
@SpringBootApplication
|
||||||
|
public class GatewayCalleeApplication2 {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(GatewayCalleeApplication2.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,71 @@
|
|||||||
|
/*
|
||||||
|
* 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.gateway.example.callee;
|
||||||
|
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.net.URLDecoder;
|
||||||
|
|
||||||
|
import com.tencent.cloud.common.constant.MetadataConstant;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.web.bind.annotation.RequestHeader;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gateway callee controller.
|
||||||
|
*
|
||||||
|
* @author Haotian Zhang
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/gateway/example/callee")
|
||||||
|
public class GatewayCalleeController {
|
||||||
|
|
||||||
|
private static Logger LOG = LoggerFactory.getLogger(GatewayCalleeController.class);
|
||||||
|
|
||||||
|
@Value("${server.port:0}")
|
||||||
|
private int port;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get information of callee.
|
||||||
|
* @return information of callee
|
||||||
|
*/
|
||||||
|
@RequestMapping("/info")
|
||||||
|
public String info() {
|
||||||
|
LOG.info("Gateway Example Callee [{}] is called.", port);
|
||||||
|
return String.format("Gateway Example Callee [%s] is called.", port);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get metadata in HTTP header.
|
||||||
|
*
|
||||||
|
* @param metadataStr metadata string
|
||||||
|
* @return metadata in HTTP header
|
||||||
|
* @throws UnsupportedEncodingException encoding exception
|
||||||
|
*/
|
||||||
|
@RequestMapping("/echo")
|
||||||
|
public String echoHeader(
|
||||||
|
@RequestHeader(MetadataConstant.HeaderName.CUSTOM_METADATA) String metadataStr)
|
||||||
|
throws UnsupportedEncodingException {
|
||||||
|
LOG.info(URLDecoder.decode(metadataStr, "UTF-8"));
|
||||||
|
return URLDecoder.decode(metadataStr, "UTF-8");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
server:
|
||||||
|
session-timeout: 1800
|
||||||
|
port: 48082
|
||||||
|
spring:
|
||||||
|
application:
|
||||||
|
name: GatewayCalleeService
|
||||||
|
cloud:
|
||||||
|
tencent:
|
||||||
|
metadata:
|
||||||
|
content:
|
||||||
|
env: green
|
||||||
|
polaris:
|
||||||
|
address: grpc://183.47.111.80:8091
|
||||||
|
namespace: default
|
@ -0,0 +1,55 @@
|
|||||||
|
/*
|
||||||
|
* 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.spi;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Instance's custom metadata, metadata will be register to polaris server.
|
||||||
|
* @author lepdou 2022-06-16
|
||||||
|
*/
|
||||||
|
public interface InstanceMetadataProvider {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the metadata of instance.
|
||||||
|
*/
|
||||||
|
Map<String, String> getMetadata();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The region of current instance.
|
||||||
|
*
|
||||||
|
* @return the region info.
|
||||||
|
*/
|
||||||
|
String getRegion();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The zone of current instance.
|
||||||
|
*
|
||||||
|
* @return the zone info.
|
||||||
|
*/
|
||||||
|
String getZone();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The campus/datacenter of current instance.
|
||||||
|
*
|
||||||
|
* @return the campus or datacenter info.
|
||||||
|
*/
|
||||||
|
String getCampus();
|
||||||
|
}
|
Loading…
Reference in new issue