pom文件
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
java代码
// 连接池配置文件
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(20);
jedisPoolConfig.setMaxIdle(10);
jedisPoolConfig.setMinIdle(5);
// 创建连接池 ,配置文件,ip,端口,超时,密码
JedisPool jedisPool = new JedisPool(jedisPoolConfig, "192.168.150.100", 6379, 3000, null);
Jedis jedis = null;
try {
//获取连接
jedis = jedisPool.getResource();
System.out.println(jedis.set("jamin", "666"));
System.out.println(jedis.get("jamin"));
} catch (Exception e) {
e.printStackTrace();
}
//连接池配置
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(2000);
jedisPoolConfig.setMaxIdle(1000);
jedisPoolConfig.setMinIdle(5);
//master名称
String masterName = "mymaster";
//哨兵
HashSet<String> hashSet = new HashSet<>();
hashSet.add(new HostAndPort("192.168.150.100", 26379).toString());
hashSet.add(new HostAndPort("192.168.150.100", 26380).toString());
hashSet.add(new HostAndPort("192.168.150.100", 26381).toString());
//创建连接池
JedisSentinelPool jedisSentinelPool = new JedisSentinelPool(masterName, hashSet, jedisPoolConfig, 3000, null);
Jedis jedis = null;
int i = 1;
while (true) {
try {
jedis = jedisSentinelPool.getResource();
jedis.set("sentinel" + i, "sentinel" + i);
System.out.println("sentinel" + i);
i++;
Thread.sleep(3000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
```java
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMinIdle(5);
// 最大空闲数量
jedisPoolConfig.setMaxIdle(10);
jedisPoolConfig.setMaxTotal(20);
//创建集群节点
HashSet<HostAndPort> jedisClusterNode = new HashSet<>();
jedisClusterNode.add(new HostAndPort("192.168.150.101", 8001));
jedisClusterNode.add(new HostAndPort("192.168.150.102", 8002));
jedisClusterNode.add(new HostAndPort("192.168.150.103", 8003));
jedisClusterNode.add(new HostAndPort("192.168.150.101", 8004));
jedisClusterNode.add(new HostAndPort("192.168.150.102", 8005));
jedisClusterNode.add(new HostAndPort("192.168.150.103", 8006));
JedisCluster cluster = null;
try {
// 第一个5000 连接超时时间 第二个5000 等待返回超时时间 10 最大尝试连接次数 jamin密码
cluster = new JedisCluster(jedisClusterNode, 5000, 5000, 10, "jamin", jedisPoolConfig);
System.out.println(cluster.set("test11", "111"));
System.out.println(cluster.get("test11"));
} catch (Exception e) {
e.printStackTrace();
}
}
```
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
spring:
redis:
#单机
# host: 192.168.150.100
# port: 6380
# 哨兵
# sentinel:
# master: mymaster
# nodes: 192.168.150.100:26379,192.168.150.100:26380,192.168.150.100:26381
# 集群
cluster:
nodes: 192.168.150.101:8001,192.168.150.101:8002,192.168.150.102:8003,192.168.150.102:8004,192.168.150.103:8005,192.168.150.103:8006
# 密码
password: jamin
package cn.jaminye.springbootredissentinel.test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
/**
* @author Jamin
* @date 2020/8/1 12:21
* 测试springboot连接redis
*/
@SpringBootTest
public class Test {
@Autowired
RedisTemplate redisTemplate;
@Autowired
StringRedisTemplate stringRedisTemplate;
@org.junit.jupiter.api.Test
public void test() {
//set值 redisTemplate使用的是jdk的序列化策略 存入数据库的是不可读的例如"\xac\xed\x00\x05t\x00\x03key"也只能使用 redisTemplate取出
redisTemplate.opsForValue().set("key", "value");
// stringRedisTemplate使用的是String的redis序列化策略 是易读的 存入数据库的是可读 也只能使用 stringRedisTemplate取出
stringRedisTemplate.opsForValue().set("key", "value");
//get值
String value = String.valueOf(redisTemplate.opsForValue().get("key"));
String values = stringRedisTemplate.opsForValue().get("key");
System.out.println(value);
System.out.println(values);
}
}
原文:https://www.cnblogs.com/JaminYe/p/13414297.html