Spring 使用动态主机和端口的 TCP 套接字客户端集成
Spring Integration for TCP Socket Client using Dynamic Host and Port
我有一个使用 Spring 与硬编码主机名和端口集成的 TCPSocketClient 工作示例。
如何修改此示例以接受要动态传递的 localhost
和端口 5877
?
即是否可以调用 ExchangeService.exchange(hostname, port, request)
之类的交换方法而不是 ExchangeService.exchange(request)
如果是,这些参数如何应用于 client
bean?
@Configuration
public class AppConfig {
@Bean
public IntegrationFlow client() {
return IntegrationFlows.from(ApiGateway.class).handle(
Tcp.outboundGateway(
Tcp.netClient("localhost", 5877)
.serializer(codec())
.deserializer(codec())
).remoteTimeout(10000)
)
.transform(Transformers.objectToString())
.get();
}
@Bean
public ByteArrayCrLfSerializer codec() {
ByteArrayCrLfSerializer crLfSerializer = new ByteArrayCrLfSerializer();
crLfSerializer.setMaxMessageSize(204800000);
return crLfSerializer;
}
@Bean
@DependsOn("client")
public ExchangeService exchangeService(ApiGateway apiGateway) {
return new ExchangeServiceImpl(apiGateway);
}
}
public interface ApiGateway {
String exchange(String out);
}
public interface ExchangeService {
public String exchange(String request);
}
@Service
public class ExchangeServiceImpl implements ExchangeService {
private ApiGateway apiGateway;
@Autowired
public ExchangeServiceImpl(ApiGateway apiGateway) {
this.apiGateway=apiGateway;
}
@Override
public String exchange(String request) {
String response = null;
try {
response = apiGateway.exchange(request);
} catch (Exception e) {
throw e;
}
return response;
}
}
对于动态处理,您可以考虑使用来自 Spring Integration Java DSL 的动态流功能:https://docs.spring.io/spring-integration/docs/current/reference/html/#java-dsl-runtime-flows
因此,每当您收到带有这些参数的请求时,您都会即时创建一个 IntegrationFlow
并将其注册到 IntegrationFlowContext
。坦率地说,我们在您的 TCP 用例的文档中有准确的示例:
IntegrationFlow flow = f -> f
.handle(Tcp.outboundGateway(Tcp.netClient("localhost", this.server1.getPort())
.serializer(TcpCodecs.crlf())
.deserializer(TcpCodecs.lengthHeader1())
.id("client1"))
.remoteTimeout(m -> 5000))
.transform(Transformers.objectToString());
IntegrationFlowRegistration theFlow = this.flowContext.registration(flow).register();
我有一个使用 Spring 与硬编码主机名和端口集成的 TCPSocketClient 工作示例。
如何修改此示例以接受要动态传递的 localhost
和端口 5877
?
即是否可以调用 ExchangeService.exchange(hostname, port, request)
之类的交换方法而不是 ExchangeService.exchange(request)
如果是,这些参数如何应用于 client
bean?
@Configuration
public class AppConfig {
@Bean
public IntegrationFlow client() {
return IntegrationFlows.from(ApiGateway.class).handle(
Tcp.outboundGateway(
Tcp.netClient("localhost", 5877)
.serializer(codec())
.deserializer(codec())
).remoteTimeout(10000)
)
.transform(Transformers.objectToString())
.get();
}
@Bean
public ByteArrayCrLfSerializer codec() {
ByteArrayCrLfSerializer crLfSerializer = new ByteArrayCrLfSerializer();
crLfSerializer.setMaxMessageSize(204800000);
return crLfSerializer;
}
@Bean
@DependsOn("client")
public ExchangeService exchangeService(ApiGateway apiGateway) {
return new ExchangeServiceImpl(apiGateway);
}
}
public interface ApiGateway {
String exchange(String out);
}
public interface ExchangeService {
public String exchange(String request);
}
@Service
public class ExchangeServiceImpl implements ExchangeService {
private ApiGateway apiGateway;
@Autowired
public ExchangeServiceImpl(ApiGateway apiGateway) {
this.apiGateway=apiGateway;
}
@Override
public String exchange(String request) {
String response = null;
try {
response = apiGateway.exchange(request);
} catch (Exception e) {
throw e;
}
return response;
}
}
对于动态处理,您可以考虑使用来自 Spring Integration Java DSL 的动态流功能:https://docs.spring.io/spring-integration/docs/current/reference/html/#java-dsl-runtime-flows
因此,每当您收到带有这些参数的请求时,您都会即时创建一个 IntegrationFlow
并将其注册到 IntegrationFlowContext
。坦率地说,我们在您的 TCP 用例的文档中有准确的示例:
IntegrationFlow flow = f -> f
.handle(Tcp.outboundGateway(Tcp.netClient("localhost", this.server1.getPort())
.serializer(TcpCodecs.crlf())
.deserializer(TcpCodecs.lengthHeader1())
.id("client1"))
.remoteTimeout(m -> 5000))
.transform(Transformers.objectToString());
IntegrationFlowRegistration theFlow = this.flowContext.registration(flow).register();