文章目录
- 一、概述
- 二、常规做法
- 1. 数据库配置分离
- 2. 代码引用配置
- 3. 编写启动类
- 4. 支持打包成可执行包
- 5. 支持可执行包打包成docker镜像
- 6. docker运行
- 三、存在问题分析
- 四、改进措施
- 1. 包含environment 变量的编排文件
- 2. 修改读取配置文件方式
- 3. 为什么可以这样做
- 五、运行效果
- 1. 本地windows运行
- 2. centos 服务器jar运行
- 3. centos 服务器docker运行
- 六、完整代码
一、概述
普通Java工程这里指非spring工程,主要指一些系统资源监控程序、数据日志清理程序
等,需要保持依赖简单,执行文件体积微小
方便部署,这些程序需要需要跟数据库交互,例如需要将监控数据持久化到db、连接数据库清理过期数据
等。
二、常规做法
1. 数据库配置分离
普通Java工程配置文件一般采用properties
,典型的配置文件如下:
jdbc.properties
jdbc.driver = com.mysql.cj.jdbc.Driver
jdbc.url = jdbc:mysql://127.0.0.1:3306/jdbc?useSSL=false&useUnicode=true&characterEncoding=utf-8&createDatabaseIfNotExist=true
jdbc.username = root
jdbc.password = root123
2. 代码引用配置
ResourceBundle config = ResourceBundle.getBundle("jdbc");PoolProperties p = new PoolProperties();p.setUrl(config.getString("jdbc.url"));p.setDriverClassName(config.getString("jdbc.driver"));p.setUsername(config.getString("jdbc.username"));p.setPassword(config.getString("jdbc.password"));
3. 编写启动类
启动类main方法,一般使用定时任务线程池,以保证程序一直运行
核心代码:
ScheduledExecutorService service = new ScheduledThreadPoolExecutor(1);service.scheduleAtFixedRate(() -> { doSomething(); // 业务方法}, 0, 60, TimeUnit.SECONDS);
4. 支持打包成可执行包
具体参考 普通Java工程可执行JAR两种打包方式探讨
5. 支持可执行包打包成docker镜像
具体参考: 容器镜像生成记
6. docker运行
在编排文件目录执行 docker-compose up -d
docker-compose.yml
version: '3.7'
services:java-with-docker:image: registry.cn-shanghai.aliyuncs.com/00fly/java-with-docker:0.0.1container_name: java-with-dockerdeploy:resources:limits:cpus: '1.0'memory: 64Mreservations:cpus: '0.05'memory: 64Mrestart: on-failurelogging:driver: json-fileoptions:max-size: '5m'max-file: '1'
三、存在问题分析
上面的做法存在最大的问题是:
- 数据库配置固定在内部配置文件jdbc.properties中
- 维护麻烦,如果是通用的需要,每换一套环境,需要重新跑流程,包括打包可执行包和镜像打包。
四、改进措施
在springboot
工程中,我们支持数据源配置在docker-compose.yml
中的environment
配置
典型例子如下:
version: '3.8'
services:springboot-dbutils:image: 00fly/springboot-dbutils:0.0.1container_name: springboot-dbutilsdeploy:resources:limits:cpus: '0.80'memory: 400Mreservations:cpus: '0.05'memory: 200Mports:- 80:8080environment:- SPRING_DATASOURCE_URL=jdbc:mysql://127.0.0.1:3306/dbutils?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&autoReconnect=true- SPRING_DATASOURCE_USERNAME=user- SPRING_DATASOURCE_PASSWORD=password123restart: on-failurelogging:driver: json-fileoptions:max-size: 5mmax-file: '1'
同理,我们也希望普通java也支持如下编排文件中的environment 变量
1. 包含environment 变量的编排文件
docker-compose.yml
version: '3.7'
services:java-with-docker:image: registry.cn-shanghai.aliyuncs.com/00fly/java-with-docker:0.0.1container_name: java-with-dockerdeploy:resources:limits:cpus: '1.0'memory: 64Mreservations:cpus: '0.05'memory: 64Mrestart: on-failureenvironment:- JDBC_URL=jdbc:mysql://192.168.15.202:3306/jdbc?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&autoReconnect=true- JDBC_USERNAME=test- JDBC_PASSWORD=test123- JDBC_DRIVER=com.mysql.cj.jdbc.Driverlogging:driver: json-fileoptions:max-size: '5m'max-file: '1'
2. 修改读取配置文件方式
将原来读取jdbc.properties的配置文件的方式修改为如下:
//docker环境下优先使用docker-compose中environment值Map<String, String> env = System.getenv();ResourceBundle config = ResourceBundle.getBundle("jdbc");String url = StringUtils.defaultIfBlank(env.get("JDBC_URL"), config.getString("jdbc.url"));String userName = StringUtils.defaultIfBlank(env.get("JDBC_USERNAME"), config.getString("jdbc.username"));String passWord = StringUtils.defaultIfBlank(env.get("JDBC_PASSWORD"), config.getString("jdbc.password"));dataSource.setUrl(url);dataSource.setUser(userName);dataSource.setPassword(passWord);
3. 为什么可以这样做
其实,这说明了:在docker-compose.yml配置的environment 值,均会被读取到System.getenv() 中
五、运行效果
1. 本地windows运行
2024-07-06 15:34:16.345 [main] INFO com.fly.simple.MainRun - ------------------------------
2024-07-06 15:34:16.421 [main] INFO com.fly.simple.MainRun - =:: -> ::\
2024-07-06 15:34:16.421 [main] INFO com.fly.simple.MainRun - =D: -> D:\
2024-07-06 15:34:16.421 [main] INFO com.fly.simple.MainRun - ALLUSERSPROFILE -> C:\ProgramData
2024-07-06 15:34:16.422 [main] INFO com.fly.simple.MainRun - APPDATA -> C:\Users\00fly\AppData\Roaming
2024-07-06 15:34:16.422 [main] INFO com.fly.simple.MainRun - COMPUTERNAME -> 7t9lppye5cj7lud
2024-07-06 15:34:16.423 [main] INFO com.fly.simple.MainRun - ChocolateyInstall -> C:\ProgramData\chocolatey
2024-07-06 15:34:16.424 [main] INFO com.fly.simple.MainRun - ChocolateyLastPathUpdate -> 133608643383155797
2024-07-06 15:34:16.424 [main] INFO com.fly.simple.MainRun - ComSpec -> C:\Windows\system32\cmd.exe
2024-07-06 15:34:16.424 [main] INFO com.fly.simple.MainRun - CommonProgramFiles -> C:\Program Files\Common Files
2024-07-06 15:34:16.425 [main] INFO com.fly.simple.MainRun - CommonProgramFiles(x86) -> C:\Program Files (x86)\Common Files
2024-07-06 15:34:16.425 [main] INFO com.fly.simple.MainRun - CommonProgramW6432 -> C:\Program Files\Common Files
2024-07-06 15:34:16.425 [main] INFO com.fly.simple.MainRun - DokanLibrary1 -> C:\Program Files\Dokan\Dokan Library-1.4.1\
2024-07-06 15:34:16.426 [main] INFO com.fly.simple.MainRun - DokanLibrary1_LibraryPath_x64 -> C:\Program Files\Dokan\Dokan Library-1.4.1\lib\
2024-07-06 15:34:16.426 [main] INFO com.fly.simple.MainRun - DokanLibrary1_LibraryPath_x86 -> C:\Program Files\Dokan\Dokan Library-1.4.1\x86\lib\
2024-07-06 15:34:16.426 [main] INFO com.fly.simple.MainRun - DriverData -> C:\Windows\System32\Drivers\DriverData
2024-07-06 15:34:16.427 [main] INFO com.fly.simple.MainRun - FPS_BROWSER_APP_PROFILE_STRING -> Internet Explorer
2024-07-06 15:34:16.427 [main] INFO com.fly.simple.MainRun - FPS_BROWSER_USER_PROFILE_STRING -> Default
2024-07-06 15:34:16.427 [main] INFO com.fly.simple.MainRun - HOMEDRIVE -> C:
2024-07-06 15:34:16.428 [main] INFO com.fly.simple.MainRun - HOMEPATH -> \Users\00fly
2024-07-06 15:34:16.428 [main] INFO com.fly.simple.MainRun - LOCALAPPDATA -> C:\Users\00fly\AppData\Local
2024-07-06 15:34:16.428 [main] INFO com.fly.simple.MainRun - LOGONSERVER -> \\7t9lppye5cj7lud
2024-07-06 15:34:16.429 [main] INFO com.fly.simple.MainRun - NODE_PATH -> D:\nodejs
2024-07-06 15:34:16.429 [main] INFO com.fly.simple.MainRun - NUMBER_OF_PROCESSORS -> 4
2024-07-06 15:34:16.429 [main] INFO com.fly.simple.MainRun - OS -> Windows_NT
2024-07-06 15:34:16.429 [main] INFO com.fly.simple.MainRun - PATHEXT -> .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
2024-07-06 15:34:16.429 [main] INFO com.fly.simple.MainRun - PROCESSOR_ARCHITECTURE -> AMD64
2024-07-06 15:34:16.429 [main] INFO com.fly.simple.MainRun - PROCESSOR_IDENTIFIER -> Intel64 Family 6 Model 85 Stepping 7, GenuineIntel
2024-07-06 15:34:16.430 [main] INFO com.fly.simple.MainRun - PROCESSOR_LEVEL -> 6
2024-07-06 15:34:16.430 [main] INFO com.fly.simple.MainRun - PROCESSOR_REVISION -> 5507
2024-07-06 15:34:16.430 [main] INFO com.fly.simple.MainRun - PSModulePath -> C:\Program Files\WindowsPowerShell\Modules;C:\Windows\system32\WindowsPowerShell\v1.0\Modules
2024-07-06 15:34:16.430 [main] INFO com.fly.simple.MainRun - PUBLIC -> C:\Users\Public
2024-07-06 15:34:16.431 [main] INFO com.fly.simple.MainRun - Path -> C:/Java/eclipse//plugins/org.eclipse.justj.openjdk.hotspot.jre.full.win32.x86_64_17.0.9.v20231028-0858/jre/bin/server;C:/Java/eclipse//plugins/org.eclipse.justj.openjdk.hotspot.jre.full.win32.x8
6_64_17.0.9.v20231028-0858/jre/bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\Git\cmd;C:\Program Files\TortoiseGit\bin;C:\Java\apache-maven-3.9.6\bin;C:\Ja
va\jdk1.8.0\bin;C:\Program Files (x86)\NetSarang\Xshell 7\;C:\Program Files (x86)\NetSarang\Xftp 7\;C:\ProgramData\chocolatey\bin;D:\nodejs\;D:\nodejs;D:\nodejs\node_modules;D:\nodejs\node_global;C:\Users\00fly\AppData\Local\Microsoft\WindowsApps;D:\nodejs\node_gl
obal;D:\Programs\Microsoft VS Code\bin;C:\Java\eclipse;
2024-07-06 15:34:16.431 [main] INFO com.fly.simple.MainRun - ProgramData -> C:\ProgramData
2024-07-06 15:34:16.431 [main] INFO com.fly.simple.MainRun - ProgramFiles -> C:\Program Files
2024-07-06 15:34:16.432 [main] INFO com.fly.simple.MainRun - ProgramFiles(x86) -> C:\Program Files (x86)
2024-07-06 15:34:16.433 [main] INFO com.fly.simple.MainRun - ProgramW6432 -> C:\Program Files
2024-07-06 15:34:16.433 [main] INFO com.fly.simple.MainRun - SESSIONNAME -> Console
2024-07-06 15:34:16.433 [main] INFO com.fly.simple.MainRun - SystemDrive -> C:
2024-07-06 15:34:16.433 [main] INFO com.fly.simple.MainRun - SystemRoot -> C:\Windows
2024-07-06 15:34:16.434 [main] INFO com.fly.simple.MainRun - TEMP -> C:\Users\00fly\AppData\Local\Temp\1
2024-07-06 15:34:16.434 [main] INFO com.fly.simple.MainRun - TMP -> C:\Users\00fly\AppData\Local\Temp\1
2024-07-06 15:34:16.434 [main] INFO com.fly.simple.MainRun - USERDOMAIN -> 7T9LPPYE5CJ7LUD
2024-07-06 15:34:16.435 [main] INFO com.fly.simple.MainRun - USERDOMAIN_ROAMINGPROFILE -> 7T9LPPYE5CJ7LUD
2024-07-06 15:34:16.435 [main] INFO com.fly.simple.MainRun - USERNAME -> 00fly
2024-07-06 15:34:16.435 [main] INFO com.fly.simple.MainRun - USERPROFILE -> C:\Users\00fly
2024-07-06 15:34:16.435 [main] INFO com.fly.simple.MainRun - classPath -> .;C:\Java\jdk1.8.0\lib
2024-07-06 15:34:16.436 [main] INFO com.fly.simple.MainRun - java_home -> C:\Java\jdk1.8.0
2024-07-06 15:34:16.436 [main] INFO com.fly.simple.MainRun - maven_home -> C:\Java\apache-maven-3.9.6
2024-07-06 15:34:16.437 [main] INFO com.fly.simple.MainRun - windir -> C:\Windows
2024-07-06 15:34:16.440 [main] INFO com.fly.simple.MainRun - ------------------------------
2024-07-06 15:34:16.526 [pool-2-thread-1] INFO com.fly.simple.MainRun - jdbc:mysql://127.0.0.1:3306/jdbc?useSSL=false&useUnicode=true&characterEncoding=utf-8&createDatabaseIfNotExist=true
2024-07-06 15:34:16.527 [pool-2-thread-1] INFO com.fly.simple.MainRun - root
2024-07-06 15:34:16.528 [pool-2-thread-1] INFO com.fly.simple.MainRun - root123
2. centos 服务器jar运行
2024-07-06 15:37:31.264 [main] INFO com.fly.simple.MainRun - ------------------------------
2024-07-06 15:37:31.270 [main] INFO com.fly.simple.MainRun - BASH_FUNC_which%% -> () { ( alias;eval ${which_declare} ) | /usr/bin/which --tty-only --read-alias --read-functions --show-tilde --show-dot "$@"
}
2024-07-06 15:37:31.271 [main] INFO com.fly.simple.MainRun - DBUS_SESSION_BUS_ADDRESS -> unix:path=/run/user/0/bus
2024-07-06 15:37:31.271 [main] INFO com.fly.simple.MainRun - HISTCONTROL -> ignoredups
2024-07-06 15:37:31.271 [main] INFO com.fly.simple.MainRun - HISTSIZE -> 10000
2024-07-06 15:37:31.271 [main] INFO com.fly.simple.MainRun - HISTTIMEFORMAT -> %F %T root
2024-07-06 15:37:31.271 [main] INFO com.fly.simple.MainRun - HOME -> /root
2024-07-06 15:37:31.271 [main] INFO com.fly.simple.MainRun - HOSTNAME -> 00fly
2024-07-06 15:37:31.272 [main] INFO com.fly.simple.MainRun - LANG -> en_US.UTF-8
2024-07-06 15:37:31.272 [main] INFO com.fly.simple.MainRun - LESSOPEN -> ||/usr/bin/lesspipe.sh %s
2024-07-06 15:37:31.272 [main] INFO com.fly.simple.MainRun - LOGNAME -> root
2024-07-06 15:37:31.272 [main] INFO com.fly.simple.MainRun - LS_COLORS -> rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.wim=01;31:*.swm=01;31:*.dwm=01;31:*.esd=01;31:*.jpg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.m4a=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.oga=01;36:*.opus=01;36:*.spx=01;36:*.xspf=01;36:
2024-07-06 15:37:31.272 [main] INFO com.fly.simple.MainRun - MAIL -> /var/spool/mail/root
2024-07-06 15:37:31.273 [main] INFO com.fly.simple.MainRun - OLDPWD -> /work
2024-07-06 15:37:31.273 [main] INFO com.fly.simple.MainRun - PATH -> /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
2024-07-06 15:37:31.273 [main] INFO com.fly.simple.MainRun - PWD -> /000/java-with-docker/target
2024-07-06 15:37:31.273 [main] INFO com.fly.simple.MainRun - SHELL -> /bin/bash
2024-07-06 15:37:31.273 [main] INFO com.fly.simple.MainRun - SHLVL -> 1
2024-07-06 15:37:31.273 [main] INFO com.fly.simple.MainRun - SSH_CLIENT -> 47.123.82.115 51181 22
2024-07-06 15:37:31.273 [main] INFO com.fly.simple.MainRun - SSH_CONNECTION -> 47.123.82.115 51181 192.168.15.202 22
2024-07-06 15:37:31.274 [main] INFO com.fly.simple.MainRun - SSH_TTY -> /dev/pts/0
2024-07-06 15:37:31.274 [main] INFO com.fly.simple.MainRun - TC_LIB_DIR -> /usr/lib64/tc
2024-07-06 15:37:31.274 [main] INFO com.fly.simple.MainRun - TERM -> xterm
2024-07-06 15:37:31.274 [main] INFO com.fly.simple.MainRun - USER -> root
2024-07-06 15:37:31.274 [main] INFO com.fly.simple.MainRun - XDG_RUNTIME_DIR -> /run/user/0
2024-07-06 15:37:31.274 [main] INFO com.fly.simple.MainRun - XDG_SESSION_ID -> 401
2024-07-06 15:37:31.274 [main] INFO com.fly.simple.MainRun - _ -> /usr/bin/java
2024-07-06 15:37:31.274 [main] INFO com.fly.simple.MainRun - which_declare -> declare -f
2024-07-06 15:37:31.275 [main] INFO com.fly.simple.MainRun - ------------------------------
2024-07-06 15:37:31.312 [pool-2-thread-1] INFO com.fly.simple.MainRun - jdbc:mysql://127.0.0.1:3306/jdbc?useSSL=false&useUnicode=true&characterEncoding=utf-8&createDatabaseIfNotExist=true
2024-07-06 15:37:31.313 [pool-2-thread-1] INFO com.fly.simple.MainRun - root
2024-07-06 15:37:31.313 [pool-2-thread-1] INFO com.fly.simple.MainRun - root123
3. centos 服务器docker运行
[+] Running 1/2⠼ Network docker-jwd_default Created 0.4s ✔ Container java-with-docker Started 0.4s
2024-07-06 15:38:45.009 [main] INFO com.fly.simple.MainRun - ------------------------------
2024-07-06 15:38:45.024 [main] INFO com.fly.simple.MainRun - HOME -> /root
2024-07-06 15:38:45.025 [main] INFO com.fly.simple.MainRun - HOSTNAME -> 2563205e02bd
2024-07-06 15:38:45.025 [main] INFO com.fly.simple.MainRun - JAVA_HOME -> /opt/java/openjdk
2024-07-06 15:38:45.025 [main] INFO com.fly.simple.MainRun - JAVA_TOOL_OPTIONS -> -XX:+IgnoreUnrecognizedVMOptions -XX:+IdleTuningGcOnIdle -Xshareclasses:name=openj9_system_scc,cacheDir=/opt/java/.scc,readonly,nonFatal
2024-07-06 15:38:45.025 [main] INFO com.fly.simple.MainRun - JAVA_VERSION -> jdk8u312-b07_openj9-0.29.0
2024-07-06 15:38:45.025 [main] INFO com.fly.simple.MainRun - JDBC_DRIVER -> com.mysql.cj.jdbc.Driver
2024-07-06 15:38:45.025 [main] INFO com.fly.simple.MainRun - JDBC_PASSWORD -> test123
2024-07-06 15:38:45.026 [main] INFO com.fly.simple.MainRun - JDBC_URL -> jdbc:mysql://192.168.15.202:3306/jdbc?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&autoReconnect=true
2024-07-06 15:38:45.026 [main] INFO com.fly.simple.MainRun - JDBC_USERNAME -> test
2024-07-06 15:38:45.026 [main] INFO com.fly.simple.MainRun - LANG -> en_US.UTF-8
2024-07-06 15:38:45.026 [main] INFO com.fly.simple.MainRun - LANGUAGE -> en_US:en
2024-07-06 15:38:45.026 [main] INFO com.fly.simple.MainRun - LC_ALL -> en_US.UTF-8
2024-07-06 15:38:45.026 [main] INFO com.fly.simple.MainRun - PATH -> /opt/java/openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
2024-07-06 15:38:45.027 [main] INFO com.fly.simple.MainRun - ------------------------------
2024-07-06 15:38:45.116 [pool-2-thread-1] INFO com.fly.simple.MainRun - jdbc:mysql://192.168.15.202:3306/jdbc?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&autoReconnect=true
2024-07-06 15:38:45.117 [pool-2-thread-1] INFO com.fly.simple.MainRun - test
2024-07-06 15:38:45.117 [pool-2-thread-1] INFO com.fly.simple.MainRun - test123
六、完整代码
https://gitcode.com/00fly/java-with-docker/overview
有任何问题和建议,都可以向我提问讨论,大家一起进步,谢谢!
-over-