一、用jetty搭建项目
(1)下载9.2.21版本jetty安装包jetty-9.2.21.zip,解压缩到目录/home/apps/jetty
(2)新建项目路径:/home/apps/bossms
(4)在项目路径下新建jetty实例路径:/home/apps/bossms/jetty
(3)在 /home/apps/bossms/jetty 下生成jetty的项目实例内容(http模块和deploy模块):
java -jar /home/apps/jetty/start.jar --add-to-start=http,deploy。
结果:
[root@localhost jetty]# java -jar /home/apps/jetty/start.jar --add-to-start=http,deploy
INFO: http initialised in ${jetty.base}/start.ini (appended)
INFO: server initialised transitively
INFO: server initialised in ${jetty.base}/start.ini (appended)
INFO: deploy initialised in ${jetty.base}/start.ini (appended)
MKDIR: ${jetty.base}/webapps
INFO: webapp enabled in <transitive>
INFO: servlet enabled in <transitive>
INFO: security enabled in <transitive>
INFO: server initialised in ${jetty.base}/${jetty.base}/start.ini
INFO: server enabled in <transitive>
在/home/apps/bossms/jetty 目录下生成jetty实例内容:start.ini 文件和webapps文件夹。
(4)从jetty安装目录/home/apps/jetty中拷贝jetty自带样例项目async-rest.war到当前jetty实 例:
cp /home/apps/jetty/demo-base/webapps/async-rest.war ./webapps
(5)启动jetty实例:在jetty实例目录下:/home/apps/bossms/jetty
java -jar /home/apps/jetty/start.jar
浏览器访问:http://ip:8080/
async-rest/
ip是设备的ip。
(6)新建linux启动脚本
从jetty安装目录中拷贝启动脚本jetty.sh 到jetty实例中:
mkidr bin
cp /home/apps/jetty/bin/jetty.sh ./bin/
新建日志路径:
mkdir logs
修改jetth.sh: 在文件开始部位加上:
#set JETTY_HOME
JETTY_HOME=/home/apps/jetty
#set JETTY_BASE
JETTY_BASE=$PWD
#set JETTY_LOGS
JETTY_LOGS=$JETTY_BASE/logs
这样就可以用 jetty.sh操作jetty实例了,如: sh ./bin/jetty.sh start。
备注:建议将jetty.sh 改名为jetty-bossms.sh,因为这个.sh文件中有定义一个 ‘NAME’ 变量指向启动脚本的名称,且‘NAME’变量有绑定其他一些配置,如PID文件等。NAME=$(echo $(basename $0) | sed -e 's/^[SK][0-9]*//' -e 's/\.sh$//')
(7)部署项目bossms
web应用部署在webapps文件夹,支持热部署,支持标准的WAR文件和Jetty配置文件,并遵循下面的约定:
1)如果文件夹example/被部署,且包含一个WEB-INF/子文件夹,则它作为标准web应用部署,否则它作为静态目录上下文部署。上下文路径是/example(即:
http://localhost:8080/example/
),但如果文件夹名是ROOT(大小写敏感),则上下文路径是/。如果文件夹名以“.d”结束,则被忽略(但可以通过直接配置来使用)。
2)文件example.war被作为标准web应用部署,上下文路径为/example(即:
http://localhost:8080/example/
)。如果文件名是ROOT(大小写敏感),则上下文路径为/。如果example.war和example/同时存在,则WAR被部署。
3)一个XML文件example.xml被部署,配置通过XML文件定义,配置必须包含上下文路径。如果example.xml和example.war存在,仅XML被部署。
如果你有一个标准的web应用,你能通过拷贝它到webapps文件夹来热部署它到Jetty。
备注:标准WAR文件或者WAR文件解压缩文件夹需要有WEB-INF/web.xml文件,否则作为静态目录上下文部署。
当部署spring boot的项目时,由于可能没有web.xml文件,会作为静态目录上下文部署,而不是web项目。解决办法:为 jetty 实例加入模块:jsp。 命令:java -jar /home/apps/jetty/start.jar --add-to-start=jsp。 如果把spring boot项目部署在$JETTY_HOME/webapps下,在$JETTY_HOME 下启动jetty,因为此时jetty实例默认拥有jsp模块,所以不会出现上面的当作静态文件目录部署的问题,而是作为web项目部署。
将项目bossms放在$JETTY_BASE/webapps目录下,更名为ROOT,假设http端口是80,则bossms访问地址:http://ip/
如果不更名,则访问地址:http://ip/bossms
(8)支持https模块
在jetty实例目录下/home/apps/bossms/jetty 执行 java -jar /home/apps/jetty/start.jar --add-to-start=https,会将https模块加入到jetty实例,同时自动将https和ssl的配置也加入到start.ini文件中。
结果:
[root@localhost jetty]# java -jar /home/apps/jetty/start.jar --add-to-start=https
INFO: https initialised in ${jetty.base}/start.ini (appended)
INFO: ssl initialised transitively
INFO: ssl initialised in ${jetty.base}/start.ini (appended)
DOWNLOAD: https://raw.githubusercontent.com/eclipse/jetty.project/master/jetty-server/src/test/config/etc/keystore?id=${jetty.tag.version} to etc/keystore
INFO: server initialised in ${jetty.base}/start.ini
INFO: server enabled in ${jetty.base}/start.ini
INFO: server enabled in <transitive>
start.ini中自动新增了以下内容:
# ---------------------------------------
# Module: https
--module=https
## HTTPS Configuration
# HTTP port to listen on
https.port=8443
# HTTPS idle timeout in milliseconds
https.timeout=30000
# HTTPS Socket.soLingerTime in seconds. (-1 to disable)
# https.soLingerTime=-1
# ---------------------------------------
# Module: ssl
--module=ssl
### SSL Keystore Configuration
# define the port to use for secure redirection
jetty.secure.port=8443
## Setup a demonstration keystore and truststore
jetty.keystore=etc/keystore
jetty.truststore=etc/keystore
## Set the demonstration passwords.
## Note that OBF passwords are not secure, just protected from casual observation
## See http://www.eclipse.org/jetty/documentation/current/configuring-security-secure-passwords.html
jetty.keystore.password=OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4
jetty.keymanager.password=OBF:1u2u1wml1z7s1z7a1wnl1u2g
jetty.truststore.password=OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4
### Set the client auth behavior
## Set to true if client certificate authentication is required
# jetty.ssl.needClientAuth=true
## Set to true if client certificate authentication is desired
# jetty.ssl.wantClientAuth=true
## Parameters to control the number and priority of acceptors and selectors
# ssl.selectors=1
# ssl.acceptors=1
# ssl.selectorPriorityDelta=0
# ssl.acceptorPriorityDelta=0
此时重启服务用https访问:https://ip:8443/async-rest/,其中ip是设备ip。
参考资料:
点击打开链接
本文详细介绍了如何使用Jetty搭建和部署服务,包括下载Jetty安装包,创建项目路径,生成Jetty实例,拷贝示例项目,启动服务,部署项目,支持HTTPS模块的配置,以及解决Spring Boot项目部署问题。通过这些步骤,可以实现Jetty服务的顺利运行和管理。
4054

被折叠的 条评论
为什么被折叠?



