spring boot 在此就不多做介绍了,想来大家对他应该都不陌生了
Hazelcast 可能大家就毕竟陌生了:
PS:另外就是——根据基准测试,Hazelcast在获取数据方面比Redis快56%,在设置数据方面比Redis快44%。
下面主要是讲讲springboot和Hazelcast的整合,并给出Hazelcast支持的数据类型MAP、List、Topic、Queue给出了使用实例。
@Configuration public class HazelcastConfig { @Bean public Config config() { Config config = new Config(); GroupConfig gc=new GroupConfig(Const.HAZELCAST_NAME);//解决同网段下,不同库项目 config.setInstanceName("hazelcast-instance") .addMapConfig(new MapConfig().setName("configuration") .setMaxSizeConfig(new MaxSizeConfig(2000, MaxSizeConfig.MaxSizePolicy.FREE_HEAP_SIZE)) .setEvictionPolicy(EvictionPolicy.LRU).setTimeToLiveSeconds(-1)) .setGroupConfig(gc); return config; } @Bean public HazelcastInstance hazelcastInstance(Config config) { HazelcastInstance hzInstance = Hazelcast.newHazelcastInstance(config); //分布式map监听 IMap<Object, Object> imap = hzInstance.getMap(Const.MAP_NAME); imap.addLocalEntryListener(new IMapListener()); //拦截器(没写内容) imap.addInterceptor(new IMapInterceptor()); //发布/订阅模式 ITopic<String> topic = hzInstance.getTopic(Const.TOPIC_NAME); topic.addMessageListener(new TopicListener()); return hzInstance; } }
public class IMapInterceptor implements MapInterceptor{ private static final long serialVersionUID = 3556808830046436753L; @Override public Object interceptGet(Object value) { // TODO Auto-generated method stub return null; } @Override public void afterGet(Object value) { // TODO Auto-generated method stub } @Override public Object interceptPut(Object oldValue, Object newValue) { // TODO Auto-generated method stub return null; } @Override public void afterPut(Object value) { // TODO Auto-generated method stub } @Override public Object interceptRemove(Object removedValue) { // TODO Auto-generated method stub return null; } @Override public void afterRemove(Object oldValue) { // TODO Auto-generated method stub } }
public class IMapListener implements EntryAddedListener<String, String>{ @Override public void entryAdded(EntryEvent<String, String> event) { // TODO Auto-generated method stub //干你监听的操作 System.out.println("MAP分布式监听:"+event.getValue()); } }
public class TopicListener implements MessageListener<String> { @Override public void onMessage(Message<String> message) { String msg=message.getMessageObject(); System.out.println("收到Topic消息:"+msg); } }
这两个我就没写在实际代码中,在test写了2个main方法
//生产数据 public class HazelcastGetStartServerMaster { public static void main(String[] args) { // 创建一个 hazelcastInstance实例 HazelcastInstance instance = Hazelcast.newHazelcastInstance(); // 创建集群Map IList<Object> clusterMap = instance.getList("myList"); clusterMap.add("list0"); clusterMap.add("list1"); // 创建集群Queue Queue<String> clusterQueue = instance.getQueue("MyQueue"); clusterQueue.offer("Hello hazelcast!"); clusterQueue.offer("Hello hazelcast queue!"); } }
//消费数据 public class HazelcastGetStartServerSlave { public static void main(String[] args) { //创建一个 hazelcastInstance实例 HazelcastInstance instance = Hazelcast.newHazelcastInstance(); IList<Object> clusterList = instance.getList("myList"); Queue<String> clusterQueue = instance.getQueue("MyQueue"); System.out.println("Map Value:" + clusterList.get(1)); System.out.println("Queue Size :" + clusterQueue.size()); System.out.println("Queue Value 1:" + clusterQueue.poll()); System.out.println("Queue Value 2:" + clusterQueue.poll()); System.out.println("Queue Size :" + clusterQueue.size()); } }
这个时候启动项目显示如下:
当前只有一个节点,端口为:5701
这个时候写个main方法测试分布式map:
public class IMapTest { public static void main(String[] args) { Config config = new Config(); GroupConfig gc=new GroupConfig(Const.HAZELCAST_NAME); config.setGroupConfig(gc); HazelcastInstance hzInstance = Hazelcast.newHazelcastInstance(config); IMap<String, String> imap = hzInstance.getMap(Const.MAP_NAME); imap.put("myKey", "myObject"); } }
运行main方法发现原来启动的项目找那个Member变成了2个,因为main方法中也启动了一个Hazelcast实例加入了集群中。imap的拦截器,我监听器都生效了。并获取到了main方法中加的数据,因为Hazelcast是集群的,数据可以在许多应用程序实例之间共享。