目录
一、 maven配置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.forecharts</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
<elasticsearch.version>7.5.0</elasticsearch.version>
</properties>
<dependencies>
<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>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.5.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>7.5.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.5.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
二、yml文件配置
spring:
redis:
cluster:
node:192.168.109.134:6379,192.168.109.134:6380,192.168.109.134:6381
password:
database: 0
pool:
max-active: 20 # 最大连接数 20个,如果是负数则没有限制
max-wait: -1 # 最大阻塞时间 如果是 负数则没有要求
max-Idle: 20 #最大空闲连接 idle 表示空闲或者虚度的意思
min-Idle: 0 #最小的空闲连接
timeout: 6000 #连接node节点超时时间
三 、 Config代码
package com.example.demo.com.exmaple.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;
import java.util.HashSet;
import java.util.Set;
@Configuration
public class RedisClusterConfig {
@Value("${spring.redis.cluster.nodes}")
private String clusternodes;
@Value("${spring.redis.database}")
private int database;
@Value("${spring.redis.pool.max-Active}")
private int maxActive;
@Value("${spring.redis.pool.max-idle}")
private int maxidle;
@Value("${spring.redis.pool.min-idle}")
private int minidle;
@Value("${spring.redis.pool.max-wait}")
private long maxwait;
@Value("${spring.redis.timeout}")
private int timeout;
@Bean
public JedisCluster getjedisCluster()
{
return new JedisCluster(getHostport(),timeout,getjedispoolConfig());
}
private Set<HostAndPort> getHostport()
{
String []nodes=clusternodes.split(",");
Set<HostAndPort> hostAndPorts=new HashSet<>();
for(String node:nodes)
{
String[] hp=node.split(":");
hostAndPorts.add(new HostAndPort(hp[0],Integer.parseInt(hp[1])));
}
return hostAndPorts;
}
private JedisPoolConfig getjedispoolConfig()
{
JedisPoolConfig jedisPoolConfig=new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(maxidle);
jedisPoolConfig.setMinIdle(minidle);
jedisPoolConfig.setMaxWaitMillis(maxwait);
jedisPoolConfig.setMaxTotal(maxActive);
return jedisPoolConfig;
}
}
四、测试用例
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisCluster;
@RestController
public class RedisJedistest {
@Autowired
private JedisCluster jedisCluster;
@GetMapping("/test")
public void test()
{
jedisCluster.set("name1","tom1");
String username=jedisCluster.get("name1");
System.out.println("username=="+username);
}
}
测试包: