1.导入maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!--简化pojo包 可省略get set 构造参数等-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.1.6.RELEASE</version>
<exclusions>
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.6.2</version>
</dependency>
2.添加application.yml配置
spring:
redis:
cluster:
ports:
- 7001
- 7002
- 7003
- 7004
- 7005
- 7006
host: 39.96.48.41
poolConfig:
max-total: 8
max-idle: 8 # 连接池中的最大空闲连接
max-wait-millis: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制)
min-idle: 0 # 连接池中的最小空闲连接
3.编写pojo
package com.lwg.chapt3.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
@Data
@AllArgsConstructor //有参构造
@NoArgsConstructor //无参构造
public class Book implements Serializable {
private String name;
private String auth;
}
4.配置Redis
package com.lwg.chapt3.redisConfig;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;
import java.util.ArrayList;
import java.util.List;
@Data
@Configuration
//声明配合文件前缀 配置文件中定义的ports数组,host以及连接时配置信息都将被注入下方的三个对应的属性中
@ConfigurationProperties("spring.redis.cluster")
public class RedisConfig {
List<Integer> ports;
String host;
JedisPoolConfig poolConfig;
//1.配置RedisClusterConfiguration实例,设置Redis密码以及Redis节点信息
@Bean
RedisClusterConfiguration redisClusterConfiguration(){
RedisClusterConfiguration configuration = new RedisClusterConfiguration();
List<RedisNode> nodes=new ArrayList<>();
for (Integer port:ports) {
nodes.add(new RedisNode(host,port));
}
configuration.setPassword(RedisPassword.of("109837"));
configuration.setClusterNodes(nodes);
return configuration;
}
//2.根据RedisClusterConfiguration实例以及连接池配置信息创建jedis连接工厂JedisConnectionFactory
@Bean
JedisConnectionFactory jedisConnectionFactory(){
JedisConnectionFactory factory = new JedisConnectionFactory(redisClusterConfiguration(),poolConfig);
return factory;
}
//3.根据JedisConnectionFactory创建RedisTemplate和StringRedisTemplate,同时配置key,value的序列化方式
@Bean
RedisTemplate redisTemplate(){
RedisTemplate redisTemplate = new RedisTemplate();
redisTemplate.setConnectionFactory(jedisConnectionFactory());
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
return redisTemplate;
}
@Bean
StringRedisTemplate stringRedisTemplate(){
StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(jedisConnectionFactory());
stringRedisTemplate.setKeySerializer(new StringRedisSerializer());
stringRedisTemplate.setValueSerializer(new StringRedisSerializer());
return stringRedisTemplate;
}
}
5.编写controller层
1)StringRedisTemplate是RedisTemplate的子类,StringRedisTemplate中的key和value都是字符串,采用的序列化方案是StringRedisSerialization,
2)而RedisTemplate则可以用来操作对象,RedisTemplate采用的序列化方案是JdkSerializationRedisSerializer,无论是StringRedisTemplate还是RedisTemplate操作Redis的方法是一样的,
3)这两种都是通过opsForValue,opsForZset/opsForSet等方法首先获得一个操作对象,再使用该操作对象完成数据的读写。
package com.lwg.chapt3.controller;
import com.lwg.chapt3.pojo.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class BookController {
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private StringRedisTemplate stringRedisTemplate;
@RequestMapping("/test1")
public void test1(){
ValueOperations ops = redisTemplate.opsForValue();
Book book = new Book();
book.setName("redis集群测试");
book.setAuth("lwg");
ops.set("b1",book);
System.out.println(ops.get("b1"));
ValueOperations<String, String> ops1 = stringRedisTemplate.opsForValue();
ops1.set("lwg1","redis集群测试1");
System.out.println(ops1.get("lwg1"));
}
}
6.运行及验证
在浏览器中输入 http://localhost:8080/test1
打开xshell 开启redis集群中的任意一个端口的客户端