Apache Camel :如何使用来自多个REST端点的数据的最佳实践

6tqwzwtp  于 2022-11-07  发布在  Apache
关注(0)|答案(1)|浏览(235)

我正在使用来自REST端点的数据(数量级为1000+),这些端点都具有相同的结构:

<server uri>/v1/source/<some ID>

我使用的RouteBuilder组件如下所示,连接到单个端点<ID>

@Component
public class Route_to_<ID> extends RouteBuilder {

@Override
public void configure() throws Exception {
    from("timer:mytimer?repeatCount=1") //
            .setBody(simple("${null}")) //
            .setHeader(Exchange.CONTENT_TYPE, simple("text/event-stream"))
            .setHeader("CamelHttpMethod", simple("GET"))
            .to(
                    <server uri>/v1/source/<ID>
                            + _deviceName + "::" + _deviceProperty //
                            + "?disableStreamCache=true" //
            ) //
            .process(data -> {
                ... do same stuff for all endpoints ...
            });
}

}
对应的SpringBootApplication如下所示:

@SpringBootApplication
@ComponentScan(basePackages = "my.package.where.components.reside")
public class MyRouteHandler {

}

是否有一种优雅的方法可以使用一个SpringBootApplication一次性启动所有到端点<ID>的单独路由?或者每个路由都需要单独启动的单独SpringBootApplication

k5ifujac

k5ifujac1#

您可以将toD与动态URI一起使用,动态URI从消息正文、标头或交换属性中获取版本ID。您还可以使用property-placeholders定义主机、端口和其他配置。
由于REST端点都使用相同的结构,因此您可以更改版本id,并对大多数(如果不是所有)REST API调用使用相同的URI。

范例:

public class ExampleTest extends CamelTestSupport {

    static final String API_DYNAMIC_URI = "https://{{api.uri}}:{{api.port}}/{{api.version}}" 
        + "/${exchangeProperty.source}/${exchangeProperty.id}"
        + "?disableStreamCache=true";

    @Test
    public void exampleTest() throws Exception {

        context.adviceWith(context.getRouteDefinition("exampleRoute"), 
            new AdviceWithRouteBuilder(){

            @Override
            public void configure() throws Exception {

                weaveById("apiEndpoint")
                    .replace()
                    .toD("mock:${exchangeProperty.source}/${exchangeProperty.id}")
                    .setBody().simple("Source: ${exchangeProperty.source} id: ${exchangeProperty.id}");
            } 
        });        

        Map<String, Object> body1 = new HashMap<>();
        body1.put("source", "source1");
        body1.put("id", "A");

        Map<String, Object> body2 = new HashMap<>();
        body2.put("source", "source2");
        body2.put("id", "B");

        MockEndpoint source1MockEndpoint = getMockEndpoint("mock:source1/A");
        source1MockEndpoint.expectedMessageCount(1);

        MockEndpoint source2MockEndpoint = getMockEndpoint("mock:source2/B");
        source2MockEndpoint.expectedMessageCount(2);

        startCamelContext();
        template.sendBody("direct:start", body1);

        template.sendBody("direct:start", body2);
        template.sendBody("direct:start", body2);

        source1MockEndpoint.assertIsSatisfied();
        source2MockEndpoint.assertIsSatisfied();
    }

    @Override
    protected RoutesBuilder createRouteBuilder() throws Exception {

        return new RouteBuilder() {

            @Override
            public void configure() throws Exception {

                from("direct:start")
                    .routeId("exampleRoute")
                    .setProperty("source").simple("${body['source']}")
                    .setProperty("id").simple("${body['id']}")
                    .toD(API_DYNAMIC_URI).id("apiEndpoint")
                    .log("Received: ${body}");
            }
        };
    }

    @Override
    protected Properties useOverridePropertiesWithPropertiesComponent() {
        Properties properties = new Properties();
        properties.put("api.uri", "localhost");
        properties.put("api.port", "3000");
        properties.put("api.version", "v1");
        return properties;
    }

    @Override
    public boolean isUseAdviceWith() {
        return true;
    }
}

相关问题