我們通過bytebuddy開發javaagent最終要部署到springboot項目中,javaagent引入了slf4j-api包,但是沒有slf4j實現類,所有報錯“failed to load class”。
這時我們有兩種解決思路:
1.在javaagent中實現slf4j。
2.在javaagent 中排除slf4j,具體實現留給我們監控的springboot項目。
毫不猶豫選擇第二種,因爲springboot一般都有slf4j的實現,如果在javaagent中也實現的話,就會產生新的錯誤,重複引用,報衝突。
第二種實現方式:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<excludes>
<exclude>junit:junit</exclude>
<exclude>org.slf4j:slf4j-api:jar:*</exclude>
</excludes>
</artifactSet>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Premain-Class>com.hupu.agent.Agent</Premain-Class>
</manifestEntries>
</transformer>
</transformers>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
總結:輕視了maven的功能,在maven打包的時候看到一大堆的日誌輸出,重來沒有認真關注過。心理上抗拒這些繁雜的日誌,導致需要花費更多的時間才能找到解決jar衝突的方法。其實日誌是軟件開發者有選擇的打印的,是查找問題,解決問題的重要線索。
找到問題的根源才能解決問題,網上找到的答案也需要自己分析才能真正解決問題。