如何通过 Apache Camel 配置端口转发路由?
How to configure route for port forwarding by Apache Camel?
这里是编解码器和数据格式配置:
package com.chorke.boot.jproxy.config;
import org.apache.camel.component.hl7.HL7DataFormat;
import org.apache.camel.component.hl7.HL7MLLPCodec;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
@Configuration
@ImportResource({"classpath*:/META-INF/camel/applicationContext-camel.xml"})
public class ApacheCamelConfig {
@SuppressWarnings("unused")
private static final Logger log = LoggerFactory.getLogger(ApacheCamelConfig.class);
@Bean
public HL7MLLPCodec hl7codec() {
HL7MLLPCodec hl7codec = new HL7MLLPCodec();
hl7codec.setCharset("UTF-8");
return hl7codec;
}
@Bean
public HL7DataFormat hl7Format() {
HL7DataFormat hl7Format = new HL7DataFormat();
return hl7Format;
}
}
这是端口转发路由:
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();
}
}
路线总结为:
============================================
request forwarded
tcp ip:port tcp ip:port
============================================
0.0.0.0:22210 192.168.0.10:22210
0.0.0.0:22211 192.168.0.11:22211
============================================
它工作正常,它是 MLLP
的特定协议。但我们的目标是路由任何请求,而不考虑它们的协议。假设它可以处理任何类型的请求,不限于 HTTP
、REST
、SOAP
、MLLP
、SMTP
、FTP
、 SMB
等等。请你帮我们配置端口转发路由,不管他们的协议如何。
Apache Camel 中没有对协议检测和路由端口转发的开箱即用支持。您最好找到一些可以做到这一点的现有产品。
这里是编解码器和数据格式配置:
package com.chorke.boot.jproxy.config;
import org.apache.camel.component.hl7.HL7DataFormat;
import org.apache.camel.component.hl7.HL7MLLPCodec;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
@Configuration
@ImportResource({"classpath*:/META-INF/camel/applicationContext-camel.xml"})
public class ApacheCamelConfig {
@SuppressWarnings("unused")
private static final Logger log = LoggerFactory.getLogger(ApacheCamelConfig.class);
@Bean
public HL7MLLPCodec hl7codec() {
HL7MLLPCodec hl7codec = new HL7MLLPCodec();
hl7codec.setCharset("UTF-8");
return hl7codec;
}
@Bean
public HL7DataFormat hl7Format() {
HL7DataFormat hl7Format = new HL7DataFormat();
return hl7Format;
}
}
这是端口转发路由:
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();
}
}
路线总结为:
============================================
request forwarded
tcp ip:port tcp ip:port
============================================
0.0.0.0:22210 192.168.0.10:22210
0.0.0.0:22211 192.168.0.11:22211
============================================
它工作正常,它是 MLLP
的特定协议。但我们的目标是路由任何请求,而不考虑它们的协议。假设它可以处理任何类型的请求,不限于 HTTP
、REST
、SOAP
、MLLP
、SMTP
、FTP
、 SMB
等等。请你帮我们配置端口转发路由,不管他们的协议如何。
Apache Camel 中没有对协议检测和路由端口转发的开箱即用支持。您最好找到一些可以做到这一点的现有产品。