Interceptor using aspect instead of reflection

pull/1242/head
shedfreewu 2 years ago
parent d721d27441
commit 0ed97436d5

@ -1,2 +1,4 @@
# Change Log # Change Log
--- ---
- [feat: support lossless register and deregister](https://github.com/Tencent/spring-cloud-tencent/issues/977)

@ -25,10 +25,10 @@ import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import com.tencent.cloud.common.metadata.StaticMetadataManager; import com.tencent.cloud.common.metadata.StaticMetadataManager;
import com.tencent.cloud.common.util.OkHttpUtil;
import com.tencent.cloud.polaris.PolarisDiscoveryProperties; import com.tencent.cloud.polaris.PolarisDiscoveryProperties;
import com.tencent.cloud.polaris.context.PolarisSDKContextManager; import com.tencent.cloud.polaris.context.PolarisSDKContextManager;
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler; import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler;
import com.tencent.cloud.polaris.util.OkHttpUtil;
import com.tencent.cloud.rpc.enhancement.stat.config.PolarisStatProperties; import com.tencent.cloud.rpc.enhancement.stat.config.PolarisStatProperties;
import com.tencent.polaris.api.config.global.StatReporterConfig; import com.tencent.polaris.api.config.global.StatReporterConfig;
import com.tencent.polaris.api.core.ProviderAPI; import com.tencent.polaris.api.core.ProviderAPI;

@ -15,7 +15,7 @@
* specific language governing permissions and limitations under the License. * specific language governing permissions and limitations under the License.
*/ */
package com.tencent.cloud.polaris.util; package com.tencent.cloud.common.util;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.InputStreamReader; import java.io.InputStreamReader;

@ -18,8 +18,6 @@
package com.tencent.cloud.common.util; package com.tencent.cloud.common.util;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
@ -70,102 +68,4 @@ public final class ReflectionUtils extends org.springframework.util.ReflectionUt
} }
return null; return null;
} }
/**
* get property of class object by property name.
*
* @param target object
* @param fieldName property name of class object
* @return value
*/
public static Object getObjectByFieldName(Object target, String fieldName) {
try {
Field field = target.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
return field.get(target);
}
catch (Exception e) {
throw new RuntimeException("getObjectByFieldName", e);
}
}
/**
* get property of parent class object by property name.
*
* @param target object
* @param fieldName property name of parent class object
* @return value
*/
public static Object getSuperObjectByFieldName(Object target, String fieldName) {
try {
Field field = target.getClass().getSuperclass().getDeclaredField(fieldName);
field.setAccessible(true);
return field.get(target);
}
catch (Exception e) {
throw new RuntimeException("getSuperObjectByFieldName", e);
}
}
/**
* set property of class object by property name.
*
* @param target object
* @param fieldName property name of class object
* @param value new value
*/
public static void setValueByFieldName(Object target, String fieldName, Object value) {
try {
Field field = target.getClass().getDeclaredField(fieldName);
setValue(target, field, value);
}
catch (Exception e) {
throw new RuntimeException("setValueByFieldName", e);
}
}
/**
* set property of parent class object by property name.
*
* @param target object
* @param fieldName property name of parent class object
* @param value new value
*/
public static void setSuperValueByFieldName(Object target, String fieldName, Object value) {
try {
Field field = target.getClass().getSuperclass().getDeclaredField(fieldName);
setValue(target, field, value);
}
catch (Exception e) {
throw new RuntimeException("setSuperValueByFieldName", e);
}
}
private static void setValue(Object target, Field field, Object value) {
try {
Field modifiers = getModifiersField();
modifiers.setAccessible(true);
modifiers.setInt(field, field.getModifiers() & ~Modifier.FINAL);
field.setAccessible(true);
field.set(target, value);
}
catch (Exception e) {
throw new RuntimeException("setValue", e);
}
}
private static Field getModifiersField() throws Exception {
Method getDeclaredFields0 = Class.class.getDeclaredMethod("getDeclaredFields0", boolean.class);
getDeclaredFields0.setAccessible(true);
Field[] fields = (Field[]) getDeclaredFields0.invoke(Field.class, false);
Field modifierField = null;
for (Field f : fields) {
if ("modifiers".equals(f.getName())) {
modifierField = f;
break;
}
}
return modifierField;
}
} }

