Skip to content

feat:support namespace exports configuration if not created. #1613

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
- [feat:support ipv6.](https://github.com/Tencent/spring-cloud-tencent/pull/1598)
- [feat:support config all recover enabled.](https://github.com/Tencent/spring-cloud-tencent/pull/1604)
- [feat:support stat reporting path aggregation.](https://github.com/Tencent/spring-cloud-tencent/pull/1608)
- [feat:support namespace exports configuration if not created.](https://github.com/Tencent/spring-cloud-tencent/pull/1613)
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ public class PolarisDiscoveryProperties {
*/
private Boolean allRecoverEnabled = true;

/**
* namespace exports if not created.
*/
private String namespaceExports;

public String getInstanceId() {
return instanceId;
}
Expand Down Expand Up @@ -265,6 +270,14 @@ public void setRegisterEnabled(Boolean registerEnabled) {
this.registerEnabled = registerEnabled;
}

public String getNamespaceExports() {
return namespaceExports;
}

public void setNamespaceExports(String namespaceExports) {
this.namespaceExports = namespaceExports;
}

@Override
public String toString() {
return "PolarisDiscoveryProperties{" +
Expand All @@ -284,6 +297,7 @@ public String toString() {
", zeroProtectionNeedTestConnectivity=" + zeroProtectionNeedTestConnectivity +
", preferIpv6=" + preferIpv6 +
", allRecoverEnabled=" + allRecoverEnabled +
", namespaceExports='" + namespaceExports + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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.registry;

import com.tencent.cloud.polaris.PolarisDiscoveryProperties;
import com.tencent.polaris.api.utils.StringUtils;

/**
* 服务注册时端口相关逻辑.
*
* @author Haotian Zhang
*/
public class NamespaceRegistrationCustomizer implements PolarisRegistrationCustomizer {

private final PolarisDiscoveryProperties polarisDiscoveryProperties;

public NamespaceRegistrationCustomizer(PolarisDiscoveryProperties polarisDiscoveryProperties) {
this.polarisDiscoveryProperties = polarisDiscoveryProperties;
}

@Override
public void customize(PolarisRegistration registration) {
String namespaceExports = polarisDiscoveryProperties.getNamespaceExports();
if (StringUtils.isNotBlank(namespaceExports)) {
registration.getMetadata().put("POLARIS_INTERNAL_NAMESPACE_EXPORTS", namespaceExports);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,9 @@ public PolarisAutoServiceRegistration polarisAutoServiceRegistration(
public PolarisWebApplicationCheck polarisWebApplicationCheck() {
return new PolarisWebApplicationCheck();
}

@Bean
public NamespaceRegistrationCustomizer namespaceRegistrationCustomizer(PolarisDiscoveryProperties polarisDiscoveryProperties) {
return new NamespaceRegistrationCustomizer(polarisDiscoveryProperties);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@
"defaultValue": true,
"description": "is all recover enable. Default: true."
},
{
"name": "spring.cloud.polaris.discovery.namespace-exports",
"type": "java.lang.String",
"description": "namespace exports if not created."
},
{
"name": "spring.cloud.polaris.discovery.eager-load.enabled",
"type": "java.lang.Boolean",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ public void testGetAndSet() {
polarisDiscoveryProperties.setAllRecoverEnabled(false);
assertThat(polarisDiscoveryProperties.getAllRecoverEnabled()).isFalse();

polarisDiscoveryProperties.setNamespaceExports("*");
assertThat(polarisDiscoveryProperties.getNamespaceExports()).isEqualTo("*");

assertThat(polarisDiscoveryProperties.toString())
.isEqualTo("PolarisDiscoveryProperties{"
+ "namespace='Test'"
Expand All @@ -110,6 +113,7 @@ public void testGetAndSet() {
+ ", zeroProtectionEnabled=false"
+ ", zeroProtectionNeedTestConnectivity=false"
+ ", preferIpv6=true"
+ ", allRecoverEnabled=false}");
+ ", allRecoverEnabled=false"
+ ", namespaceExports='*'}");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void testCustomize() {
this.contextRunner.run(context -> {
PolarisRegistration polarisRegistration = context.getBean(PolarisRegistration.class);
polarisRegistration.customize();
PolarisRegistrationCustomizer customizer = context.getBean(PolarisRegistrationCustomizer.class);
TestPolarisRegistrationCustomizer customizer = context.getBean(TestPolarisRegistrationCustomizer.class);
verify(customizer, times(1)).customize(any(PolarisRegistration.class));
});
}
Expand All @@ -71,8 +71,14 @@ public void testCustomize() {
@EnableAutoConfiguration
static class PolarisServiceRegistryAutoConfiguration {
@Bean
public PolarisRegistrationCustomizer polarisRegistrationCustomizer() {
return mock(PolarisRegistrationCustomizer.class);
public TestPolarisRegistrationCustomizer testPolarisRegistrationCustomizer() {
return mock(TestPolarisRegistrationCustomizer.class);
}
}

static class TestPolarisRegistrationCustomizer implements PolarisRegistrationCustomizer {
@Override
public void customize(PolarisRegistration polarisRegistration) {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@

import com.tencent.cloud.common.spi.InstanceMetadataProvider;
import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
import com.tencent.cloud.common.util.inet.PolarisInetUtils;
import com.tencent.polaris.api.utils.StringUtils;
import com.tencent.polaris.metadata.core.constant.MetadataConstants;

import static com.tencent.cloud.common.constant.MetadataConstant.DefaultMetadata.DEFAULT_METADATA_SOURCE_SERVICE_NAME;
import static com.tencent.cloud.common.constant.MetadataConstant.DefaultMetadata.DEFAULT_METADATA_SOURCE_SERVICE_NAMESPACE;
Expand All @@ -48,10 +51,20 @@ public DefaultInstanceMetadataProvider(ApplicationContextAwareUtils applicationC

@Override
public Map<String, String> getMetadata() {
return new HashMap<String, String>() {{
put(DEFAULT_METADATA_SOURCE_SERVICE_NAMESPACE, LOCAL_NAMESPACE);
put(DEFAULT_METADATA_SOURCE_SERVICE_NAME, LOCAL_SERVICE);
}};
HashMap<String, String> defaultInstanceMetadata = new HashMap<>();
defaultInstanceMetadata.put(DEFAULT_METADATA_SOURCE_SERVICE_NAMESPACE, LOCAL_NAMESPACE);
defaultInstanceMetadata.put(DEFAULT_METADATA_SOURCE_SERVICE_NAME, LOCAL_SERVICE);

String ipv4Address = PolarisInetUtils.getIpString(false);
if (StringUtils.isNotBlank(ipv4Address)) {
defaultInstanceMetadata.put(MetadataConstants.ADDRESS_IPV4, ipv4Address);
}
String ipv6Address = PolarisInetUtils.getIpString(true);
if (StringUtils.isNotBlank(ipv6Address)) {
defaultInstanceMetadata.put(MetadataConstants.ADDRESS_IPV6, ipv6Address);
}

return defaultInstanceMetadata;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
@ExtendWith({MockitoExtension.class, SystemStubsExtension.class})
public class StaticMetadataManagerTest {

private static MockedStatic<ApplicationContextAwareUtils> mockedApplicationContextAwareUtils;
/**
* EnvironmentVariablesRule.
*/
Expand All @@ -65,8 +66,6 @@ public class StaticMetadataManagerTest {
@Mock
private MetadataLocalProperties metadataLocalProperties;

private static MockedStatic<ApplicationContextAwareUtils> mockedApplicationContextAwareUtils;

@BeforeAll
static void beforeAll() {
mockedApplicationContextAwareUtils = Mockito.mockStatic(ApplicationContextAwareUtils.class);
Expand Down Expand Up @@ -137,7 +136,7 @@ public void testCustomSPIMetadata() {
Arrays.asList(new MockedMetadataProvider(), new DefaultInstanceMetadataProvider(null)));

Map<String, String> metadata = metadataManager.getAllCustomMetadata();
assertThat(metadata.size()).isEqualTo(5);
assertThat(metadata.size()).isEqualTo(6);
assertThat(metadata.get("k1")).isEqualTo("v1");
assertThat(metadata.get("k2")).isEqualTo("v22");
assertThat(metadata.get("k3")).isEqualTo("v33");
Expand Down Expand Up @@ -182,7 +181,7 @@ public void testMergedMetadata() {
Arrays.asList(new MockedMetadataProvider(), new DefaultInstanceMetadataProvider(null)));

Map<String, String> metadata = metadataManager.getMergedStaticMetadata();
assertThat(metadata.size()).isEqualTo(8);
assertThat(metadata.size()).isEqualTo(9);
assertThat(metadata.get("k1")).isEqualTo("v1");
assertThat(metadata.get("k2")).isEqualTo("v22");
assertThat(metadata.get("k3")).isEqualTo("v33");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ public void postProcessEnvironment(ConfigurableEnvironment environment, SpringAp
polarisEnvProperties.put("spring.cloud.tencent.metadata.content.zone", zone);
}

// global namespace enabled
String globalNamespaceEnabled = environment.getProperty("global_namespace_enabled");
if (StringUtils.isNotBlank(globalNamespaceEnabled) && StringUtils.equals("true", globalNamespaceEnabled)) {
polarisEnvProperties.put("spring.cloud.polaris.discovery.namespace-exports", "*");
}

MapPropertySource propertySource = new MapPropertySource("polaris-env-properties", polarisEnvProperties);
environment.getPropertySources().addFirst(propertySource);
}
Expand Down