camel 调用远程http接口

camel 调用远程http 接口 实现

maven 依赖

<dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-core</artifactId>
            <version>2.22.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-http</artifactId>
            <version>2.22.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-netty-http</artifactId>
            <version>2.22.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId> camel-jetty</artifactId>
            <version>2.22.0</version>
        </dependency>

代码:

package com.jd.first;

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.model.ModelCamelContext;

public class HttpRemoteRequest extends RouteBuilder {

    public static void main(String[] args)  {
        try {
            // 这是camel上下文对象,整个路由的驱动全靠它了。
            ModelCamelContext camelContext = new DefaultCamelContext();
            // 启动route
            camelContext.start();
            // 将我们编排的一个完整消息路由过程,加入到上下文中
            camelContext.addRoutes(new HttpRemoteRequest());

            /*
             * ==========================
             * 为什么我们先启动一个Camel服务
             * 再使用addRoutes添加编排好的路由呢?
             * 这是为了告诉各位读者,Apache Camel支持动态加载/卸载编排的路由
             * 这很重要,因为后续设计的Broker需要依赖这种能力
             * ==========================
             * */

            // 通用没有具体业务意义的代码,只是为了保证主线程不退出
            synchronized (HttpRemoteRequest.class) {
                HttpRemoteRequest.class.wait();
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("FirstTest exception "+e.getMessage());
        }
    }

    public void configure() throws Exception {
        from("netty-http:http://127.0.0.1:8282/doHelloWorld").to("direct:getContact");
        from("direct:getContact").to("https://free-api.heweather.com/s6/weather/forecast?parameters&&bridgeEndpoint=true");
    }
}

其中camle http 模块有一参数进行说明:

英文不好就直接贴原文了 http://camel.apache.org/http4.html

大致是说 如果设置成true ,http模块会忽略Exchange里的HTTP_URI 里的头 并且用当前endpoint里的URI进行请求。   

名字

默认值描述

bridgeEndpoint

false

If trueHttpProducer will ignore the Exchange.HTTP_URI header, and use the endpoint's URI for request.

If throwExcpetionOnFailure=false the HttpProducer will return all fault responses to the caller.

Also, if true then HttpProducer and CamelServlet will skip the gzip processing if the content-encoding is gzip.


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值