Springboot+ehcache 实现集群消息同步

这个方式只适合两台服务器,多台不适用

近期由于项目上使用了集群方式部署,而我的项目中又用到了ehcache缓存,所有就要实现在集群中如何同步消息的问题。下面就是我的解决方案

使用 JGroups 集群配置

下面我们看具体步骤

1.添加pom依赖

        <!-- JGroups for Ehcache clustering -->
        <dependency>
        <groupId>org.jgroups</groupId>
        <artifactId>jgroups</artifactId>
        <version>3.6.13.Final</version>
        </dependency>
        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache-jgroupsreplication</artifactId>
            <version>1.7</version>
        </dependency>

2.修改ehcache.xml的配置

我们有两台服务器,所有在配置上稍微有点区别

192.168.1.10的配置

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">

    <!-- 磁盘缓存位置 -->
    <diskStore path="java.io.tmpdir"/>
    <!-- JGroups 集群配置(两台服务器使用相同配置) -->
   
    <cacheManagerPeerProviderFactory
            class="net.sf.ehcache.distribution.jgroups.JGroupsCacheManagerPeerProviderFactory"
            properties="connect=TCP(bind_addr=192.168.1.10;bind_port=17800;port_range=20):
                    TCPPING(initial_hosts=192.168.1.10[17800],192.168.1.9[17800];
                    port_range=0;timeout=3000):
                    MERGE3(min_interval=10000;max_interval=30000):
                    FD_SOCK:FD_ALL(timeout=12000;interval=3000):
                    VERIFY_SUSPECT(timeout=3000):
                    pbcast.NAKACK2(use_mcast_xmit=false;xmit_interval=500):
                    UNICAST3:
                    pbcast.STABLE(stability_delay=1000;desired_avg_gossip=50000;max_bytes=4M):
                    pbcast.GMS(join_timeout=10000;print_local_addr=true;max_join_attempts=10):
                    FRAG2(frag_size=60K):
                    pbcast.FLUSH"
            propertySeparator="::"/>

    <!-- 默认缓存 -->
    <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            maxElementsOnDisk="10000000"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU">
        <persistence strategy="localTempSwap"/>
    </defaultCache>

    <!-- 临时cache -->
    <cache name="temp_cache"
           maxElementsInMemory="10000"
           eternal="false"
           timeToIdleSeconds="1800"
           timeToLiveSeconds="1800"
           maxElementsOnDisk="10000000"
           diskExpiryThreadIntervalSeconds="120"
           memoryStoreEvictionPolicy="LRU">
        <persistence strategy="localTempSwap"/>
    </cache>

    <!-- 持久化cache -->
    <cache name="eternal_cache"
           maxElementsInMemory="10000"
           eternal="true"
           timeToIdleSeconds="120"
           timeToLiveSeconds="120"
           overflowToDisk="true"
           diskPersistent="true"
           diskExpiryThreadIntervalSeconds="10">
    </cache>

    <!-- 字典cache -->
    <cache name="sys_dict"
           maxElementsInMemory="10000"
           eternal="false"
           timeToIdleSeconds="0"
           timeToLiveSeconds="0"
           maxElementsOnDisk="10000000"
           diskExpiryThreadIntervalSeconds="120"
           memoryStoreEvictionPolicy="LRU">
        <persistence strategy="localTempSwap"/>
        <cacheEventListenerFactory
                class="net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory"
                properties="replicateAsynchronously=true,
                            replicatePuts=true,
                            replicateUpdates=true,
                            replicateUpdatesViaCopy=true,
                            replicateRemovals=true"/>
    </cache>

 

    <!-- 验证码缓存cache -->
    <cache name="captcha_codes"
           maxElementsInMemory="10000"
           eternal="false"
           timeToIdleSeconds="180"
           timeToLiveSeconds="180"
           maxElementsOnDisk="10000000"
           diskExpiryThreadIntervalSeconds="120"
           memoryStoreEvictionPolicy="LFU">
        <persistence strategy="localTempSwap"/>
        <cacheEventListenerFactory
                class="net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory"
                properties="replicateAsynchronously=true,
                            replicatePuts=true,
                            replicateUpdates=true,
                            replicateUpdatesViaCopy=true,
                            replicateRemovals=true"/>
    </cache>

    <!-- 用户缓存cache -->
    <cache name="login_tokens"
           maxElementsInMemory="10000"
           eternal="false"
           timeToIdleSeconds="1800"
           timeToLiveSeconds="1800"
           maxElementsOnDisk="10000000"
           diskExpiryThreadIntervalSeconds="120"
           memoryStoreEvictionPolicy="LFU">
        <persistence strategy="localTempSwap"/>
        <cacheEventListenerFactory
                class="net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory"
                properties="replicateAsynchronously=true,
                            replicatePuts=true,
                            replicateUpdates=true,
                            replicateUpdatesViaCopy=true,
                            replicateRemovals=true"/>
    </cache>

 
