在 Spring Boot 中使用现有的 http 服务器作为骆驼端点 [英] Use existing http server in spring boot as camel endpoint

查看:28
本文介绍了在 Spring Boot 中使用现有的 http 服务器作为骆驼端点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 spring boot starter web 的 spring boot 应用程序.这将创建一个正在运行的 Tomcat 实例并设置在端口上运行的 http 服务器.在我的骆驼路线中,我想使用这个 http 服务器作为 http 请求的组件,但我不知道如何使用它.我看到很多配置码头实例并从中消费的例子,但是我实际上不会运行两个 http 服务器吗?我只想拥有一个.我假设 http 服务器已经自动连接,因为我可以使用其他 spring 代码(例如 RestController)使用它,并且我也可以在我的 spring 启动日志中看到它启动.

I have a spring boot application that uses the spring boot starter web. This creates a running Tomcat instance and sets up the http server running on a port. Within my camel route, I want to use this http server as the component for http requests, but I can't figure out how to utilize it. I see many many examples of configuring a jetty instance and consuming from it, but then wouldn't I in effect have two http servers running? I only want to have one. I assume the http server is already autowired up since I can consume from it with other spring code (such as a RestController) and I can see it started in my spring boot logs as well.

@Component
public class ExampleRoute extends RouteBuilder
{
    @Override
    public void configure() throws Exception
    {

        //@formatter:off

        from( <want to take in an http request here> )
            .log( LoggingLevel.INFO, log, "Hello World!" );

        //@formatter:on

    }
}

推荐答案

这里有一个例子:https://github.com/camelinaction/camelinaction2/tree/master/chapter7/springboot-camel

您可以注册一个 ServletRegistrationBean 来使用 Spring Boot 设置 Camel Servlet.

You can to register a ServletRegistrationBean that setup the Camel Servlet with Spring Boot.

@Bean
ServletRegistrationBean camelServlet() {
    // use a @Bean to register the Camel servlet which we need to do
    // because we want to use the camel-servlet component for the Camel REST service
    ServletRegistrationBean mapping = new ServletRegistrationBean();
    mapping.setName("CamelServlet");
    mapping.setLoadOnStartup(1);
    // CamelHttpTransportServlet is the name of the Camel servlet to use
    mapping.setServlet(new CamelHttpTransportServlet());
    mapping.addUrlMappings("/camel/*");
    return mapping;
}

但是,对于 Camel 2.19,我们计划使其更简单和 OOTB:https://issues.apache.org/jira/browse/CAMEL-10416

However for Camel 2.19 we plan on make this simpler and OOTB: https://issues.apache.org/jira/browse/CAMEL-10416

然后你就可以了

from("servlet:foo")
  .to("bean:foo");

调用 Camel 路由的 HTTP url 将是 http:localhost:8080/camel/foo

Where the HTTP url to call that Camel route will be http:localhost:8080/camel/foo

这篇关于在 Spring Boot 中使用现有的 http 服务器作为骆驼端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