Apache Camel的路由图


在本文中,我将向您介绍如何使用Apache Camel为应用程序构建路由图,跟踪这些路由的状态并为其收集度量。
我们在春季应用程序和Apache ServiceMix中使用Apache Camel。 而且,如果单独服务中的路由是可以理解的并且很容易看到,那么在数据总线中,如果有很多这样的路由,那就不是那么简单了。


什么是Apache ServiceMix

Apache Camel是一个开放源代码框架,用于通过使用简单的dsl和大量现成的数据访问组件来集成应用程序。


Apache ServiceMix是基于Apache ActiveMQ,Camel,CXF,Karaf的开源解决方案,可让您构建集成解决方案的平台。 Apache ServiceMix可用作企业服务总线。 在这种情况下,骆驼将使用xml,java,scala等形式的dsl简化总线中路由的创建。 例如,如果我们需要将消息从一个队列传输到另一个队列(不用考虑为什么我们需要这样做),我们可以在xml文件(下面的示例)中描述路由,将其放入所需的服务目录中,并将对其进行部署。


<?xml version="1.0" encoding="UTF-8"?> <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd"> <camelContext xmlns="http://camel.apache.org/schema/blueprint"> <route> <from uri="file:camel/input"/> <log message="Moving ${file:name} to the output directory"/> <to uri="file:camel/output"/> </route> </camelContext> </blueprint> 

所描述的路由将文件从一个目录传输到另一个目录。


多年来,轮胎已经积累了一百多条复杂程度各异的路线,并且我们逐渐了解到,记住所有这些联系变得越来越困难。 用手绘制路线图或以表格形式描述路线图似乎不再是一种方便且容易获得支持的解决方案。 但是,开始似乎自动构建路线图可以节省所有人。


要生成图形,您需要顶点和边。 其中,我们蒙蔽了一些美丽的东西!


路线元素


路由的入口点(是一个)由from语句描述,并带有端点指示。 即 为


 <from uri="file:camel/input"/> 

端点将是file:camel/input 。 他告诉我们,在路由开始时,文件将从camel/input目录中获取。


路由的出口点(有很多出口点,这就是为什么我表示复数)的确定方式有所不同-取决于消息传递模板以及所指示的端点。 在上面的示例中,此点由来描述。 即 为


 <to uri="file:camel/output"/> 

端点将是file:camel/output 。 他告诉我们,在路由的末尾,文件将保存到camel/output目录。


端点是我们需要的顶端。 肋骨将自行确定路线。


获取路线说明


Servicemix提供了使用JMX访问各种信息的功能,我们将使用jolokia通过http访问这些信息。


例如,以对路由的此描述为例


 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"> <camelContext xmlns="http://camel.apache.org/schema/spring"> <route id="the-clock-is-ticking"> <from uri="timer://foo?fixedRate=true&amp;period=1000"/> <to uri="jms:topic:timer?connectionFactory=demo"/> </route> <route id="service-a"> <from uri="jms:topic:timer?connectionFactory=demo"/> <to uri="jms:queue:service-a?connectionFactory=demo"/> </route> <route id="service-a-log"> <from uri="jms:queue:service-a?connectionFactory=demo"/> <to uri="log:service-a"/> </route> </camelContext> </beans> 

路线清单


方法http://host:8181/jolokia/read/org.apache.camel:type=routes,*返回包含详细信息的路由列表。


