简体   繁体   中英

Hazelcast Spring Session pass node instance

Trying to configure Spring Session using Hazelcast. It works okay out of the box using this doc - but it uses default hazelcast node. In my case I am running several nodes(those are in different clusters) on the same JVM, and I need use particular hazelcast instance to store my session. I haven't find much information, how to configure it(pass hazelcast instance name\\or instance itself).

Will appreciate any help.

Session storage is sharded across the available nodes. You can't chose which node hosts any particular session. Further, if Hazelcast needs to rebalance the shards, sessions may get moved from one node to another.

The instance-name parameter just dictates whether to try to find an existing connection to the cluster or whether to start a new connnection.

Perhaps the question is why do you need to control which node hosts which session?

Next code let me to configure Spring Session to use particular node, instead of default one. Only question I have is how to configure session time to live. hazelcastSessionRepository.setDefaultMaxInactiveInterval(3600) sets only max inactive time, not time to live.

@Configuration
@EnableSpringHttpSession
public class HazelcastSessionConfig {

    @Bean//default
    public HazelcastInstance hazelcastInstance() {
        Config config = new Config();
        config.setInstanceName("cache-node");
        config.getGroupConfig().setName("cluster-1");
        return Hazelcast.newHazelcastInstance(config);
    }

    @Bean//Node I need to use
    public HazelcastInstance hazelcastSessionInstance() {

        Config config = new Config();
        config.setInstanceName("session-node");
        config.getGroupConfig().setName("cluster-2");
        MapAttributeConfig attributeConfig = new MapAttributeConfig()
                .setName(HazelcastSessionRepository.PRINCIPAL_NAME_ATTRIBUTE)
                .setExtractor(PrincipalNameExtractor.class.getName());


        final MapConfig mapConfig = config.getMapConfig(HazelcastSessionRepository.DEFAULT_SESSION_MAP_NAME);
        mapConfig
                .addMapAttributeConfig(attributeConfig)
                .addMapIndexConfig(new MapIndexConfig(
                        HazelcastSessionRepository.PRINCIPAL_NAME_ATTRIBUTE, false));

        return Hazelcast.newHazelcastInstance(config);
    }


    @Bean//Pass node here
    public HazelcastSessionRepository sessionRepository(HazelcastInstance hazelcastSessionInstance) {
        final HazelcastSessionRepository hazelcastSessionRepository = new HazelcastSessionRepository(hazelcastSessionInstance);
        hazelcastSessionRepository.setDefaultMaxInactiveInterval(3600);
        return hazelcastSessionRepository;
    }

}

Next code let me to configure Spring Session to use particular node, instead of default one. Only question I have is how to configure session time to live.

Have you tried to set the TTL seconds of the map config for session-node instance? If you ensure that the sessions are going to be stored in cluster-2 then setting the TTL for the instance can solve your problem:

@Bean
public HazelcastInstance hazelcastSessionInstance() {

    Config config = new Config();
    config.setInstanceName("session-node");
    config.getGroupConfig().setName("cluster-2");
    MapAttributeConfig attributeConfig = new MapAttributeConfig()
            .setName(HazelcastSessionRepository.PRINCIPAL_NAME_ATTRIBUTE)
            .setExtractor(PrincipalNameExtractor.class.getName());


    final MapConfig mapConfig = config.getMapConfig(HazelcastSessionRepository.DEFAULT_SESSION_MAP_NAME);
    mapConfig
            .setTimeToLiveSeconds(300)
            .addMapAttributeConfig(attributeConfig)
            .addMapIndexConfig(new MapIndexConfig(
                    HazelcastSessionRepository.PRINCIPAL_NAME_ATTRIBUTE, false));

    return Hazelcast.newHazelcastInstance(config);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM