• SpringBoot连接redis


    1 docker安装redis(6.2.2)

    1.1 下载redis镜像

    sudo docker pull redis:6.2.2
    
    • 1

    1.2 设置配置文件

    手动创建redis.conf,并添加如下配置参数。

    # 可远程连接
    # bind 127.0.0.1
    # 解除保护模式
    protected-mode no
    # 数据持久化
    appendonly yes
    # 设置密码
    requirepass 123456
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    也可以在网上下载redis配置文件,注意需要修改里面的配置。

    wget http://download.redis.io/redis-stable/redis.conf
    
    • 1

    授权配置文件

    chmod 777 redis.conf
    
    • 1

    1.3 生成容器

    sudo docker run -itd \
    --name myredis \
    -p 6379:6379 \
    -v /home/redis/redis.conf:/etc/redis/redis.conf \
    -v /home/redis/data:/data \
    redis:6.2.2 redis-server /etc/redis/redis.conf
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    2 简单使用redis

    # 进入redis
    sudo docker exec -it myredis /bin/bash
    
    # 使用redis
    # 参数--raw,表示按原生数据显示,可以解决中文显示乱码,按原始格式打印
    redis-cli --raw
    
    # 输入密码
    auth 123456
    
    # (1) 键值对
    # 创建键值对
    set name mason
    # 查看键值对
    get name
    
    # (2) 列表
    # 插入列表
    RPUSH user "河南大学"
    # 查看列表中所有数据
    LRANGE users 0 -1
    
    # (3) hash
    # 插入键值对,key:book, field: title, value: "河南大学发展"
    HSET book title "河南大学发展"
    # 查看键值对
    HGET book title
    
    # redis清空所有数据
    flushall
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    3 SpringBoot连接redis

    3.1 工程结构

    在这里插入图片描述

    3.2 pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    	<modelVersion>4.0.0</modelVersion>
    	<parent>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-parent</artifactId>
    		<version>2.2.5.RELEASE</version>
    		<relativePath/> <!-- lookup parent from repository -->
    	</parent>
    	<groupId>com.example</groupId>
    	<artifactId>myredis</artifactId>
    	<version>1.0.0</version>
    	<name>myredis</name>
    	<description>Myredis project for Spring Boot</description>
    	<properties>
    		<java.version>11</java.version>
    	</properties>
    	<dependencies>
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-web</artifactId>
    		</dependency>
    
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-data-redis</artifactId>
    		</dependency>
    
    		<!-- Init the entity -->
    		<dependency>
    			<groupId>org.projectlombok</groupId>
    			<artifactId>lombok</artifactId>
    			<version>1.18.20</version>
    			<scope>provided</scope>
    		</dependency>
    
    
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-test</artifactId>
    			<scope>test</scope>
    		</dependency>
    
    	</dependencies>
    
    	<build>
    		<plugins>
    			<plugin>
    				<groupId>org.springframework.boot</groupId>
    				<artifactId>spring-boot-maven-plugin</artifactId>
    				<version>2.3.1.RELEASE</version>
    			</plugin>
    		</plugins>
    	</build>
    
    </project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57

    3.3 application.yml

    spring:
      redis:
        host: 192.168.108.100
        port: 6379
        password: 123456
    
    • 1
    • 2
    • 3
    • 4
    • 5

    3.4 conf

    ConfigRedis.java

    package com.example.myredis.config;
    
    import org.springframework.beans.factory.annotation.Autowired;
    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.StringRedisSerializer;
    
    @Configuration
    public class ConfigRedis {
        @Autowired
        private RedisConnectionFactory redisConnectionFactory;
    
        @Bean
        public RedisTemplate<String, Object> redisTemplate(){
            RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
    
            // 序列化key
            redisTemplate.setKeySerializer(new StringRedisSerializer());
            redisTemplate.setValueSerializer(new StringRedisSerializer());
    
            // 序列化hash
            redisTemplate.setHashKeySerializer(new StringRedisSerializer());
            redisTemplate.setHashValueSerializer(new StringRedisSerializer());
    
            // 连接redis数据库
            redisTemplate.setConnectionFactory(redisConnectionFactory);
    
            return redisTemplate;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    3.5 controller

    RequestController.java

    package com.example.myredis.controller;
    
    import com.example.myredis.entity.User;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import java.util.HashMap;
    import java.util.Map;
    import java.util.concurrent.TimeUnit;
    
    
    @Controller
    @RequestMapping("/redis")
    @ResponseBody
    public class RequestController {
    
        @Autowired
        private RedisTemplate<String, Object> redisTemplate;
    
        @GetMapping(value = "/key")
        public String putKey(String key, String value){
            this.redisTemplate.opsForValue().set(key,value,30, TimeUnit.SECONDS);
    
            System.out.println(this.redisTemplate.opsForValue().get(key));
            return "1";
        }
    
        @GetMapping(value = "/object")
        public String putObject(){
            User user = new User("河南大学", 10);
            // 序列化对象
            this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(User.class));
            this.redisTemplate.opsForValue().set("user", user,30, TimeUnit.SECONDS);
    
            System.out.println(this.redisTemplate.opsForValue().get("user"));
            return "1";
        }
    
        @GetMapping(value = "/list")
        public String putList(){
    
            this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(User.class));
            this.redisTemplate.opsForList().leftPush("users", new User("河南大学", 10));
    
            System.out.println(this.redisTemplate.opsForList().range("users", 0, -1));
            return "1";
        }
    
        @GetMapping(value = "/map")
        public String putMap(){
            Map<String, String> myMap = new HashMap<>();
            myMap.put("name", "河南大学");
            myMap.put("age", "20");
            this.redisTemplate.opsForHash().putAll("map", myMap);
    
            System.out.println(this.redisTemplate.opsForHash().get("map", "name"));
            return "1";
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66

    3.6 entity

    User.java

    package com.example.myredis.entity;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class User{
        private String name;
        private int age;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    3.7 Application

    package com.example.myredis;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class MyredisApplication {
    
    	public static void main(String[] args) {
    		SpringApplication.run(MyredisApplication.class, args);
    	}
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
  • 相关阅读:
    量化交易之nicegui篇 - dataframe控件以及交互
    企业网络“卫生”实用指南(上)
    day-05 TCP半关闭 ----- DNS ----- 套接字的选项
    一款自动生成CRUD代码的自定义Starter
    Ubuntu系统下使用apt-get安装Mysql8
    2022大厂高频面试题之HTML篇
    RTCM数据解码
    Nginx反向代理和负载均衡
    【机器学习】红酒数据集和加利福尼亚的房价数据的随机森林算法详解
    Java中几个特殊的运算符
  • 原文地址:https://blog.csdn.net/make_progress/article/details/125554205