• IDEA使用docker进行调试


    背景

    手头有个任务,需要用java通过jni调用一个开源算法库gmssl的功能,但是gmssl只提供了源码,需要编译后才能使用。按照通常的做法,我们会部署好centos的虚拟机和开发环境,安装好gmssl的依赖环境,然后再基于这个部署好的开发环境进行开发和调试。

    这样的做法,会在开发和部署过程中会出现一些问题:

    • 对虚拟机做的改动,没有效记录
    • 虚拟机的体积过大,保存麻烦
    • 在生成环境中,需要重新编译部署,比较麻烦

    为了解决上面提到的问题,通过引入docker,并支持快速调试。主要思路如下:

    • 使用Dockerfile部署开发环境
    • 在进程的启动脚本中,加入远程调试的指令
    • IDEA配置远程调试的环境
    • IDEA在执行远程调试前,通过maven对Dockerfile生成docker镜像并进行部署
    • docker镜像部署完成后,IDEA能够连接到镜像中,进行调试

    操作步骤

    Dockerfile内容

    #build docker build -t 10.10.8.166:5000/gmssl .
    
    FROM 10.10.8.166:5000/centos-java:latest
    
    RUN yum -y update
    RUN yum install -y unzip
    RUN yum install -y gcc
    RUN yum install -y openssl-devel
    RUN yum install -y perl
    
    RUN wget https://codeload.github.com/guanzhi/GmSSL/zip/2.0
    RUN unzip 2.0
    RUN rm -rf 2.0
    
    WORKDIR "/GmSSL-2.0"
    RUN ./config
    RUN make && make install
    
    ADD maven/test-1.0-SNAPSHOT.jar /opt/test.jar
    ADD maven/startup.sh /opt/startup.sh
    RUN chmod +x /opt/startup.sh
    
    WORKDIR "/opt"
    EXPOSE "5005"
    ENTRYPOINT ["/opt/startup.sh"]    
    

    Pom配置

    使用docker-maven-plugin,对docker文件进行打包、发布处理

    <plugin>
        <groupId>io.fabric8</groupId>
        <artifactId>docker-maven-plugin</artifactId>
        <version>0.15.16</version>
        <executions>
            <execution>
                <id>package</id>
                <phase>package</phase>
                <goals>
                    <goal>build</goal>
                </goals>
            </execution>
            <execution>
                <id>deploy</id>
                <phase>deploy</phase>
                <goals>
                    <goal>build</goal>
                    <goal>push</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <verbose>true</verbose>
            <machine>
                <name>default</name>
                <autoCreate>true</autoCreate>
                <createOptions>
                    <driver>virtualbox</driver>
                </createOptions>
            </machine>
            <images>
                <image>
                    <!-- Artifact Image-->
                    <name>10.10.8.166:5000/gmssl:${project.version}</name>
                    <run>
                        <ports>
                            <port>5005:5005</port>
                        </ports>
                    </run>
                    <build>
                        <dockerFileDir>${project.basedir}/docker</dockerFileDir>
                        <assembly>
                            <mode>dir</mode>
                            <inline>
                                <id>gmssl-it</id>
                                <fileSets>
                                    <fileSet>
                                        <includes>
                                            <include>*.jar</include>
                                        </includes>
                                        <directory>${project.build.directory}</directory>
                                        <outputDirectory>/</outputDirectory>
                                    </fileSet>
                                    <fileSet>
                                        <includes>
                                            <include>*.sh</include>
                                        </includes>
                                        <directory>${project.build.directory}/classes</directory>
                                        <outputDirectory>/</outputDirectory>
                                    </fileSet>
                                </fileSets>
                            </inline>
                        </assembly>
                    </build>
                </image>
            </images>
        </configuration>
    </plugin>
    

    远程调试配置

    为了保证镜像保持最新,需要将before launch设置为:

    • mvn package
    • mvn docker:stop
    • mvn docker:start

    执行mvn docker:stop的目的是为了能够在启动前,将之前运行的镜像删除。

    远程调试配置.png

    startup脚本配置

    在startup脚本中,加入远程调试的脚本

    #!/bin/bash
    java -agentlib:jdwp=transport=dt_socket,address=5005,suspend=y,server=y -cp /opt/*:/opt Main
    

    改进

    • 远程调试的配置修改为mvn package docker:stop docker:start进行加速
    • 远程调试结束后,停止运行镜像
  • 相关阅读:
    Topics
    telnetlib-telnet客户端操作
    logging-日志信息管理
    B.2 工具spy++
    B.1 XPath 获取技巧
    pyinstaller-将Python程序打包成一个独立可执行软件包
    探讨HTTP中敏感数据的安全性传输方案
    shell->一个经典的shell脚本结构
    c->再次封装已有函数的快速方法
    c->推荐的更安全的代码写法
  • 原文地址:https://www.cnblogs.com/xiaohunshi/p/5892067.html
Copyright © 2020-2023  润新知