记录:451
场景:在Spring Boot微服务使用JedisCluster操作Redis集群的Hash哈希散列数据类型。
版本:JDK 1.8,Spring Boot 2.6.3,redis-6.2.5,jedis-3.7.1。
1.微服务中配置Redis信息
1.1在pom.xml添加依赖
pom.xml文件:
<dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>3.7.1</version>
</dependency>
解析:在Spring Boot中默认集成jedis,使用无需加版本号,本例版本3.7.1是Spring Boot 2.6.3对应的版本。
1.2在application.yml中配置Redis集群信息
(1)application.yml配置内容
hub:example:redis:jedis-cluster:password: demo12345678connection-timeout: 60000so-timeout: 1000max-attempts: 100nodes:- 192.168.19.161:27001- 192.168.19.161:27002- 192.168.19.162:27001- 192.168.19.162:27002- 192.168.19.163:27001- 192.168.19.163:27002
(2)解析
在application.yml中配置内容是自定义的,对应自定义配置类JedisClusterProperties。
类全称:com.hub.example.config.JedisClusterProperties。
自定义配置类使用如下注解生效:
@ConfigurationProperties(prefix = "hub.example.redis.jedis-cluster")
1.3加载简要逻辑
Spring Boot微服务在启动时,自动注解机制会读取application.yml的配置信息注入到自定义配置类JedisClusterProperties对象的对应属性。因此,在Spring环境中就能取到Redis集群的配置信息。
Spring从JedisClusterProperties对象中取配置注入到JedisCluster客户端中。因此,JedisCluster客户端就能对Redis集群做增、删、改、查等操作。
2.配置JedisCluster
JedisCluster是jedis框架中封装的操作Redis的客户端。
类全称:redis.clients.jedis.JedisCluster
2.1配置JedisClusterProperties
JedisClusterProperties是自定义配置了,作用是加载application.yml中Redis集群的配置信息。
使用@ConfigurationProperties注解生效,使用注解的prefix指定配置application.yml中前缀。
@Component
@ConfigurationProperties(prefix = "hub.example.redis.jedis-cluster")
public class JedisClusterProperties {private List<String> nodes;private String password;private int connectionTimeout;private int soTimeout;private int maxAttempts;public List<String> getNodes() {return nodes;}public void setNodes(List<String> nodes) {this.nodes = nodes;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public int getConnectionTimeout() {return connectionTimeout;}public void setConnectionTimeout(int connectionTimeout) {this.connectionTimeout = connectionTimeout;}public int getSoTimeout() {return soTimeout;}public void setSoTimeout(int soTimeout) {this.soTimeout = soTimeout;}public int getMaxAttempts() {return maxAttempts;}public void setMaxAttempts(int maxAttempts) {this.maxAttempts = maxAttempts;}
}
2.2配置JedisCluster
@Configuration
public class JedisClusterConfig {@AutowiredJedisClusterProperties jedisClusterProperties;@Bean("jedisCluster")public JedisCluster getJedisCluster(JedisPoolConfig jedisPoolConfigCluster) {List<String> nodesList = jedisClusterProperties.getNodes();Set<HostAndPort> nodesSet = new HashSet<>();for (String ipAndPort : nodesList) {String[] ipAndPortPair = ipAndPort.split(":");nodesSet.add(new HostAndPort(ipAndPortPair[0].trim(), Integer.parseInt(ipAndPortPair[1].trim())));}return new JedisCluster(nodesSet,jedisClusterProperties.getConnectionTimeout(),jedisClusterProperties.getSoTimeout(),jedisClusterProperties.getMaxAttempts(),jedisClusterProperties.getPassword(),jedisPoolConfigCluster);}@Bean("jedisPoolConfigCluster")public JedisPoolConfig jedisPoolConfigCluster() {JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();jedisPoolConfig.setMaxTotal(30);jedisPoolConfig.setMaxIdle(30);jedisPoolConfig.setMinIdle(1);jedisPoolConfig.setNumTestsPerEvictionRun(-1);jedisPoolConfig.setTestOnBorrow(true);jedisPoolConfig.setTestOnReturn(false);jedisPoolConfig.setBlockWhenExhausted(false);return jedisPoolConfig;}
}
2.3解析
配置JedisCluster后,在Spring环境中,使用@Autowired注解注入JedisCluster实例操作Redis集群。
3.使用Jedis操作Redis集群Hash哈希散列
3.1简要说明
使用JedisCluster操作集群Hash哈希散列,常用操作:增、查、改、删、设置超时等。
3.2操作示例
@RestController
@RequestMapping("/hub/example/operateJedisCluster")
@Slf4j
public class OperateJedisClusterController {@Autowiredprivate JedisCluster jedisCluster;/*** 使用JedisCluster操作Redis集群Hash哈希散列类型数据*/@GetMapping("/f04")public Object f04() {log.info("JedisCluster操作Redis集群开始...");// 1.增// 1.1写入一条数据jedisCluster.hset("J:2023060804:01", "suzhou", "苏州");jedisCluster.hset("J:2023060804:02", "hangzhou", "杭州");// 1.2写入批量数据Map<String, String> hash = new HashMap<>();hash.put("suzhou01", "苏州-姑苏");hash.put("suzhou02", "苏州-相城");jedisCluster.hset("J:2023060804:01", hash);// 1.3写入批量数据,以byte[]方式byte[] keyF = "J:2023060804:03".getBytes(StandardCharsets.UTF_8);byte[] fieldF = "suzhou05".getBytes(StandardCharsets.UTF_8);byte[] valueF = "吴江".getBytes(StandardCharsets.UTF_8);jedisCluster.hset(keyF, fieldF, valueF);// 2.查// 2.1查询一条数据String city001 = jedisCluster.hget("J:2023060804:01", "suzhou");// 2.2查询批量数据Map<String, String> map = jedisCluster.hgetAll("J:2023060804:01");// 2.3查询hash长度Long len = jedisCluster.hlen("J:2023060804:01");// 2.4查询hash全部KeySet<String> keyResult = jedisCluster.hkeys("J:2023060804:01");keyResult.forEach((key) -> {log.info("keyResult=" + key);});// 2.5查询hash全部KeyList<String> valueResult = jedisCluster.hvals("J:2023060804:01");valueResult.forEach((value) -> {log.info("valueResult=" + value);});// 3.改jedisCluster.hset("J:2023060804:01", "suzhou", "苏州-主城");// 4.删long time = 8000;log.info("{}秒后,删除J:2023060804:01的suzhou01,删除J:2023060804:02", time / 1000);ThreadUtil.sleep(time);jedisCluster.hdel("J:2023060804:01", "suzhou01");jedisCluster.del("J:2023060804:02");// 5.设置超时,单位秒jedisCluster.expire("J:2023060804:03", 600L);// 6.判断键是否存在Boolean result = jedisCluster.hexists("J:2023060804:01", "suzhou02");return "执行成功";}
}
3.3测试验证
使用Postman测试。
请求RUL:http://127.0.0.1:18205/hub-205-redis/hub/example/operateJedisCluster/f04
以上,感谢。
2023年6月8日