配置 Jetty Servlet 代理

一则或许对你有用的小广告

欢迎加入小哈的星球 ,你将获得:专属的项目实战 / Java 学习路线 / 一对一提问 / 学习打卡/ 赠书活动

目前,正在 星球 内带小伙伴们做第一个项目:全栈前后端分离博客项目,采用技术栈 Spring Boot + Mybatis Plus + Vue 3.x + Vite 4手把手,前端 + 后端全栈开发,从 0 到 1 讲解每个功能点开发步骤,1v1 答疑,陪伴式直到项目上线,目前已更新了 204 小节,累计 32w+ 字,讲解图:1416 张,还在持续爆肝中,后续还会上新更多项目,目标是将 Java 领域典型的项目都整上,如秒杀系统、在线商城、IM 即时通讯、权限管理等等,已有 870+ 小伙伴加入,欢迎点击围观

可以从 这里 下载示例应用程序

假设您将 app1、app2、someotherapp 部署到 devhost:3000 和 testhost:3000 然后可以按如下方式访问这些应用程序

开发实例
http://devhost:3000/app1
http://devhost:3000/app2
http://devhost:3000/someotherapp

测试实例
http://testhost:3000/app1
http://testhost:3000/app2
http://testhost:3000/someotherapp

很多时候,一个应用程序依赖于同一服务器上的其他应用程序,因此,您的应用程序可能包含如下代码片段:


 <link title="Stylesheet" href="/someotherapp/style.css" rel="stylesheet" media="screen" type="text/css" />

此外,这些也可以直接访问

http://devhost:3000/someotherapp/style.css
http://testhost:3000/someotherapp/style.css

此外,无论是嵌入式还是使用 jetty-maven-plugin,Jetty 都是本地开发的最佳选择。

在这种情况下,在本地复制整个环境没有意义,而是可以在本地环境中 代理 这些 url,这样 /someotherapp/style.css 将被代理到 http://devhost:3000/someotherapp/style.css

这是实现它的方法。

1) 将 ProxyServlet.Transparent 映射到 /someotherapp/*
2)设置Servlet初始化参数如下
ProxyTo ==> http://devhost:3000/someotherapp
前缀 ==> /someotherapp

以上两步可以分两种方式完成

使用 Java 配置


 <link title="Stylesheet" href="/someotherapp/style.css" rel="stylesheet" media="screen" type="text/css" />

使用 Maven Jetty 插件配置


 <link title="Stylesheet" href="/someotherapp/style.css" rel="stylesheet" media="screen" type="text/css" />

src/main/etc/jetty.xml


 <link title="Stylesheet" href="/someotherapp/style.css" rel="stylesheet" media="screen" type="text/css" />