fix: lossless plugin log support ns (#1244)

Co-authored-by: shedfreewu <shedfreewu@tencent.com>
pull/1245/head
shedfreewu 4 months ago committed by GitHub
parent 4c7a9c8288
commit b1b354c784
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -55,7 +55,14 @@
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-polaris-discovery</artifactId>
<scope>test</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.2.9.RELEASE</version>
<optional>true</optional>
</dependency>
<dependency>

@ -18,6 +18,7 @@
package com.tencent.cloud.plugin.lossless;
import com.tencent.cloud.plugin.lossless.config.LosslessProperties;
import com.tencent.cloud.plugin.lossless.transfomer.DiscoveryNamespaceGetter;
import com.tencent.cloud.polaris.context.PolarisSDKContextManager;
import com.tencent.polaris.api.pojo.BaseInstance;
import org.aspectj.lang.ProceedingJoinPoint;
@ -44,12 +45,16 @@ public class LosslessRegistryAspect {
private PolarisSDKContextManager polarisSDKContextManager;
private DiscoveryNamespaceGetter discoveryNamespaceGetter;
public LosslessRegistryAspect(ServiceRegistry serviceRegistry, Registration registration,
LosslessProperties losslessProperties, PolarisSDKContextManager polarisSDKContextManager) {
LosslessProperties losslessProperties, PolarisSDKContextManager polarisSDKContextManager,
DiscoveryNamespaceGetter discoveryNamespaceGetter) {
this.serviceRegistry = serviceRegistry;
this.registration = registration;
this.losslessProperties = losslessProperties;
this.polarisSDKContextManager = polarisSDKContextManager;
this.discoveryNamespaceGetter = discoveryNamespaceGetter;
}
@Pointcut("execution(public * org.springframework.cloud.client.serviceregistry.ServiceRegistry.register(..))")
@ -66,7 +71,7 @@ public class LosslessRegistryAspect {
public Object invokeRegister(ProceedingJoinPoint joinPoint) throws Throwable {
// web started, get port from registration
BaseInstance instance = SpringCloudLosslessActionProvider.getBaseInstance(registration);
BaseInstance instance = SpringCloudLosslessActionProvider.getBaseInstance(registration, discoveryNamespaceGetter);
Runnable registerAction = () -> executeJoinPoint(joinPoint);

@ -22,6 +22,7 @@ import java.util.Map;
import com.tencent.cloud.common.util.OkHttpUtil;
import com.tencent.cloud.plugin.lossless.config.LosslessProperties;
import com.tencent.cloud.plugin.lossless.transfomer.DiscoveryNamespaceGetter;
import com.tencent.polaris.api.plugin.lossless.InstanceProperties;
import com.tencent.polaris.api.plugin.lossless.LosslessActionProvider;
import com.tencent.polaris.api.pojo.BaseInstance;
@ -88,13 +89,16 @@ public class SpringCloudLosslessActionProvider implements LosslessActionProvider
losslessProperties.getHealthCheckPath(), headers);
}
public static BaseInstance getBaseInstance(Registration registration) {
return getBaseInstance(registration, registration.getPort());
public static BaseInstance getBaseInstance(Registration registration, DiscoveryNamespaceGetter namespaceGetter) {
return getBaseInstance(registration, registration.getPort(), namespaceGetter);
}
public static BaseInstance getBaseInstance(Registration registration, Integer port) {
// for common spring cloud registration, not set namespace
public static BaseInstance getBaseInstance(Registration registration, Integer port,
DiscoveryNamespaceGetter namespaceGetter) {
DefaultBaseInstance baseInstance = new DefaultBaseInstance();
if (namespaceGetter != null) {
baseInstance.setNamespace(namespaceGetter.getNamespace());
}
baseInstance.setService(registration.getServiceId());
// before web start, port in registration not init
baseInstance.setPort(port);

@ -18,10 +18,20 @@
package com.tencent.cloud.plugin.lossless.config;
import com.alibaba.cloud.nacos.ConditionalOnNacosDiscoveryEnabled;
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import com.tencent.cloud.plugin.lossless.LosslessRegistryAspect;
import com.tencent.cloud.plugin.lossless.transfomer.DiscoveryNamespaceGetter;
import com.tencent.cloud.plugin.lossless.transfomer.NacosDiscoveryNamespaceGetter;
import com.tencent.cloud.plugin.lossless.transfomer.PolarisDiscoveryNamespaceGetter;
import com.tencent.cloud.polaris.PolarisDiscoveryProperties;
import com.tencent.cloud.polaris.context.ConditionalOnPolarisEnabled;
import com.tencent.cloud.polaris.context.PolarisSDKContextManager;
import com.tencent.cloud.polaris.discovery.ConditionalOnPolarisDiscoveryEnabled;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
@ -42,7 +52,33 @@ public class LosslessAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public LosslessRegistryAspect losslessRegistryAspect(ServiceRegistry serviceRegistry, Registration registration,
LosslessProperties losslessProperties, PolarisSDKContextManager polarisSDKContextManager) {
return new LosslessRegistryAspect(serviceRegistry, registration, losslessProperties, polarisSDKContextManager);
LosslessProperties losslessProperties, PolarisSDKContextManager polarisSDKContextManager,
@Autowired(required = false) DiscoveryNamespaceGetter discoveryNamespaceGetter) {
return new LosslessRegistryAspect(serviceRegistry, registration, losslessProperties, polarisSDKContextManager, discoveryNamespaceGetter);
}
@ConditionalOnClass(name = "com.alibaba.cloud.nacos.NacosDiscoveryProperties")
static class Nacos {
@Bean
@ConditionalOnMissingBean
@ConditionalOnNacosDiscoveryEnabled
@ConditionalOnBean(NacosDiscoveryProperties.class)
public NacosDiscoveryNamespaceGetter nacosDiscoveryNamespaceGetter(
NacosDiscoveryProperties nacosDiscoveryProperties) {
return new NacosDiscoveryNamespaceGetter(nacosDiscoveryProperties);
}
}
@ConditionalOnClass(name = "com.tencent.cloud.polaris.PolarisDiscoveryProperties")
static class Polaris {
@Bean
@ConditionalOnMissingBean
@ConditionalOnPolarisDiscoveryEnabled
@ConditionalOnBean(PolarisDiscoveryProperties.class)
public PolarisDiscoveryNamespaceGetter polarisDiscoveryNamespaceGetter(
PolarisDiscoveryProperties polarisDiscoveryProperties) {
return new PolarisDiscoveryNamespaceGetter(polarisDiscoveryProperties);
}
}
}

@ -0,0 +1,28 @@
/*
* 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.plugin.lossless.transfomer;
/**
* Interface for discovery namespace getter.
*
* @author Shedfree Wu
*/
public interface DiscoveryNamespaceGetter {
String getNamespace();
}

@ -0,0 +1,47 @@
/*
* 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.plugin.lossless.transfomer;
import java.lang.reflect.Method;
import java.util.Properties;
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import com.alibaba.nacos.client.naming.utils.InitUtils;
import org.springframework.util.ReflectionUtils;
/**
* Discovery namespace getter for Nacos.
*
* @author Shedfree Wu
*/
public class NacosDiscoveryNamespaceGetter implements DiscoveryNamespaceGetter {
private String namespace;
public NacosDiscoveryNamespaceGetter(NacosDiscoveryProperties nacosDiscoveryProperties) {
// getNacosProperties is private in low version of spring-cloud-starter-alibaba-nacos-discovery
Method method = ReflectionUtils.findMethod(NacosDiscoveryProperties.class, "getNacosProperties");
method.setAccessible(true);
this.namespace = InitUtils.initNamespaceForNaming(
(Properties) ReflectionUtils.invokeMethod(method, nacosDiscoveryProperties));
}
public String getNamespace() {
return namespace;
}
}

@ -0,0 +1,38 @@
/*
* 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.plugin.lossless.transfomer;
import com.tencent.cloud.polaris.PolarisDiscoveryProperties;
/**
* Discovery namespace getter for Polaris.
*
* @author Shedfree Wu
*/
public class PolarisDiscoveryNamespaceGetter implements DiscoveryNamespaceGetter {
private String namespace;
public PolarisDiscoveryNamespaceGetter(PolarisDiscoveryProperties polarisDiscoveryProperties) {
this.namespace = polarisDiscoveryProperties.getNamespace();
}
public String getNamespace() {
return namespace;
}
}

@ -38,6 +38,7 @@ public class LosslessConfigModifierTest {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(TestApplication.class))
.withPropertyValues("spring.cloud.nacos.discovery.enabled=false")
.withPropertyValues("spring.cloud.polaris.enabled=true")
.withPropertyValues("spring.cloud.polaris.lossless.enabled=true")
.withPropertyValues("spring.cloud.polaris.lossless.port=20000")
@ -48,6 +49,7 @@ public class LosslessConfigModifierTest {
.withPropertyValues("spring.cloud.gateway.enabled=false");
private final ApplicationContextRunner disabledContextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(TestApplication.class))
.withPropertyValues("spring.cloud.nacos.discovery.enabled=false")
.withPropertyValues("spring.cloud.polaris.enabled=true")
.withPropertyValues("spring.cloud.polaris.lossless.enabled=false")
.withPropertyValues("spring.application.name=test")

@ -19,15 +19,22 @@ package com.tencent.cloud.plugin.lossless;
import java.util.Collections;
import com.alibaba.cloud.nacos.NacosServiceAutoConfiguration;
import com.alibaba.cloud.nacos.discovery.NacosDiscoveryAutoConfiguration;
import com.alibaba.cloud.nacos.registry.NacosRegistration;
import com.alibaba.cloud.nacos.registry.NacosServiceRegistryAutoConfiguration;
import com.alibaba.cloud.nacos.util.UtilIPv6AutoConfiguration;
import com.tencent.cloud.common.util.OkHttpUtil;
import com.tencent.cloud.plugin.lossless.config.LosslessAutoConfiguration;
import com.tencent.cloud.plugin.lossless.config.LosslessPropertiesBootstrapConfiguration;
import com.tencent.cloud.plugin.lossless.transfomer.DiscoveryNamespaceGetter;
import com.tencent.cloud.polaris.context.PolarisSDKContextManager;
import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration;
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryAutoConfiguration;
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryClientConfiguration;
import com.tencent.cloud.polaris.registry.PolarisRegistration;
import com.tencent.cloud.polaris.registry.PolarisServiceRegistry;
import com.tencent.polaris.api.pojo.BaseInstance;
import com.tencent.polaris.api.pojo.ServiceKey;
import com.tencent.polaris.test.mock.discovery.NamingServer;
import org.junit.jupiter.api.AfterAll;
@ -39,7 +46,10 @@ import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
import org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration;
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties;
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationUtils;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.cloud.commons.util.UtilAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import static org.assertj.core.api.Assertions.assertThat;
@ -72,6 +82,7 @@ public class LosslessRegistryAspectTest {
PolarisPropertiesConfiguration.class,
PolarisDiscoveryClientConfiguration.class,
PolarisDiscoveryAutoConfiguration.class))
.withPropertyValues("spring.cloud.nacos.discovery.enabled=false")
.withPropertyValues("spring.cloud.polaris.lossless.delayRegisterInterval=5000")
.withPropertyValues("spring.cloud.polaris.lossless.healthCheckPath=")
.withPropertyValues("spring.cloud.polaris.lossless.port=" + LOSSLESS_PORT_1)
@ -91,6 +102,7 @@ public class LosslessRegistryAspectTest {
PolarisPropertiesConfiguration.class,
PolarisDiscoveryClientConfiguration.class,
PolarisDiscoveryAutoConfiguration.class))
.withPropertyValues("spring.cloud.nacos.discovery.enabled=false")
.withPropertyValues("spring.cloud.polaris.lossless.healthCheckInterval=1000")
.withPropertyValues("spring.cloud.polaris.lossless.healthCheckPath=/test")
.withPropertyValues("spring.cloud.polaris.lossless.port=28082")
@ -102,6 +114,31 @@ public class LosslessRegistryAspectTest {
.withPropertyValues("spring.cloud.polaris.discovery.namespace=" + NAMESPACE_TEST)
.withPropertyValues("spring.cloud.polaris.discovery.token=xxxxxx");
private final WebApplicationContextRunner nacosContextRunner = new WebApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(
AutoServiceRegistrationProperties.class,
UtilAutoConfiguration.class,
UtilIPv6AutoConfiguration.class,
NacosDiscoveryAutoConfiguration.class,
NacosServiceAutoConfiguration.class,
NacosServiceRegistryAutoConfiguration.class,
LosslessAutoConfiguration.class,
LosslessPropertiesBootstrapConfiguration.class,
PolarisContextAutoConfiguration.class)
)
.withPropertyValues("spring.cloud.nacos.discovery.enabled=true")
.withPropertyValues("spring.cloud.nacos.discovery.namespace=" + NAMESPACE_TEST)
.withPropertyValues("spring.cloud.polaris.discovery.enabled=false")
.withPropertyValues("spring.cloud.polaris.lossless.delayRegisterInterval=5000")
.withPropertyValues("spring.cloud.polaris.lossless.healthCheckPath=")
.withPropertyValues("spring.cloud.polaris.lossless.port=" + LOSSLESS_PORT_1)
.withPropertyValues("spring.application.name=" + SERVICE_PROVIDER)
.withPropertyValues("server.port=" + APPLICATION_PORT)
.withPropertyValues("spring.cloud.polaris.localIpAddress=" + HOST)
.withPropertyValues("spring.cloud.polaris.localPort=" + APPLICATION_PORT)
.withPropertyValues("spring.cloud.polaris.address=grpc://127.0.0.1:10081")
.withPropertyValues("spring.cloud.polaris.discovery.token=xxxxxx");
@BeforeAll
static void beforeAll() throws Exception {
namingServer = NamingServer.startNamingServer(10081);
@ -181,6 +218,21 @@ public class LosslessRegistryAspectTest {
});
}
@Test
public void testNaocsRegister() {
this.nacosContextRunner.run(context -> {
DiscoveryNamespaceGetter discoveryNamespaceGetter = context.getBean(DiscoveryNamespaceGetter.class);
Registration registration = context.getBean(Registration.class);
assertThat(registration instanceof NacosRegistration).isTrue();
assertThat(discoveryNamespaceGetter.getNamespace()).isEqualTo(NAMESPACE_TEST);
BaseInstance baseInstance = SpringCloudLosslessActionProvider.getBaseInstance(registration, discoveryNamespaceGetter);
assertThat(baseInstance.getNamespace()).isEqualTo(NAMESPACE_TEST);
});
}
@Configuration
@EnableAutoConfiguration

Loading…
Cancel
Save