How to configure Hazelcast for session caching using spring while limiting it to a set of nodes?

前端 未结 3 1268
花落未央
花落未央 2021-01-03 11:55

I need to set up Hazelcast session caching using Spring. Using the configuration from the hazelcast docs is simple. However, its insufficient. I need to use a different c

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-03 12:23

    Thank you for the suggested answer. However, I think I've solved this using the following configuration. I would appreciate any feedback from anyone about this configuration.

    My approach:

    1) Establish the instance with a spring configuration. 2) Enhance the instance with a minimally configured hazelcast.xml file with the Web filter configuration. Note that mulitcast and tcp-ip joiners are false.

    web.xml:

    
    
    
    
        
            contextConfigLocation
            
                /WEB-INF/spring/servlet-context.xml,
                /WEB-INF/spring/root-context.xml,
                ....
                /WEB-INF/spring/hazelcastContext.xml
            
        
    
    
    
    
        org.springframework.web.context.ContextLoaderListener
    
    
    ....
    
    
        hazelcast-filter
        com.hazelcast.web.WebFilter
        
        
            map-name
            my-sessions
        
        
        
            sticky-session
            true
        
        
        
            cookie-name
            hazelcast.sessionId
        
        
        
            cookie-domain
            .mycompany.com
        
        
        
            cookie-secure
            false
        
        
        
            cookie-http-only
            false
        
        
        
            debug
            true
        
        
    
        
            config-location
            /WEB-INF/classes/hazelcast.xml
        
    
        `enter code here`
        
            instance-name
            myapp
        
        
        
            use-client
            false
        
        
        
            client-config-location
            /WEB-INF/classes/hazelcast-client.properties
        
            
            
                shutdown-on-destroy
                true
            
        
        
            hazelcast-filter
            /*
            FORWARD
            INCLUDE
            REQUEST
        
    
    ...
    
    
    

    hazelcast.xml (copied from hazelcast.xml inside jar file)...

    
    
    ...
    
        5701
        
            
            0
        
        
            
                224.2.2.3
                54327
            
            
                127.0.0.1
            
     ...
        
     ...
    
    
    ...  
    
    
    

    Spring config....

    
    
    
    
    
    
    
        
            
            ${hz.instance.name}
            
            
            
            
            
            
                
                    
                    
                        ${hz.members}
                    
                
            
            
            
        
    
    

    Property file....

    #-- Hazelcast properties.
    hz.instance.name = myapp
    hz.group.name = CERT
    hz.group.password = cert
    hz.management.center.enabled = true
    hz.management.center.url = http://127.0.0.1:8080/mancenter
    hz.network.port = 5701
    hz.network.port.auto.increment = true
    hz.multicast.enabled = true
    hz.tcp.ip.enabled = false
    hz.members = 127.0.0.1
    hz.executor.service.core.pool.size = 2
    hz.executor.service.max.pool.size = 30
    hz.executor.service.keep.alive.seconds = 30
    hz.map.backup.count=2
    hz.map.max.size=0
    hz.map.eviction.percentage=30
    hz.map.read.backup.data=true
    hz.map.cache.value=true
    hz.map.eviction.policy=NONE
    hz.map.merge.policy=hz.ADD_NEW_ENTRY
    

    Root Context.xml

    
    
    
    ...
    
        
            
                
                    
                
            
        
    
    ...
    
        
    

    Tomcat Config...

    
    ...
    
        
                        
            
    ...
    
    
    

    Tomcat output (Note: This is a restart scenario of a two node hazelcast group. In this scenario, Node 1 is restarted. The output of Node 2 shows the drop of node 1 from the group and then its return to the group).

    Node 1 of Group CERT

    Nov 19, 2013 4:27:56 PM com.hazelcast.impl.AddressPicker
    INFO: Prefer IPv4 stack is true.
    Nov 19, 2013 4:27:56 PM com.hazelcast.impl.AddressPicker
    INFO: Picked Address[10.23.43.13]:5701, using socket ServerSocket[addr=/0:0:0:0:0:0:0:0,localport=5701], bind any local is true
    Nov 19, 2013 4:27:57 PM com.hazelcast.system
    INFO: [10.23.43.13]:5701 [CERT] Hazelcast Community Edition 2.4 (20121017) starting at Address[10.23.43.13]:5701
    Nov 19, 2013 4:27:57 PM com.hazelcast.system
    INFO: [10.23.43.13]:5701 [CERT] Copyright (C) 2008-2012 Hazelcast.com
    Nov 19, 2013 4:27:57 PM com.hazelcast.impl.LifecycleServiceImpl
    INFO: [10.23.43.13]:5701 [CERT] Address[10.23.43.13]:5701 is STARTING
    Nov 19, 2013 4:27:57 PM com.hazelcast.impl.Node
    INFO: [10.23.43.13]:5701 [CERT] ** setting master address to Address[10.23.43.14]:5701
    Nov 19, 2013 4:27:57 PM com.hazelcast.impl.MulticastJoiner
    INFO: [10.23.43.13]:5701 [CERT] Connecting to master node: Address[10.23.43.14]:5701
    Nov 19, 2013 4:27:57 PM com.hazelcast.nio.ConnectionManager
    INFO: [10.23.43.13]:5701 [CERT] 54106 accepted socket connection from /10.23.43.14:5701
    Nov 19, 2013 4:27:57 PM com.hazelcast.impl.Node
    INFO: [10.23.43.13]:5701 [CERT] ** setting master address to Address[10.23.43.14]:5701
    Nov 19, 2013 4:27:57 PM com.hazelcast.impl.Node
    INFO: [10.23.43.13]:5701 [CERT] ** setting master address to Address[10.23.43.14]:5701
    Nov 19, 2013 4:27:58 PM com.hazelcast.impl.Node
    INFO: [10.23.43.13]:5701 [CERT] ** setting master address to Address[10.23.43.14]:5701
    Nov 19, 2013 4:27:58 PM com.hazelcast.impl.Node
    INFO: [10.23.43.13]:5701 [CERT] ** setting master address to Address[10.23.43.14]:5701
    Nov 19, 2013 4:27:59 PM com.hazelcast.impl.Node
    INFO: [10.23.43.13]:5701 [CERT] ** setting master address to Address[10.23.43.14]:5701
    Nov 19, 2013 4:27:59 PM com.hazelcast.impl.Node
    INFO: [10.23.43.13]:5701 [CERT] ** setting master address to Address[10.23.43.14]:5701
    Nov 19, 2013 4:28:00 PM com.hazelcast.impl.Node
    INFO: [10.23.43.13]:5701 [CERT] ** setting master address to Address[10.23.43.14]:5701
    Nov 19, 2013 4:28:00 PM com.hazelcast.impl.Node
    INFO: [10.23.43.13]:5701 [CERT] ** setting master address to Address[10.23.43.14]:5701
    Nov 19, 2013 4:28:01 PM com.hazelcast.impl.Node
    INFO: [10.23.43.13]:5701 [CERT] ** setting master address to Address[10.23.43.14]:5701
    Nov 19, 2013 4:28:01 PM com.hazelcast.impl.Node
    INFO: [10.23.43.13]:5701 [CERT] ** setting master address to Address[10.23.43.14]:5701
    Nov 19, 2013 4:28:02 PM com.hazelcast.impl.Node
    INFO: [10.23.43.13]:5701 [CERT] ** setting master address to Address[10.23.43.14]:5701
    Nov 19, 2013 4:28:02 PM com.hazelcast.impl.Node
    INFO: [10.23.43.13]:5701 [CERT] ** setting master address to Address[10.23.43.14]:5701
    Nov 19, 2013 4:28:02 PM com.hazelcast.cluster.ClusterManager
    INFO: [10.23.43.13]:5701 [CERT]
    
    Members [2] {
            Member [10.23.43.14]:5701
            Member [10.23.43.13]:5701 this
    }
    
    Nov 19, 2013 4:28:04 PM com.hazelcast.impl.LifecycleServiceImpl
    INFO: [10.23.43.13]:5701 [CERT] Address[10.23.43.13]:5701 is STARTED
    Nov 19, 2013 4:28:04 PM com.hazelcast.impl.management.ManagementCenterService
    INFO: [10.23.43.13]:5701 [CERT] Hazelcast will connect to Management Center on address:     http://localhost:8080/mancenter/
    Nov 19, 2013 4:28:04 PM com.hazelcast.config.UrlXmlConfig
    INFO: Configuring Hazelcast from 'jndi:/localhost/pwc-ui/WEB-INF/classes/hazelcast.xml'.
    Nov 19, 2013 4:28:04 PM com.hazelcast.web.WebFilter
    INFO: sticky:true, debug: true, shutdown-on-destroy: true, map-name: my-sessions
    Nov 19, 2013 4:28:05 PM org.apache.catalina.startup.HostConfig deployDescriptor
    

    Node 2 of Group CERT (note the drop and re-add)

    Nov 19, 2013 4:27:11 PM com.hazelcast.nio.Connection
    INFO: [10.23.43.14]:5701 [CERT] Connection [Address[10.23.43.13]:5701] lost. Reason: java.io.IOException[Connection reset by peer]
    Nov 19, 2013 4:27:11 PM com.hazelcast.nio.ReadHandler
    WARNING: [10.23.43.14]:5701 [CERT] hz.pwc.IO.thread-1 Closing socket to endpoint Address[10.23.43.13]:5701, Cause:java.io.IOException: Connection reset by peer
    Nov 19, 2013 4:27:12 PM com.hazelcast.nio.SocketConnector
    INFO: [10.23.43.14]:5701 [CERT] Could not connect to: /10.23.43.13:5701. Reason: ConnectException[Connection refused]
    Nov 19, 2013 4:27:13 PM com.hazelcast.nio.SocketConnector
    INFO: [10.23.43.14]:5701 [CERT] Could not connect to: /10.23.43.13:5701. Reason: ConnectException[Connection refused]
    Nov 19, 2013 4:27:14 PM com.hazelcast.nio.SocketConnector
    INFO: [10.23.43.14]:5701 [CERT] Could not connect to: /10.23.43.13:5701. Reason: ConnectException[Connection refused]
    Nov 19, 2013 4:27:14 PM com.hazelcast.nio.ConnectionMonitor
    WARNING: [10.23.43.14]:5701 [CERT] Removing connection to endpoint Address[10.23.43.13]:5701 Cause => java.net.ConnectException {Connection refused}, Error-Count: 5
    Nov 19, 2013 4:27:14 PM com.hazelcast.cluster.ClusterManager
    INFO: [10.23.43.14]:5701 [CERT] Removing Address Address[10.23.43.13]:5701
    Nov 19, 2013 4:27:14 PM com.hazelcast.impl.PartitionManager
    INFO: [10.23.43.14]:5701 [CERT] Starting to send partition replica diffs...true
    Nov 19, 2013 4:27:14 PM com.hazelcast.cluster.ClusterManager
    INFO: [10.23.43.14]:5701 [CERT]
    
    Members [1] {
            Member [10.23.43.14]:5701 this
    }
    
    Nov 19, 2013 4:27:18 PM com.hazelcast.impl.PartitionManager
    INFO: [10.23.43.14]:5701 [CERT] Total 0 partition replica diffs have been processed.
    Nov 19, 2013 4:27:18 PM com.hazelcast.impl.PartitionManager
    INFO: [10.23.43.14]:5701 [CERT] Re-partitioning cluster data... Immediate-Tasks: 0, Scheduled-Tasks: 0
    Nov 19, 2013 4:27:57 PM com.hazelcast.nio.SocketAcceptor
    INFO: [10.23.43.14]:5701 [CERT] 5701 is accepting socket connection from /10.23.43.13:54106
    Nov 19, 2013 4:27:57 PM com.hazelcast.nio.ConnectionManager
    INFO: [10.23.43.14]:5701 [CERT] 5701 accepted socket connection from /10.23.43.13:54106
    Nov 19, 2013 4:27:57 PM com.hazelcast.web.WebFilter
    INFO: Created new session with id: HZ650FDF62693F45A99AC0C30BBD8840B0
    Nov 19, 2013 4:27:57 PM com.hazelcast.web.WebFilter
    INFO: 195 is sessions.size and originalSessions.size: 195
    Nov 19, 2013 4:27:57 PM com.hazelcast.web.WebFilter
    INFO: PUTTING SESSION HZ650FDF62693F45A99AC0C30BBD8840B0
    Nov 19, 2013 4:28:02 PM com.hazelcast.cluster.ClusterManager
    INFO: [10.23.43.14]:5701 [CERT]
    
    Members [2] {
            Member [10.23.43.14]:5701 this
            Member [10.23.43.13]:5701
    }
    
    Nov 19, 2013 4:28:02 PM com.hazelcast.impl.PartitionManager
    INFO: [10.23.43.14]:5701 [CERT] Re-partitioning cluster data... Immediate-Tasks: 271, Scheduled-Tasks: 0
    Nov 19, 2013 4:28:24 PM com.hazelcast.web.WebFilter
    INFO: Created new session with id: HZAD50E5F483CC448C9FA7CB66D65848BB
    Nov 19, 2013 4:28:24 PM com.hazelcast.web.WebFilter
    INFO: 196 is sessions.size and originalSessions.size: 196
    Nov 19, 2013 4:28:24 PM com.hazelcast.web.WebFilter
    INFO: PUTTING SESSION HZAD50E5F483CC448C9FA7CB66D65848BB
    Nov 19, 2013 4:28:24 PM com.hazelcast.web.WebFilter
    INFO: Request is instance of RequestWrapper! Continue...
    Nov 19, 2013 4:28:24 PM com.hazelcast.web.WebFilter
    INFO: Request is instance of RequestWrapper! Continue...
    Nov 19, 2013 4:28:24 PM com.hazelcast.web.WebFilter
    INFO: Request is instance of RequestWrapper! Continue...
    Nov 19, 2013 4:28:24 PM com.hazelcast.web.WebFilter
    INFO: Request is instance of RequestWrapper! Continue...
    Nov 19, 2013 4:28:50 PM com.hazelcast.web.WebFilter
    INFO: Created new session with id: HZC9553A4C330044CA8A0C20549EE23BF0
    Nov 19, 2013 4:28:50 PM com.hazelcast.web.WebFilter
    INFO: 197 is sessions.size and originalSessions.size: 197
    Nov 19, 2013 4:28:50 PM com.hazelcast.web.WebFilter
    INFO: PUTTING SESSION HZC9553A4C330044CA8A0C20549EE23BF0
    Nov 19, 2013 4:28:50 PM com.hazelcast.web.WebFilter
    INFO: Request is instance of RequestWrapper! Continue...
    Nov 19, 2013 4:28:50 PM com.hazelcast.web.WebFilter
    INFO: Request is instance of RequestWrapper! Continue...
    Nov 19, 2013 4:28:50 PM com.hazelcast.web.WebFilter
    INFO: Request is instance of RequestWrapper! Continue...
    Nov 19, 2013 4:28:50 PM com.hazelcast.web.WebFilter
    INFO: Request is instance of RequestWrapper! Continue...
    10069.275: [GC [PSYoungGen: 693173K->3458K(695488K)] 877908K->188718K(2093632K), 0.0224650 secs] [Times: user=0.04 sys=0.00, real=0.02 secs]
    Nov 19, 2013 4:29:18 PM com.hazelcast.web.WebFilter
    INFO: Created new session with id: HZE46365454C2C45F98A7947AC40E404BB
    Nov 19, 2013 4:29:18 PM com.hazelcast.web.WebFilter
    INFO: 198 is sessions.size and originalSessions.size: 198
    Nov 19, 2013 4:29:18 PM com.hazelcast.web.WebFilter
    INFO: PUTTING SESSION HZE46365454C2C45F98A7947AC40E404BB
    

提交回复
热议问题