【问题标题】:How can you make a Java DSL servlet using Camel in .war format that can deployed and run in Tomcat?如何使用 Camel 以 .war 格式制作可以在 Tomcat 中部署和运行的 Java DSL servlet?
【发布时间】:2023-03-29 18:50:01
【问题描述】:

过去几天我一直在搜索如何在没有任何 Tomcat 或 Camel 知识的情况下执行以下操作,但我很惊讶我没有找到任何好的资源:

制作一个 .war 文件,该文件将部署到 Tomcat 中(例如,来自 Manager App),它将接受给定 URI /test 上的请求,并将请求转发到 PHP 中的内部服务,该服务在 localhost:8222/index.php?q=test 上运行。

我设法通过在camel-archetype-java 之上构建将请求转发到不同网址的工作示例,路由器如下所示:

package camelinaction;

import org.apache.camel.builder.RouteBuilder;

public class MyRouteBuilder extends RouteBuilder {

    public void configure() {
        String targetUrl = "https://another.service.com/api/test";
        from("jetty:http://127.0.0.1:25566/test?matchOnUriPrefix=true")
                .to("jetty:" + targetUrl + "?bridgeEndpoint=true&throwExceptionOnFailure=false");
    }

}

我还设法从 camel-example-servlet-tomcat 示例中创建了一个 .war 文件,该文件在 Camel 中并成功部署在 tomcat 中。该示例在其项目文件夹中没有任何 Java 代码,并且基本上仅由 .xml 文件和一个 .html 页面组成,当请求由 tomcat 提供的相关 servlet 路径时,由 Camel servlet 提供该页面。 该项目的基本 xml 如下所示:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:camel="http://camel.apache.org/schema/spring"
       xsi:schemaLocation="
         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
         http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">

  <camelContext xmlns="http://camel.apache.org/schema/spring">

    <route id="helloRoute">
      <!-- incoming requests from the servlet is routed -->
      <from uri="servlet:hello"/>
      <choice>
        <when>
          <!-- is there a header with the key name? -->
          <header>name</header>
          <!-- yes so return back a message to the user -->
          <transform>
            <simple>Hi I am ${sysenv.HOSTNAME}. Hello ${header.name} how are you today?</simple>
          </transform>
        </when>
        <otherwise>
          <!-- if no name parameter then output a syntax to the user -->
          <transform>
            <constant>Add a name parameter to uri, eg ?name=foo</constant>
          </transform>
        </otherwise>
      </choice>
    </route>

  </camelContext>

</beans>

有人将如何结合这两个示例/功能来实现将来自 tomcat 的请求与骆驼一起转发到不同端点的最终目标?

【问题讨论】:

    标签: java tomcat servlets apache-camel


    【解决方案1】:

    由于您已经在可以处理 HTTP 的 servlet 容器中部署此 WAR,因此无需使用 camel-jetty 组件,但可以利用 camel-servlet 和 camel-servlet-listener 组件。

    您的第一个示例使用 Camel 的 Java DSL,第二个示例遵循 XML DSL,第一次使用可能有点吓人。我找不到任何结合特定场景的示例,因此我将quick demo 组合在一起,它可以部署在 servlet 容器中,并且可以将调用路由到另一个 HTTP 服务。这是一个很小的演示,需要一些修改。我用 jetty 对其进行了测试,但还没有尝试将它扔到 Tomcat 上。

    在 web.xml 文件中,以下部分控制上下文。

    <!-- Camel servlet mapping -->
    <servlet-mapping>
    <servlet-name>CamelServlet</servlet-name>
    <url-pattern>/camel/*</url-pattern>
    </servlet-mapping>
    

    只有一个java文件,路由配置,如下图

    public class DefaultRouteBuilder extends RouteBuilder {
    
    @Override
    public void configure() throws Exception {
    
        from("servlet:hello?matchOnUriPrefix=true")
        .routeId("HTTP Bridging Route")
        .log("Request: ${in.header."+ Exchange.HTTP_METHOD +"} to ${in.header."+ Exchange.HTTP_URI +"}")
        .to("https://another.service.com?bridgeEndpoint=true");
    }
    
    }
    

    一旦 serverlet 启动,您就可以访问 HTTP 资源,该资源由 camel 支持,地址为 http://server/&lt;context root&gt;/camel/hello 希望对您有所帮助。

    【讨论】:

    • 正是我需要的!如果我理解正确,我还可以通过在 .xml 文件中添加更多 &lt;context-param&gt; 块来添加多个端点/路由。
    • 对于任何使用 Java 8 的人,您都需要调整 pom 文件来编译项目:stackoverflow.com/a/42525941/4761457
    • 对不起!我忘了提到我的目标是 JVM 11。要在 http://server/&lt;context root&gt;/camel 下添加额外的 servlet 端点,您可以在 DefaultRouteBuilder 类中添加新的 Servlet 路由。像from("servlet:hola?matchOnUriPrefix=true") 这样的另一个端点应该可以解决问题。
    猜你喜欢
    • 1970-01-01
    • 2010-12-30
    • 2019-03-07
    • 1970-01-01
    • 2020-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多