如何在apache camel中为http4组件添加负载均衡。对于http4组件代码检查如下

时间:2018-05-02 13:08:20

标签: apache-camel

对于下面的http4组件代码检查。

rules.getRules().forEach(x->{
            from("jetty:http://0.0.0.0:"+rules.getPort()+"/"+x.getFrom()+"??matchOnUriPrefix=true")
            .to("http4://"+x.getTo()+"?bridgeEndpoint=true&throwExceptionOnFailure=false");
            System.out.println(“Ieration Route:  ”+x);});

请检查下面的样本路由(X值)

Ieration Route:  {"RouteRule":{ "from":"posts", "to":"jsonplaceholder.typicode.com/posts/?"}}
Ieration Route:  {"RouteRule":{ "from":"users", "to":"reqres.in/api/users"}}
Ieration Route:  {"RouteRule":{ "from":"countries", "to":"restcountries.eu/rest/v2/"}}

Application.yml文件

routes:
  port: 8088
  route:
    -
      from: posts
      to: jsonplaceholder.typicode.com/posts/?
    -
      from: users
      to: reqres.in/api/users
    -
      from: countries
      to: restcountries.eu/rest/v2/

我们可以在上面的场景中添加多个服务器吗?在yml文件配置中,如果是,我们如何添加它。

感谢您的帮助。

由于

1 个答案:

答案 0 :(得分:1)

根据骆驼文档The Load Balancer Pattern allows you to delegate to one of a number of endpoints using a variety of different load balancing policies camel-urlrewrite 组件将允许您插入url重写机制。

可以按以下方式配置此故障转移负载平衡器:

from("jetty:http://{host}:{{port}}/{context_1}?matchOnUriPrefix=true")
.loadBalance().failover(Exception.class)
.to("jetty:http://{host}:{{port2}}/context_2?bridgeEndpoint=true&urlRewrite=#myRewrite")
.to("jetty:http://{host}:{{port2}}/{other_context}?bridgeEndpoint=true&urlRewrite=#myRewrite");

详细代码可在此处找到:https://github.com/apache/camel/tree/master/components/camel-urlrewrite/src/test/java/org/apache/camel/component/urlrewrite

*此组件要求您的Camel路由从基于servlet的端点开始,例如您的代码已经存在的Jetty等。

相关的Camel Doc @ http://camel.apache.org/load-balancer.html