一、安装
1,wget http://download.redis.io/releases/redis-5.0.0.tar.gz
2,tar –xvf redis-5.0.0.tar.gz
3,cd redis-5.0.0
4,make
5,make install
安装完检查
redis-server -v
![]()
二、启动
1,编写配置文件
mkdir redis_cluster
mkdir 7001 7002 7003 7004 7005 7006

编写配置文件redis.conf,以7001示例
port 7001
#bind 0.0.0.0
daemonize yes
pidfile /var/run/redis_7001.pid
cluster-enabled yes
cluster-config-file nodes_7001.conf
cluser-node-timeout 15000
appendonly yes
protected-mode no

把配置文件中三处7001修改为对应的端口,然后每个文件夹里面放一个配置文件7001/redis.conf,7002/redis.conf,7003/redis.conf,7004/redis.conf,7005/redis.conf,7006/redis.conf
比如7002文件夹中的redis.conf配置为
port 7002
#bind 0.0.0.0
daemonize yes
pidfile /var/run/redis_7002.pid
cluster-enabled yes
cluster-config-file nodes_7002.conf
cluser-node-timeout 15000
appendonly yes
protected-mode no
下一步启动,回到redis_cluster目录
redis-server ./7001/redis.conf
redis-server ./7002/redis.conf
redis-server ./7003/redis.conf
redis-server ./7004/redis.conf
redis-server ./7005/redis.conf
redis-server ./7006/redis.conf
查看启动情况,第一个是我docker起的不用管
ps -ef | grep redis

使用集群创建命令
redis-cli --cluser create 192.168.200.128:7001 192.168.200.128:7002 192.168.200.128:7003 192.168.200.128:7004 192.168.200.128:7005 192.168.200.128:7006 --cluster-replicas 1
然后进一个节点
redis-cli -c -h 192.168.200.128 -p 7001
cluster nodes
cluster info

集群创建成功了
三、程序连接
pom依赖
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml</groupId>
<artifactId>jackson-xml-databind</artifactId>
<version>0.6.0</version>
</dependency>
</dependencies>
config配置类
package redis.study.config;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import java.util.Arrays;
@Configuration
public class redisConfig extends CachingConfigurerSupport {
@Bean
public RedisTemplate<String,Object> getRedisTemplate(RedisConnectionFactory redisConnectionFactory){
RedisTemplate<String,Object> redisTemplate = new RedisTemplate<String, Object>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer();
redisTemplate.setDefaultSerializer(serializer);
return redisTemplate;
}
@Bean
@Override
public KeyGenerator keyGenerator(){
return (target,method,objects) -> {
StringBuilder sb = new StringBuilder();
sb.append(target.getClass().getName()).append(method.getName()).append(Arrays.toString(objects));
return sb.toString();
};
}
}
yml配置
server:
port: 8001
servlet:
context-path: /
spring:
redis:
cluster:
nodes:
- 192.168.200.128:7001
- 192.168.200.128:7002
- 192.168.200.128:7003
- 192.168.200.128:7004
- 192.168.200.128:7005
- 192.168.200.128:7006
max-redirects: 3
database: 0
jedis:
pool:
#连接池最大连接数默认是8
max-active: 1000
#连接池最大阻塞时间,-1表示无限制,默认-1
max-wait: -1
#连接池最大空闲数,默认8
max-idle: 10
#连接池最小空闲数,默认0
min-idle: 10
测试类
package mytest.RedisTest;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import redis.Application;
@SpringBootTest(classes = Application.class)
@RunWith(SpringRunner.class)
public class Test1 {
@Autowired
RedisTemplate redisTemplate;
@Test
public void test1(){
Long aLong = redisTemplate.opsForList().leftPush("jinlong", "wang");
System.out.println(redisTemplate.opsForList().rightPop("jinlong"));
//System.out.println(aLong);
}
}
结果:

如果没有连接上,排查一下:
1,IP是否能ping通
2,保护模式有没有关
3,防火墙有没有关systemcl stop firewalld
ok,集群的搭建以及程序连接完成了!
本文详细介绍了Redis的安装、启动及程序连接过程。安装通过一系列命令完成,启动需编写配置文件并在指定目录启动各节点,使用命令创建集群。程序连接涉及pom依赖、config配置类、yml配置和测试类,若连接不上可从IP、保护模式、防火墙等方面排查。
571





