SpringBoot整合Redis、和Redis集群

一、SpringBoot整合Redis

步骤:1.添加依赖:

<!--redis -->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--jedis -->
<dependency>
   <groupId>redis.clients</groupId>
   <artifactId>jedis</artifactId>
   <version>2.9.0</version>
</dependency>

 

2.application.properties添加Redis的链接信息:

#单个Redis
#spring.redis.host=127.0.0.1
#spring.redis.port=6379

3.springboot启动类添加缓存注解:@EnableCaching

4.在需要缓存的地方使用:@Cacheable(value = "findAllUser")

一般在频繁查询的方法里面里面使用,value则是key值。

 

 

哇,真的就几句话就实现了Redis的缓存了。

 

 

二、SpringBoot整合Redis集群

1.application.properties添加置Redis集群的信息:

#redis集群  模拟4个Redis集群节点
spring.redis.cluster.nodes=127.0.0.1:7001,127.0.0.1:7002,127.0.0.1:7003,127.0.0.1:7004

2.springboot里面添加Redis集群的配置:

package com.zero.learn_spring_boot.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 java.util.HashSet;
import java.util.Set;

/**
 * Created by 81046 on 2018-08-30
 */
@Configuration
public class RedisConfig {

    @Value("${spring.redis.cluster.nodes}")
    private String clusterNodes;
    @Bean
    public JedisCluster getJedisCluster(){
        //分割集群节点
        String[] cNodes = clusterNodes.split(",");
        //创建set集合对象
        Set<HostAndPort> nodes =new HashSet<>();
        for (String node:cNodes) {
            //127.0.0.1:7001
            String[] hp = node.split(":");
            nodes.add(new HostAndPort(hp[0],Integer.parseInt(hp[1])));
        }
        //创建Redis集群对象
        JedisCluster jedisCluster=new JedisCluster(nodes);
        return jedisCluster;
    }
}

 

3.在service里面使用该集群:

@Autowired
private JedisCluster jedisCluster;

@Override
public String findRedis(){
    jedisCluster.set("username","倚天屠龙记");
    return jedisCluster.get("username");
}

4.在controller里面访问该方法:

@GetMapping("/findRedis")
public String findRedis(){
    return userServiceMyBatis.findRedis();
}

自此实现了Redis的集群。

 

 

 

 

下面纯属个人谎言,勿看:

到现在我自己感觉,集群就是相当于有多个单机的Redis,系统缓存的时候,看哪个闲着就优先使用谁存储,这样就实现了更快的速度存储了。

 

 

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
下面是一个简单的 SpringBoot 整合 Redis 集群的代码示例: 1. 添加 Redis 依赖 在 pom.xml 文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 2. 配置 Redis 集群 在 application.properties 文件中添加以下配置: ```properties # 集群节点 spring.redis.cluster.nodes=redis://localhost:7001,redis://localhost:7002,redis://localhost:7003,redis://localhost:7004,redis://localhost:7005,redis://localhost:7006 # 连接超时时间 spring.redis.timeout=10000 # 最大重定向次数 spring.redis.cluster.max-redirects=3 ``` 3. 编写 RedisTemplate 配置类 ```java @Configuration public class RedisConfig { @Autowired private RedisConnectionFactory redisConnectionFactory; @Bean public RedisTemplate<String, Object> redisTemplate() { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory); // 设置序列化方式 redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); return redisTemplate; } } ``` 4. 使用 RedisTemplate 操作 Redis ```java @Service public class RedisService { @Autowired private RedisTemplate<String, Object> redisTemplate; public void set(String key, Object value) { redisTemplate.opsForValue().set(key, value); } public Object get(String key) { return redisTemplate.opsForValue().get(key); } } ``` 以上就是一个简单的 SpringBoot 整合 Redis 集群的代码示例。需要注意的是,Redis 集群的配置方式可能因版本而异,请根据实际情况进行调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值