1. 概述
项目中,创建的活动内容存入redis,然后需要用到活动内容的地方,从redis去取,然后参与计算。
活动数据的一个特点是更新不频繁、数据量不大。因为项目部署一般是多机器、多实例,除了redis,有没有其他实现呢?
Guava的 loading cache是本地缓存,数据量不是很大时 可以适用(如果有大量的key-value数据缓存本地,本机也吃不消啊),
然后多机器多实例怎么同步呢?想到了zookeeper....
2. 代码
2.1 模拟多实例
package TestZK; import org.apache.curator.RetryPolicy; import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFrameworkFactory; import org.apache.curator.framework.recipes.cache.PathChildrenCache; import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent; import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener; import org.apache.curator.retry.ExponentialBackoffRetry; /** * @Author: rocky * @Date: Created in 2018/5/20. */ public class InstanceOne { private static final String ADDRESS = "xxx:2181"; private static final String PATH = "/strategy"; private static CuratorFramework client; static { RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3); client = CuratorFrameworkFactory.builder() .connectString(ADDRESS) .sessionTimeoutMs(5000) .connectionTimeoutMs(5000) .retryPolicy(retryPolicy) // .namespace(BASE) .build(); client.start(); } public static void main(String[] args) throws Exception { startCache(); Thread.sleep(Integer.MAX_VALUE); } private static void startCache() throws Exception { PathChildrenCache childrenCache = new PathChildrenCache(client, PATH, true); childrenCache.start(); childrenCache.getListenable().addListener(new PathChildrenCacheListener() { public void childEvent(CuratorFramework curatorFramework, PathChildrenCacheEvent event) throws Exception { System.out.println("catch that: the path of changed node "+ event.getData().getPath() + ", the data of changed node is " + new String(event.getData().getData())); //load data to loading cache(guava) doSomething(); } }); } //load data to loading cache private static void doSomething() { // LoadingCache<String, Map<Integer, DriverIntegral>> driverIntegralCache = CacheBuilder.newBuilder().maximumSize(500) // .expireAfterWrite(5, TimeUnit.MINUTES).build(new CacheLoader<String, Map<Integer, DriverIntegral>>() { // @Override // public Map<Integer, DriverIntegral> load(String key) throws Exception { // Map<Integer, DriverIntegral> integerDriverIntegralMap = ..; // logger.info("guava load ::driverIntegralMap"+integerDriverIntegralMap!=null?integerDriverIntegralMap.toString():""); // return integerDriverIntegralMap; // } // }); // // Map<Integer, DriverIntegral> driverIntegralMap = driverIntegralCache.get(INTEGRAL); } } package TestZK; import org.apache.curator.RetryPolicy; import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFrameworkFactory; import org.apache.curator.framework.recipes.cache.PathChildrenCache; import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent; import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener; import org.apache.curator.retry.ExponentialBackoffRetry; /** * @Author: rocky * @Date: Created in 2018/5/20. */ public class InstanceTwo { private static final String ADDRESS = "xxx:2181"; private static final String PATH = "/strategy"; private static CuratorFramework client; static { RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3); client = CuratorFrameworkFactory.builder() .connectString(ADDRESS) .sessionTimeoutMs(5000) .connectionTimeoutMs(5000) .retryPolicy(retryPolicy) // .namespace(BASE) .build(); client.start(); } public static void main(String[] args) throws Exception { startCache(); Thread.sleep(Integer.MAX_VALUE); } private static void startCache() throws Exception { PathChildrenCache childrenCache = new PathChildrenCache(client, PATH, true); childrenCache.start(); childrenCache.getListenable().addListener(new PathChildrenCacheListener() { public void childEvent(CuratorFramework curatorFramework, PathChildrenCacheEvent event) throws Exception { System.out.println("catch that: the path of changed node "+ event.getData().getPath() + ", the data of changed node is " + new String(event.getData().getData())); } }); } } package TestZK; import org.apache.curator.RetryPolicy; import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFrameworkFactory; import org.apache.curator.framework.recipes.cache.PathChildrenCache; import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent; import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener; import org.apache.curator.retry.ExponentialBackoffRetry; /** * @Author: rocky * @Date: Created in 2018/5/20. */ public class InstanceThree { private static final String ADDRESS = "xxx:2181"; private static final String PATH = "/strategy"; private static CuratorFramework client; static { RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3); client = CuratorFrameworkFactory.builder() .connectString(ADDRESS) .sessionTimeoutMs(5000) .connectionTimeoutMs(5000) .retryPolicy(retryPolicy) // .namespace(BASE) .build(); client.start(); } public static void main(String[] args) throws Exception { startCache(); Thread.sleep(Integer.MAX_VALUE); } private static void startCache() throws Exception { PathChildrenCache childrenCache = new PathChildrenCache(client, PATH, true); childrenCache.start(); childrenCache.getListenable().addListener(new PathChildrenCacheListener() { public void childEvent(CuratorFramework curatorFramework, PathChildrenCacheEvent event) throws Exception { System.out.println("catch that: the path of changed node is "+ event.getData().getPath() + ", the data of changed node is " + new String(event.getData().getData())); } }); } }