首页 > 解决方案 > 如何在 apache camel 中为 http4 组件添加负载平衡。对于下面的 http4 组件代码检查

问题描述

对于下面的 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/"}}

应用程序.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 文件配置中,如果是,我们如何添加它。

感谢您的帮助。

谢谢

标签: apache-camel

解决方案


根据骆驼文档骆驼urlrewriteThe Load Balancer Pattern allows you to delegate to one of a number of endpoints using a variety of different load balancing policies组件 将允许您插入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您的代码已经拥有的端点。

相关骆驼文档@http ://camel.apache.org/load-balancer.html


推荐阅读