【极光系列】springboot集成redis

【极光系列】springboot集成redis

tips:主要用于快速搭建环境以及部署项目入门

gitee地址

直接下载源码可用 https://gitee.com/shawsongyue/aurora.git

模块:aurora_redis

window安装redis安装步骤

1.下载资源包

直接下载解压:https://pan.baidu.com/s/1lJZsgAINREGTFq7TksA05g
提取码:18ki

2.配置环境变量

此电脑--属性--高级系统设置--环境变量--找到系统变量path(新建E:\redis)--确定---应用即可

3.启动redis服务

#1.进入cmd命令行

#2.指定路径配置启动
redis-server E:\redis\redis.windows.conf

【极光系列】springboot集成redis_第1张图片

4.设置密码

配置路径在解压目录下:E:\redis\redis.windows.conf,在配置文件中找到 # requirepass foobared,然后在下面增加一行requirepass 后面即是你需要定义的密码,我定义的是aurora:requirepass aurora

配置以后再重新执行步骤3启动服务

【极光系列】springboot集成redis_第2张图片

5.连接客户端

#进入cmd命令行,注意里面存储的数据是二进制乱码
redis-cli

#认证
auth aurora

#设置数据
set "mykey" "myvalue"

#获取数据
get "mykey"

【极光系列】springboot集成redis_第3张图片

项目集成reids

1.引入pom.xml依赖



    4.0.0

    com.xsy
    aurora_redis
    1.0-SNAPSHOT

    
    
        org.springframework.boot
        spring-boot-starter-parent
        2.3.5.RELEASE
    

    
    
        
        1.8
        
        3.8.1
        
        UTF-8
        
        UTF-8
        
        1.2.75
    

    
    

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
        
            org.projectlombok
            lombok
        

        
        
            com.alibaba
            fastjson
            ${fastjson.version}
        

        
        
            org.springframework.boot
            spring-boot-starter-data-redis
        

    

    
    
        ${project.name}
        
        
            
                src/main/resources
            
            
                src/main/java
                
                    **/*.xml
                
            
        

        
        
            
                
                
                    org.springframework.boot
                    spring-boot-maven-plugin
                    ${spring.boot.version}
                    
                        true
                        ${project.build.finalName}
                    
                    
                        
                            
                                repackage
                            
                        
                    
                

                
                
                    maven-compiler-plugin
                    ${maven.plugin.version}
                    
                        ${java.version}
                        ${java.version}
                        UTF-8
                        
                            -parameters
                        
                    
                
            
        
    

    
    
        
            aliyun-repos
            https://maven.aliyun.com/nexus/content/groups/public/
            
                false
            
        
    

    
    
        
            aliyun-plugin
            https://maven.aliyun.com/nexus/content/groups/public/
            
                false
            
        
    


2.修改配置

#服务配置
server:
  #端口
  port: 7004

#spring配置
spring:
  #应用配置
  application:
    #应用名
    name: aurora_redis

  #redis配置
  redis:
    host: localhost
    port: 6379
    password: aurora
    #Redis使用的数据库
    database: 0


3.包结构如下

【极光系列】springboot集成redis_第4张图片

4.创建主启动类

package com.aurora;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author 浅夏的猫
 * @description 主启动类
 * @date 22:46 2024/1/13
 */
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

5.创建redis配置类

package com.aurora.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @description redis配置类
 * @author 浅夏的猫
 * @datetime 22:12 2024/1/15
*/
@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate redisTemplate(LettuceConnectionFactory lettuceConnectionFactory){
        RedisTemplate redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(lettuceConnectionFactory);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new StringRedisSerializer());
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
}

6.创建控制类验证

package com.aurora.controller;

import com.aurora.utils.RedisUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author 浅夏的猫
 * @description 资源controller
 * @date 22:38 2024/1/13
 */
@RestController
@RequestMapping("/resource")
public class ResourceController {

    private static Logger logger = LoggerFactory.getLogger(ResourceController.class);

    @Resource
    private RedisUtil redisUtil;

    /**
     * @return java.util.List
     * @description 查询全部资源数据列表
     * @author 浅夏的猫
     * @date 22:40 2024/1/13
     */
    @RequestMapping("/get")
    public String get() {
        redisUtil.set("aurora", "小白");
        Object aurora = redisUtil.get("aurora") == null ? "" : redisUtil.get("aurora");
        logger.info("测试获取值:{}", aurora.toString());
        return aurora.toString();
    }
}

【极光系列】springboot集成redis_第5张图片

你可能感兴趣的:(极光系列,spring,boot,redis,后端,java)