我想将请求重定向到另一个处理请求并执行业务逻辑的服务器。我尝试使用ProxyServlet,但它不起作用。
这是我的主服务器实现,它获取所有请求:运行在localhost:8081
Server server = new Server(8081);
// 2. Creaing the WebAppContext for the created content
WebAppContext ctx = new WebAppContext();
ctx.setResourceBase("src/main/webapp");
ctx.setContextPath("/jetty-jsp-example");
ServletHolder jerseyServlet = ctx.addServlet(ProxyServlet.class,"/*");
jerseyServlet.setInitOrder(0);
jerseyServlet.setInitParameter("maxThreads", "1");
jerseyServlet.setInitParameter("proxyTo", "localhost:8080");
ctx.setAttribute("o:Prg.eclipse.jetty.server.webapp.ContainerIncludeJarPattern",".*/[^/]*jstl.*\\.jar$");
org.eclipse.jetty.webapp.Configuration.ClassList classlist = org.eclipse.jetty.webapp.Configuration.ClassList.setServerDefault(server);
classlist.addAfter("org.eclipse.jetty.webapp.FragmentConfiguration", "org.eclipse.jetty.plus.webapp.EnvConfiguration", "org.eclipse.jetty.plus.webapp.PlusConfiguration");
classlist.addBefore("org.eclipse.jetty.webapp.JettyWebXmlConfiguration", "org.eclipse.jetty.annotations.AnnotationConfiguration");
server.setHandler(ctx);
server.start();
server.join();
我想将此请求转发到在端口8080上运行的服务器。我的目标服务URL路径是:localhost:8080 / jetty -jsp-example / sv / endpoint。
我已经导入了jetty代理,如下所示
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-proxy</artifactId>
<version>9.3.8.v20160314</version>
此外,我读到我们的自定义代理servlet应该被要求扩展proxyservlet类并覆盖rewriteURL()方法。但是当我尝试它时,它给了我编译错误。
任何人让我如何解决这个问题。我用jetty 9.3
答案 0 :(得分:2)
BalancerServlet
和以下init参数:
// give your servlet a name
jerseyServlet.setName("proxy");
// parameter is 'balancerMember.NAME.proxyTo'
jerseyServlet.setInitParameter("balancerMember.proxy.proxyTo", "http://yourdownstreamserver.com/");
配置文档有点稀疏;我最终不得不看source code。另请注意,Jetty代理不支持通过SSL代理下游服务。