Hemin
Hemin

Reputation: 752

How to implement sticky session in spring-integration tcp gateway?

I am currently working on a tcp socket server using spring integration. so far i have below mentioned code. But there are possibilities of load balance multiple nodes of this socket server. For that I need to have stick sessions of socket connections context which can be shared between nodes. Can anyone suggest possible solution?

@Bean
    public IntegrationFlow listnerServerFlow(
            TcpNetServerConnectionFactory serverConnectionFactory,
            DeviceListenerService deviceListenerService) {
        return IntegrationFlows
                .from(Tcp.inboundGateway(serverConnectionFactory))
                .handle(deviceListenerService::processRequest)
                .get();
    }

@Bean
    public TcpNetServerConnectionFactory serverConnectionFactory() {
        TcpNetServerConnectionFactory connectionFactory = new TcpNetServerConnectionFactory(2424);
        connectionFactory.setSerializer(new ByteArrayCrLfSerializer());
        connectionFactory.setDeserializer(new ByteArrayCrLfSerializer());
        connectionFactory.setSingleUse(false);
        connectionFactory.setSoKeepAlive(true);
        return connectionFactory;
    }

Upvotes: 1

Views: 508

Answers (2)

Gary Russell
Gary Russell

Reputation: 174664

With this setting

connectionFactory.setSingleUse(false);

there is only one shared connection so it is always sticky (to one server).

You can use a ThreadAffinityConnectionFactory to bind a connection to each calling thread.

Upvotes: 1

Max Peng
Max Peng

Reputation: 3227

In a common case, you can add a unique identifier in the request and store the session data in a separate Redis.

No matter which node is handling the request, the node can retrieve the session data from the Redis given the identifier in the request.

Hopefully, that can help you work out a solution.

Upvotes: 2

Related Questions