导入jedis
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
</dependency>
新建一个redis.properties配置文件
1. Springboot整合redis
1.1 配置文件
redis.host=192.168.126.129
redis.port=6379
1.2 编写jedis测试类
public class RedisTests {
@Test
public void test01(){
Jedis jedis = new Jedis("192.168.126.129",6379);
jedis.set("test", "1111");
System.out.println(jedis.keys("*"));
}
}
1.3 编写redis配置类
@Configuration
@PropertySource("classpath:/properties/redis.properties")
public class RedisConfig {
@Value("${redis.host}")
private String host;
@Value("${redis.port}")
private Integer port;
@Bean
public Jedis jedis(){
return new Jedis(host,port);
}
}
2.Springboot整合redis分片
2.1 配置文件
redis.nodes=192.168.126.129:6379,192.168.126.129:6380,192.168.126.129:6381
2.2 编写jedis测试类
public class RedisTests {
@Test
public void test05(){
List<JedisShardInfo> shardInfoList = new ArrayList<>();
shardInfoList.add(new JedisShardInfo("192.168.126.129", 6379));
shardInfoList.add(new JedisShardInfo("192.168.126.129", 6380));
shardInfoList.add(new JedisShardInfo("192.168.126.129", 6381));
ShardedJedis shardedJedis = new ShardedJedis(shardInfoList);
shardedJedis.set("shards", "redis-shards");
System.out.println(shardedJedis.get("shards"));
}
}
2.3 编写redis配置类
@Configuration
@PropertySource("classpath:/properties/redis.properties")
public class RedisConfig {
@Value("${redis.nodes}")
private String nodes;
@Bean
public ShardedJedis jedis(){
List<JedisShardInfo> shards = new ArrayList<>();
String[] nodeArray= nodes.trim().split(",");
for (String strNode : nodeArray){
String host = strNode.split(":")[0];
int port = Integer.parseInt(strNode.split(":")[1]);
JedisShardInfo info = new JedisShardInfo(host, port);
shards.add(info);
}
return new ShardedJedis(shards);
}
}
3.Springboot整合redis哨兵
3.1 配置文件
redis.sentinel=192.168.126.129:26379
3.2 编写jedis测试类
public class RedisTests {
@Test
public void test05(){
Set<String> set = new HashSet<>();
set.add("192.168.126.129:26379");
JedisSentinelPool sentinelPool = new JedisSentinelPool("mymaster",set);
Jedis jedis = sentinelPool.getResource();
jedis.set("aa","哨兵测试");
System.out.println(jedis.get("aa"));
}
}
3.3 编写redis配置类
@Configuration
@PropertySource("classpath:/properties/redis.properties")
public class RedisConfig {
@Value("${redis.sentinel}")
private String sentinel;
@Bean
public JedisSentinelPool jedis(){
Set<String> set = new HashSet<>();
set.add(sentinel);
return new JedisSentinelPool("mymaster",set);
}
}
4.Springboot整合redis集群
4.1 配置文件
redis.clusters=192.168.126.129:7000,192.168.126.129:7001,192.168.126.129:7002,192.168.126.129:7003,192.168.126.129:7004,192.168.126.129:7005
4.2 编写jedis测试类
public class RedisTests {
@Test
public void testCluster(){
Set<HostAndPort> sets = new HashSet<>();
sets.add(new HostAndPort("192.168.126.129", 7000));
sets.add(new HostAndPort("192.168.126.129", 7001));
sets.add(new HostAndPort("192.168.126.129", 7002));
sets.add(new HostAndPort("192.168.126.129", 7003));
sets.add(new HostAndPort("192.168.126.129", 7004));
sets.add(new HostAndPort("192.168.126.129", 7005));
JedisCluster jedisCluster = new JedisCluster(sets);
jedisCluster.set("test", "shishi");
System.out.println(jedisCluster.get("test"));
}
}
4.3 编写redis配置类
@Configuration
@PropertySource("classpath:/properties/redis.properties")
public class RedisConfig {
@Value("${redis.clusters}")
private String clusters;
@Bean
public JedisCluster jedis(){
Set<HostAndPort> nodes = new HashSet<>();
String[] nodeArray= clusters.trim().split(",");
for (String strNode : nodeArray){
String host = strNode.split(":")[0];
int port = Integer.parseInt(strNode.split(":")[1]);
nodes.add(new HostAndPort(host,port));
}
return new JedisCluster(nodes);
}
}