如何使用系统属性和弹簧配置Hazelcast-WM

时间:2013-10-13 21:21:15

标签: spring-3 servlet-3.0 hazelcast

我正在尝试使用 hazelcast-wm 3.0 hazelcast-spring 3.0 spring 3.2.2 servlet-api 3.0 。

的web.xml

<listener>
    <listener-class>com.company.PropertiesListener</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>false</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>
    <!-- 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>false</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>hazelcast-context.xml</param-value>
    </init-param>
    <!-- Do you want to use an existing HazelcastInstance? Default is null. -->
    <init-param>
        <param-name>instance-name</param-name>
        <param-value>hz.session.instance</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>

<listener>
    <listener-class>com.hazelcast.web.SessionListener</listener-class>
</listener>

hazelcast-context.xml中

<hz:hazelcast id="hz.session.cluster" depends-on="hazelcast.properties">
    <hz:config>
        <hz:instance-name>hz.session.instance</hz:instance-name>
        <hz:group name="${cluster.name:my-sessions}" password="${cluster.password:mypass}" />
        <hz:properties>
            <hz:property name="hazelcast.logging.type">slf4j</hz:property>
            <hz:property name="hazelcast.version.check.enabled">false</hz:property>
            <hz:property name="hazelcast.jmx">true</hz:property>
        </hz:properties>
        <hz:network port="${cluster.network.port:5701}"
            public-address="${cluster.member.address:127.0.0.1}"
            port-auto-increment="${cluster.network.port-auto-increment:true}">
            <hz:join>
                <hz:multicast enabled="${cluster.network.multicast.enabled:false}"
                    multicast-group="${cluster.network.multicast.group:224.2.2.3}"
                    multicast-port="${cluster.network.multicast.port:54327}" />
                <hz:tcp-ip enabled="${cluster.network.tcpip:true}">
                    <hz:members>${cluster.members:127.0.0.1}</hz:members>
                </hz:tcp-ip>
            </hz:join>
        </hz:network>
    </hz:config>
</hz:hazelcast>

PropertiesListener设置System.properties:cluster.members,cluster.member.address等,具体取决于正在运行的计算机。

如果我使用Application的Spring Context创建hazelcast实例,那么效果很好:

@Configuration
@ImportResource("classpath:hazelcast-context.xml")
public class HazelcastConfig {

} 

效果很好! Hazelcast实例绑定到正确的网络接口。

但问题是当我使用WebFilter时(之前我删除了HazelcastConfig类,所以不要两次创建Hazelcast实例。

这些是日志中显示的消息:

Oct 13, 2013 9:09:28 PM com.hazelcast.config.UrlXmlConfig
INFO: Configuring Hazelcast from 'file:/home/neuquino/springsource/vfabric-tc-server-developer-2.9.2.RELEASE/my-app/wtpwebapps/my-app-web/WEB-INF/classes/hazelcast-context.xml'.
Oct 13, 2013 9:09:28 PM com.hazelcast.config.XmlConfigBuilder
WARNING: Could not find a value for property  'cluster.name:my-sessions' on node: null
Oct 13, 2013 9:09:28 PM com.hazelcast.config.XmlConfigBuilder
WARNING: Could not find a value for property  'cluster.password:mypass' on node: null
Oct 13, 2013 9:09:28 PM com.hazelcast.config.XmlConfigBuilder
WARNING: Could not find a value for property  'cluster.network.port:5701' on node: null
Oct 13, 2013 9:09:28 PM com.hazelcast.config.XmlConfigBuilder
WARNING: Could not find a value for property  'cluster.network.port-auto-increment:true' on node: null
Oct 13, 2013 9:09:28 PM com.hazelcast.config.XmlConfigBuilder
WARNING: Could not find a value for property  'cluster.member.address:127.0.0.1' on node: null
Oct 13, 2013 9:09:28 PM com.hazelcast.config.XmlConfigBuilder
WARNING: Could not find a value for property  'cluster.network.multicast.enabled:false' on node: null
Oct 13, 2013 9:09:28 PM com.hazelcast.config.XmlConfigBuilder
WARNING: Could not find a value for property  'cluster.network.multicast.group:224.2.2.3' on node: null
Oct 13, 2013 9:09:28 PM com.hazelcast.config.XmlConfigBuilder
WARNING: Could not find a value for property  'cluster.network.multicast.port:54327' on node: null
Oct 13, 2013 9:09:28 PM com.hazelcast.config.XmlConfigBuilder
WARNING: Could not find a value for property  'cluster.network.tcpip:true' on node: null
Oct 13, 2013 9:09:28 PM com.hazelcast.config.XmlConfigBuilder
WARNING: Could not find a value for property  'cluster.members:127.0.0.1' on node: null
Oct 13, 2013 9:09:29 PM com.hazelcast.instance.DefaultAddressPicker
INFO: Prefer IPv4 stack is true.
Oct 13, 2013 9:09:29 PM com.hazelcast.instance.DefaultAddressPicker
INFO: Picked Address[10.8.254.133]:5701, using socket ServerSocket[addr=/0:0:0:0:0:0:0:0,localport=5701], bind any local is true
Oct 13, 2013 9:09:29 PM com.hazelcast.system
INFO: [10.8.254.133]:5701 [dev] Hazelcast Community Edition 3.0.2 (20130906) starting at Address[10.8.254.133]:5701
Oct 13, 2013 9:09:29 PM com.hazelcast.system
INFO: [10.8.254.133]:5701 [dev] Copyright (C) 2008-2013 Hazelcast.com
Oct 13, 2013 9:09:29 PM com.hazelcast.instance.Node
INFO: [10.8.254.133]:5701 [dev] Creating MulticastJoiner
Oct 13, 2013 9:09:29 PM com.hazelcast.core.LifecycleService
INFO: [10.8.254.133]:5701 [dev] Address[10.8.254.133]:5701 is STARTING
Oct 13, 2013 9:09:34 PM com.hazelcast.cluster.MulticastJoiner
INFO: [10.8.254.133]:5701 [dev] 


Members [1] {
    Member [10.8.254.133]:5701 this
}

Oct 13, 2013 9:09:34 PM com.hazelcast.core.LifecycleService
INFO: [10.8.254.133]:5701 [dev] Address[10.8.254.133]:5701 is STARTED

正确的IP应该是11.1.0.133($ {cluster.member.address}值),Hazelcast正在选择我的PC的一个网络接口,但不使用$ {cluster.member.address}值。

如何将WebFilter与系统属性一起使用?

提前致谢!

1 个答案:

答案 0 :(得分:0)

您可以尝试自己阅读属性并修改从XML创建的Config实例,也可以自己创建Config实例。 XmlConfigBuilder应该为你做的伎俩。也许我会添加一种方法来将创建后监听器注册到Spring模块。