@ -15,7 +15,7 @@
* specific language governing permissions and limitations under the License. * specific language governing permissions and limitations under the License.
*/ */
package com.tencent.cloud.polaris.util; package com.tencent.cloud.common.util;
import org.assertj.core.util.Maps; import org.assertj.core.util.Maps;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -37,7 +37,13 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Haotian Zhang * @author Haotian Zhang
*/ */
@ExtendWith(SpringExtension.class) @ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = OkHttpUtilTest.TestApplication.class, properties = {"spring.application.name=test", "spring.cloud.polaris.discovery.register=false"}) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
classes = OkHttpUtilTest.TestApplication.class,
properties = {
"spring.application.name=test",
"spring.cloud.polaris.discovery.register=false",
"spring.cloud.gateway.enabled=false"
})
public class OkHttpUtilTest { public class OkHttpUtilTest {
@LocalServerPort @LocalServerPort
@ -47,6 +53,7 @@ public class OkHttpUtilTest {
public void testGet() { public void testGet() {
assertThat(OkHttpUtil.get("http://localhost:" + port + "/test", Maps.newHashMap("key", "value"))).isTrue(); assertThat(OkHttpUtil.get("http://localhost:" + port + "/test", Maps.newHashMap("key", "value"))).isTrue();
assertThat(OkHttpUtil.checkUrl("localhost", port, "/test", Maps.newHashMap("key", "value"))).isTrue(); assertThat(OkHttpUtil.checkUrl("localhost", port, "/test", Maps.newHashMap("key", "value"))).isTrue();
assertThat(OkHttpUtil.checkUrl("localhost", port, "test", Maps.newHashMap("key", "value"))).isTrue();
assertThat(OkHttpUtil.get("http://localhost:" + port + "/error", Maps.newHashMap("key", "value"))).isFalse(); assertThat(OkHttpUtil.get("http://localhost:" + port + "/error", Maps.newHashMap("key", "value"))).isFalse();
assertThat(OkHttpUtil.get("http://localhost:55555/error", Maps.newHashMap("key", "value"))).isFalse(); assertThat(OkHttpUtil.get("http://localhost:55555/error", Maps.newHashMap("key", "value"))).isFalse();
} }

@ -88,6 +88,11 @@
<groupId>com.tencent.cloud</groupId> <groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-discovery-adapter-plugin</artifactId> <artifactId>spring-cloud-starter-tencent-discovery-adapter-plugin</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-lossless-plugin</artifactId>
</dependency>
</dependencies> </dependencies>
<build> <build>

@ -193,6 +193,12 @@
<version>${revision}</version> <version>${revision}</version>
</dependency> </dependency>
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-lossless-plugin</artifactId>
<version>${revision}</version>
</dependency>
<!-- third part framework dependencies --> <!-- third part framework dependencies -->
<dependency> <dependency>
<groupId>com.google.guava</groupId> <groupId>com.google.guava</groupId>

@ -25,11 +25,6 @@
<artifactId>spring-cloud-tencent-commons</artifactId> <artifactId>spring-cloud-tencent-commons</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-polaris-discovery</artifactId>
</dependency>
<dependency> <dependency>
<groupId>com.tencent.polaris</groupId> <groupId>com.tencent.polaris</groupId>
<artifactId>lossless-register</artifactId> <artifactId>lossless-register</artifactId>
@ -57,6 +52,12 @@
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-polaris-discovery</artifactId>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>com.tencent.polaris</groupId> <groupId>com.tencent.polaris</groupId>
<artifactId>polaris-test-mock-discovery</artifactId> <artifactId>polaris-test-mock-discovery</artifactId>

@ -1,95 +0,0 @@
/*
* 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;
import com.tencent.cloud.common.util.ReflectionUtils;
import com.tencent.cloud.plugin.lossless.config.LosslessProperties;
import com.tencent.cloud.polaris.context.PolarisSDKContextManager;
import com.tencent.polaris.api.pojo.BaseInstance;
import com.tencent.polaris.api.pojo.DefaultBaseInstance;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
/**
* Wrap Spring Bean and proxy for serviceRegistry.
*
* @author Shedfree Wu
*/
public class LosslessBeanPostProcessor implements BeanPostProcessor {
private PolarisSDKContextManager polarisSDKContextManager;
private LosslessProperties losslessProperties;
private Registration registration;
private Integer port;
public LosslessBeanPostProcessor(PolarisSDKContextManager polarisSDKContextManager,
LosslessProperties losslessProperties, Registration registration, Integer port) {
this.polarisSDKContextManager = polarisSDKContextManager;
this.losslessProperties = losslessProperties;
this.registration = registration;
this.port = port;
}
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof AbstractAutoServiceRegistration) {
wrap(bean, polarisSDKContextManager, losslessProperties, registration, port);
}
return bean;
}
public static void wrap(Object bean, PolarisSDKContextManager polarisSDKContextManager,
LosslessProperties losslessProperties, Registration registration, Integer port) {
LosslessProxyServiceRegistry proxyServiceRegistry;
String clsName = bean.getClass().getCanonicalName();
if (clsName.contains("org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration")) {
ServiceRegistry<Registration> registry =
(ServiceRegistry<Registration>) ReflectionUtils.
getObjectByFieldName(bean, "serviceRegistry");
proxyServiceRegistry = new LosslessProxyServiceRegistry(registry, polarisSDKContextManager, registration);
ReflectionUtils.setValueByFieldName(bean, "serviceRegistry", proxyServiceRegistry);
}
else {
ServiceRegistry<Registration> registry =
(ServiceRegistry<Registration>) ReflectionUtils.
getSuperObjectByFieldName(bean, "serviceRegistry");
proxyServiceRegistry = new LosslessProxyServiceRegistry(registry, polarisSDKContextManager, registration);
ReflectionUtils.setSuperValueByFieldName(bean, "serviceRegistry", proxyServiceRegistry);
}
SpringCloudLosslessActionProvider losslessActionProvider =
new SpringCloudLosslessActionProvider(proxyServiceRegistry, losslessProperties);
polarisSDKContextManager.getLosslessAPI().setLosslessActionProvider(
getBaseInstance(registration, port), losslessActionProvider);
}
public static BaseInstance getBaseInstance(Registration registration, Integer port) {
// registration 通用,不设置 ns
DefaultBaseInstance baseInstance = new DefaultBaseInstance();
baseInstance.setService(registration.getServiceId());
// 由于 PolarisRegistration 的 port 在 web 启动后才能生成,需从外部传入
baseInstance.setPort(port);
baseInstance.setHost(registration.getHost());
return baseInstance;
}
}

@ -1,93 +0,0 @@
/*
* 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;
import java.util.concurrent.atomic.AtomicBoolean;
import com.tencent.cloud.polaris.context.PolarisSDKContextManager;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
/**
* Lossless proxy for {@link ServiceRegistry}.
*
* @author Shedfree Wu
*/
public class LosslessProxyServiceRegistry implements ServiceRegistry<Registration> {
private final ServiceRegistry<Registration> target;
private Registration registration;
private PolarisSDKContextManager polarisSDKContextManager;
private final AtomicBoolean doneDeregister = new AtomicBoolean(false);
public LosslessProxyServiceRegistry(ServiceRegistry<Registration> target,
PolarisSDKContextManager polarisSDKContextManager, Registration registration) {
this.target = target;
this.polarisSDKContextManager = polarisSDKContextManager;
this.registration = registration;
}
@Override
public void register(Registration registration) {
this.registration = registration;
// web started, get port from registration
polarisSDKContextManager.getLosslessAPI().losslessRegister(
LosslessBeanPostProcessor.getBaseInstance(registration, registration.getPort()));
}
@Override
public void deregister(Registration registration) {
if (doneDeregister.compareAndSet(false, true)) {
target.deregister(registration);
}
}
public void deregister() {
// 需要兼容其他 discovery, spring cloud deregister 统一幂等处理
if (registration != null && doneDeregister.compareAndSet(false, true)) {
target.deregister(registration);
}
}
public ServiceRegistry<Registration> getTarget() {
return target;
}
public Registration getRegistration() {
return registration;
}
@Override
public void close() {
target.close();
}
@Override
public void setStatus(Registration registration, String status) {
target.setStatus(registration, status);
}
@Override
public <T> T getStatus(Registration registration) {
return target.getStatus(registration);
}
}

@ -0,0 +1,105 @@
/*
* 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;
import java.util.concurrent.atomic.AtomicBoolean;
import com.tencent.cloud.plugin.lossless.config.LosslessProperties;
import com.tencent.cloud.polaris.context.PolarisSDKContextManager;
import com.tencent.polaris.api.pojo.BaseInstance;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
/**
* Intercept for of register and deregister.
*
* @author Shedfree Wu
*/
@Aspect
public class LosslessRegistryAspect {
private ServiceRegistry serviceRegistry;
private Registration registration;
private LosslessProperties losslessProperties;
private PolarisSDKContextManager polarisSDKContextManager;
private final AtomicBoolean doneDeregister = new AtomicBoolean(false);
public LosslessRegistryAspect(ServiceRegistry serviceRegistry, Registration registration,
LosslessProperties losslessProperties, PolarisSDKContextManager polarisSDKContextManager) {
this.serviceRegistry = serviceRegistry;
this.registration = registration;
this.losslessProperties = losslessProperties;
this.polarisSDKContextManager = polarisSDKContextManager;
}
@Pointcut("execution(public * org.springframework.cloud.client.serviceregistry.ServiceRegistry.register(..))")
public void registerPointcut() {
}
@Pointcut("execution(public * org.springframework.cloud.client.serviceregistry.ServiceRegistry.deregister(..))")
public void deregisterPointcut() {
}
@Around("registerPointcut()")
public Object invokeRegister(ProceedingJoinPoint joinPoint) throws Throwable {
// web started, get port from registration
BaseInstance instance = SpringCloudLosslessActionProvider.getBaseInstance(registration);
Runnable registerAction = () -> executeJoinPoint(joinPoint);
SpringCloudLosslessActionProvider losslessActionProvider =
new SpringCloudLosslessActionProvider(serviceRegistry, registration, losslessProperties, registerAction);
polarisSDKContextManager.getLosslessAPI().setLosslessActionProvider(instance, losslessActionProvider);
polarisSDKContextManager.getLosslessAPI().losslessRegister(instance);
// return void
return null;
}
@Around("deregisterPointcut()")
public Object invokeDeregister(ProceedingJoinPoint joinPoint) throws Throwable {
if (doneDeregister.compareAndSet(false, true)) {
return joinPoint.proceed();
}
else {
return null;
}
}
public void executeJoinPoint(ProceedingJoinPoint joinPoint) {
try {
joinPoint.proceed();
}
catch (Throwable e) {
throw new RuntimeException(e);
}
}
}

@ -19,15 +19,17 @@ package com.tencent.cloud.plugin.lossless;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.function.Consumer;
import com.tencent.cloud.common.util.OkHttpUtil;
import com.tencent.cloud.plugin.lossless.config.LosslessProperties; import com.tencent.cloud.plugin.lossless.config.LosslessProperties;
import com.tencent.cloud.polaris.util.OkHttpUtil;
import com.tencent.polaris.api.plugin.lossless.InstanceProperties; import com.tencent.polaris.api.plugin.lossless.InstanceProperties;
import com.tencent.polaris.api.plugin.lossless.LosslessActionProvider; import com.tencent.polaris.api.plugin.lossless.LosslessActionProvider;
import com.tencent.polaris.api.pojo.BaseInstance;
import com.tencent.polaris.api.pojo.DefaultBaseInstance;
import com.tencent.polaris.api.utils.StringUtils; import com.tencent.polaris.api.utils.StringUtils;
import org.springframework.cloud.client.serviceregistry.Registration; import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
/** /**
@ -36,22 +38,20 @@ import org.springframework.http.HttpHeaders;
* @author Shedfree Wu * @author Shedfree Wu
*/ */
public class SpringCloudLosslessActionProvider implements LosslessActionProvider { public class SpringCloudLosslessActionProvider implements LosslessActionProvider {
private ServiceRegistry<Registration> serviceRegistry;
private LosslessProxyServiceRegistry losslessProxyServiceRegistry;
private LosslessProperties losslessProperties; private LosslessProperties losslessProperties;
private Consumer<Registration> registrationConsumer; private Runnable originalRegisterAction;
private Registration registration; private Registration registration;
public SpringCloudLosslessActionProvider( public SpringCloudLosslessActionProvider(ServiceRegistry<Registration> serviceRegistry, Registration registration,
LosslessProxyServiceRegistry losslessProxyServiceRegistry, LosslessProperties losslessProperties, Runnable originalRegisterAction) {
LosslessProperties losslessProperties) { this.serviceRegistry = serviceRegistry;
this.losslessProxyServiceRegistry = losslessProxyServiceRegistry; this.registration = registration;
this.losslessProperties = losslessProperties; this.losslessProperties = losslessProperties;
this.registrationConsumer = losslessProxyServiceRegistry.getTarget()::register; this.originalRegisterAction = originalRegisterAction;
this.registration = losslessProxyServiceRegistry.getRegistration();
} }
@Override @Override
@ -61,12 +61,13 @@ public class SpringCloudLosslessActionProvider implements LosslessActionProvider
@Override @Override
public void doRegister(InstanceProperties instanceProperties) { public void doRegister(InstanceProperties instanceProperties) {
registrationConsumer.accept(registration); // use lambda to do original register
originalRegisterAction.run();
} }
@Override @Override
public void doDeregister() { public void doDeregister() {
losslessProxyServiceRegistry.deregister(); serviceRegistry.deregister(registration);
} }
/** /**
@ -86,4 +87,18 @@ public class SpringCloudLosslessActionProvider implements LosslessActionProvider
return OkHttpUtil.checkUrl("localhost", registration.getPort(), return OkHttpUtil.checkUrl("localhost", registration.getPort(),
losslessProperties.getHealthCheckPath(), headers); losslessProperties.getHealthCheckPath(), headers);
} }
public static BaseInstance getBaseInstance(Registration registration) {
return getBaseInstance(registration, registration.getPort());
}
public static BaseInstance getBaseInstance(Registration registration, Integer port) {
// for common spring cloud registration, not set namespace
DefaultBaseInstance baseInstance = new DefaultBaseInstance();
baseInstance.setService(registration.getServiceId());
// before web start, port in registration not init
baseInstance.setPort(port);
baseInstance.setHost(registration.getHost());
return baseInstance;
}
} }

@ -18,12 +18,13 @@
package com.tencent.cloud.plugin.lossless.config; package com.tencent.cloud.plugin.lossless.config;
import com.tencent.cloud.plugin.lossless.LosslessBeanPostProcessor; import com.tencent.cloud.plugin.lossless.LosslessRegistryAspect;
import com.tencent.cloud.polaris.context.ConditionalOnPolarisEnabled;
import com.tencent.cloud.polaris.context.PolarisSDKContextManager; import com.tencent.cloud.polaris.context.PolarisSDKContextManager;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cloud.client.serviceregistry.Registration; import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
@ -34,18 +35,14 @@ import org.springframework.context.annotation.Import;
* @author Shedfree Wu * @author Shedfree Wu
*/ */
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
@ConditionalOnPolarisEnabled
@Import(LosslessPropertiesAutoConfiguration.class) @Import(LosslessPropertiesAutoConfiguration.class)
public class LosslessAutoConfiguration { public class LosslessAutoConfiguration {
@Value("${server.port:8080}")
private Integer port;
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public LosslessBeanPostProcessor losslessBeanPostProcessor(Registration registration, public LosslessRegistryAspect losslessRegistryAspect(ServiceRegistry serviceRegistry, Registration registration,
PolarisSDKContextManager polarisSDKContextManager, LosslessProperties losslessProperties, PolarisSDKContextManager polarisSDKContextManager) {
LosslessProperties losslessProperties) { return new LosslessRegistryAspect(serviceRegistry, registration, losslessProperties, polarisSDKContextManager);
return new LosslessBeanPostProcessor(polarisSDKContextManager, losslessProperties,
registration, port);
} }
} }

@ -18,6 +18,8 @@
package com.tencent.cloud.plugin.lossless.config; package com.tencent.cloud.plugin.lossless.config;
import com.tencent.cloud.polaris.context.ConditionalOnPolarisEnabled;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
@ -29,6 +31,7 @@ import org.springframework.context.annotation.Configuration;
* @author Shedfree Wu * @author Shedfree Wu
*/ */
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
@ConditionalOnPolarisEnabled
@EnableConfigurationProperties(LosslessProperties.class) @EnableConfigurationProperties(LosslessProperties.class)
public class LosslessPropertiesAutoConfiguration { public class LosslessPropertiesAutoConfiguration {

@ -17,6 +17,7 @@
package com.tencent.cloud.plugin.lossless.config; package com.tencent.cloud.plugin.lossless.config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
@ -27,6 +28,7 @@ import org.springframework.context.annotation.Import;
* @author Shedfree Wu * @author Shedfree Wu
*/ */
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
@ConditionalOnProperty("spring.cloud.polaris.enabled")
@Import(LosslessPropertiesAutoConfiguration.class) @Import(LosslessPropertiesAutoConfiguration.class)
public class LosslessPropertiesBootstrapConfiguration { public class LosslessPropertiesBootstrapConfiguration {

@ -19,6 +19,7 @@ package com.tencent.cloud.plugin.lossless;
import java.util.Collections; import java.util.Collections;
import com.tencent.cloud.common.util.OkHttpUtil;
import com.tencent.cloud.plugin.lossless.config.LosslessAutoConfiguration; import com.tencent.cloud.plugin.lossless.config.LosslessAutoConfiguration;
import com.tencent.cloud.plugin.lossless.config.LosslessPropertiesBootstrapConfiguration; import com.tencent.cloud.plugin.lossless.config.LosslessPropertiesBootstrapConfiguration;
import com.tencent.cloud.polaris.context.PolarisSDKContextManager; import com.tencent.cloud.polaris.context.PolarisSDKContextManager;
@ -27,7 +28,6 @@ import com.tencent.cloud.polaris.discovery.PolarisDiscoveryAutoConfiguration;
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryClientConfiguration; import com.tencent.cloud.polaris.discovery.PolarisDiscoveryClientConfiguration;
import com.tencent.cloud.polaris.registry.PolarisRegistration; import com.tencent.cloud.polaris.registry.PolarisRegistration;
import com.tencent.cloud.polaris.registry.PolarisServiceRegistry; import com.tencent.cloud.polaris.registry.PolarisServiceRegistry;
import com.tencent.cloud.polaris.util.OkHttpUtil;
import com.tencent.polaris.api.pojo.ServiceKey; import com.tencent.polaris.api.pojo.ServiceKey;
import com.tencent.polaris.test.mock.discovery.NamingServer; import com.tencent.polaris.test.mock.discovery.NamingServer;
import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterAll;
@ -46,11 +46,11 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode; import static org.assertj.core.api.Assertions.assertThatCode;
/** /**
* Test for {@link LosslessProxyServiceRegistry}. * Test for {@link LosslessRegistryAspect}.
* *
* @author Shedfree Wu * @author Shedfree Wu
*/ */
public class LosslessServiceRegistryTest { public class LosslessRegistryAspectTest {
private static String NAMESPACE_TEST = "Test"; private static String NAMESPACE_TEST = "Test";
@ -60,7 +60,7 @@ public class LosslessServiceRegistryTest {
private static int APPLICATION_PORT = 19091; private static int APPLICATION_PORT = 19091;
private static int LOSSLESS_PORT_1 = 28081; private static int LOSSLESS_PORT_1 = 28083;
private static NamingServer namingServer; private static NamingServer namingServer;

@ -83,7 +83,7 @@ public class PolarisSDKContextManager {
providerAPI = null; providerAPI = null;
} }
// destroy ProviderAPI // destroy LosslessAPI
if (Objects.nonNull(losslessAPI)) { if (Objects.nonNull(losslessAPI)) {
((AutoCloseable) losslessAPI).close(); ((AutoCloseable) losslessAPI).close();
losslessAPI = null; losslessAPI = null;

@ -46,4 +46,12 @@ public class PolarisContextAutoConfigurationTest {
assertThat(polarisSDKContextManager).isNotNull(); assertThat(polarisSDKContextManager).isNotNull();
}); });
} }
@Test
public void testLosslessAPIProperties() {
contextRunner.run(context -> {
PolarisSDKContextManager polarisSDKContextManager = context.getBean(PolarisSDKContextManager.class);
assertThat(polarisSDKContextManager.getLosslessAPI()).isNotNull();
});
}
} }

Loading…
Cancel
Save