如何使用可以部署的 .war 格式的 Camel 和 Tomcat 中的 运行 制作 Java DSL servlet?
How can you make a Java DSL servlet using Camel in .war format that can deployed and run in Tomcat?
过去几天我一直在搜索如何在不了解 Tomcat 或 Camel 的情况下执行以下操作,我很惊讶我没有找到任何好的资源:
制作一个 .war 文件,该文件将被部署到 Tomcat(例如来自 Manager App),它将接受给定 URI 上的请求 /test
并将用于 ward 对 PHP 中运行于 localhost:8222/index.php?q=test
.
的内部服务的请求
我已经设法通过在 camel-archetype-java
之上构建一个 forwar 向不同的 url 发出请求的工作示例,路由器如下所示:
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
示例中的 Camel 中创建了一个 .war 文件,并在 tomcat 中成功部署了它。该示例虽然在其项目文件夹中没有任何 Java 代码,但基本上仅由 .xml 文件和一个 .html 页面组成,该页面在请求时由 Camel servlet 提供tomcat 提供的相关 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>
有人会如何将两者结合起来 examples/functionalities 以实现将来自 tomcat 的请求用 warded 与 camel 连接到不同端点的最终目标?
由于您已经在可以处理 HTTP 的 servlet 容器中部署此 WAR,因此无需使用 camel-jetty 组件,但可以利用 camel-servlet 和 camel-servlet-listener 组件.
您的第一个示例使用 Camel 的 Java DSL,第二个示例使用 XML DSL,第一次使用时可能有点吓人。我找不到任何结合特定场景的样本,所以我一起破解了一个可以部署在 servlet 容器中并可以将调用路由到另一个 HTTP 服务的 quick demo。这是一个很小的演示,需要进行一些修改。我用 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");
}
}
servelet 启动后,您可以访问 HTTP 资源,由 http://server/<context root>/camel/hello
的 camel 提供支持
我希望它能帮助你开始。
过去几天我一直在搜索如何在不了解 Tomcat 或 Camel 的情况下执行以下操作,我很惊讶我没有找到任何好的资源:
制作一个 .war 文件,该文件将被部署到 Tomcat(例如来自 Manager App),它将接受给定 URI 上的请求 /test
并将用于 ward 对 PHP 中运行于 localhost:8222/index.php?q=test
.
我已经设法通过在 camel-archetype-java
之上构建一个 forwar 向不同的 url 发出请求的工作示例,路由器如下所示:
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
示例中的 Camel 中创建了一个 .war 文件,并在 tomcat 中成功部署了它。该示例虽然在其项目文件夹中没有任何 Java 代码,但基本上仅由 .xml 文件和一个 .html 页面组成,该页面在请求时由 Camel servlet 提供tomcat 提供的相关 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>
有人会如何将两者结合起来 examples/functionalities 以实现将来自 tomcat 的请求用 warded 与 camel 连接到不同端点的最终目标?
由于您已经在可以处理 HTTP 的 servlet 容器中部署此 WAR,因此无需使用 camel-jetty 组件,但可以利用 camel-servlet 和 camel-servlet-listener 组件.
您的第一个示例使用 Camel 的 Java DSL,第二个示例使用 XML DSL,第一次使用时可能有点吓人。我找不到任何结合特定场景的样本,所以我一起破解了一个可以部署在 servlet 容器中并可以将调用路由到另一个 HTTP 服务的 quick demo。这是一个很小的演示,需要进行一些修改。我用 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");
}
}
servelet 启动后,您可以访问 HTTP 资源,由 http://server/<context root>/camel/hello
的 camel 提供支持
我希望它能帮助你开始。