feat: add new key for java agent nacos discovery. (#1768)

2021
Fishtail 6 days ago committed by GitHub
parent ffbe8749a5
commit cf39c5bedd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -9,3 +9,4 @@
- [fix:replace with string inside @ConditionalOnClass.](https://github.com/Tencent/spring-cloud-tencent/pull/1755)
- [deps:upgrade GitHub Actions to v5.](https://github.com/Tencent/spring-cloud-tencent/pull/1757)
- [fix: send unit header in tsf gw.](https://github.com/Tencent/spring-cloud-tencent/pull/1759)
- [feat: add new key for java agent nacos discovery.](https://github.com/Tencent/spring-cloud-tencent/pull/1768)

@ -42,9 +42,9 @@ public class DiscoveryEnabledCondition implements Condition {
isDiscoveryEnabled |= isConsulDiscoveryEnabled;
boolean isNacosDiscoveryEnabled = Boolean.parseBoolean(
conditionContext.getEnvironment().getProperty("spring.cloud.nacos.enabled", "false"))
&& Boolean.parseBoolean(
conditionContext.getEnvironment().getProperty("spring.cloud.nacos.discovery.enabled", "true"));
conditionContext.getEnvironment().getProperty("spring.cloud.nacos.discovery.enabled", "false"))
|| Boolean.parseBoolean(
conditionContext.getEnvironment().getProperty("polaris.agent.nacos.discovery.enabled", "false"));
isDiscoveryEnabled |= isNacosDiscoveryEnabled;

@ -32,7 +32,7 @@ import static shade.polaris.com.alibaba.nacos.api.common.Constants.DEFAULT_NAMES
@ConfigurationProperties("spring.cloud.nacos")
public class NacosContextProperties {
@Value("${spring.cloud.nacos.discovery.enabled:#{'false'}}")
@Value("${polaris.agent.nacos.discovery.enabled:${spring.cloud.nacos.discovery.enabled:#{'false'}}}")
private boolean discoveryEnabled;
/**

@ -40,10 +40,12 @@ public class RegisterEnabledCondition implements Condition {
isRegisterEnabled |= isConsulRegisterEnabled;
boolean isNacosRegisterEnabled = Boolean
.parseBoolean(conditionContext.getEnvironment()
.getProperty("spring.cloud.nacos.enabled", "false"))
&& Boolean.parseBoolean(conditionContext.getEnvironment()
boolean isNacosDiscoveryEnabled = Boolean.parseBoolean(
conditionContext.getEnvironment().getProperty("spring.cloud.nacos.discovery.enabled", "false")) ||
Boolean.parseBoolean(
conditionContext.getEnvironment()
.getProperty("polaris.agent.nacos.discovery.enabled", "false"));
boolean isNacosRegisterEnabled = isNacosDiscoveryEnabled && Boolean.parseBoolean(conditionContext.getEnvironment()
.getProperty("spring.cloud.nacos.discovery.register-enabled", "true"));
isRegisterEnabled |= isNacosRegisterEnabled;

@ -0,0 +1,152 @@
/*
* Tencent is pleased to support the open source community by making spring-cloud-tencent available.
*
* Copyright (C) 2021 Tencent. 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;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;
/**
* Test for {@link DiscoveryEnabledCondition}.
*
* @author fishtailfu
*/
public class DiscoveryEnabledConditionTest {
private final DiscoveryEnabledCondition condition = new DiscoveryEnabledCondition();
@Test
public void testPolarisDiscoveryEnabled() {
ConditionContext context = Mockito.mock(ConditionContext.class);
Environment environment = Mockito.mock(Environment.class);
AnnotatedTypeMetadata metadata = Mockito.mock(AnnotatedTypeMetadata.class);
when(context.getEnvironment()).thenReturn(environment);
when(environment.getProperty("spring.cloud.polaris.discovery.enabled", "true")).thenReturn("true");
when(environment.getProperty("spring.cloud.consul.enabled", "false")).thenReturn("false");
when(environment.getProperty("spring.cloud.nacos.discovery.enabled", "false")).thenReturn("false");
when(environment.getProperty("polaris.agent.nacos.discovery.enabled", "false")).thenReturn("false");
boolean result = condition.matches(context, metadata);
assertThat(result).isTrue();
}
@Test
public void testPolarisDiscoveryDisabled() {
ConditionContext context = Mockito.mock(ConditionContext.class);
Environment environment = Mockito.mock(Environment.class);
AnnotatedTypeMetadata metadata = Mockito.mock(AnnotatedTypeMetadata.class);
when(context.getEnvironment()).thenReturn(environment);
when(environment.getProperty("spring.cloud.polaris.discovery.enabled", "true")).thenReturn("false");
when(environment.getProperty("spring.cloud.consul.enabled", "false")).thenReturn("false");
when(environment.getProperty("spring.cloud.nacos.discovery.enabled", "false")).thenReturn("false");
when(environment.getProperty("polaris.agent.nacos.discovery.enabled", "false")).thenReturn("false");
boolean result = condition.matches(context, metadata);
assertThat(result).isFalse();
}
@Test
public void testConsulDiscoveryEnabled() {
ConditionContext context = Mockito.mock(ConditionContext.class);
Environment environment = Mockito.mock(Environment.class);
AnnotatedTypeMetadata metadata = Mockito.mock(AnnotatedTypeMetadata.class);
when(context.getEnvironment()).thenReturn(environment);
when(environment.getProperty("spring.cloud.polaris.discovery.enabled", "true")).thenReturn("false");
when(environment.getProperty("spring.cloud.consul.enabled", "false")).thenReturn("true");
when(environment.getProperty("spring.cloud.consul.discovery.enabled", "true")).thenReturn("true");
when(environment.getProperty("spring.cloud.nacos.discovery.enabled", "false")).thenReturn("false");
when(environment.getProperty("polaris.agent.nacos.discovery.enabled", "false")).thenReturn("false");
boolean result = condition.matches(context, metadata);
assertThat(result).isTrue();
}
@Test
public void testNacosDiscoveryEnabled() {
ConditionContext context = Mockito.mock(ConditionContext.class);
Environment environment = Mockito.mock(Environment.class);
AnnotatedTypeMetadata metadata = Mockito.mock(AnnotatedTypeMetadata.class);
when(context.getEnvironment()).thenReturn(environment);
when(environment.getProperty("spring.cloud.polaris.discovery.enabled", "true")).thenReturn("false");
when(environment.getProperty("spring.cloud.consul.enabled", "false")).thenReturn("false");
when(environment.getProperty("spring.cloud.nacos.discovery.enabled", "false")).thenReturn("true");
when(environment.getProperty("polaris.agent.nacos.discovery.enabled", "false")).thenReturn("false");
boolean result = condition.matches(context, metadata);
assertThat(result).isTrue();
}
@Test
public void testPolarisAgentNacosDiscoveryEnabled() {
ConditionContext context = Mockito.mock(ConditionContext.class);
Environment environment = Mockito.mock(Environment.class);
AnnotatedTypeMetadata metadata = Mockito.mock(AnnotatedTypeMetadata.class);
when(context.getEnvironment()).thenReturn(environment);
when(environment.getProperty("spring.cloud.polaris.discovery.enabled", "true")).thenReturn("false");
when(environment.getProperty("spring.cloud.consul.enabled", "false")).thenReturn("false");
when(environment.getProperty("spring.cloud.nacos.discovery.enabled", "false")).thenReturn("false");
when(environment.getProperty("polaris.agent.nacos.discovery.enabled", "false")).thenReturn("true");
boolean result = condition.matches(context, metadata);
assertThat(result).isTrue();
}
@Test
public void testBothNacosPropertiesEnabled() {
ConditionContext context = Mockito.mock(ConditionContext.class);
Environment environment = Mockito.mock(Environment.class);
AnnotatedTypeMetadata metadata = Mockito.mock(AnnotatedTypeMetadata.class);
when(context.getEnvironment()).thenReturn(environment);
when(environment.getProperty("spring.cloud.polaris.discovery.enabled", "true")).thenReturn("false");
when(environment.getProperty("spring.cloud.consul.enabled", "false")).thenReturn("false");
when(environment.getProperty("spring.cloud.nacos.discovery.enabled", "false")).thenReturn("true");
when(environment.getProperty("polaris.agent.nacos.discovery.enabled", "false")).thenReturn("true");
boolean result = condition.matches(context, metadata);
assertThat(result).isTrue();
}
@Test
public void testAllDiscoveryDisabled() {
ConditionContext context = Mockito.mock(ConditionContext.class);
Environment environment = Mockito.mock(Environment.class);
AnnotatedTypeMetadata metadata = Mockito.mock(AnnotatedTypeMetadata.class);
when(context.getEnvironment()).thenReturn(environment);
when(environment.getProperty("spring.cloud.polaris.discovery.enabled", "true")).thenReturn("false");
when(environment.getProperty("spring.cloud.consul.enabled", "false")).thenReturn("false");
when(environment.getProperty("spring.cloud.consul.discovery.enabled", "true")).thenReturn("false");
when(environment.getProperty("spring.cloud.nacos.discovery.enabled", "false")).thenReturn("false");
when(environment.getProperty("polaris.agent.nacos.discovery.enabled", "false")).thenReturn("false");
boolean result = condition.matches(context, metadata);
assertThat(result).isFalse();
}
}

@ -0,0 +1,81 @@
/*
* Tencent is pleased to support the open source community by making spring-cloud-tencent available.
*
* Copyright (C) 2021 Tencent. 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.extend.nacos;
import com.tencent.cloud.polaris.context.PolarisSDKContextManager;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Test for {@link NacosContextProperties} with polaris agent property.
*
* @author fishtailfu
*/
@ExtendWith(SpringExtension.class)
@SpringBootTest(
classes = NacosContextPropertiesAgentTest.TestApplication.class,
properties = {
"spring.cloud.polaris.address=grpc://127.0.0.1:8091",
"spring.cloud.polaris.discovery.enabled=false",
"polaris.agent.nacos.discovery.enabled=true",
"spring.cloud.nacos.discovery.enabled=false",
"spring.cloud.nacos.server-addr=127.0.0.1:8848",
"spring.cloud.nacos.discovery.register-enabled=true",
"spring.cloud.nacos.discovery.group=polaris",
"spring.cloud.nacos.username=nacos",
"spring.cloud.nacos.password=nacos",
"spring.cloud.nacos.discovery.cluster-name=polaris",
"spring.cloud.nacos.context-path=/nacos"
}
)
@ActiveProfiles("agent-test")
public class NacosContextPropertiesAgentTest {
@Autowired
private NacosContextProperties nacosContextProperties;
@Test
public void testPolarisAgentNacosDiscoveryEnabled() {
assertThat(nacosContextProperties).isNotNull();
// Test that polaris.agent.nacos.discovery.enabled takes precedence
assertThat(nacosContextProperties.isDiscoveryEnabled()).isTrue();
assertThat(nacosContextProperties.getServerAddr()).isEqualTo("127.0.0.1:8848");
assertThat(nacosContextProperties.isRegisterEnabled()).isTrue();
assertThat(nacosContextProperties.getGroup()).isEqualTo("polaris");
assertThat(nacosContextProperties.getUsername()).isEqualTo("nacos");
assertThat(nacosContextProperties.getPassword()).isEqualTo("nacos");
assertThat(nacosContextProperties.getClusterName()).isEqualTo("polaris");
assertThat(nacosContextProperties.getContextPath()).isEqualTo("/nacos");
}
@SpringBootApplication
protected static class TestApplication {
static {
PolarisSDKContextManager.innerDestroy();
}
}
}

@ -61,6 +61,7 @@ public class NacosContextPropertiesTest {
@Test
public void testDefaultInitialization() {
assertThat(nacosContextProperties.isDiscoveryEnabled()).isTrue();
assertThat(nacosContextProperties).isNotNull();
assertThat(nacosContextProperties.getServerAddr()).isEqualTo("127.0.0.1:8848");
assertThat(nacosContextProperties.isRegisterEnabled()).isTrue();

@ -0,0 +1,190 @@
/*
* Tencent is pleased to support the open source community by making spring-cloud-tencent available.
*
* Copyright (C) 2021 Tencent. 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 org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;
/**
* Test for {@link RegisterEnabledCondition}.
*
* @author fishtailfu
*/
public class RegisterEnabledConditionTest {
private final RegisterEnabledCondition condition = new RegisterEnabledCondition();
@Test
public void testPolarisRegisterEnabled() {
ConditionContext context = Mockito.mock(ConditionContext.class);
Environment environment = Mockito.mock(Environment.class);
AnnotatedTypeMetadata metadata = Mockito.mock(AnnotatedTypeMetadata.class);
when(context.getEnvironment()).thenReturn(environment);
when(environment.getProperty("spring.cloud.polaris.discovery.register", "true")).thenReturn("true");
when(environment.getProperty("spring.cloud.consul.enabled", "false")).thenReturn("false");
when(environment.getProperty("spring.cloud.nacos.discovery.enabled", "false")).thenReturn("false");
when(environment.getProperty("polaris.agent.nacos.discovery.enabled", "false")).thenReturn("false");
boolean result = condition.matches(context, metadata);
assertThat(result).isTrue();
}
@Test
public void testPolarisRegisterDisabled() {
ConditionContext context = Mockito.mock(ConditionContext.class);
Environment environment = Mockito.mock(Environment.class);
AnnotatedTypeMetadata metadata = Mockito.mock(AnnotatedTypeMetadata.class);
when(context.getEnvironment()).thenReturn(environment);
when(environment.getProperty("spring.cloud.polaris.discovery.register", "true")).thenReturn("false");
when(environment.getProperty("spring.cloud.consul.enabled", "false")).thenReturn("false");
when(environment.getProperty("spring.cloud.nacos.discovery.enabled", "false")).thenReturn("false");
when(environment.getProperty("polaris.agent.nacos.discovery.enabled", "false")).thenReturn("false");
boolean result = condition.matches(context, metadata);
assertThat(result).isFalse();
}
@Test
public void testConsulRegisterEnabled() {
ConditionContext context = Mockito.mock(ConditionContext.class);
Environment environment = Mockito.mock(Environment.class);
AnnotatedTypeMetadata metadata = Mockito.mock(AnnotatedTypeMetadata.class);
when(context.getEnvironment()).thenReturn(environment);
when(environment.getProperty("spring.cloud.polaris.discovery.register", "true")).thenReturn("false");
when(environment.getProperty("spring.cloud.consul.enabled", "false")).thenReturn("true");
when(environment.getProperty("spring.cloud.consul.discovery.register", "true")).thenReturn("true");
when(environment.getProperty("spring.cloud.nacos.discovery.enabled", "false")).thenReturn("false");
when(environment.getProperty("polaris.agent.nacos.discovery.enabled", "false")).thenReturn("false");
boolean result = condition.matches(context, metadata);
assertThat(result).isTrue();
}
@Test
public void testNacosRegisterEnabled() {
ConditionContext context = Mockito.mock(ConditionContext.class);
Environment environment = Mockito.mock(Environment.class);
AnnotatedTypeMetadata metadata = Mockito.mock(AnnotatedTypeMetadata.class);
when(context.getEnvironment()).thenReturn(environment);
when(environment.getProperty("spring.cloud.polaris.discovery.register", "true")).thenReturn("false");
when(environment.getProperty("spring.cloud.consul.enabled", "false")).thenReturn("false");
when(environment.getProperty("spring.cloud.nacos.discovery.enabled", "false")).thenReturn("true");
when(environment.getProperty("polaris.agent.nacos.discovery.enabled", "false")).thenReturn("false");
when(environment.getProperty("spring.cloud.nacos.discovery.register-enabled", "true")).thenReturn("true");
boolean result = condition.matches(context, metadata);
assertThat(result).isTrue();
}
@Test
public void testNacosDiscoveryEnabledButRegisterDisabled() {
ConditionContext context = Mockito.mock(ConditionContext.class);
Environment environment = Mockito.mock(Environment.class);
AnnotatedTypeMetadata metadata = Mockito.mock(AnnotatedTypeMetadata.class);
when(context.getEnvironment()).thenReturn(environment);
when(environment.getProperty("spring.cloud.polaris.discovery.register", "true")).thenReturn("false");
when(environment.getProperty("spring.cloud.consul.enabled", "false")).thenReturn("false");
when(environment.getProperty("spring.cloud.nacos.discovery.enabled", "false")).thenReturn("true");
when(environment.getProperty("polaris.agent.nacos.discovery.enabled", "false")).thenReturn("false");
when(environment.getProperty("spring.cloud.nacos.discovery.register-enabled", "true")).thenReturn("false");
boolean result = condition.matches(context, metadata);
assertThat(result).isFalse();
}
@Test
public void testPolarisAgentNacosRegisterEnabled() {
ConditionContext context = Mockito.mock(ConditionContext.class);
Environment environment = Mockito.mock(Environment.class);
AnnotatedTypeMetadata metadata = Mockito.mock(AnnotatedTypeMetadata.class);
when(context.getEnvironment()).thenReturn(environment);
when(environment.getProperty("spring.cloud.polaris.discovery.register", "true")).thenReturn("false");
when(environment.getProperty("spring.cloud.consul.enabled", "false")).thenReturn("false");
when(environment.getProperty("spring.cloud.nacos.discovery.enabled", "false")).thenReturn("false");
when(environment.getProperty("polaris.agent.nacos.discovery.enabled", "false")).thenReturn("true");
when(environment.getProperty("spring.cloud.nacos.discovery.register-enabled", "true")).thenReturn("true");
boolean result = condition.matches(context, metadata);
assertThat(result).isTrue();
}
@Test
public void testPolarisAgentNacosDiscoveryEnabledButRegisterDisabled() {
ConditionContext context = Mockito.mock(ConditionContext.class);
Environment environment = Mockito.mock(Environment.class);
AnnotatedTypeMetadata metadata = Mockito.mock(AnnotatedTypeMetadata.class);
when(context.getEnvironment()).thenReturn(environment);
when(environment.getProperty("spring.cloud.polaris.discovery.register", "true")).thenReturn("false");
when(environment.getProperty("spring.cloud.consul.enabled", "false")).thenReturn("false");
when(environment.getProperty("spring.cloud.nacos.discovery.enabled", "false")).thenReturn("false");
when(environment.getProperty("polaris.agent.nacos.discovery.enabled", "false")).thenReturn("true");
when(environment.getProperty("spring.cloud.nacos.discovery.register-enabled", "true")).thenReturn("false");
boolean result = condition.matches(context, metadata);
assertThat(result).isFalse();
}
@Test
public void testBothNacosPropertiesEnabled() {
ConditionContext context = Mockito.mock(ConditionContext.class);
Environment environment = Mockito.mock(Environment.class);
AnnotatedTypeMetadata metadata = Mockito.mock(AnnotatedTypeMetadata.class);
when(context.getEnvironment()).thenReturn(environment);
when(environment.getProperty("spring.cloud.polaris.discovery.register", "true")).thenReturn("false");
when(environment.getProperty("spring.cloud.consul.enabled", "false")).thenReturn("false");
when(environment.getProperty("spring.cloud.nacos.discovery.enabled", "false")).thenReturn("true");
when(environment.getProperty("polaris.agent.nacos.discovery.enabled", "false")).thenReturn("true");
when(environment.getProperty("spring.cloud.nacos.discovery.register-enabled", "true")).thenReturn("true");
boolean result = condition.matches(context, metadata);
assertThat(result).isTrue();
}
@Test
public void testAllRegisterDisabled() {
ConditionContext context = Mockito.mock(ConditionContext.class);
Environment environment = Mockito.mock(Environment.class);
AnnotatedTypeMetadata metadata = Mockito.mock(AnnotatedTypeMetadata.class);
when(context.getEnvironment()).thenReturn(environment);
when(environment.getProperty("spring.cloud.polaris.discovery.register", "true")).thenReturn("false");
when(environment.getProperty("spring.cloud.consul.enabled", "false")).thenReturn("false");
when(environment.getProperty("spring.cloud.consul.discovery.register", "true")).thenReturn("false");
when(environment.getProperty("spring.cloud.nacos.discovery.enabled", "false")).thenReturn("false");
when(environment.getProperty("polaris.agent.nacos.discovery.enabled", "false")).thenReturn("false");
when(environment.getProperty("spring.cloud.nacos.discovery.register-enabled", "true")).thenReturn("false");
boolean result = condition.matches(context, metadata);
assertThat(result).isFalse();
}
}

@ -23,7 +23,6 @@ spring:
ip-address: 127.0.0.1
prefer-ip-address: true
nacos:
enabled: true
context-path: /nacos
discovery:
enabled: true

Loading…
Cancel
Save