Использование Apache Camel REST DSL в нескольких контекстах

В ходе работы над нашей интеграционной платформой Entaxy у нас возникла задача запуска разных REST API в разных Camel контекстах. Для этих целей был выбран Camel REST DSL, который мы успешно использовали для других задач. Но почти сразу мы натолкнулись на проблему, которая ставила под угрозу всю идею - если определить в разных Camel контекстах разные REST API с одинаковым портом и contextPath, то такие бандлы не запустятся. Точнее стартанёт один, а остальные выдадут ошибку Caused by: java.net.BindException: Address already in use. В качестве примера попробуйте задеплоить 2 маршрута ниже:

<?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"
           xmlns:jaxrs="http://cxf.apache.org/blueprint/jaxrs"
           xmlns:cxf="http://cxf.apache.org/blueprint/core"
           xmlns:jpa="http://aries.apache.org/xmlns/jpa/v2.0.0"
           xmlns:tx="http://aries.apache.org/xmlns/transactions/v1.2.0"
           xsi:schemaLocation="
             http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
             http://cxf.apache.org/blueprint/jaxrs http://cxf.apache.org/schemas/blueprint/jaxrs.xsd
             http://cxf.apache.org/blueprint/core http://cxf.apache.org/schemas/blueprint/core.xsd
             ">

    <camelContext id="logContext" xmlns="http://camel.apache.org/schema/blueprint">
        <restConfiguration bindingMode="auto" component="netty-http" contextPath="/rest" port="8182"/>

            <rest>
                  <description>Log rest service</description>

            <get uri="/log">
                <to uri="log:?level=INFO"/>
            </get>
        </rest>
    </camelContext>

</blueprint>

и

<?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"
           xmlns:jaxrs="http://cxf.apache.org/blueprint/jaxrs"
           xmlns:cxf="http://cxf.apache.org/blueprint/core"
           xmlns:jpa="http://aries.apache.org/xmlns/jpa/v2.0.0"
           xmlns:tx="http://aries.apache.org/xmlns/transactions/v1.2.0"
           xsi:schemaLocation="
             http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
             http://cxf.apache.org/blueprint/jaxrs http://cxf.apache.org/schemas/blueprint/jaxrs.xsd
             http://cxf.apache.org/blueprint/core http://cxf.apache.org/schemas/blueprint/core.xsd
             ">

    <camelContext id="logContext1" xmlns="http://camel.apache.org/schema/blueprint">
        <restConfiguration bindingMode="auto" component="netty-http" contextPath="/rest" port="8182"/>

            <rest>
                  <description>Log rest service</description>

            <get uri="/log1">
                <to uri="log:?level=INFO"/>
            </get>
        </rest>
    </camelContext>

</blueprint>

Выходило не очень хорошо, но Apache CXF нам не подходил, как и чистый camel-http - стали искать возможности. В итоге вся проблема оказалась в используемом компоненте - стоило поменять netty-http на jetty и вуаля - всё работает. Надеюсь будет полезно - удачных интеграций!

06.07.2022