In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "the application of JVM memory-level distributed cache Hazelcast". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "the application of JVM memory-level distributed cache Hazelcast".
Hazelcast is an in-memory distributed computing platform for managing data and executing applications in parallel.
1. It is written in Java.
two。 Unlike some other in-memory databases such as redis, Hazelcast is multithreaded, which means that you can benefit from all available CPU kernels.
3. Unlike other in-memory data grids-it is designed for distributed environments. It supports an unlimited number of map and caches per cluster.
According to the benchmark, Hazelcast is 56% faster than Redis in getting data and 44% faster than Redis in setting up data.
Hazelcast is a highly scalable data distribution and clustering platform. Properties include:
Provide java.util. {Queue, Set, List, Map} distributed implementation.
Provides java.util.concurrency.locks.Lock distributed implementation.
Provides java.util.concurrent.ExecutorService distributed implementation.
Provides a distributed MultiMap for one-to-many relationships.
Provides distributed Topic (topics) for publish / subscribe.
Integration with J2EE container and transaction support through JCA.
Provides Socket layer encryption for secure clustering.
Synchronous and asynchronous persistence are supported.
Monitor and manage clusters through JMX.
Dynamic HTTP Session clusters are supported.
Dynamic segmentation is realized by backup.
Dynamic failure recovery is supported.
Let's look at how to use Hazelcast
Pom
Com.hazelcast hazelcast-all 3.10.1
Open cache @ EnableCaching in the SpringBoot main program
@ EnableCaching@EnableFeignClients@EnableDiscoveryClient@EnableTransactionManagement@SpringBootApplicationpublic class ProductproviderApplication {public static void main (String [] args) {ApplicationContext app = SpringApplication.run (ProductproviderApplication.class, args); SpringBootUtil.setApplicationContext (app);}}
Configuration class
@ Configurationpublic class HazelcastConfiguration {@ Bean public Config hazelCastConfig () {Config config = new Config (); config.setInstanceName ("hazelcast-instance") .addMapConfig (new MapConfig (). SetName ("configuration") .setMaxSizeConfig (new MaxSizeConfig (200,200)) .setEvictionPolicy (EvictionPolicy.LFU) .setTimeToLiveSeconds (- 1); return config;}}
It is explained by yesterday's auto parts classification entity class. Here, note that the serialization interface Serializable must be implemented.
@ AllArgsConstructor@NoArgsConstructorpublic class ProviderProductLevel implements Provider,Serializable {@ Getter @ Setter private Long id; @ Getter @ Setter private String code; @ Getter @ Setter private String name; @ Getter @ Getter @ Setter private Integer level; @ Setter private ProductType productType; @ Getter @ Setter private String pictureUrl; @ Getter @ Setter private List otherProperties; @ Getter private List productListProviders = new CopyOnWriteArrayList () Public ProviderProductLevel (Long id,String code,String name,Integer sort,ProductType productType) {this.id = id; this.code = code; this.name = name; this.sort = sort; this.level = 1; this.productType = productType;} public ProviderProductLevel (Long id,String code,String name,Integer sort,String pictureUrl) {this.id = id; this.code = code; this.name = name This.sort = sort; this.level = 2; this.pictureUrl = pictureUrl;} @ Override public boolean addProvider (Provider provider) {LevelDao levelDao = SpringBootUtil.getBean (LevelDao.class); if (provider instanceof ProviderProductLevel) {levelDao.addLevelToLevel (new ParamLevel (this.id, ((ProviderProductLevel) provider). GetId ());} return this.productListProviders.add (provider);}}
Write a Controller, in which the distributed cache Map is established. The irrelevant content is not explained, but only the distributed cache content is explained.
@ Slf4j@RestControllerpublic class LevelController {/ / create a distributed cache node private HazelcastInstance instance = Hazelcast.newHazelcastInstance (); @ Autowired private LevelDao levelDao; / / first-level classified cache / / private Map cachelevelMap1 = new ConcurrentHashMap (); private Map cachelevelMap1 = instance.getMap ("level1") / * * Save level 1 auto parts category * @ param level * @ return * / @ ProductAnnotation @ SuppressWarnings ("unchecked") @ Transactional @ PostMapping ("/ productprovider-anon/savelevel1") public Result saveLevel1 (@ RequestBody ProviderProductLevel level) {ProviderFactory.createProviderProductLevel1 (level,true); if (! cachelevelMap1.containsKey (level.getId () {cachelevelMap1.put (level.getId (), level) } return Result.success ("saved successfully") } / * Save the secondary auto parts Long id and add the secondary product classification to the subcategory of the primary product category * @ param id * @ param level * @ return * / @ SuppressWarnings ("unchecked") @ Transactional @ PostMapping ("/ productprovider-anon/savelevel2") public Result saveLevel2 (@ RequestParam ("id") Long id, @ RequestBody ProviderProductLevel level) {ProviderFactory.createProviderProductLevel2 (level,true) If (cachelevelMap1.containsKey (id)) {/ / here you must get the first-level classification object, add the second-level classification, and then put it back into the cache / / directly using cachelevelMap1.get (id) .addProvider (level), you will not be able to add / / level to the CopyOnRightArrayList Provider level1 = cachelevelMap1.get (id); level1.addProvider (level) CachelevelMap1.put (id,level1); log.info ("cache acquisition" + JSONObject.toJSONString (cachelevelMap1.get (id);} else {Provider level1 = levelDao.findLevel1 (id); log.info (JSONObject.toJSONString (level1)); level1.addProvider (level); cachelevelMap1.put (id,level1);} return Result.success ("saved successfully") } @ GetMapping ("/ productprovider-anon/getprovider") public Provider getProvider (@ RequestParam ("id") Long id) {return cachelevelMap1.get (id);}}
We set the port number to 8106 and start the project for that port. We can see some logs like this.
2019-07-10 10 main 19V 38.381 INFO [productprovider,] 10216-[main] com.hazelcast.instance.AddressPicker: [LOCAL] [dev] [3.10.1] Prefer IPv4 stack is true.
2019-07-10 10 main 19V 38.764 INFO [productprovider,] 10216-[main] com.hazelcast.instance.AddressPicker: [LOCAL] [dev] [3.10.1] Picked [192.168.192.15]: 5702, using socket ServerSocket [addr=/0:0:0:0:0:0:0:0,localport=5702], bind any local is true
2019-07-10 10 com.hazelcast.system: [main] com.hazelcast.system: [192.168.192.15]: 5702 [dev] [3.10.1] Hazelcast 3.10.1 (20180521-66f881d) starting at [192.168.192.15]: 5702
2019-07-10 10 main 19V 38.765 INFO [productprovider,] 10216-[main] com.hazelcast.system: [192.168.192.15]: 5702 [dev] [3.10.1] Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
2019-07-10 10 main 19V 38.766 INFO [productprovider,] 10216-[main] com.hazelcast.system: [192.168.192.15]: 5702 [dev] [3.10.1] Configured Hazelcast Serialization version: 1
2019-07-10 10 main 19V 38.766 INFO [productprovider,] 10216-[main] com.hazelcast.instance.Node: [192.168.192.15]: 5702 [dev] [3.10.1] A non-empty group password is configured for the Hazelcast member. Starting with Hazelcast version 3.8.2, members with the same group name, but with different group passwords (that do not use authentication) form a cluster. The group password configuration will be removed completely in a future release.
2019-07-10 10 main 19V 38.770 INFO [productprovider,] 10216-[main] c.h.s.i.o.impl.BackpressureRegulator: [192.168.192.15]: 5702 [dev] [3.10.1] Backpressure is disabled
2019-07-10 10 main 1915 INFO 38.770 INFO [productprovider,] 10216-[main] h.s.i.o.i.InboundResponseHandlerSupplier: [192.168.192.15]: 5702 [dev] [3.10.1] Running with 2 response threads
2019-07-10 10 main 19V 39.422 INFO [productprovider,] 10216-[main] com.hazelcast.instance.Node: [192.168.192.15]: 5702 [dev] [3.10.1] Creating MulticastJoiner
2019-07-10 10 main 19V 39.428 INFO [productprovider,] 10216-[main] c.h.s.i.o.impl.OperationExecutorImpl: [192.168.192.15]: 5702 [dev] [3.10.1] Starting 8 partition threads and 5 generic threads (1 dedicated for priority tasks)
2019-07-10 10 main 19V 39.430 INFO [productprovider,] 10216-[main] c.h.internal.diagnostics.Diagnostics: [192.168.192.15]: 5702 [dev] [3.10.1] Diagnostics disabled. To enable add-Dhazelcast.diagnostics.enabled=true to the JVM arguments.
2019-07-10 10 main 1940 INFO [productprovider,] 10216-[main] com.hazelcast.core.LifecycleService: [192.168.192.15]: 5702 [dev] [3.10.1] [192.168.192.15]: 5702 is STARTING
2019-07-10 10 main 1915 INFO [productprovider,] 10216-[main] c.h.i.cluster.impl.MulticastJoiner: [192.168.192.15]: 5702 [dev] [3.10.1] Trying to join to discovered node: [192.168.192.15]: 5701
2019-07-10 10 cached.thread-3 19V 39.549 INFO [productprovider,] 10216-[cached.thread-3] com.hazelcast.nio.tcp.TcpIpConnector: [192.168.192.15]: 5702 [dev] [3.10.1] Connecting to / 192.168.192.15 cached.thread-3 5701, timeout: 0, bind-any: true
2019-07-10 10 INFO 19V 39.558 INFO [productprovider,] 10216-[thread-Acceptor] com.hazelcast.nio.tcp.TcpIpAcceptor: [192.168.192.15]: 5701 [dev] [3.10.1] Accepting socket connection from / 192.168.192.15 thread-Acceptor 4508
2019-07-10 10 c.h.nio.tcp.TcpIpConnectionManager: [thread-Acceptor] c.h.nio.tcp.TcpIpConnectionManager: [192.168.192.15]: 5701 [dev] [3.10.1] Established socket connection between / 192.168.192.15 INFO 5701 and / 192.168.192.154dro8
2019-07-10 10 c.h.nio.tcp.TcpIpConnectionManager: [192.168.192.15]: 5702 [dev] [3.10.1] Established socket connection between / 192.168.192.15 c.h.nio.tcp.TcpIpConnectionManager 4508 and / 192.168.192.15155701
2019-07-10 10 ration.thread-0 196.571 INFO [productprovider,] 10216-[ration.thread-0] c.h.internal.cluster.ClusterService: [192.168.192.15]: 5701 [dev] [3.10.1]
Members {size:2, ver:2} [
Member [192.168.192.15]: 5701-34da2d53-d8d6-4959-aaf2-bd6cda24f0fa this
Member [192.168.192.15]: 5702-70b959c7-ebb5-4226-b343-76e7354cbd94
]
2019-07-10 10 ration.thread-0 196.584 INFO [productprovider,] 10216-[ration.thread-0] com.hazelcast.system: [192.168.192.15]: 5702 [dev] [3.10.1] Cluster version set to 3.10
2019-07-10 10 ration.thread-0 19V 46.589 INFO [productprovider,] 10216-[ration.thread-0] c.h.internal.cluster.ClusterService: [192.168.192.15]: 5702 [dev] [3.10.1]
Members {size:2, ver:2} [
Member [192.168.192.15]: 5701-34da2d53-d8d6-4959-aaf2-bd6cda24f0fa
Member [192.168.192.15]: 5702-70b959c7-ebb5-4226-b343-76e7354cbd94 this
]
2019-07-10 10 main 19V 47.568 INFO [productprovider,] 10216-[main] com.hazelcast.core.LifecycleService: [192.168.192.15]: 5702 [dev] [3.10.1] [192.168.192.15]: 5702 is STARTED
It shows that it opens a 5702-port distributed cache system.
We changed the port of Springboot to 8006 and started the same project on that port, which we can see in the startup log
2019-07-10 10 INFO 2316.328 INFO [productprovider,] 11764-[main] com.hazelcast.instance.AddressPicker: [LOCAL] [dev] [3.10.1] Prefer IPv4 stack is true.
2019-07-10 10 INFO 2316.687 INFO [productprovider,] 11764-[main] com.hazelcast.instance.AddressPicker: [LOCAL] [dev] [3.10.1] Picked [192.168.192.15]: 5704, using socket ServerSocket [addr=/0:0:0:0:0:0:0:0,localport=5704], bind any local is true
2019-07-10 10 INFO 2316.688 INFO [productprovider,] 11764-[main] com.hazelcast.system: [dev] [3.10.1] Hazelcast 3.10.1 (20180521-66f881d) starting at [192.168.192.15]: 5704
2019-07-10 10 INFO 2316.689 INFO [productprovider,] 11764-[main] com.hazelcast.system: [192.168.192.15]: 5704 [dev] [3.10.1] Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
2019-07-10 10 INFO 2316.689 INFO [productprovider,] 11764-[main] com.hazelcast.system: [192.168.192.15]: 5704 [dev] [3.10.1] Configured Hazelcast Serialization version: 1
2019-07-10 10 INFO 2316.689 INFO [productprovider,] 11764-[main] com.hazelcast.instance.Node: [192.168.192.15]: 5704 [dev] [3.10.1] A non-empty group password is configured for the Hazelcast member. Starting with Hazelcast version 3.8.2, members with the same group name, but with different group passwords (that do not use authentication) form a cluster. The group password configuration will be removed completely in a future release.
2019-07-10 10 INFO 2316.692 INFO [productprovider,] 11764-[main] c.h.s.i.o.impl.BackpressureRegulator: [192.168.192.15]: 5704 [dev] [3.10.1] Backpressure is disabled
2019-07-10 10 INFO 2316.692 INFO [productprovider,] 11764-[main] h.s.i.o.i.InboundResponseHandlerSupplier: [192.168.192.15]: 5704 [dev] [3.10.1] Running with 2 response threads
2019-07-10 10 INFO 2317.310 INFO [productprovider,] 11764-[main] com.hazelcast.instance.Node: [192.168.192.15]: 5704 [dev] [3.10.1] Creating MulticastJoiner
2019-07-10 10 INFO 2317.315 INFO [productprovider,] 11764-[main] c.h.s.i.o.impl.OperationExecutorImpl: [192.168.192.15]: 5704 [dev] [3.10.1] Starting 8 partition threads and 5 generic threads (1 dedicated for priority tasks)
2019-07-10 10 INFO 2317.317 INFO [productprovider,] 11764-[main] c.h.internal.diagnostics.Diagnostics: [192.168.192.15]: 5704 [dev] [3.10.1] Diagnostics disabled. To enable add-Dhazelcast.diagnostics.enabled=true to the JVM arguments.
2019-07-10 10 INFO 2317.317 INFO [productprovider,] 11764-[main] com.hazelcast.core.LifecycleService: [192.168.192.15]: 5704 [dev] [3.10.1] [192.168.192.15]: 5704 is STARTING
2019-07-10 10 INFO 2317.419 INFO [productprovider,] 11764-[main] c.h.i.cluster.impl.MulticastJoiner: [192.168.192.15]: 5704 [dev] [3.10.1] Trying to join to discovered node: [192.168.192.15]: 5701
2019-07-10 10 INFO 2317.420 INFO [productprovider,] 11764-[cached.thread-3] com.hazelcast.nio.tcp.TcpIpConnector: [192.168.192.15]: 5704 [dev] [3.10.1] Connecting to / 192.168.192.15 cached.thread-3 5701, timeout: 0, bind-any: true
2019-07-10 10 INFO 2317.420 INFO [productprovider,] 11764-[cached.thread-3] c.h.nio.tcp.TcpIpConnectionManager: [192.168.192.15]: 5704 [dev] [3.10.1] Established socket connection between / 192.168.192.15 c.h.nio.tcp.TcpIpConnectionManager 4670 and / 192.168.192.155701
2019-07-10 10 INFO 23427 INFO [productprovider,] 11764-[ration.thread-2] com.hazelcast.system: [192.168.192.15]: 5704 [dev] [3.10.1] Cluster version set to 3.10
2019-07-10 10 ration.thread-2 23429 INFO [productprovider,] 11764-[ration.thread-2] c.h.internal.cluster.ClusterService: [192.168.192.15]: 5704 [dev] [3.10.1]
Members {size:4, ver:4} [
Member [192.168.192.15]: 5701-34da2d53-d8d6-4959-aaf2-bd6cda24f0fa
Member [192.168.192.15]: 5702-70b959c7-ebb5-4226-b343-76e7354cbd94
Member [192.168.192.15]: 5703-02829ed9-0ddf-4675-ba9e-15c1c03b4a50
Member [192.168.192.15]: 5704-6ecd17c1-425c-4be0-8914-827f51fdbf3e this
]
2019-07-10 10 INFO 23431 INFO [productprovider,] 11764-[thread-Acceptor] com.hazelcast.nio.tcp.TcpIpAcceptor: [192.168.192.15]: 5704 [dev] [3.10.1] Accepting socket connection from / 192.168.192.15Frost 4674
2019-07-10 10 thread-Acceptor 23 INFO 23.432 INFO [productprovider,] 11764-[thread-Acceptor] c.h.nio.tcp.TcpIpConnectionManager: [192.168.192.15]: 5704 [dev] [3.10.1] Established socket connection between / 192.168.192.15 c.h.nio.tcp.TcpIpConnectionManager 5704 and / 192.168.192.154674
2019-07-10 10 INFO 23436 INFO [productprovider,] 11764-[ration.thread-0] c.h.internal.cluster.ClusterService: [192.168.192.15]: 5703 [dev] [3.10.1]
Members {size:4, ver:4} [
Member [192.168.192.15]: 5701-34da2d53-d8d6-4959-aaf2-bd6cda24f0fa
Member [192.168.192.15]: 5702-70b959c7-ebb5-4226-b343-76e7354cbd94
Member [192.168.192.15]: 5703-02829ed9-0ddf-4675-ba9e-15c1c03b4a50 this
Member [192.168.192.15]: 5704-6ecd17c1-425c-4be0-8914-827f51fdbf3e
]
2019-07-10 10 cached.thread-1 23 INFO 23.437 INFO [productprovider,] 11764-[cached.thread-1] com.hazelcast.nio.tcp.TcpIpConnector: [192.168.192.15]: 5703 [dev] [3.10.1] Connecting to / 192.168.192.15 cached.thread-1 5704, timeout: 0, bind-any: true
2019-07-10 10 INFO 23438 INFO [productprovider,] 11764-[cached.thread-1] c.h.nio.tcp.TcpIpConnectionManager: [192.168.192.15]: 5703 [dev] [3.10.1] Established socket connection between / 192.168.192.15 c.h.nio.tcp.TcpIpConnectionManager 4675 and / 192.168.192.155704
2019-07-10 10 INFO 23438 INFO [productprovider,] 11764-[thread-Acceptor] com.hazelcast.nio.tcp.TcpIpAcceptor: [192.168.192.15]: 5704 [dev] [3.10.1] Accepting socket connection from / 192.168.192.15Frost 4675
2019-07-10 10 INFO 23439 INFO [productprovider,] 11764-[thread-Acceptor] c.h.nio.tcp.TcpIpConnectionManager: [192.168.192.15]: 5704 [dev] [3.10.1] Established socket connection between / 192.168.192.15 c.h.nio.tcp.TcpIpConnectionManager 5704 and / 192.168.192.154675
2019-07-10 10 cached.thread-5 23 INFO 23.440 INFO [productprovider,] 11764-[cached.thread-5] com.hazelcast.nio.tcp.TcpIpConnector: [192.168.192.15]: 5704 [dev] [3.10.1] Connecting to / 192.168.192.15 cached.thread-5 5702, timeout: 0, bind-any: true
2019-07-10 10 INFO 23444 INFO [productprovider,] 11764-[cached.thread-5] c.h.nio.tcp.TcpIpConnectionManager: [192.168.192.15]: 5704 [dev] [3.10.1] Established socket connection between / 192.168.192.15 c.h.nio.tcp.TcpIpConnectionManager 4676 and / 192.168.192.155702
2019-07-10 10 INFO 23444 INFO [productprovider,] 11764-[cached.thread-2] com.hazelcast.nio.tcp.TcpIpConnector: [192.168.192.15]: 5704 [dev] [3.10.1] Connecting to / 192.168.192.15 cached.thread-2 5703, timeout: 0, bind-any: true
2019-07-10 10 INFO 23447 INFO [productprovider,] 11764-[thread-Acceptor] com.hazelcast.nio.tcp.TcpIpAcceptor: [192.168.192.15]: 5703 [dev] [3.10.1] Accepting socket connection from / 192.168.192.15Frost 4677
2019-07-10 10 INFO 23447 INFO [productprovider,] 11764-[cached.thread-2] c.h.nio.tcp.TcpIpConnectionManager: [192.168.192.15]: 5704 [dev] [3.10.1] Established socket connection between / 192.168.192.15 c.h.nio.tcp.TcpIpConnectionManager 4677 and / 192.168.192.155703
2019-07-10 10 INFO 23447 INFO [productprovider,] 11764-[thread-Acceptor] c.h.nio.tcp.TcpIpConnectionManager: [192.168.192.15]: 5703 [dev] [3.10.1] Established socket connection between / 192.168.192.15 c.h.nio.tcp.TcpIpConnectionManager 5703 and / 192.168.192.154677
2019-07-10 10 INFO INFO [productprovider,] 11764-[main] com.hazelcast.core.LifecycleService: [192.168.192.15]: 5704 [dev] [3.10.1] [192.168.192.15]: 5704 is STARTED
Let's first add a first-class accessory to the process of port 8006.
2019-07-10 10 DEBUG 233.408 DEBUG [productprovider,7c69f637b05303a0,7c69f637b05303a0,false] 11764-[nio-8006-exec-1] c.c.p.d.L.saveProviderProductLevel1: = > Preparing: insert into product_level (id,code,name,sort,level,product_type) values (?,?)
2019-07-10 10 DEBUG 233.431 DEBUG [productprovider,7c69f637b05303a0,7c69f637b05303a0,false] 11764-[nio-8006-exec-1] c.c.p.d.L.saveProviderProductLevel1: = > Parameters: 2459048597577007931 (Long), 10003 (String), car necklace (String), 3 (Integer), 1 (Integer), car products (String)
2019-07-10 10 DEBUG 233.454 DEBUG [productprovider,7c69f637b05303a0,7c69f637b05303a0,false] 11764-[nio-8006-exec-1] c.c.p.d.L.saveProviderProductLevel1: Preparing: insert into product_level (id,code,name,sort,level,picture_url) values (?)
2019-07-10 10 nio-8106-exec-1 26 DEBUG 02.613 DEBUG [productprovider,deca9b0c23fc065f,deca9b0c23fc065f,false] 10216-[nio-8106-exec-1] c.c.p.d.L.saveProviderProductLevel2: = > Parameters: 2459048746827121467 (Long), 100201 (String), Advanced necklace (String), 1 (Integer), 2 (Integer), sdfasgfd23dg (String)
2019-07-10 10 nio-8106-exec-1 26 c.c.p.d.L.saveProviderProductLevel2 02.635 DEBUG [productprovider,deca9b0c23fc065f,deca9b0c23fc065f,false] 10216-[nio-8106-exec-1] c.c.p.d.L.saveProviderProductLevel2: Preparing: update product_level set parent_id=? Where id=?
2019-07-10 10 nio-8106-exec-1 26 DEBUG 02.666 DEBUG [productprovider,deca9b0c23fc065f,deca9b0c23fc065f,false] 10216-- [nio-8106-exec-1] c.c.p.dao.LevelDao.addLevelToLevel: = > Parameters: 2459048597577007931 (Long), 2459048746827121467 (Long)
2019-07-10 10 nio-8106-exec-1 26 c.c.p.dao.LevelDao.addLevelToLevel 02.689 DEBUG [productprovider,deca9b0c23fc065f,deca9b0c23fc065f,false] 10216-[nio-8106-exec-1] c.c.p.dao.LevelDao.addLevelToLevel:
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.