undertow 多个 web 服务 url

Posted

技术标签:

【中文标题】undertow 多个 web 服务 url【英文标题】:undertow multiple webservices url 【发布时间】:2015-03-19 20:34:50 【问题描述】:

我想在我的地址上添加这样的内容。 如何将 servlet1、servlet2、servlet3 绑定到 localhost:8080,这样我就可以有不同的 url 来调用,例如

localhost:8080/servlet

localhost:8080/servlet1

localhost:8080/servlet2

localhost:8080/servlet3

我有这段代码只适用于添加的最后一个 url(servlet)

DeploymentInfo servletBuilder = deployment()
                .setClassLoader(ServletServer.class.getClassLoader())
                .setContextPath(MYAPP)
                .setDeploymentName("test.war")
                .addServlets(
                        servlet(servletName,  x)
                        .addInitParam("message", output)
                        .addMapping("/"+servletName.toLowerCase()));

        DeploymentManager manager = defaultContainer().addDeployment(servletBuilder);
        manager.deploy();

        HttpHandler servletHandler = manager.start();
        path = Handlers.path(Handlers.redirect(MYAPP))
                .addPrefixPath(MYAPP, servletHandler);

【问题讨论】:

【参考方案1】:
DeploymentInfo servletBuilder = deployment()
.addServlets(
 servlet("servlet1", YourServlet1.class).addInitParam("param1", "Value A")
.addMapping("/servlet1/*"),
 servlet("servlet2", YourServlet2.class).addInitParam("param2", "Value B")
.addMapping("/servlet2/*"),
 servlet("servlet3", YourServlet3.class).addInitParam("param2", "Value C")
.addMapping("/servlet3/*")  // as many as you like.
);

使用此处的信息: https://github.com/undertow-io/undertow/tree/master/examples/src/main/java/io/undertow/examples/servlet

【讨论】:

尝试在您发布的链接中运行这些类,但我在线程“main”java.util.ServiceConfigurationError 中收到此错误异常:io.undertow.servlet.ServletExtension:Provider io.undertow.websockets。 jsr.Bootstrap 无法在 java.util.ServiceLoader.access$100(ServiceLoader.java:185) 的 java.util.ServiceLoader$LazyIterator.nextService(ServiceLoader) 的 java.util.ServiceLoader.fail(ServiceLoader.java:232) 实例化.java:384) 考虑使用hightail.com/send 分享您的完整SAMPLE 项目。它可能有助于排除故障。 嗨,此示例代码有效:hightail.com/download/bXBaTXRRaFJQb0xIRHRVag【参考方案2】:

根据 Undertow 1.2.5 示例代码修改:

来自此 URL 的可运行项目将于 2015 年 6 月 10 日到期: https://www.hightail.com/download/bXBaTXRRaFJQb0xIRHRVag


package servlet;

import javax.servlet.ServletException;

import io.undertow.Handlers;
import io.undertow.Undertow;

import io.undertow.server.HttpHandler;
import io.undertow.server.handlers.PathHandler;
import io.undertow.servlet.api.DeploymentInfo;
import io.undertow.servlet.api.DeploymentManager;

import static io.undertow.servlet.Servlets.defaultContainer;
import static io.undertow.servlet.Servlets.deployment;
import static io.undertow.servlet.Servlets.servlet;

/**
 * @author Stuart Douglas
 */
public class ServletServer 

    public static final String MYAPP = "/myapp";

    public static void main(final String[] args) 
        try 

            DeploymentInfo servletBuilder = deployment()
                    .setClassLoader(ServletServer.class.getClassLoader())
                    .setContextPath(MYAPP)
                    .setDeploymentName("doesnotmatter.war")
                    .addServlets(
                            servlet("MessageServletA", MessageServletA.class)
                            .addInitParam("message", "Hello AAA\n")
                            .addMapping("/msgA/*"),
                            servlet("MessageServletB", MessageServletB.class)
                            .addInitParam("message", "Hello BBB\n")
                            .addMapping("/msgB/*"),
                            servlet("MessageServletC", MessageServletC.class)
                            .addInitParam("message", "Hello CCC\n")
                            .addMapping("/msgC/*")               
                    );

            DeploymentManager manager = defaultContainer().addDeployment(servletBuilder);
            manager.deploy();

            HttpHandler servletHandler = manager.start();
            PathHandler path = Handlers.path(Handlers.redirect(MYAPP))
                    .addPrefixPath(MYAPP, servletHandler);
            Undertow server = Undertow.builder()
                    .addHttpListener(8080, "localhost")
                    .setHandler(path)
                    .build();
            server.start();
         catch (ServletException e) 
            throw new RuntimeException(e);
        
    


请在 shell 中使用此命令进行测试:


# Runs in background
java -cp target/undertowservlet-jar-with-dependencies.jar servlet.Servletrver &

curl http://localhost:8080/myapp/msgA/
curl http://localhost:8080/myapp/msgB/
curl http://localhost:8080/myapp/msgC/

【讨论】:

以上是关于undertow 多个 web 服务 url的主要内容,如果未能解决你的问题,请参考以下文章

使用 WildFly (Undertow) 托管多个域

开微服务项目tomcat更换成undertow

Undertow:使用现有的 Servlet 实例

SpringBoot2使用Undertow来提高应用性能(spring-boot-starter-undertow)

吊打tomcat的高性能web服务器

使用 Undertow 服务 AngularJS