spring boot deploy with javaagent

原创
2019/05/04 12:01
阅读数 3.1K

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

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
1 收藏
0
分享
返回顶部
顶部