0

我正在尝试使用hazelcast-wm 3.0hazelcast-spring 3.0spring 3.2.2servlet-api 3.0对 http 会话进行集群化。

网页.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 等,取决于运行的机器。

如果我使用应用程序的 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 与系统属性一起使用?

提前致谢!

4

1 回答 1

0

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

于 2013-10-14T06:07:07.630 回答