【发布时间】: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