Hazelcast客户端弹簧配置

时间:2017-07-27 09:23:25

标签: spring hazelcast

我创建了一个使用spring,hibernate,jpa的Hazelcast管理器。我可以开始我的hazelcast实例。

我遇到的问题是我不知道如何使用spring config配置hazelcast客户端。我想在其他服务器组件中使用hazelcast-client

我真的不知道如何开始

任何帮助将不胜感激

以下是我对hazelcast服务器的弹簧配置

约翰

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:hz="http://www.hazelcast.com/schema/spring"
       xmlns:tx="http://www.springframework.org/schema/tx" 
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx.xsd
                           http://www.hazelcast.com/schema/spring
                           http://www.hazelcast.com/schema/spring/hazelcast-spring.xsd">

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>file:${ecs.config.path}/ecs.properties</value>
                <value>classpath*:config/ecs.properties</value>
            </list>
        </property> 
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
        <property name="ignoreResourceNotFound" value="true" />
    </bean>

    <context:component-scan base-package="nl.ict.psa.ecs.hazelcast.dao,nl.ict.psa.ecs.hazelcast.mapstores,nl.ict.psa.ecs.hazelcast.service" />

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${datasource.driverClassName}" />
        <property name="url" value="${datasource.url}" />
        <property name="username" value="${datasource.username}"/>
        <property name="password" value="${datasource.password}"/>
    </bean>

    <bean id="hazelcast" class="com.hazelcast.core.Hazelcast"/>

    <bean id="entityManagerFactoryBean"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">

        <property name="dataSource" ref="dataSource" />
        <property name="persistenceUnitName" value="PU" />
        <property name="packagesToScan">
            <list>
                <value>nl.ict.psa.hazelcast.model.ecs</value>
                <value>nl.ict.psa.hazelcast.model.ecs.ocr</value>
            </list>
        </property>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.archive.autodetection">class</prop>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            </props>
        </property>
    </bean>

    <tx:annotation-driven />

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactoryBean" />
    </bean>

    <bean id="transpInfo" class="nl.ict.psa.ecs.hazelcast.mapstores.TranspInfoMapStore"/>

    <hz:hazelcast id="instance">
        <hz:config>
            <hz:network port="5701" port-auto-increment="true">
                <hz:join>
                    <hz:multicast enabled="false"/>
                </hz:join>
            </hz:network>

            <hz:map name="transp" read-backup-data="true">
                <hz:map-store enabled="true" write-delay-seconds="60"
                              initial-mode="LAZY"
                              implementation="transpInfo"/>
            </hz:map>
        </hz:config>
    </hz:hazelcast>

</beans>

2 个答案:

答案 0 :(得分:3)

配置多个Ip实用地使用。

@Bean
public ClientConfig clientConfig() {
  ClientConfig clientConfig = new ClientConfig();
  ClientNetworkConfig networkConfig = clientConfig.getNetworkConfig();
  networkConfig.addAddress("172.17.0.4:5701", "172.17.0.6:5701")
    .setSmartRouting(true)
    .addOutboundPortDefinition("34700-34710")
    .setRedoOperation(true)
    .setConnectionTimeout(5000)
    .setConnectionAttemptLimit(5);

  return clientConfig;

}

@Bean
public HazelcastInstance hazelcastInstance(ClientConfig clientConfig) {
  return HazelcastClient.newHazelcastClient(clientConfig);
}

我建议必须阅读。 http://docs.hazelcast.org/docs/latest/manual/html-single/index.html#configuring-client-connection-strategy

答案 1 :(得分:1)

像这样(来自https://github.com/neilstevenson/spring-boot-autoconfigure-test/tree/master/hazelcast-imdg-client

@Configuration
@ConditionalOnMissingBean(ClientConfig.class)
static class HazelcastClientConfigConfiguration {

    @Bean
    public ClientConfig clientConfig() throws Exception {
            return new XmlClientConfigBuilder().build();
    }
}

@Configuration
@ConditionalOnMissingBean(HazelcastInstance.class)
static class HazelcastClientConfiguration {

    @Bean
    public HazelcastInstance hazelcastInstance(ClientConfig clientConfig) {
        return HazelcastClient.newHazelcastClient(clientConfig);
    }
}

尽量避免使用XML,Spring正在逐渐远离它。