当前位置:网站首页>Redis builds clusters
Redis builds clusters
2022-07-06 07:20:00 【Yao, Muzi】
redis colony , As the name implies, it is composed of multiple redis The server , As a whole , Serve the system ,redis Cluster is a decentralized service mode , stay redis In the cluster , It is composed of multiple groups of simple clusters in master-slave mode, which are combined to form a large redis colony , Common maintenance in the cluster 16384 individual slot( slot ), Comparatively speaking ,redis Cluster replication is better than master-slave replication , Sentinel mode increases the ability to write , Better fault tolerance . Start building below
1、 Prepare the configuration file redis6379.conf、redis6380.conf、redis6381.conf、redis6389.conf、redis6390.conf、redis6391.conf, This configuration file is used when setting up a cluster , It will be divided into three groups of simple clusters in master-slave mode , The first three are hosts (master), The last three are slaves (slave). The configuration is as follows :
# Introduce the public configuration part
include ./redis.conf
# Set up pid file location
pidfile ./redis_6379.pid
# Set the port number
port 6379
# Set up rdb File name
dbfilename "dump6379.rdb"
# The following are the meals and accommodations related to cluster configuration
# Turn on cluster mode
cluster-enabled yes
# Set the node cluster configuration information file name
cluster-config-file nodes-6379.conf
# Node timeout
cluster-node-timeout 15000
The common parts of all configuration files are the same , You just need to modify the port and file name to the specified configuration information
2、 Start each node
./redis-server XXX.conf
3、 Carry out orders
Entering installation is compilation redis File path of src Under the table of contents
*/redis-5.0.9/src, Use redis-cli perform
Be careful : In a higher version , Has inherited ruby Environmental Science , If the lower version , You need to install it yourself ruby Environmental Science
./redis-cli --cluster create --cluster-replicas 1 -a lixl123 192.168.1.35:6379 192.168.1.35:6380 192.168.1.35:6381 192.168.1.35:6389 192.168.1.35:6390 192.168.1.35:6391
In the command 1 Represents creating a cluster using simple mode
-a Representative password
Create success !
When connecting to the cluster , Some changes , Switch to the installation path again
./redis-cli -c -p + Primary node port number ( Any one in the cluster can )
Use command cluster nodes You can view the cluster information
java Use redisTemplate Connect redis Clusters have also been adjusted , Default redisTemplate It is used. lettuce Connect redis, Use here jedis Connect
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>spring-boot</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<!-- exclude lettuce -->
<exclusions>
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- introduce jedis Related to the package -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Profile information
server:
port: 8981
spring:
cache:
redis:
time-to-live: 10000
redis:
timeout: 5000
database: 0
password: lixl123
cluster:
nodes:
- 192.168.1.35:6379
- 192.168.1.35:6380
- 192.168.1.35:6381
- 192.168.1.35:6389
- 192.168.1.35:6390
- 192.168.1.35:6391
max-redirects: 3
jedis:
pool:
max-active: 8
max-wait: -1
max-idle: 8
min-idle: 0
Configuration class information
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
// Use Jackson2JsonRedisSerialize Replace the default jdkSerializeable serialize
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
// key use String How to serialize
template.setKeySerializer(stringRedisSerializer);
// hash Of key Also used String How to serialize
template.setHashKeySerializer(stringRedisSerializer);
// value The serialization method adopts jackson
template.setValueSerializer(jackson2JsonRedisSerializer);
// hash Of value The serialization method adopts jackson
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
The test method
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestRedisController {
@Autowired
RedisTemplate redisTemplate;
@RequestMapping("/setRedis2")
public String setRedis2(@RequestParam(value = "key",required = false)String key,@RequestParam(value = "value",required = false)String value){
if (StringUtils.hasText(key)&&StringUtils.hasText(value)){
redisTemplate.opsForValue().set(key,value);
} else {
redisTemplate.opsForValue().set("name","lixl123");
}
return "true";
}
@RequestMapping("/getRedis2")
public String getRedis2(@RequestParam(value = "key",required = false)String key){
String result = "";
if (StringUtils.hasText(key)){
result = (String)redisTemplate.opsForValue().get(key);
} else {
result = (String)redisTemplate.opsForValue().get("name");
}
return result;
}
}
The results
tips:
cluster nodes View cluster information
cluster keyslot k1 # Calculation key Slot value for
cluster countkeysinslot + Slot value # Check the number of values in the slot , You can only view the slots maintained by your server
cluster getkeysinslot + Slot value # Return all in the slot key
cluster-require-full-coverage: yes
When all nodes in a slot are down , The whole cluster will not be able to provide services
If the value is set to no, Then the down slot cannot be accessed ( read / Write ), Other slots are not affected
边栏推荐
- “无聊猿” BAYC 的内忧与外患
- TypeScript 变量作用域
- Openjudge noi 2.1 1749: Digital Square
- word怎么只删除英语保留汉语或删除汉语保留英文
- [MySQL learning notes 29] trigger
- TypeScript 接口属性
- leetcode1020. Number of enclaves (medium)
- OpenJudge NOI 2.1 1661:Bomb Game
- Uni app third party package configuration network request
- 杰理之需要修改 gatt 的 profile 定义【篇】
猜你喜欢
leetcode35. 搜索插入位置(简单,找插入位置,不同写法)
word中如何删除某符号前面或后面所有的文字
Upgraded wechat tool applet source code for mobile phone detection - supports a variety of main traffic modes
Leetcode59. spiral matrix II (medium)
UWA pipeline version 2.2.1 update instructions
首发织梦百度推送插件全自动收录优化seo收录模块
杰理之开发板上电开机,就可以手机打开 NRF 的 APP【篇】
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
Cookie Technology & session Technology & ServletContext object
Kubernetes cluster builds ZABBIX monitoring platform
随机推荐
Typescript indexable type
The author is dead? AI is conquering mankind with art
杰理之BLE【篇】
OpenGL ES 学习初识(1)
TypeScript接口与泛型的使用
杰理之如若需要大包发送,需要手机端修改 MTU【篇】
[online problem processing] how to kill the corresponding process when the MySQL table deadlock is caused by the code
作者已死?AI正用藝術征服人類
word中如何删除某符号前面或后面所有的文字
How MySQL merges data
【mysql学习笔记30】锁(非教程)
word中把帶有某個符號的行全部選中,更改為標題
Raspberry pie serial port login and SSH login methods
Win10 64 bit Mitsubishi PLC software appears oleaut32 DLL access denied
Openjudge noi 2.1 1749: Digital Square
微信公众号无限回调授权系统源码 全网首发
Oracle database 11gr2 uses TDE transparent data encryption to report an error ora28353. If you run to close the wallet, you will report an error ora28365. If you run to open the wallet, you will repor
杰理之AD 系列 MIDI 功能说明【篇】
“无聊猿” BAYC 的内忧与外患
word设置目录