In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how the integration of spring boot and in-memory database Hazelcast is. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Preface
Spring boot will not make any more introductions here. I think everyone should be familiar with him.
Hazelcast may be a stranger to everyone after all:
Simple and easy to use
Hazelcast is written in Java and has no other dependencies. You can create a cluster by simply introducing the jar package into the project's classpath.
No master-slave mode
Unlike many NoSQL solutions, Hazelcast nodes are peer-to-peer. There is no master-slave relationship; all members store the same amount of data and do equal processing to avoid a single point of failure.
Elastic and scalable
Hazelcast aims to expand thousands of members. When a new member starts, the cluster is automatically discovered and the storage and processing capacity is linearly increased. Members stay connected and communicate with each other through TCP.
Read and write quickly and efficiently
Hazelcast stores all data in memory, providing fast and efficient read and write capabilities based on memory.
PS: in addition, according to the benchmark, Hazelcast is 56% faster than Redis in getting data and 44% faster than Redis in setting up data.
Use case
The following is mainly about the integration of springboot and Hazelcast, and gives examples of the data types MAP, List, Topic and Queue supported by Hazelcast.
Integrated configuration: @ Configurationpublic class HazelcastConfig {@ Bean public Config config () {Config config = new Config (); GroupConfig gc=new GroupConfig (Const.HAZELCAST_NAME) / / solve the problem under the same network segment Different library projects 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); / / distributed map snooping IMap imap = hzInstance.getMap (Const.MAP_NAME); imap.addLocalEntryListener (new IMapListener ()) / / interceptor (not written) imap.addInterceptor (new IMapInterceptor ()); / / publish / subscribe mode ITopic topic = hzInstance.getTopic (Const.TOPIC_NAME); topic.addMessageListener (new TopicListener ()); return hzInstance }} interceptor implementation of map data changes: 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}} listener implementation of map data changes: public class IMapListener implements EntryAddedListener {@ Override public void entryAdded (EntryEvent event) {/ / TODO Auto-generated method stub / / do what you are listening to Operation System.out.println ("MAP distributed snooping:" + event.getValue ()) }} Topic subscription to receive messages: public class TopicListener implements MessageListener {@ Override public void onMessage (Message message) {String msg=message.getMessageObject (); System.out.println ("receive Topic message:" + msg);}} instances of List and Queue
I didn't write these two in the actual code. I wrote two main methods in test.
/ / production data public class HazelcastGetStartServerMaster {public static void main (String [] args) {/ / create a hazelcastInstance instance HazelcastInstance instance = Hazelcast.newHazelcastInstance (); / / create a cluster Map IList clusterMap = instance.getList ("myList"); clusterMap.add ("list0"); clusterMap.add ("list1"); / / create a cluster Queue Queue clusterQueue = instance.getQueue ("MyQueue") ClusterQueue.offer ("Hello hazelcast!"); clusterQueue.offer ("Hello hazelcast queue!");}} / / consumption data public class HazelcastGetStartServerSlave {public static void main (String [] args) {/ / create a hazelcastInstance instance HazelcastInstance instance = Hazelcast.newHazelcastInstance (); IList clusterList = instance.getList ("myList"); Queue 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 ());}}
At this time, the startup project is shown as follows:
Currently there is only one node, and the port is: 5701
At this point, write a main method to test the 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 imap = hzInstance.getMap (Const.MAP_NAME) Imap.put ("myKey", "myObject");}}
Running the main method found that the original project found that the Member became two, because a Hazelcast instance was also launched in the main method to join the cluster. Imap interceptors, my listeners are working. And get the data added in the main method, because Hazelcast is clustered and the data can be shared among many application instances.
The above is what the integration of spring boot with in-memory database Hazelcast is like. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.