nginx unit java 试用
unit 当前已经支持java了,当时支持基于servlet 的开发模式,以下是一个简单的学习
基于官方的demo
环境准备
- docker-compose文件
version: "3"
services:
app:
build: ./
ports:
- "8080:8080"
- "9000:9000"
- dockerfile
# keep our base image as small as possible
FROM nginx/unit:1.11.0-minimal
# port used by the listener in config.json
EXPOSE 8080
# add Java language and corresponding language module
RUN apt update \
&& apt install -y apt-transport-https gnupg1 \
&& curl https://nginx.org/keys/nginx_signing.key | apt-key add - \
&& echo "deb https://packages.nginx.org/unit/debian/ stretch unit" \
> /etc/apt/sources.list.d/unit.list \
&& echo "deb-src https://packages.nginx.org/unit/debian/ stretch unit" \
>> /etc/apt/sources.list.d/unit.list \
&& apt update \
&& mkdir -p /usr/share/man/man1/ \
&& apt install -y openjdk-8-jdk unit-jsc8 \
# final cleanup
&& apt remove -y apt-transport-https gnupg1 \
&& apt autoremove --purge -y \
&& rm -rf /var/lib/apt/lists/* /etc/apt/sources.list.d/*.list
# application setup
RUN mkdir /www/ && echo \
'<%@page language="java" contentType="text/plain"%><%="Hello, Unit!"%>'\
> /www/index.jsp \
# launch Unit
&& unitd --control unix:/var/run/control.unit.sock \
# upload the app config to Unit
&& curl -X PUT --data-binary '{ \
"listeners": { \
"*:8080": { \
"pass": "applications/java_app" \
} \
}, \
"applications": { \
"java_app": { \
"type": "java", \
"webapp": "/www/" \
} \
} \
}' --unix-socket /var/run/control.unit.sock http://localhost/config/
配置说明
对于java 项目的支持配置如下:
{
"type": "java",
"classpath": ["/www/qwk2mart/lib/qwk2mart-2.0.0.jar"],
"options": ["-Dlog_path=/var/log/qwk2mart.log"],
"webapp": "/www/qwk2mart/qwk2mart.war"
}
type 指定为java 语言,classpath 为依赖的jar 包 ,options指定jvm启动的一些参数,比如上边的日志,webapp,指定应用程序的路径
或者未解压包(war)的文件
启动&&测试
- 启动
docker-compose build && docker-compose up -d
- 效果
说明
关于集成java 的原理还需要看看源码,从原理上还是cgi编程,同时使用doker 运行有点太大了