Springboot 整合 Redis

第1关:Springboot 整合 Redis

任务描述

本关任务:实现 Springboot 整合 Redis。

相关知识

为了完成本关任务,你需要掌握:

  1. 什么是 Redis;
  2. 为什么用 Redis;
  3. Springboot 整合 Redis。
什么是 Redis
  1. redis 是一个开源的、使用 C 语言编写的、支持网络交互的、可基于内存也可持久化的 Key-Value 数据库。

  2. redis 是一个以 key-value 存储的数据库结构型服务器,它支持的数据结构类型包括:字符串(String)、链表(lists)、哈希表(hash)、集合(set)、有序集合(Zset)等。为了保证读取的效率,redis 把数据对象都存储在内存当中,它可以支持周期性的把更新的数据写入磁盘文件中。而且它还提供了交集和并集,以及一些不同方式排序的操作。

为什么用 Redis

原因很简单,快!这个问题在大并发,高负载的网站中必须考虑 redis 数据库中的所有数据都存储在内存中。由于内存的读写速度远快于硬盘,因此 Redis 的的的在性能上对比其他基于硬盘存储的数据库有非常明显的优势。

Springboot 整合 Redis
  1. 导入依赖:
     
      
    1. <!--引入redis-->
    2. <dependency>
    3. <groupId>org.springframework.boot</groupId>
    4. <artifactId>spring-boot-starter-data-redis</artifactId>
    5. </dependency>
    6. <!--springboot2.x集成redis所需common-pool2-->
    7. <dependency>
    8. <groupId>org.apache.commons</groupId>
    9. <artifactId>commons-pool2</artifactId>
    10. <version>2.7.0</version>
    11. </dependency>
  2. 配置 redis:
     
      
    1. @Bean
    2. public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
    3. RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
    4. // 设置value的序列化规则和 key的序列化规则
    5. redisTemplate.setKeySerializer(new StringRedisSerializer());
    6. redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); //通用
    7. // hash类型
    8. redisTemplate.setHashKeySerializer(new StringRedisSerializer());
    9. redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
    10. //注入连接工厂
    11. redisTemplate.setConnectionFactory(redisConnectionFactory);
    12. return redisTemplate;
    13. }
  3. 配置 yaml:
     
      
    1. #redis 的 yaml 配置
    2. spring:
    3. redis:
    4. # redis服务器地址(默认为loaclhost)
    5. host: localhost
    6. # redis端口(默认为6379)
    7. port: 6379
    8. # redis连接超时时间(单位毫秒)
    9. timeout: 1000
    10. # redis数据库索引(默认为0)
    11. database: 0
    12. lettuce:
    13. pool:
    14. # 最大可用连接数(默认为8,负数表示无限)
    15. max-active: 15
    16. # 从连接池中获取连接最大等待时间(默认为-1,单位为毫秒,负数表示无限)
    17. max-wait: 5000ms
    18. # 最大空闲连接数(默认为8,负数表示无限)
    19. max-idle: 10
  4. redis 简单的存取值操作:
     
      
    1. //注入 redis
    2. @Autowired
    3. RedisTemplate redisTemplate;
    4. @Override
    5. public String getRedis() {
    6. //redis 中存值
    7. this.redisTemplate.opsForValue().set("frontToken", "测试redis");
    8. //取出存入 redis 中的值
    9. String value = (String) redisTemplate.opsForValue().get("frontToken");
    10. return value;
    11. }

编程要求

  1. 在 RedisConfig.java 、application-dev.yaml 、UserServiceImpl.java 中 Begin-End 处根据提示补充代码。
  2. 实现 Springboot 整合 Redis 并进行简单的存取值操作。

测试说明

平台会对你编写的代码进行测试:发送请求测试代码是否运行成功。

预期输出: 测试


开始你的任务吧,祝你成功!

RedisConfig.java

package com;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;


@Configuration
public class RedisConfig {

    /**
     * redisTemplate 序列化使用的jdkSerializeable, 存储二进制字节码, 所以自定义序列化类
     * @param redisConnectionFactory
     * @return
     */
    //redis 的config配置
    /******************************Begin******************************/
    @Bean  
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {  
    RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();  
    // 设置value的序列化规则和 key的序列化规则  
    redisTemplate.setKeySerializer(new StringRedisSerializer());  
    redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());    //通用
    //  hash类型  
    redisTemplate.setHashKeySerializer(new StringRedisSerializer());  
    redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
    //注入连接工厂  
    redisTemplate.setConnectionFactory(redisConnectionFactory);  
    return redisTemplate;  
}   
    /******************************End******************************/

}

application-dev.yaml

#redis 的 yaml 配置  
spring:  
redis: 
# redis服务器地址(默认为loaclhost)
 host: localhost  
# redis端口(默认为6379)
 port: 6379  
 # redis连接超时时间(单位毫秒)
 timeout: 1000  
 # redis数据库索引(默认为0)
 database: 0  
 lettuce:  
   pool:  
    # 最大可用连接数(默认为8,负数表示无限)
     max-active: 15  
    # 从连接池中获取连接最大等待时间(默认为-1,单位为毫秒,负数表示无限)
     max-wait: 5000ms  
    # 最大空闲连接数(默认为8,负数表示无限)
     max-idle: 10 

UserServiceImpl.java

package com.www.service.impl;

import com.www.entity.User;
import com.www.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * serviceimpl
 */
@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    RedisTemplate redisTemplate;

    
    @Override
    public String getRedis() {
        this.redisTemplate.opsForValue().set("frontToken", "测试");
        //取出存入 redis 中的值
        /******************************Begin******************************/
        String value ;
        value = (String) redisTemplate.opsForValue().get("frontToken"); 
        /******************************End******************************/
        return value;
    }
    
}

加油,同学们!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值