为什么骆驼上下文无法解析 Spring boot fat jar 中的路由?
Why camel context unable to resolve route inside Spring boot fat jar?
这里是 spring boot fat jar camel 项目 结构:
/opt/java/spring/boot/fat/jar/camel/project/
├─ menus.json
├─ proxy.json
└─ chorke─boot─launch-1.0.00-SNAPSHOT.jar!
├─ META-INF/
├─ org/springframework/boot/loader/
├─ com/chorke/boot/launch/
├─ application.properties
├─ application.yml
├─ log4j.xml
└─ lib/
├─ annotations-2.0.0.jar
├─ camel-core-2.15.2.jar
├─ camel-hl7-2.15.2.jar
├─ camel-mina2-2.15.2.jar
├─ camel-spring-2.15.2.jar
├─ camel-spring-javaconfig-2.15.2.jar
├─ chorke-boot-jproxy-1.0.00-SNAPSHOT.jar!
│ ├─ META-INF/
│ │ ├─ camel/
│ │ │ └─ applicationContext-camel.xml
│ │ └─ property/
│ └─ com/chorke/boot/jproxy/
│
├─ chorke-boot-webapp-1.0.00-SNAPSHOT.jar
├─ chorke-comn-spring-1.0.00-SNAPSHOT.jar
├─ commons-collections-3.2.1.jar
├─ spring-context-4.1.6.RELEASE.jar
├─ spring-core-4.1.6.RELEASE.jar
├─ spring-webmvc-4.1.6.RELEASE.jar
├─ ..more..more..and..more..jar
├─ tomcat-embed-core-8.0.23.jar
└─ zuul-core-1.0.28.jar
这是路线:
package com.chorke.boot.jproxy.route;
import org.apache.camel.builder.RouteBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ProxyRoute extends RouteBuilder {
private static final Logger log =LoggerFactory.getLogger(ProxyRoute.class);
@Override
public void configure() throws Exception {
from("mina2:tcp://0.0.0.0:22210?codec=#hl7codec&sync=true").process(new Processor() {
public void process(Exchange exchange) throws Exception {
String body = exchange.getIn().getBody(String.class);
log.info("Port-Forwarded body:\n {}", body);
}
}).to("mina2:tcp://192.168.0.10:22210?codec=#hl7codec&sync=true").end();
from("mina2:tcp://0.0.0.0:22211?codec=#hl7codec&sync=true").process(new Processor() {
public void process(Exchange exchange) throws Exception {
String body = exchange.getIn().getBody(String.class);
log.info("Port-Forwarded body:\n {}", body);
}
}).to("mina2:tcp://192.168.0.11:22211?codec=#hl7codec&sync=true").end();
}
}
这是骆驼上下文配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:camel="http://camel.apache.org/schema/spring"
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.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
<bean id="properties" class="org.apache.camel.component.properties.PropertiesComponent">
<property name="locations">
<list>
<value>classpath:/META-INF/property/mina2.properties</value>
</list>
</property>
</bean>
<camel:camelContext id="camelContext">
<camel:packageScan>
<camel:package>com.chorke.boot</camel:package>
<camel:includes>**.route.*</camel:includes>
</camel:packageScan>
</camel:camelContext>
</beans>
这些骆驼路线在制作 fat jar 之前效果很好。当 Spring boot make fat jar 文件时,camel 包扫描器无法动态解析这些路由。是 spring 引导的错误还是 camel 包扫描器 关于 fat jar 文件的限制?
对于这种情况,您有什么解决方案吗?
spring fatjar 使用不同的类加载,因此不受支持/工作。
您可以使用 spring componentScan 而不是 packageScan,并使用 spring @Component
注释声明您的路由生成器 类。
请参阅 using contextScan 部分:http://camel.apache.org/spring.html
这里是 spring boot fat jar camel 项目 结构:
/opt/java/spring/boot/fat/jar/camel/project/
├─ menus.json
├─ proxy.json
└─ chorke─boot─launch-1.0.00-SNAPSHOT.jar!
├─ META-INF/
├─ org/springframework/boot/loader/
├─ com/chorke/boot/launch/
├─ application.properties
├─ application.yml
├─ log4j.xml
└─ lib/
├─ annotations-2.0.0.jar
├─ camel-core-2.15.2.jar
├─ camel-hl7-2.15.2.jar
├─ camel-mina2-2.15.2.jar
├─ camel-spring-2.15.2.jar
├─ camel-spring-javaconfig-2.15.2.jar
├─ chorke-boot-jproxy-1.0.00-SNAPSHOT.jar!
│ ├─ META-INF/
│ │ ├─ camel/
│ │ │ └─ applicationContext-camel.xml
│ │ └─ property/
│ └─ com/chorke/boot/jproxy/
│
├─ chorke-boot-webapp-1.0.00-SNAPSHOT.jar
├─ chorke-comn-spring-1.0.00-SNAPSHOT.jar
├─ commons-collections-3.2.1.jar
├─ spring-context-4.1.6.RELEASE.jar
├─ spring-core-4.1.6.RELEASE.jar
├─ spring-webmvc-4.1.6.RELEASE.jar
├─ ..more..more..and..more..jar
├─ tomcat-embed-core-8.0.23.jar
└─ zuul-core-1.0.28.jar
这是路线:
package com.chorke.boot.jproxy.route;
import org.apache.camel.builder.RouteBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ProxyRoute extends RouteBuilder {
private static final Logger log =LoggerFactory.getLogger(ProxyRoute.class);
@Override
public void configure() throws Exception {
from("mina2:tcp://0.0.0.0:22210?codec=#hl7codec&sync=true").process(new Processor() {
public void process(Exchange exchange) throws Exception {
String body = exchange.getIn().getBody(String.class);
log.info("Port-Forwarded body:\n {}", body);
}
}).to("mina2:tcp://192.168.0.10:22210?codec=#hl7codec&sync=true").end();
from("mina2:tcp://0.0.0.0:22211?codec=#hl7codec&sync=true").process(new Processor() {
public void process(Exchange exchange) throws Exception {
String body = exchange.getIn().getBody(String.class);
log.info("Port-Forwarded body:\n {}", body);
}
}).to("mina2:tcp://192.168.0.11:22211?codec=#hl7codec&sync=true").end();
}
}
这是骆驼上下文配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:camel="http://camel.apache.org/schema/spring"
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.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
<bean id="properties" class="org.apache.camel.component.properties.PropertiesComponent">
<property name="locations">
<list>
<value>classpath:/META-INF/property/mina2.properties</value>
</list>
</property>
</bean>
<camel:camelContext id="camelContext">
<camel:packageScan>
<camel:package>com.chorke.boot</camel:package>
<camel:includes>**.route.*</camel:includes>
</camel:packageScan>
</camel:camelContext>
</beans>
这些骆驼路线在制作 fat jar 之前效果很好。当 Spring boot make fat jar 文件时,camel 包扫描器无法动态解析这些路由。是 spring 引导的错误还是 camel 包扫描器 关于 fat jar 文件的限制?
对于这种情况,您有什么解决方案吗?
spring fatjar 使用不同的类加载,因此不受支持/工作。
您可以使用 spring componentScan 而不是 packageScan,并使用 spring @Component
注释声明您的路由生成器 类。
请参阅 using contextScan 部分:http://camel.apache.org/spring.html