spring boot and memory integrated database Hazelcast

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_18537055/article/details/98802183

Foreword

spring boot here do not introduced, I wanted to come to him, should not a stranger

Hazelcast everyone, after all, a stranger may:

  • Easy to use
    Hazelcast is written in Java, no other dependencies. Simply put the jar package introduced classpath project to create a cluster.
  • Master-slave mode
    with many different solutions NoSQL, Hazelcast node is a point to point. No master-slave relationship; all members of the same amount of data storage, and equal treatment, to avoid a single point of failure.
  • Elastic scalability
    Hazelcast designed to extend the tens of thousands of members. New members start, the cluster will automatically find and linear increase storage and processing capacity. Between members stay connected and communicate over TCP.
  • Fast and efficient read
    Hazelcast all data stored in memory, memory-based literacy provide fast and efficient.

PS: The other is - according to the benchmark, Hazelcast in accessing data faster than 56% Redis, in terms of setting data faster than 44% Redis. 

example

The following are the main talk about integration springboot and Hazelcast and gives Hazelcast data types supported by MAP, List, Topic, Queue given use case.

Integration configurations:

@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;
	}
}

map data changes interceptors to achieve:

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
		
	}
}

map data change listener implementation:

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());
	}

}

Topic subscribe to receive messages:

public class TopicListener implements MessageListener<String> {
	
	@Override
	public void onMessage(Message<String> message) {
		String msg=message.getMessageObject();
		System.out.println("收到Topic消息:"+msg);
	}
}

Examples of List and Queue

The two I did not write the actual code, the test write two main methods

//生产数据
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());
    }
}

This time start items are shown below:

Currently only one node, port: 5701

This time to write a main method to test distributed 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");
    }
}

Run the main method found that the project start looking for that Member becomes two, because the main method also launched a Hazelcast instance joined the cluster. imap interceptors, my listeners have come into force. And get added to the main method of data because Hazelcast the cluster, the data can be shared among many application instance.

Guess you like

Origin blog.csdn.net/qq_18537055/article/details/98802183