Windows安装Redis集群,并整合SpringBoot2
Redis是用C语言开发的,一般越是接近底层的语言,它所开发出来的东西,在运行速度上就越快,性能上也越优。Redis不仅仅是内存数据库,也可以将数据持久化到文件里,还可以集群。Redis集群又分为三种,一种是主从库模式,一种是Sentinel模式,还有一种就是Cluster模式。
现在来探讨一下Cluster模式,Redis在Windows上是如何集成的。
首先,Redis下载安装配置;
1)由于这里使用的win10系统,官网上又没有支持win10的最新版本,所以只能使用3.2版本的redis。redis下载地址: https://github.com/MSOpenTech/redis/releases

2)下载后,解压到一个文件夹下,用来创建redis集群;

3)打开redis.windows.conf,加入以下配置,并把默认配置注释掉;
port 6379
#开启实例的集群模式
cluster-enabled yes
#设定保存节点配置文件的路径
cluster-config-file nodes-6379.conf
cluster-node-timeout 5000
appendonly yes
4)加一个start.bat文件,用于每个redis的启动,内容如下;
@echo off
redis-server.exe redis.windows.conf
@pause
5)把Redis-6379文件夹拷贝成五份,把文件名和配置文件里的端口修改掉;

第二步,Ruby的安装;
1)下载地址:https://rubyinstaller.org/downloads/

2)安装;



第三步,安装Ruby关于Redis的驱动redis-xxxx.gem;
1)下载地址:https://rubygems.org/pages/download

2)解压,来到解压目录,执行命令;
ruby setup.rb

3)安装gem驱动
gem install redis

第四步,下载redis-trib.rb
1)下载地址:https://raw.githubusercontent.com/MSOpenTech/redis/3.2/src/redis-trib.rb
将文件另存在redis-cluster文件夹下;

第五步,运行集群命令;
1)把每个redis启动,点击目录下的start.bat文件启动,共启动六个redis窗口;

2)使用管理员身份进入redis集群文件夹下,执行集群命令;
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

看到这个界面,redis集群已经安装成功了,接下来使用客户端进行连接测试;

安装完成后,可以更改配置文件,设置密码,然后重启;
requirepass 123123
masterauth 123123
接下来,就看看是springboot2下是如何集成的;
首先,在pom中引入必须的架包;
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
第二步,添加配置文件;
#Redis cluster
spring.redis.lettuce.pool.max-idle=100
spring.redis.lettuce.pool.min-idle=50
spring.redis.lettuce.pool.max-wait=2000
spring.redis.cluster.nodes=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
spring.redis.cluster.max-redirects=6
spring.redis.password=
第三步,添加redis的config文件;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* redis集群配置
* @author 程就人生
* @date 2020年1月17日
*/
@Configuration
@AutoConfigureAfter(RedisAutoConfiguration.class)
public class CRedisConfig {
@SuppressWarnings({ "rawtypes", "unchecked" })
@Bean
public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
template.setConnectionFactory(factory);
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;
}
}
第四步,测试文件;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class IndexController {
@Autowired
private RedisTemplate<String,Object> redisTemplate;
@GetMapping("/index")
public void add(String key,String val){
redisTemplate.opsForHash().put("AAAA", key, val);
}
}
最后,测试;
启动项目,在浏览器地址栏输入:http://localhost:8080/index?key=aa&val=bbbbb

参考资料:
https://blog.csdn.net/zsg88/article/details/73715947
redis各版本功能说明
https://www.cnblogs.com/xingzc/p/9546849.html