Redis4集群搭建

目录

安装集群

需要文件

安装

上传文件解压

编译

准备配置文件

安装ruby和启动各个redis节点

创建集群

集群查看

集群检查

集群添加节点

集群的节点关闭

springboot集成

yml配置

pom文件

redisTemplate


安装集群

需要文件

redis-3.3.5.gem

redis-4.0.10.tar.gz

安装

上传文件解压

上传文件redis-3.3.5.gem、 redis-4.0.10.tar.gz 到/opt目录

cd /opt

tar -zxvf redis-4.0.10.tar.gz

编译

cd redis-4.0.10

make && make  install PREFIX=/opt/redis-cluster      这样相关的数据配置文件就都在这个目录,要不然有些会在/root/  下,如下面这些

mkdir ../redis-cluster

准备配置文件

cp /opt/redis-4.0.10/redis.conf  /opt/redis-cluster   

复制6份redis.conf 修改为redis7000-7005.conf,内容修改以7000为例如下

修改项

注意事项

appendonly yes 
bind 本机IP地址 
cluster-config-file  /opt/redis-cluster/nodes-7000.conf执行启动命令这个文件默认是在当前目录生成,使用绝对路径可以防止下次重启执行命令在当前路径下找不到,不能自动加入到集群
cluster-enabled yes 
daemonize  yes 
dbfilename  dump7000.rdb相对路径,在哪个目录执行启动命令就在那个目录生成,所以统一在/opt/redis-cluster 这个目录执行启动命令
pidfile /var/run/redis_7000.pid 
port 7000 

安装ruby和启动各个redis节点

yum install -y ruby
gem install redis-3.3.5.gem

/opt/redis-4.0.10/src/redis-server /opt/redis-cluster/redis7000.conf > /opt/redis-cluster/redis-7000.out 2>&1 &
/opt/redis-4.0.10/src/redis-server /opt/redis-cluster/redis7001.conf > /opt/redis-cluster/redis-7001.out 2>&1 &
/opt/redis-4.0.10/src/redis-server /opt/redis-cluster/redis7002.conf > /opt/redis-cluster/redis-7002.out 2>&1 &
/opt/redis-4.0.10/src/redis-server /opt/redis-cluster/redis7003.conf > /opt/redis-cluster/redis-7003.out 2>&1 &
/opt/redis-4.0.10/src/redis-server /opt/redis-cluster/redis7004.conf > /opt/redis-cluster/redis-7004.out 2>&1 &
/opt/redis-4.0.10/src/redis-server /opt/redis-cluster/redis7005.conf > /opt/redis-cluster/redis-7005.out 2>&1 &

创建集群

/opt/redis-4.0.10/src/redis-trib.rb create --replicas 1 192.168.25.62:7000 192.168.25.62:7001 192.168.27.79:7002 192.168.27.79:7003 192.168.27.106:7004 192.168.27.106:7005

集群查看

/opt/redis-4.0.10/src/redis-trib.rb info 192.168.25.62:7000

集群检查


/opt/redis-4.0.10/src/redis-trib.rb check 192.168.25.62:7000

集群添加节点

/opt/redis-4.0.10/src/redis-trib.rb add-node --slave 192.168.27.79:7003  192.168.25.62:7000(已存在的节点)

集群的节点关闭

/opt/redis-4.0.10/src/redis-cli -h 192.168.25.62  -p 7001 shutdown   

注意:在上次执行命令的路径执行,或者在nodes-7000.conf文件的路径执行

springboot集成

yml配置

spring:
  redis:
    # 地址
#    host: 192.168.27.106
    # 端口,默认为6379
#    port: 7004
    # 数据库索引
#    database: 0
    # 密码
    password:
    # 连接超时时间
    timeout: 10s
    lettuce:
      pool:
        # 连接池中的最小空闲连接
        min-idle: 0
        # 连接池中的最大空闲连接
        max-idle: 8
        # 连接池的最大数据库连接数
        max-active: 8
        # #连接池最大阻塞等待时间(使用负值表示没有限制)
        max-wait: -1ms
    cluster:
      max-redirects: 3
      nodes:
       - 192.168.25.62:7000
       - 192.168.25.62:7001
       - 192.168.27.79:7002
       - 192.168.27.79:7003
       - 192.168.27.106:7004
       - 192.168.27.106:7005

pom文件

     <!-- redis 缓存操作 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

        <!-- pool 对象池 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
        </dependency>

redisTemplate其他地方正常用redisTemplate自动驻入

这里设置了所有的key的默认失效时间(3小时)和序列化方式

或者直接使用spring提供的注解@Cacheable("hello:info")在方法上


import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.time.Duration;

@Configuration
@EnableCaching
public class RedisConfig {
    private static final StringRedisSerializer STRING_SERIALIZER = new StringRedisSerializer();
    private static final GenericJackson2JsonRedisSerializer JACKSON__SERIALIZER = new GenericJackson2JsonRedisSerializer();

    @Bean
    public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
        //设置缓存过期时间
        RedisCacheConfiguration redisCacheCfg = RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofHours(3))
                .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(STRING_SERIALIZER))
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(JACKSON__SERIALIZER));
        return RedisCacheManager.builder(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory))
                .cacheDefaults(redisCacheCfg)
                .build();
    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值