如何将Hazelcast配置为使用spring进行会话缓存,同时将其限制为一组节点?

编程入门 行业动态 更新时间:2024-10-10 19:24:21
本文介绍了如何将Hazelcast配置为使用spring进行会话缓存,同时将其限制为一组节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要使用Spring设置Hazelcast会话缓存.使用hazelcast文档中的配置非常简单.但是,其不足.我需要为我的每个环境(DEV,QA,PROD)使用不同的配置文件.另外(理想情况下),我想使用Spring bean的几个属性,这些属性将在spring容器的初始化期间设置.根据hazelcast文档,我需要为每个环境设置一个组.像这样:

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 configuration file for each one of my environments (DEV, QA, PROD). Alternatively (Ideally), I would like to use several properties from a spring bean that would be set during the initialization of the spring container. According to the hazelcast documentation, all I need to do set a group for each of my environments. like so:

<hazelcast> <group> <name>dev</name> <password>dev-pass</password> </group> ... </hazelcast>

作为奖励,我希望缓存是用于会话和应用程序级对象(地图,队列等)的单个缓存.

As a bonus, I would like the cache to be a single cache used for both sessions and application level objects (Maps, Queues, etc).

任何人都可以分享一个有关如何做到这一点的例子吗?谢谢您的帮助.

Could anyone share an example on how they would do this? Thank you for your help.

推荐答案

谢谢您的建议答案.但是,我认为我已经使用以下配置解决了此问题.任何人都对此配置有任何反馈意见,我将不胜感激.

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.

我的方法:

1)使用spring配置建立实例. 2)使用带有Web过滤器配置的最低配置的hazelcast.xml文件增强实例.请注意,mulitcast和tcp-ip连接器是错误的.

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:

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="www.w3/2001/XMLSchema-instance" xmlns="java.sun/xml/ns/javaee" xmlns:web="java.sun/xml/ns/javaee/web-app_3_0.xsd" xsi:schemaLocation="java.sun/xml/ns/javaee java.sun/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <!-- The definition of the Root Spring Container shared by all Servlets and Filters --> <context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/spring/servlet-context.xml, /WEB-INF/spring/root-context.xml, .... /WEB-INF/spring/hazelcastContext.xml </param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> .... <filter> <filter-name>hazelcast-filter</filter-name> <filter-class>com.hazelcast.web.WebFilter</filter-class> <!-- Name of the distributed map storing your web session objects --> <init-param> <param-name>map-name</param-name> <param-value>my-sessions</param-value> </init-param> <!-- How is your load-balancer configured? stick-session means all requests of a session is routed to the node where the session is first created. This is excellent for performance. If sticky-session is set to false, when a session is updated on a node, entry for this session on all other nodes is invalidated. You have to know how your load-balancer is configured before setting this parameter. Default is true. --> <init-param> <param-name>sticky-session</param-name> <param-value>true</param-value> </init-param> <!-- Name of session id cookie --> <init-param> <param-name>cookie-name</param-name> <param-value>hazelcast.sessionId</param-value> </init-param> <!-- Domain of session id cookie. Default is based on incoming request. --> <init-param> <param-name>cookie-domain</param-name> <param-value>.mycompany</param-value> </init-param> <!-- Should cookie only be sent using a secure protocol? Default is false. --> <init-param> <param-name>cookie-secure</param-name> <param-value>false</param-value> </init-param> <!-- Should HttpOnly attribute be set on cookie ? Default is false. --> <init-param> <param-name>cookie-http-only</param-name> <param-value>false</param-value> </init-param> <!-- Are you debugging? Default is false. --> <init-param> <param-name>debug</param-name> <param-value>true</param-value> </init-param> <!-- Configuration xml location; * as servlet resource OR * as classpath resource OR * as URL Default is one of hazelcast-default.xml or hazelcast.xml in classpath. --> <init-param> <param-name>config-location</param-name> <param-value>/WEB-INF/classes/hazelcast.xml</param-value> </init-param> <!-- Do you want to use an existing HazelcastInstance? Default is null. -->`enter code here` <init-param> <param-name>instance-name</param-name> <param-value>myapp</param-value> </init-param> <!-- Do you want to connect as a client to an existing cluster? Default is false. --> <init-param> <param-name>use-client</param-name> <param-value>false</param-value> </init-param> <!-- Client configuration location; * as servlet resource OR * as classpath resource OR * as URL Default is null. --> <init-param> <param-name>client-config-location</param-name> <param-value>/WEB-INF/classes/hazelcast-client.properties</param-value> </init-param> <!-- Do you want to shutdown HazelcastInstance during web application undeploy process? Default is true. --> <init-param> <param-name>shutdown-on-destroy</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>hazelcast-filter</filter-name> <url-pattern>/*</url-pattern> <dispatcher>FORWARD</dispatcher> <dispatcher>INCLUDE</dispatcher> <dispatcher>REQUEST</dispatcher> </filter-mapping> ... </web-app>

