spring boot deploy with javaagent

博客围绕Spring Boot中agent的部署展开。Spring Boot通常以fatjar方式启动,本地启动和生产环境部署有所不同。针对生产环境绝对路径变化问题,给出两种方案,一是将agent和fatjar放同一目录,二是将agent打包至fatJar中,还提及相关参考内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

spring boot 通常会打成一个fatjar的方式启动,如果我们本地启动(增加agent)则是这样子:

java -javaagent:/path/myAgent.jar -jar myFat.jar

但是部署到生产环境,绝对路径是会变的,所以两种方案:

和fatjar放在同一目录下,/target

<plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.demo.Application</mainClass>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>com.demo</groupId>
                                    <artifactId>myAgent</artifactId>
                                    <version>x.x.x</version>
                                    <type>jar</type>
                                    <overWrite>false</overWrite>
                                    <outputDirectory>${project.build.directory}</outputDirectory>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
  1. 指定spring boot maven plugin的mainClass属性
  2. 使用maven copy 插件,将 myAgent.jar 复制到target目录
  3. docker脚本
COPY ./ztarget/springBootApp.jar /path/
COPY ./target/myAgent.jar /path/
  1. 服务器上可执行脚本 java -javaagent:myAgent.jar -jar myFat.jar

将agent打包至fatJar中

参考: Including Java Agent in Standalone Spring Boot Application

效果:java -javaagent:myFat.jar -jar myFat.jar

转载于:https://my.oschina.net/tigerlene/blog/3045085

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值