</ehcache>

192.168.1.9的配置

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">

    <!-- 磁盘缓存位置 -->
    <diskStore path="java.io.tmpdir"/>
    <!-- JGroups 集群配置(两台服务器使用相同配置) -->
   
    <cacheManagerPeerProviderFactory
            class="net.sf.ehcache.distribution.jgroups.JGroupsCacheManagerPeerProviderFactory"
            properties="connect=TCP(bind_addr=192.168.1.9;bind_port=17800;port_range=20):
                    TCPPING(initial_hosts=192.168.1.10[17800],192.168.1.9[17800];
                    port_range=0;timeout=3000):
                    MERGE3(min_interval=10000;max_interval=30000):
                    FD_SOCK:FD_ALL(timeout=12000;interval=3000):
                    VERIFY_SUSPECT(timeout=3000):
                    pbcast.NAKACK2(use_mcast_xmit=false;xmit_interval=500):
                    UNICAST3:
                    pbcast.STABLE(stability_delay=1000;desired_avg_gossip=50000;max_bytes=4M):
                    pbcast.GMS(join_timeout=10000;print_local_addr=true;max_join_attempts=10):
                    FRAG2(frag_size=60K):
                    pbcast.FLUSH"
            propertySeparator="::"/>

    <!-- 默认缓存 -->
    <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            maxElementsOnDisk="10000000"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU">
        <persistence strategy="localTempSwap"/>
    </defaultCache>

    <!-- 临时cache -->
    <cache name="temp_cache"
           maxElementsInMemory="10000"
           eternal="false"
           timeToIdleSeconds="1800"
           timeToLiveSeconds="1800"
           maxElementsOnDisk="10000000"
           diskExpiryThreadIntervalSeconds="120"
           memoryStoreEvictionPolicy="LRU">
        <persistence strategy="localTempSwap"/>
    </cache>

    <!-- 持久化cache -->
    <cache name="eternal_cache"
           maxElementsInMemory="10000"
           eternal="true"
           timeToIdleSeconds="120"
           timeToLiveSeconds="120"
           overflowToDisk="true"
           diskPersistent="true"
           diskExpiryThreadIntervalSeconds="10">
    </cache>

    <!-- 字典cache -->
    <cache name="sys_dict"
           maxElementsInMemory="10000"
           eternal="false"
           timeToIdleSeconds="0"
           timeToLiveSeconds="0"
           maxElementsOnDisk="10000000"
           diskExpiryThreadIntervalSeconds="120"
           memoryStoreEvictionPolicy="LRU">
        <persistence strategy="localTempSwap"/>
        <cacheEventListenerFactory
                class="net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory"
                properties="replicateAsynchronously=true,
                            replicatePuts=true,
                            replicateUpdates=true,
                            replicateUpdatesViaCopy=true,
                            replicateRemovals=true"/>
    </cache>

 

    <!-- 验证码缓存cache -->
    <cache name="captcha_codes"
           maxElementsInMemory="10000"
           eternal="false"
           timeToIdleSeconds="180"
           timeToLiveSeconds="180"
           maxElementsOnDisk="10000000"
           diskExpiryThreadIntervalSeconds="120"
           memoryStoreEvictionPolicy="LFU">
        <persistence strategy="localTempSwap"/>
        <cacheEventListenerFactory
                class="net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory"
                properties="replicateAsynchronously=true,
                            replicatePuts=true,
                            replicateUpdates=true,
                            replicateUpdatesViaCopy=true,
                            replicateRemovals=true"/>
    </cache>

    <!-- 用户缓存cache -->
    <cache name="login_tokens"
           maxElementsInMemory="10000"
           eternal="false"
           timeToIdleSeconds="1800"
           timeToLiveSeconds="1800"
           maxElementsOnDisk="10000000"
           diskExpiryThreadIntervalSeconds="120"
           memoryStoreEvictionPolicy="LFU">
        <persistence strategy="localTempSwap"/>
        <cacheEventListenerFactory
                class="net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory"
                properties="replicateAsynchronously=true,
                            replicatePuts=true,
                            replicateUpdates=true,
                            replicateUpdatesViaCopy=true,
                            replicateRemovals=true"/>
    </cache>

 
</ehcache>

两台服务器配置区别就在bind_addr,需要配置自己的ip,网上也有方式说是可以自动获取,我的服务器是双网卡,我试了之后不太好使,所有就写死了。

上面就是两台服务器之前实现ehcache消息同步的方法,这个方案也适用两台服务器多个子系统间ehcache同步

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值