service-a路线的示例返回数据:


 "org.apache.camel:context=a.xml,name=\"service-a\",type=routes": { "StatisticsEnabled": true, "EndpointUri": "jms:\/\/topic:timer?connectionFactory=demo", "CamelManagementName": "a.xml", "ExchangesCompleted": 173, "LastProcessingTime": 2, "ExchangesFailed": 0, "Description": null, "FirstExchangeCompletedExchangeId": "ID-...", "StartTimestamp": "2018-12-17T07:01:12Z", "FirstExchangeCompletedTimestamp": "2018-12-17T07:01:13Z", "LastExchangeFailureTimestamp": null, "MaxProcessingTime": 35, "LastExchangeCompletedTimestamp": "2018-12-17T07:04:05Z", "Load15": "", "DeltaProcessingTime": -8, "OldestInflightDuration": null, "ExternalRedeliveries": 0, "ExchangesTotal": 173, "ResetTimestamp": "2018-12-17T07:01:12Z", "ExchangesInflight": 0, "MeanProcessingTime": 4, "LastExchangeFailureExchangeId": null, "FirstExchangeFailureExchangeId": null, "Uptime": "2 minutes", "CamelId": "camel-3", "TotalProcessingTime": 827, "FirstExchangeFailureTimestamp": null, "RouteId": "service-a", "RoutePolicyList": "", "FailuresHandled": 0, "MessageHistory": true, "Load05": "", "OldestInflightExchangeId": null, "State": "Started", "InflightExchanges": 0, "Redeliveries": 0, "MinProcessingTime": 0, "LastExchangeCompletedExchangeId": "ID-...", "Tracing": false, "Load01": "" } 

有很多RouteId ,其中, RouteIdContextEndpointUriStateUptime对于图的构建特别重要。


值得一提的是,该方法沿以下路线返回指标: ExchangesTotalExchangesCompletedExchangesFailedExchangesInflight等。


上面的方法可以满足我们90%的数据需求,但它不会返回的是有关路线出口的信息。 要获取此信息,需要使用获取路由详细信息的方法和获取方案的方法。 其中一种方法是不够的,因为在某些情况下,这些方法不会返回形成出口点列表所需的所有数据。


路线详情


路线详细信息从该方法获得。
http://host:8181/jolokia/exec/org.apache.camel:context=a.xml,type=routes,name="service-a"/createRouteStaticEndpointJson(boolean)/true


返回的数据示例:


 { "request": { "mbean": "org.apache.camel:context=a.xml,name=\"service-a\",type=routes", "arguments": ["true"], "type": "exec", "operation": "createRouteStaticEndpointJson(boolean)" }, "value": "{\"routes\": { \"service-a\": { \"inputs\": [ { \"uri\": \"jms:\/\/topic:timer?connectionFactory=demo\" } ], \"outputs\": [ { \"uri\": \"jms:\/\/queue:service-a?connectionFactory=demo\" } ] }}\n}\n", "timestamp": 1545040570, "status": 200 } 

路线图


路线图是从方法获得的
http://host:8181/jolokia/exec/org.apache.camel:context=a.xml,type=routes,name="service-a"/dumpRouteAsXml(boolean)/true


该方法仅当在其中绘制时才以xml形式返回路由图。 例如,如果使用org.apache.camel.builder.RouteBuilder (用于在spring应用程序中描述路由)描述路由,则该方法将不返回任何内容。


返回的数据示例:


 { "request": { "mbean": "org.apache.camel:context=a.xml,name=\"service-a\",type=routes", "arguments": ["true"], "type": "exec", "operation": "dumpRouteAsXml(boolean)" }, "value": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n<route customId=\"true\" id=\"service-a\" xmlns=\"http:\/\/camel.apache.org\/schema\/spring\">\n <from uri=\"jms:topic:timer?connectionFactory=demo\"\/>\n <to uri=\"jms:queue:service-a?connectionFactory=demo\" id=\"to5\"\/>\n<\/route>\n", "timestamp": 1545040727, "status": 200 } 

画图


结合收到的所有信息,您可以安全地绘制图形,我使用vis.js并获得了此结果

点是进入点和出口点,边是路线,路线上的灰度数字是ExchangesTotal指标。


图形化多种服务


所描述的图形构造方法也适用于不仅在数据总线中而且在应用程序中使用骆驼的情况。 例如,以这种方式在应用程序中描述了路线:


 @Component public class EventRoutes extends RouteBuilder { @Override public void configure() throws Exception { from("jms:topic:timer") .inOnly("bean:service?method=handle"); } } 

您可以合并来自servicemix和应用程序的路由上的所有数据,并绘制一个通用图形

注意,图中从jms:topic:timerbean:service的新路线bean:service出现。


结论


通过实施所描述的构造路线图的方法,我们能够获得公交车和综合服务的整体情况。 其实我们轮胎的图看起来像这样

概念证明应用程序可以在这里看到-github

Source: https://habr.com/ru/post/zh-CN435594/


All Articles