软件开发定制redis搭建集群

,软件开发定制顾名思义的就是由多台redis服务器,软件开发定制作为一个整体,软件开发定制为系统进行服务,redis软件开发定制集群是一个无中心化的软件开发定制一种服务模式,在redis集群中,由多组主从模式的简单集群共同组件成一个大的redis集群,集群中共同维护16384个slot(插槽),相对比而言,redis集群比主从复制,哨兵模式增加了写的能力,容错性能更强。下边开始搭建
1、准备配置文件redis6379.conf、redis6380.conf、redis6381.conf、redis6389.conf、redis6390.conf、redis6391.conf,此种配置文件在搭建集群的时候,会分为三组主从模式的简单集群,前三台为主机(master),后三个分别为从机(slave)。配置内容如下:

# 引入公共的配置部分include ./redis.conf#设置pid文件位置pidfile ./redis_6379.pid# 设置端口号port 6379# 设置rdb文件名称dbfilename "dump6379.rdb"# 以下是集群配置相关餐宿# 开启集群模式cluster-enabled yes# 设置节点集群配置信息文件名称cluster-config-file nodes-6379.conf# 节点超时时间cluster-node-timeout 15000
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

所有配置文件公共部分内容相同,只是需要修改端口和文件名称为指定的配置信息
2、启动各个节点
./redis-server XXX.conf
3、执行命令
进入到安装是编译redis的文件路径的src目录下
*/redis-5.0.9/src,使用redis-cli执行
注意:在高版本中,已经继承了ruby环境,如果低版本的,需要自己安装ruby环境

./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
  • 1

命令中的 1 代表使用简单模式创建集群
-a 代表密码



创建成功!
连接集群时候,有些变化,再次切换到安装路径下
./redis-cli -c -p + 主节点端口号(集群中任意一个都可以)
使用命令cluster nodes可以查看集群信息

java使用连接redis集群也有所调整,默认redisTemplate是用了lettuce连接redis,这里使用jedis连接

<?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>            <!-- 排除lettuce -->            <exclusions>                <exclusion>                    <groupId>io.lettuce</groupId>                    <artifactId>lettuce-core</artifactId>                </exclusion>            </exclusions>        </dependency>        <!-- 引入jedis相关的包 -->        <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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56

配置文件信息

server:  port: 8981spring:  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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

配置类信息

    @Bean    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {        RedisTemplate<String, Object> template = new RedisTemplate<>();        template.setConnectionFactory(factory);        // 使用Jackson2JsonRedisSerialize 替换默认的jdkSerializeable序列化        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采用String的序列化方式        template.setKeySerializer(stringRedisSerializer);        // hash的key也采用String的序列化方式        template.setHashKeySerializer(stringRedisSerializer);        // value序列化方式采用jackson        template.setValueSerializer(jackson2JsonRedisSerializer);        // hash的value序列化方式采用jackson        template.setHashValueSerializer(jackson2JsonRedisSerializer);        template.afterPropertiesSet();        return template;    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

测试方法

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;@RestControllerpublic 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;    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

调用结果


tips:
cluster nodes 查看集群信息

cluster keyslot k1 #计算key的插槽值

cluster countkeysinslot + 插槽值 # 查看插槽内值的个数,只能查看自己服务器维护的插槽

cluster getkeysinslot + 插槽值 #返回插槽内的所有key

cluster-require-full-coverage: yes
当某个插槽的全部节点都宕机的情况下,整个集群将全部不能提供服务
如果该值设置为no,则宕机的插槽不能访问(读/写),其他插槽不受影响

网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发