hazelcast.xml(从jar文件中的hazelcast.xml复制)...

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

<hazelcast xsi:schemaLocation="www.hazelcast/schema/config hazelcast-config-2.4.xsd" xmlns="www.hazelcast/schema/config" xmlns:xsi="www.w3/2001/XMLSchema-instance"> ... <network> <port auto-increment="true">5701</port> <outbound-ports> <!-- Allowed port range when connecting to other nodes. 0 or * means use system provided port. --> <ports>0</ports> </outbound-ports> <join> <multicast enabled="false"> <multicast-group>224.2.2.3</multicast-group> <multicast-port>54327</multicast-port> </multicast> <tcp-ip enabled="false"> <interface>127.0.0.1</interface> </tcp-ip> ... </join> ... </network> ... </hazelcast>

Spring config ....

Spring config....

<beans xmlns="www.springframework/schema/beans" xmlns:xsi="www.w3/2001/XMLSchema-instance" xmlns:hz="www.hazelcast/schema/spring" xsi:schemaLocation="www.springframework/schema/beans www.springframework/schema/beans/spring-beans-3.0.xsd www.hazelcast/schema/spring www.hazelcast/schema/spring/hazelcast-spring-2.4.xsd"> <bean id="hazelcast" class="com.hazelcast.core.Hazelcast" /> <!-- Hazelcast Instance configuration --> <hz:hazelcast id="myapp"> <hz:config> <!-- Hazelcast Instance Name --> <hz:instance-name>${hz.instance.name}</hz:instance-name> <!-- Hazelcast Group Name and Password --> <hz:group name="${hz.group.name}" password="${hz.group.password}" /> <!-- Hazelcast Management Center URL --> <hz:management-center enabled="${hz.management.center.enabled}" url="${hz.management.center.url}" /> <!-- Hazelcast Tcp based network configuration --> <hz:network port="${hzwork.port}" port-auto-increment="${hzwork.port.auto.increment}"> <hz:join> <hz:multicast enabled="${hz.multicast.enabled}" multicast-group="224.2.2.3" multicast-port="54327" /> <hz:tcp-ip enabled="${hz.tcp.ip.enabled}"> <hz:members>${hz.members}</hz:members> </hz:tcp-ip> </hz:join> </hz:network> <!-- Hazelcast Distributed Map configuration --> <hz:map name="map" backup-count="${hz.map.backup.count}" max-size="${hz.map.max.size}" eviction-percentage="${hz.map.eviction.percentage}" read-backup-data="${hz.map.read.backup.data}" eviction-policy="${hz.map.eviction.policy}" merge-policy="${hz.map.merge.policy}" /> </hz:config> </hz:hazelcast>

属性文件....

#-- Hazelcast properties. hz.instance.name = myapp hz.group.name = CERT hz.group.password = cert hz.management.center.enabled = true hz.management.center.url = 127.0.0.1:8080/mancenter hzwork.port = 5701 hzwork.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

Root Context.xml

<beans xmlns="www.springframework/schema/beans" xmlns:xsi="www.w3/2001/XMLSchema-instance" xmlns:hz="www.hazelcast/schema/spring" xmlns:jee="www.springframework/schema/jee" xmlns:context="www.springframework/schema/context" xsi:schemaLocation="www.springframework/schema/beans www.springframework/schema/beans/spring-beans-3.1.xsd www.springframework/schema/context www.springframework/schema/context/spring-context-3.1.xsd www.springframework/schema/jee www.springframework/schema/jee/spring-jee-3.1.xsd www.hazelcast/schema/spring www.hazelcast/schema/spring/hazelcast-spring-2.4.xsd"> ... <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <jee:jndi-lookup jndi-name="java:comp/env/config_file" /> </list> </property> </bean> ... </beans>

Tomcat配置...

Tomcat Config...

<Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true"> ... <Context docBase="myapp" path="/myapp" reloadable="true" source="org.eclipse.jst.j2ee.server:pwc-ws"> <Environment description="" name="config_file" override="false" type="java.lang.String" value="file:c:/path/to/config/myapp.properties" /> </Context> ... </Host>

Tomcat输出(注意:这是两个节点hazelcast组的重新启动场景.在这种情况下,节点1重新启动.节点2的输出显示节点1从该组中删除,然后返回到该组).

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).

CERT组的节点1

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 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: 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

CERT组的节点2(注意放置并重新添加)

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.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

更多推荐

如何将Hazelcast配置为使用spring进行会话缓存,同时将其限制为一组节点?

本文发布于:2023-11-28 11:55:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1642305.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:将其   节点   缓存   如何将   spring

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!