Skip to content

Commit

Permalink
feat: 优化日志打印,增加polarisLoggingApplicationListener的配置 (#177)
Browse files Browse the repository at this point in the history
* fix: optimize the discovery inject logic and fix nacos config not working issue

* feat: 删除无用的导入

* feat: 增加actuator到demo中

* fix: add lossless healthcheck for example

* fix: add configuration comments

* fix: 修复事件日志不输出的问题

* release: release version to 1.7.0-RC1

* feat: new add java agent for hoxton

* feat: support service registry and config

* feat: refrator all inject logics into BeanInjector

* feat: add gray release example & dispatch bean register into plugins

* feat: add environment parameter to obtain the configuration

* feat: 增加元数据传输相关拦截器

* feat: 调整example打包脚本

* fix: 修复脚本没法跳转的问题

* fix: 修正样例的配置文件

* fix: 修复熔断无法加载的问题

* fix: 更新北极星的版本号依赖

* fix: 调整版本依赖

* feat:对齐deployment与application中的服务名定义

* feat: optimize default-plugin.conf to load once per lifecyle

* feat: 优化日志打印,增加polarisLoggingApplicationListener的配置
  • Loading branch information
andrewshan authored May 13, 2024
1 parent 7390b61 commit 3cfdeb1
Show file tree
Hide file tree
Showing 23 changed files with 285 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static <T> Class<T> getClazz(String clazzName, ClassLoader classLoader) {
try {
return (Class<T>) Class.forName(clazzName, false, classLoader);
} catch (ClassNotFoundException e) {
logger.warn(String.format("class %s not found, error %s", clazzName, e.getLocalizedMessage()));
logger.info(String.format("class %s not found", clazzName));
}
return null;
}
Expand Down
3 changes: 2 additions & 1 deletion polaris-agent-plugins/spring-cloud-plugins/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
<module>spring-cloud-2020-plugin</module>
<module>spring-cloud-hoxton-plugin</module>
<module>spring-cloud-2023-plugin</module>
</modules>
<module>spring-cloud-plugin-common</module>
</modules>

<properties>
<jdk.version>1.8</jdk.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<properties>
<spring.cloud.version>Hoxton.SR12</spring.cloud.version>
<spring.cloud.tencent.version>1.14.0-Hoxton.SR12-SNAPSHOT</spring.cloud.tencent.version>
<spring.boot.version>2.3.7.RELEASE</spring.boot.version>
<spring.boot.version>2.3.12.RELEASE</spring.boot.version>
<slf4j.vesion>1.7.30</slf4j.vesion>
</properties>

Expand Down Expand Up @@ -61,6 +61,11 @@
</dependencyManagement>

<dependencies>
<dependency>
<groupId>com.tencent.polaris</groupId>
<artifactId>spring-cloud-plugin-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import cn.polarismesh.agent.plugin.spring.cloud.interceptor.ConfigurationParserInterceptor;
import cn.polarismesh.agent.plugin.spring.cloud.interceptor.ConfigurationPostProcessorInterceptor;
import cn.polarismesh.agent.plugin.spring.cloud.interceptor.RegisterBeanInterceptor;
import cn.polarismesh.agent.plugin.spring.cloud.interceptor.SpringFactoriesLoaderInterceptor;

/**
* Polaris Spring Cloud hoxton Plugin
Expand All @@ -57,6 +58,9 @@ private void addPolarisTransformers(TransformOperations operations) {

// 注入bean定义的调整设置
operations.transform(Constant.BEAN_DEFINITION_REGISTRY, RegisterBeanDefinitionTransform.class);

// 注入JNI定义
operations.transform(Constant.SPRING_FACTORIES_LOADER, SpringFactoriesLoaderTransform.class);
}

public static class ConfigurationParserTransform implements TransformCallback {
Expand Down Expand Up @@ -100,4 +104,19 @@ public byte[] doInTransform(Instrumentor instrumentor, ClassLoader classLoader,
return target.toBytecode();
}
}

public static class SpringFactoriesLoaderTransform implements TransformCallback {

@Override
public byte[] doInTransform(Instrumentor instrumentor, ClassLoader classLoader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classFileBuffer) throws InstrumentException {
InstrumentClass target = instrumentor.getInstrumentClass(classLoader, className, classFileBuffer);
InstrumentMethod constructMethod = target.getDeclaredMethod("loadSpringFactories", "java.lang.ClassLoader");
if (constructMethod != null) {
constructMethod.addInterceptor(SpringFactoriesLoaderInterceptor.class);
}

return target.toBytecode();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
import org.springframework.core.env.Environment;

public class CircuitBreakerBeanInjector implements BeanInjector {
@Override
public String getModule() {
return "spring-cloud-starter-tencent-polaris-circuitbreaker";
}

@Override
public void onBootstrapStartup(Object configurationParser, Constructor<?> configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) {
Object polarisCircuitBreakerBootstrapConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisCircuitBreakerBootstrapConfiguration.class, "polarisCircuitBreakerBootstrapConfiguration");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
import org.springframework.core.env.Environment;

public class CommonBeanInjector implements BeanInjector {
@Override
public String getModule() {
return "spring-cloud-tencent-commons";
}

@Override
public void onBootstrapStartup(Object configurationParser, Constructor<?> configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
import org.springframework.core.env.Environment;

public class ConfigBeanInjector implements BeanInjector {
@Override
public String getModule() {
return "spring-cloud-starter-tencent-polaris-config";
}

@Override
public void onBootstrapStartup(Object configurationParser, Constructor<?> configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) {
Object polarisConfigBootstrapAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisConfigBootstrapAutoConfiguration.class, "polarisConfigBootstrapAutoConfiguration");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
import org.springframework.core.env.Environment;

public class LoadbalancerBeanInjector implements BeanInjector {
@Override
public String getModule() {
return "spring-cloud-tencent-polaris-loadbalancer";
}

@Override
public void onBootstrapStartup(Object configurationParser, Constructor<?> configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
import org.springframework.core.env.Environment;

public class LosslessBeanInjector implements BeanInjector {
@Override
public String getModule() {
return "spring-cloud-tencent-lossless-plugin";
}

@Override
public void onBootstrapStartup(Object configurationParser, Constructor<?> configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) {
Object losslessPropertiesBootstrapConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, LosslessPropertiesBootstrapConfiguration.class, "losslessPropertiesBootstrapConfiguration");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@

public class MetadataTransferBeanInjector implements BeanInjector {

@Override
public String getModule() {
return "spring-cloud-starter-tencent-metadata-transfer";
}

@Override
public void onBootstrapStartup(Object configurationParser, Constructor<?> configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.polarismesh.agent.core.common.utils.ReflectionUtils;
import cn.polarismesh.agent.plugin.spring.cloud.common.BeanInjector;
Expand All @@ -33,6 +37,18 @@
import org.springframework.core.env.Environment;

public class PolarisContextBeanInjector implements BeanInjector {
@Override
public String getModule() {
return "spring-cloud-tencent-polaris-context";
}

@Override
public Map<String, List<String>> getClassNameForType() {
Map<String, List<String>> values = new HashMap<>();
values.put("org.springframework.context.ApplicationListener", Collections.singletonList("com.tencent.cloud.polaris.context.logging.PolarisLoggingApplicationListener"));
return values;
}

@Override
public void onBootstrapStartup(Object configurationParser,
Constructor<?> configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) {
Expand All @@ -55,13 +71,5 @@ public void onApplicationStartup(Object configurationParser,
ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisContextPostConfiguration, Constant.DEFAULT_EXCLUSION_FILTER);
registry.registerBeanDefinition("polarisContextPostConfiguration", BeanDefinitionBuilder.genericBeanDefinition(
PolarisContextPostConfiguration.class).getBeanDefinition());
Object polarisLoggingApplicationListener = ReflectionUtils.invokeConstructor(configClassCreator, PolarisLoggingApplicationListener.class, "polarisLoggingApplicationListener");
ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisLoggingApplicationListener, Constant.DEFAULT_EXCLUSION_FILTER);
registry.registerBeanDefinition("polarisLoggingApplicationListener", BeanDefinitionBuilder.genericBeanDefinition(
PolarisLoggingApplicationListener.class).getBeanDefinition());
// Object polarisSDKContextManager = ReflectionUtils.invokeConstructor(configClassCreator, PolarisSDKContextManager.class, "polarisSDKContextManager");
// ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisSDKContextManager, Constant.DEFAULT_EXCLUSION_FILTER);
// registry.registerBeanDefinition("polarisSDKContextManager", BeanDefinitionBuilder.genericBeanDefinition(
// PolarisSDKContextManager.class).getBeanDefinition());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
import org.springframework.core.env.Environment;

public class RateLimitBeanInjector implements BeanInjector {
@Override
public String getModule() {
return "spring-cloud-starter-tencent-polaris-ratelimit";
}

@Override
public void onBootstrapStartup(Object configurationParser, Constructor<?> configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) {
Object polarisRateLimitPropertiesBootstrapConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisRateLimitPropertiesBootstrapConfiguration.class, "polarisRateLimitPropertiesBootstrapConfiguration");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
import org.springframework.core.env.Environment;

public class RegistryBeanInjector implements BeanInjector {
@Override
public String getModule() {
return "spring-cloud-starter-tencent-polaris-discovery";
}

@Override
public void onBootstrapStartup(Object configurationParser, Constructor<?> configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) {
Object discoveryPropertiesBootstrapAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, DiscoveryPropertiesBootstrapAutoConfiguration.class, "discoveryPropertiesBootstrapAutoConfiguration");
Expand Down Expand Up @@ -65,9 +70,5 @@ public void onApplicationStartup(Object configurationParser, Constructor<?> conf
ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisDiscoveryEndpointAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER);
registry.registerBeanDefinition("polarisDiscoveryEndpointAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition(
PolarisDiscoveryEndpointAutoConfiguration.class).getBeanDefinition());
// Object polarisFeignBeanPostProcessor = ReflectionUtils.invokeConstructor(configClassCreator, PolarisFeignBeanPostProcessor.class, "polarisFeignBeanPostProcessor");
// ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisFeignBeanPostProcessor, Constant.DEFAULT_EXCLUSION_FILTER);
// registry.registerBeanDefinition("polarisFeignBeanPostProcessor", BeanDefinitionBuilder.genericBeanDefinition(
// PolarisFeignBeanPostProcessor.class).getBeanDefinition());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
import org.springframework.core.io.ClassPathResource;

public class RouterBeanInjector implements BeanInjector {
@Override
public String getModule() {
return "spring-cloud-starter-tencent-polaris-router";
}

@Override
public void onBootstrapStartup(Object configurationParser, Constructor<?> configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
import org.springframework.core.env.Environment;

public class RpcEnhancementBeanInjector implements BeanInjector {
@Override
public String getModule() {
return "spring-cloud-tencent-rpc-enhancement";
}

@Override
public void onBootstrapStartup(Object configurationParser, Constructor<?> configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) {
Object polarisStatPropertiesBootstrapConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisStatPropertiesBootstrapConfiguration.class, "polarisStatPropertiesBootstrapConfiguration");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
import cn.polarismesh.agent.plugin.spring.cloud.inject.RegistryBeanInjector;
import cn.polarismesh.agent.plugin.spring.cloud.inject.RouterBeanInjector;
import cn.polarismesh.agent.plugin.spring.cloud.inject.RpcEnhancementBeanInjector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinition;
Expand All @@ -49,6 +51,8 @@

public class ConfigurationParserInterceptor implements Interceptor {

private static final Logger LOGGER = LoggerFactory.getLogger(ConfigurationParserInterceptor.class);

private final List<BeanInjector> beanInjectors = new ArrayList<>();

public ConfigurationParserInterceptor() {
Expand Down Expand Up @@ -99,8 +103,10 @@ public void after(Object target, Object[] args, Object result, Throwable throwab
BeanDefinitionRegistry registry = (BeanDefinitionRegistry) ReflectionUtils.getObjectByFieldName(target, "registry");
Environment environment = (Environment) ReflectionUtils.getObjectByFieldName(target, "environment");
for (BeanInjector beanInjector : beanInjectors) {
LOGGER.info("[PolarisJavaAgent] start to inject bootstrap bean definitions in module {}", beanInjector.getModule());
beanInjector.onBootstrapStartup(target, constructor, processConfigurationClass, registry, environment);
}
LOGGER.info("[PolarisJavaAgent] successfully injected spring cloud tencent bootstrap bean definitions");

} else if (isMainBeanDefinition(beanDefinitionHolder)) {
Class<?> clazz = ClassUtils.getClazz("org.springframework.context.annotation.ConfigurationClass", null);
Expand All @@ -111,8 +117,10 @@ public void after(Object target, Object[] args, Object result, Throwable throwab
BeanDefinitionRegistry registry = (BeanDefinitionRegistry) ReflectionUtils.getObjectByFieldName(target, "registry");
Environment environment = (Environment) ReflectionUtils.getObjectByFieldName(target, "environment");
for (BeanInjector beanInjector : beanInjectors) {
LOGGER.info("[PolarisJavaAgent] start to inject application bean definitions in module {}", beanInjector.getModule());
beanInjector.onApplicationStartup(target, constructor, processConfigurationClass, registry, environment);
}
LOGGER.info("[PolarisJavaAgent] successfully injected spring cloud tencent application bean definitions");

}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ public void before(Object target, Object[] args) {
return;
}
for (PropertiesPropertySource propertiesPropertySource : propertySources) {
LOGGER.info("[PolarisJavaAgent] start to add propertiesPropertySource {}", propertiesPropertySource.getName());
environment.getPropertySources().addFirst(propertiesPropertySource);
}
LOGGER.info("[PolarisJavaAgent] successfully injected agent properties into environment, size is " + propertySources.size());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,6 @@ public void before(Object target, Object[] args) {
}
BeanDefinition beanDefinition = (BeanDefinition) args[1];
beanDefinition.setPrimary(true);
LOGGER.info("[PolarisJavaAgent] bean {} has been made primary", beanName);
}
}
Loading

0 comments on commit 3cfdeb1

Please sign in to comment.