SpringBoot 整合 SpringDataRedis

1.1 相关依赖

  在使用脚手架创建项目时勾选 Spring Data Redis 即可,也可以创建完 Spring Boot 项目后手动添加以下依赖。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>





1.2 配置文件

spring:
  redis:
    port: 6379  # Redis 服务器连接端口
    host: 127.0.0.1 # Redis 服务器地址
    database: 0 # Redis 数据库索引(默认为0)
    password: # Redis 服务器连接密码(默认为空)
    timeout: 5000ms # 连接超时时间(毫秒)
    jedis:
      pool:
        max-active: 8 # 连接池最大连接数(使用负值表示没有限制)
        max-wait: -1ms  # 连接池最大阻塞等待时间(使用负值表示没有限制)
        max-idle: 8 # 连接池中的最大空闲连接
        min-idle: 0 # 连接池中的最小空闲连接





1.3 示例

/**
 * Created with IntelliJ IDEA.
 *
 * @author Demo_Null
 * @date 2020/10/10
 * @description Spring Boot 整合 Spring Data Redis
 */
@SpringBootTest
public class RedisTest {
    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void redis() {
        redisTemplate.opsForValue().set("name", "jack");
        Object name = redisTemplate.opsForValue().get("name");
        System.out.println(name);
    }
}

在这里插入图片描述


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot中,可以通过配置文件或编程方式来设置Redis线程池。以下是通过编程方式实现整合Redis并设置Redis线程池的步骤: 1. 首先,在你的Spring Boot项目中,添加对Redis和Lettuce(Redis的Java客户端)的依赖。可以在项目的pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>io.lettuce</groupId> <artifactId>lettuce-core</artifactId> </dependency> ``` 2. 在application.properties或application.yml配置文件中添加Redis相关配置,包括主机名、端口号、密码等。例如: ```yaml spring.redis.host=localhost spring.redis.port=6379 spring.redis.password= ``` 3. 创建Redis连接工厂类,可以使用LettuceConnectionFactory。在该类中,你可以设置与Redis连接相关的属性,包括连接池配置。下面是一个示例: ```java import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisStandaloneConfiguration; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; @Configuration public class RedisConfig { @Value("${spring.redis.host}") private String redisHost; @Value("${spring.redis.port}") private int redisPort; @Value("${spring.redis.password}") private String redisPassword; @Bean public LettuceConnectionFactory redisConnectionFactory() { RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(redisHost, redisPort); config.setPassword(redisPassword); return new LettuceConnectionFactory(config); } @Bean public RedisTemplate<String, Object> redisTemplate() { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory()); return redisTemplate; } } ``` 在上述示例中,LettuceConnectionFactory被创建为一个Bean,并通过RedisStandaloneConfiguration设置了连接的主机名、端口号和密码。然后,将该连接工厂设置到RedisTemplate中。 4. 在需要使用Redis的地方,注入RedisTemplate,并调用其相关方法来操作Redis。例如: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; @Service public class MyService { private final RedisTemplate<String, Object> redisTemplate; @Autowired public MyService(RedisTemplate<String, Object> redisTemplate) { this.redisTemplate = redisTemplate; } public void saveData(String key, Object value) { redisTemplate.opsForValue().set(key, value); } public Object getData(String key) { return redisTemplate.opsForValue().get(key); } } ``` 上述示例中的MyService类注入了RedisTemplate,并通过redisTemplate实现了保存数据和获取数据的方法。 这样,你就可以使用Spring Boot整合Redis并设置Redis线程池了。注意,在实际应用中,你可能还需要做一些其他配置和异常处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值