In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how to build redis cluster clusters and configure springboot2.3.x under windows. 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.
1. Software environment
Redis-x64-3.2.100.zip
Redis-trib.rb
Rubyinstaller-2.3.3-x64.exe
two。 Decompress redis
Extract the downloaded redis to the Redis-cluster directory on D disk, and then copy out five copies at the port of
6379,6380,6381,6382,6383,6384
3. Modify the redis.windows.conf configuration file under each redis folder
Go to each folder, find redis.windows.conf, and change these parameters
Port 6379cluster-enabled yescluster-config-file nodes-6379.confcluster-node-timeout 15000appendonly yes
Create a new start.bat file under each folder
Title redis-6379redis-server.exe redis.windows.conf
4. Install Ruby
Redis's cluster is scripted using ruby, so the system needs a Ruby environment
5. Open the cmd window to execute
Gem install redis
6. Start each redis and install the cluster script
Copy redis-trib.rb to the redis-cluster folder
Redis-trib.rb create-- replicas 1 127.0.0.1 6379 127.0.0.1 Fraser 6380 127.0.1 Discovery 6381 127.0.0.1 Flux 6382 127.0.1 1V 6383 127.0.1
-- replicas 1 means that each master database has 1 slave databases. There can be no less than 3 master nodes, so we used 6 redis
The commands in the cluster startup script .bat are as follows
Start / d "D:\ redis-cluster\ Redis-6379" start.batstart / d "D:\ redis-cluster\ Redis-6380" start.batstart / d "D:\ redis-cluster\ Redis-6381" start.batstart / d "D:\ redis-cluster\ Redis-6382" start.batstart / d "D:\ redis-cluster\ Redis-6383" start.batstart / d "D:\ redis-cluster\ Redis-6384" start.batping / n 3 127.1 > nulruby redis-trib.rb create-- replicas 1 127 .0.0.1: 6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384
This operation must be completed in step 6, and after the cluster is created, otherwise the cluster creation fails. Set a password for the cluster. The password needs to be consistent, otherwise it will fail.
Masterauth redispasswordrequirepass redispassword
7. Integrate POM.XML with springboot2.3.x
Org.springframework.boot spring-boot-starter-parent 2.3.3.RELEASE org.springframework.boot spring-boot-starter-data-redis
Application.yml configuration
Spring: redis: cluster: nodes: 127.0.0.1:6379127.0.0.1:6380127.0.0.1:6381127.0.0.1:6382127.0.0.1:6383127.0.0.1:6384
8.RedisUtil.java
Package com.example.elasticsearchdemo.util;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.*;import org.springframework.stereotype.Component;import java.io.Serializable;import java.util.List;import java.util.Set;import java.util.concurrent.TimeUnit;@Componentpublic class RedisUtils {@ Autowired private RedisTemplate redisTemplate / * * @ param key * @ param value * @ return * / public boolean set (final String key, Object value) {boolean result = false; try {ValueOperations operations = redisTemplate.opsForValue (); operations.set (key, value); result = true } catch (Exception e) {e.printStackTrace ();} return result;} / * write cache setting aging time * * @ param key * @ param value * @ return * / public boolean set (final String key, Object value, Long expireTime) {boolean result = false Try {ValueOperations operations = redisTemplate.opsForValue (); operations.set (key, value); redisTemplate.expire (key, expireTime, TimeUnit.SECONDS); result = true;} catch (Exception e) {e.printStackTrace ();} return result } / * batch delete the corresponding value * * @ param keys * / public void remove (final String... Keys) {for (String key: keys) {remove (key);}} / * batch delete the corresponding value (with transaction, this method is required if transaction is used in business code) * * @ param keys * / public void removeTransactional (final String... Keys) {for (String key: keys) {removeTransactional (key);}} / * * bulk delete key * * @ param pattern * / public void removePattern (final String pattern) {Set keys = redisTemplate.keys (pattern); if (keys.size () > 0) redisTemplate.delete (keys) } / * delete the corresponding value * * @ param key * / public void remove (final String key) {if (exists (key)) {redisTemplate.delete (key) }} / * determine whether there is a corresponding value * * @ param key * @ return * / public boolean exists (final String key) {return redisTemplate.hasKey (key) in the cache. } / * * read cache * * @ param key * @ return * / public Object get (final String key) {ValueOperations operations = redisTemplate.opsForValue (); return operations.get (key) } / * Hash add * * @ param key * @ param hashKey * @ param value * / public void hmSet (String key, Object hashKey, Object value) {HashOperations hash = redisTemplate.opsForHash (); hash.put (key, hashKey, value) } / * Hash to get data * * @ param key * @ param hashKey * @ return * / public Object hmGet (String key, Object hashKey) {HashOperations hash = redisTemplate.opsForHash (); return hash.get (key, hashKey) } / * add * * @ param k * @ param v * / public void lPush (String k, Object v) {ListOperations list = redisTemplate.opsForList (); list.rightPush (k, v) } / * list get * * @ param k * @ param l * @ param L1 * @ return * / public List lRange (String k, long l, long L1) {ListOperations list = redisTemplate.opsForList (); return list.range (k, l, L1) } / * Collection add * * @ param key * @ param value * / public void add (String key, Object value) {SetOperations set = redisTemplate.opsForSet (); set.add (key, value) } / * Collection get * * @ param key * @ return * / public Set setMembers (String key) {SetOperations set = redisTemplate.opsForSet (); return set.members (key) } / * * ordered collection add * * @ param key * @ param value * @ param scoure * / public void zAdd (String key, Object value, double scoure) {ZSetOperations zset = redisTemplate.opsForZSet (); zset.add (key, value, scoure) } / * * ordered collections get * * @ param key * @ param scoure * @ param scoure1 * @ return * / public Set rangeByScore (String key, double scoure, double scoure1) {ZSetOperations zset = redisTemplate.opsForZSet (); return zset.rangeByScore (key, scoure, scoure1) } / * * locking * * @ param key * @ return * / public boolean tryLock (String key) {try {long currTime = System.currentTimeMillis (); / / successful locking return redisTemplate.opsForValue (). SetIfAbsent (key, currTime);} finally {redisTemplate.expire (key, 5, TimeUnit.SECONDS) }}}
9.RedisController
Package com.test;import com.test.RedisUtils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class RedisController {@ Autowired private RedisUtils redisUtils; @ RequestMapping ("/ redis") public String redis (String key) {redisUtils.set (key, "sdfsdfsdf"); return "OK-" + key;}}
Connect each redis with the tool, and you can see that there are key and value in each redis. Redis cluster environment built successfully
[set the maximum available memory for redis]
Maxmemory 100mb
[policy to achieve maximum memory]
Maxmemory-policy noeviction refuses to write
[possible pit]
[error] CLUSTERDOWN Hash slot not served when performing a set operation
There are no allocation slots, because the redis cluster allocates 16384 slots to store data. If there is no allocation slot, the error is reported as above.
What's the reason?
The reason is the wrong operation when using ruby to build the cluster at last.
Redis-trib.rb create-- replicas 1127.0.0.1: 6379127.0.0.1 6380127.0.1purl 6381127.0.0.0.1purl 6382127.0.1purl 6383127.0.0.1purl 6384
There will be a prompt when the above execution is finished.
Can I set the above configuration? (type 'yes' to accept):
You need to type yes instead of the abbreviation y
It is this error that causes the allocation slot failure.
The above content is how to build redis cluster cluster and configure springboot2.3.x under windows. Have you learned the 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.