Springboot整合Redis说明

导入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);
    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值