Skip to content

Commit 8637a80

Browse files
fix:fix log framework conflict. (#204)
* feat:update example with log4j2. * fix:fix log framework conflict. * fix:fix log framework conflict. * fix:fix log framework conflict. * fix:fix log framework conflict.
1 parent 9b12412 commit 8637a80

File tree

9 files changed

+206
-127
lines changed

9 files changed

+206
-127
lines changed

polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/provider-a/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,16 @@
4040

4141
<dependencies>
4242
<!-- 简单的 Spring Cloud Web 依赖 -->
43+
<dependency>
44+
<groupId>org.apache.logging.log4j</groupId>
45+
<artifactId>log4j-core</artifactId>
46+
<version>2.17.1</version>
47+
</dependency>
48+
4349
<dependency>
4450
<groupId>org.springframework.boot</groupId>
4551
<artifactId>spring-boot-starter-log4j2</artifactId>
52+
<version>2.3.12.RELEASE</version>
4653
</dependency>
4754

4855
<dependency>

polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2022-plugin/pom.xml

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,25 @@
2323

2424
<dependencyManagement>
2525
<dependencies>
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter-logging</artifactId>
29+
<scope>provided</scope>
30+
</dependency>
31+
<dependency>
32+
<groupId>ch.qos.logback</groupId>
33+
<artifactId>logback-classic</artifactId>
34+
<scope>provided</scope>
35+
</dependency>
36+
<dependency>
37+
<groupId>ch.qos.logback</groupId>
38+
<artifactId>logback-core</artifactId>
39+
<scope>provided</scope>
40+
</dependency>
2641
<dependency>
2742
<groupId>org.springframework.boot</groupId>
2843
<artifactId>spring-boot-starter-web</artifactId>
44+
<scope>provided</scope>
2945
</dependency>
3046
<dependency>
3147
<groupId>org.springframework.boot</groupId>
@@ -61,10 +77,6 @@
6177
<artifactId>spring-cloud-plugin-common</artifactId>
6278
<version>${project.version}</version>
6379
</dependency>
64-
<dependency>
65-
<groupId>org.springframework.cloud</groupId>
66-
<artifactId>spring-cloud-starter-openfeign</artifactId>
67-
</dependency>
6880
<dependency>
6981
<groupId>com.tencent.polaris</groupId>
7082
<artifactId>polaris-agent-core-extension</artifactId>
@@ -153,6 +165,16 @@
153165
<!-- <artifactId>connector-polaris-grpc</artifactId>-->
154166
<!-- </exclusion>-->
155167

168+
<exclusion>
169+
<groupId>com.tencent.polaris</groupId>
170+
<artifactId>connector-nacos</artifactId>
171+
</exclusion>
172+
173+
<exclusion>
174+
<groupId>com.tencent.polaris</groupId>
175+
<artifactId>connector-consul</artifactId>
176+
</exclusion>
177+
156178
<exclusion>
157179
<groupId>com.tencent.polaris</groupId>
158180
<artifactId>connector-composite</artifactId>

polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2022-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/MainPlugin.java

Lines changed: 81 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717

1818
package cn.polarismesh.agent.plugin.spring.cloud;
1919

20-
import java.security.ProtectionDomain;
21-
2220
import cn.polarismesh.agent.core.extension.AgentPlugin;
2321
import cn.polarismesh.agent.core.extension.PluginContext;
2422
import cn.polarismesh.agent.core.extension.instrument.InstrumentClass;
@@ -33,91 +31,93 @@
3331
import cn.polarismesh.agent.plugin.spring.cloud.interceptor.RegisterBeanInterceptor;
3432
import cn.polarismesh.agent.plugin.spring.cloud.interceptor.SpringFactoriesLoaderInterceptor;
3533

34+
import java.security.ProtectionDomain;
35+
3636
/**
3737
* Polaris Spring Cloud hoxton Plugin
3838
*
3939
* @author shuhanliu
4040
*/
4141
public class MainPlugin implements AgentPlugin {
4242

43-
public void init(PluginContext context) {
44-
System.setProperty(Constant.AGENT_CONF_PATH, context.getAgentDirPath());
45-
TransformOperations operations = context.getTransformOperations();
46-
addPolarisTransformers(operations);
47-
}
48-
49-
/**
50-
* add polaris transformers
51-
*/
52-
private void addPolarisTransformers(TransformOperations operations) {
53-
54-
// 注入默认配置
55-
operations.transform(Constant.CONFIGURATION_CLAZZ_POST_PROCESSOR, ConfigurationPostProcessorTransform.class);
56-
57-
// 注入bootstrap的bean定义
58-
operations.transform(Constant.CONFIGURATION_CLAZZ_PARSER, ConfigurationParserTransform.class);
59-
60-
// 注入bean定义的调整设置
61-
operations.transform(Constant.BEAN_DEFINITION_REGISTRY, RegisterBeanDefinitionTransform.class);
62-
63-
// 注入JNI定义
64-
operations.transform(Constant.SPRING_FACTORIES_LOADER, SpringFactoriesLoaderTransform.class);
65-
}
66-
67-
public static class ConfigurationParserTransform implements TransformCallback {
68-
69-
@Override
70-
public byte[] doInTransform(Instrumentor instrumentor, ClassLoader classLoader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classFileBuffer) throws InstrumentException {
71-
InstrumentClass target = instrumentor.getInstrumentClass(classLoader, className, classFileBuffer);
72-
InstrumentMethod constructMethod = target.getDeclaredMethod("parse", "java.util.Set");
73-
if (constructMethod != null) {
74-
constructMethod.addInterceptor(ConfigurationParserInterceptor.class);
75-
}
76-
77-
return target.toBytecode();
78-
}
79-
}
80-
81-
public static class ConfigurationPostProcessorTransform implements TransformCallback {
82-
83-
@Override
84-
public byte[] doInTransform(Instrumentor instrumentor, ClassLoader classLoader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classFileBuffer) throws InstrumentException {
85-
InstrumentClass target = instrumentor.getInstrumentClass(classLoader, className, classFileBuffer);
86-
InstrumentMethod constructMethod = target.getDeclaredMethod("processConfigBeanDefinitions", "org.springframework.beans.factory.support.BeanDefinitionRegistry");
87-
if (constructMethod != null) {
88-
constructMethod.addInterceptor(ConfigurationPostProcessorInterceptor.class);
89-
}
90-
91-
return target.toBytecode();
92-
}
93-
}
94-
95-
public static class RegisterBeanDefinitionTransform implements TransformCallback {
96-
97-
@Override
98-
public byte[] doInTransform(Instrumentor instrumentor, ClassLoader classLoader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classFileBuffer) throws InstrumentException {
99-
InstrumentClass target = instrumentor.getInstrumentClass(classLoader, className, classFileBuffer);
100-
InstrumentMethod constructMethod = target.getDeclaredMethod("registerBeanDefinition", "java.lang.String", "org.springframework.beans.factory.config.BeanDefinition");
101-
if (constructMethod != null) {
102-
constructMethod.addInterceptor(RegisterBeanInterceptor.class);
103-
}
104-
105-
return target.toBytecode();
106-
}
107-
}
108-
109-
public static class SpringFactoriesLoaderTransform implements TransformCallback {
110-
111-
@Override
112-
public byte[] doInTransform(Instrumentor instrumentor, ClassLoader classLoader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classFileBuffer) throws InstrumentException {
113-
InstrumentClass target = instrumentor.getInstrumentClass(classLoader, className, classFileBuffer);
114-
InstrumentMethod constructMethod = target.getDeclaredMethod("loadSpringFactories", "java.lang.ClassLoader");
115-
if (constructMethod != null) {
116-
constructMethod.addInterceptor(SpringFactoriesLoaderInterceptor.class);
117-
}
118-
119-
return target.toBytecode();
120-
}
121-
}
43+
public void init(PluginContext context) {
44+
System.setProperty(Constant.AGENT_CONF_PATH, context.getAgentDirPath());
45+
TransformOperations operations = context.getTransformOperations();
46+
addPolarisTransformers(operations);
47+
}
48+
49+
/**
50+
* add polaris transformers
51+
*/
52+
private void addPolarisTransformers(TransformOperations operations) {
53+
54+
// 注入默认配置
55+
operations.transform(Constant.CONFIGURATION_CLAZZ_POST_PROCESSOR, ConfigurationPostProcessorTransform.class);
56+
57+
// 注入bootstrap的bean定义
58+
operations.transform(Constant.CONFIGURATION_CLAZZ_PARSER, ConfigurationParserTransform.class);
59+
60+
// 注入bean定义的调整设置
61+
operations.transform(Constant.BEAN_DEFINITION_REGISTRY, RegisterBeanDefinitionTransform.class);
62+
63+
// 注入JNI定义
64+
operations.transform(Constant.SPRING_FACTORIES_LOADER, SpringFactoriesLoaderTransform.class);
65+
}
66+
67+
public static class ConfigurationParserTransform implements TransformCallback {
68+
69+
@Override
70+
public byte[] doInTransform(Instrumentor instrumentor, ClassLoader classLoader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classFileBuffer) throws InstrumentException {
71+
InstrumentClass target = instrumentor.getInstrumentClass(classLoader, className, classFileBuffer);
72+
InstrumentMethod constructMethod = target.getDeclaredMethod("parse", "java.util.Set");
73+
if (constructMethod != null) {
74+
constructMethod.addInterceptor(ConfigurationParserInterceptor.class);
75+
}
76+
77+
return target.toBytecode();
78+
}
79+
}
80+
81+
public static class ConfigurationPostProcessorTransform implements TransformCallback {
82+
83+
@Override
84+
public byte[] doInTransform(Instrumentor instrumentor, ClassLoader classLoader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classFileBuffer) throws InstrumentException {
85+
InstrumentClass target = instrumentor.getInstrumentClass(classLoader, className, classFileBuffer);
86+
InstrumentMethod constructMethod = target.getDeclaredMethod("processConfigBeanDefinitions", "org.springframework.beans.factory.support.BeanDefinitionRegistry");
87+
if (constructMethod != null) {
88+
constructMethod.addInterceptor(ConfigurationPostProcessorInterceptor.class);
89+
}
90+
91+
return target.toBytecode();
92+
}
93+
}
94+
95+
public static class RegisterBeanDefinitionTransform implements TransformCallback {
96+
97+
@Override
98+
public byte[] doInTransform(Instrumentor instrumentor, ClassLoader classLoader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classFileBuffer) throws InstrumentException {
99+
InstrumentClass target = instrumentor.getInstrumentClass(classLoader, className, classFileBuffer);
100+
InstrumentMethod constructMethod = target.getDeclaredMethod("registerBeanDefinition", "java.lang.String", "org.springframework.beans.factory.config.BeanDefinition");
101+
if (constructMethod != null) {
102+
constructMethod.addInterceptor(RegisterBeanInterceptor.class);
103+
}
104+
105+
return target.toBytecode();
106+
}
107+
}
108+
109+
public static class SpringFactoriesLoaderTransform implements TransformCallback {
110+
111+
@Override
112+
public byte[] doInTransform(Instrumentor instrumentor, ClassLoader classLoader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classFileBuffer) throws InstrumentException {
113+
InstrumentClass target = instrumentor.getInstrumentClass(classLoader, className, classFileBuffer);
114+
InstrumentMethod constructMethod = target.getConstructor("java.lang.ClassLoader", "java.util.Map");
115+
if (constructMethod != null) {
116+
constructMethod.addInterceptor(SpringFactoriesLoaderInterceptor.class);
117+
}
118+
119+
return target.toBytecode();
120+
}
121+
}
122122

123123
}

polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2022-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/SpringFactoriesLoaderInterceptor.java

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,16 @@
1717

1818
package cn.polarismesh.agent.plugin.spring.cloud.interceptor;
1919

20+
import cn.polarismesh.agent.core.common.utils.ReflectionUtils;
2021
import cn.polarismesh.agent.core.extension.interceptor.Interceptor;
2122
import cn.polarismesh.agent.plugin.spring.cloud.common.BeanInjector;
2223
import cn.polarismesh.agent.plugin.spring.cloud.inject.*;
24+
import com.tencent.polaris.api.utils.CollectionUtils;
2325
import org.slf4j.Logger;
2426
import org.slf4j.LoggerFactory;
2527

26-
import java.util.ArrayList;
27-
import java.util.List;
28-
import java.util.Map;
28+
import java.util.*;
2929
import java.util.concurrent.ConcurrentHashMap;
30-
import java.util.function.Function;
3130

3231
public class SpringFactoriesLoaderInterceptor implements Interceptor {
3332

@@ -55,38 +54,36 @@ public SpringFactoriesLoaderInterceptor() {
5554
@SuppressWarnings("unchecked")
5655
@Override
5756
public void after(Object target, Object[] args, Object result, Throwable throwable) {
58-
if (args[0] == null) {
59-
return;
57+
Map<String, List<String>> oldFactories = (Map<String, List<String>>) ReflectionUtils.getObjectByFieldName(target, "factories");
58+
if (CollectionUtils.isEmpty(oldFactories)) {
59+
oldFactories = new HashMap<>();
60+
}
61+
Map<String, List<String>> newFactories = new HashMap<>();
62+
for (Map.Entry<String, List<String>> entry : oldFactories.entrySet()) {
63+
newFactories.put(entry.getKey(), new ArrayList<>(entry.getValue()));
6064
}
61-
ClassLoader classLoader = (ClassLoader) args[0];
62-
parsedClasses.computeIfAbsent(classLoader, new Function<ClassLoader, Boolean>() {
63-
@Override
64-
public Boolean apply(ClassLoader classLoader) {
65-
Map<String, List<String>> loadedClasses = (Map<String, List<String>>) result;
6665

67-
for (BeanInjector beanInjector : beanInjectors) {
68-
LOGGER.info("[PolarisJavaAgent] start to inject JNI definition in module {}", beanInjector.getModule());
69-
Map<String, List<String>> classNames = beanInjector.getClassNameForType();
70-
if (classNames.isEmpty()) {
71-
continue;
72-
}
73-
for (Map.Entry<String, List<String>> entry : classNames.entrySet()) {
74-
List<String> existsValues = loadedClasses.get(entry.getKey());
75-
List<String> toAddValues = entry.getValue();
76-
if (null != existsValues) {
77-
for (String toAddValue : toAddValues) {
78-
if (existsValues.contains(toAddValue)) {
79-
continue;
80-
}
81-
existsValues.add(toAddValue);
82-
}
83-
} else {
84-
classNames.put(entry.getKey(), toAddValues);
66+
for (BeanInjector beanInjector : beanInjectors) {
67+
LOGGER.info("[PolarisJavaAgent] start to inject JNI definition in module {}", beanInjector.getModule());
68+
Map<String, List<String>> classNames = beanInjector.getClassNameForType();
69+
if (CollectionUtils.isEmpty(classNames)) {
70+
continue;
71+
}
72+
for (Map.Entry<String, List<String>> entry : classNames.entrySet()) {
73+
List<String> existsValues = newFactories.get(entry.getKey());
74+
List<String> toAddValues = entry.getValue();
75+
if (null != existsValues) {
76+
for (String toAddValue : toAddValues) {
77+
if (existsValues.contains(toAddValue)) {
78+
continue;
8579
}
80+
existsValues.add(toAddValue);
8681
}
82+
} else {
83+
classNames.put(entry.getKey(), toAddValues);
8784
}
88-
return true;
8985
}
90-
});
86+
}
87+
ReflectionUtils.setValueByFieldName(target, "factories", Collections.unmodifiableMap(newFactories));
9188
}
9289
}

polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/pom.xml

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,26 @@
2323

2424
<dependencyManagement>
2525
<dependencies>
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter-logging</artifactId>
29+
<version>${spring.boot.version}</version>
30+
<scope>provided</scope>
31+
</dependency>
32+
<dependency>
33+
<groupId>ch.qos.logback</groupId>
34+
<artifactId>logback-classic</artifactId>
35+
<scope>provided</scope>
36+
</dependency>
37+
<dependency>
38+
<groupId>ch.qos.logback</groupId>
39+
<artifactId>logback-core</artifactId>
40+
<scope>provided</scope>
41+
</dependency>
2642
<dependency>
2743
<groupId>org.springframework.boot</groupId>
2844
<artifactId>spring-boot-starter-web</artifactId>
45+
<scope>provided</scope>
2946
</dependency>
3047
<dependency>
3148
<groupId>org.springframework.boot</groupId>
@@ -61,10 +78,6 @@
6178
<artifactId>spring-cloud-plugin-common</artifactId>
6279
<version>${project.version}</version>
6380
</dependency>
64-
<dependency>
65-
<groupId>org.springframework.cloud</groupId>
66-
<artifactId>spring-cloud-starter-openfeign</artifactId>
67-
</dependency>
6881
<dependency>
6982
<groupId>com.tencent.polaris</groupId>
7083
<artifactId>polaris-agent-core-extension</artifactId>
@@ -153,6 +166,16 @@
153166
<!-- <artifactId>connector-polaris-grpc</artifactId>-->
154167
<!-- </exclusion>-->
155168

169+
<exclusion>
170+
<groupId>com.tencent.polaris</groupId>
171+
<artifactId>connector-nacos</artifactId>
172+
</exclusion>
173+
174+
<exclusion>
175+
<groupId>com.tencent.polaris</groupId>
176+
<artifactId>connector-consul</artifactId>
177+
</exclusion>
178+
156179
<exclusion>
157180
<groupId>com.tencent.polaris</groupId>
158181
<artifactId>connector-composite</artifactId>

0 commit comments

Comments
 (0)