我是Spring Integration的新手,並嘗試使用TCP支持。我已經實現了一個非常基本的TCP服務器,它接受連接並只記錄來自客戶端的消息。這工作正常,但我堅持找出如何獲得客戶端連接的IP地址(服務器計算機有多個IP地址)的本地。看起來MessageHeader
只包含遠程地址地址。Spring Integration TCP:如何獲得本地IP地址
這裏是我的配置:
@EnableIntegration
@Configuration
public class SpringConfig {
@Bean
public MessageChannel requestChannel() {
return new DirectChannel();
}
@Bean
public AbstractServerConnectionFactory serverConnectionFactory() {
TcpNetServerConnectionFactory connectionFactory =
new TcpNetServerConnectionFactory(2000);
return connectionFactory;
}
@Bean
public TcpReceivingChannelAdapter tcpReceivingChannelAdapter() {
TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
adapter.setOutputChannel(requestChannel());
adapter.setConnectionFactory(serverConnectionFactory());
return adapter;
}
@Bean
public PrintService printService() {
return new PrintService();
}
}
而且我PrintService
:
@MessageEndpoint
public class PrintService {
@ServiceActivator(inputChannel = "requestChannel")
public String print(Message<byte[]> message) {
// How can I get the local IP address here?
}
}
編輯1
基本上,我想從java.net.Socket.getLocalAddress()
可有結果,我print(...)
功能。也許是這樣的:message.getHeaders().get(IpHeaders.LOCAL_IP_ADDRESS)
我也嘗試通過繼承TcpMessageMapper
的子類並覆蓋supplyCustomHeaders(...)
來添加自定義消息頭,但我找不到如何訪問底層套接字。
你願意分享一下你想看的東西以及從哪裏可以看到嗎?我們可能會認爲這是一個框架改進。 –
@ArtemBilan請參閱上面的編輯1。 – user2286693
請看我的答案。 –