Redis安装与SpringBoot项目整合详细教程

Redis 简介

Redis是一个使用C编写的基于内存的NoSQL数据库,它是目前最流行的键值对存储数据库
Redis安装与SpringBoot项目整合详细教程

Redis 安装

下载 Redis

执行下面的命令

1.创建redis文件夹 mkdir redis
2.进入redis文件夹 cd redis
3.使用wget命令下载redis:wget http://download.redis.io/releases/redis-5.0.3.tar.gz
(没有wget命令,先使用yum install wget 安装)

[[email protected] ~]$ pwd
/home/master
[[email protected] ~]$ mkdir redis
[[email protected] ~]$ cd redis
[[email protected] redis]$ wget http://download.redis.io/releases/redis-5.0.3.tar.gz

安装 Redis

1.解压下载的文件夹并进行编译

[[email protected] redis]$ tar -zxvf redis-5.0.3.tar.gz
[[email protected] redis]$ cd redis-5.0.3
[[email protected] redis-5.0.3]$ make MALLOC=libc
  • 当出现下面这些错误时说明缺少gcc,使用命令 yum install gcc 安装
    Redis安装与SpringBoot项目整合详细教程
    由于我当前不是root用户,使用sudo 命令安装后 yes 就好了,安装成功后再进行上述的编译安装
[[email protected] redis-5.0.3]$ sudo yum install gcc

gcc安装完成
Redis安装与SpringBoot项目整合详细教程
编译Redis,并安装

[[email protected] redis-5.0.3]$ make MALLOC=libc
[[email protected] redis-5.0.3]$ sudo make install

安装完成
Redis安装与SpringBoot项目整合详细教程

配置 Redis

Redis 安装成功后,编辑配置redis.conf 文件,进行如下修改

[[email protected] redis-5.0.3]$ vim redis.conf 

1.设置运行外网连接:注释#bind 127.0.0.1,默认情况下只允许本地连接
2.设置登录密码:添加一行 requirepass 12345,12345是密码
3.设置Redis 后台启动:找到daemonize no 改成 daemonize yes
4.关闭保护模式:找到protected-mode yes 改成 protected-mode no

配置CenOS

为了使外网能连接上Redis,还需要关闭CenOS的防火墙
CenOS 6.5 命令如下,其他版本的系统请查阅相关命令

[[email protected] redis-5.0.3]$ sudo chkconfig iptables off

Redis 启动

[[email protected] redis-5.0.3]$ redis-server redis.conf 
[[email protected] redis-5.0.3]$ redis-cli -a 12345

Redis安装与SpringBoot项目整合详细教程
测试set,get命令
Redis安装与SpringBoot项目整合详细教程
至此,单机版的Redis 配置完成,下面进行和spring boot 项目的整合。

整合Spring Boot

1.创建Spring Boot 项目
这里使用IDEA快速创建
修改pom.xml 文件,添加redis 依赖和 thymeleaf依赖

<!--thymeleaf 依赖包-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

<!--redis 依赖包-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    <exclusions>
        <exclusion>
            <groupId>io.lettuce</groupId>
            <artifactId>lettuce-core</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<!--redis client-->
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
</dependency>

2.配置Redis
application.properties中redis的配置如下

# 配置 thymeleaf
spring.thymeleaf.cache=false
spring.thymeleaf.check-template=true
spring.thymeleaf.check-template-location=true
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.servlet.content-type=text/html
spring.thymeleaf.suffix=.html

# 配置 redis
spring.redis.database=0
spring.redis.host=192.168.18.101 #cenos虚拟机IP
spring.redis.port=6379           #redis 绑定端口
spring.redis.password=12345      #redis 登录密码
spring.redis.jedis.pool.max-active=8
spring.redis.jedis.pool.max-idle=8
spring.redis.jedis.pool.max-wait=-1ms
spring.redis.jedis.pool.min-idle=0

3.创建实体类和Controller
Student 类实现Serializable接口

package com.ffish.spring_boot_reids.model;

import java.io.Serializable;

/**
 * @Auther: shup
 * @Date: 2019/3/17 12:07
 * @Description: TODO
 */
public class Student implements Serializable {
    private String name;
    private String gender;
    private String hometown;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getHometown() {
        return hometown;
    }

    public void setHometown(String hometown) {
        this.hometown = hometown;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", gender='" + gender + '\'' +
                ", hometown='" + hometown + '\'' +
                '}';
    }
}

StudentController测试类

package com.ffish.spring_boot_reids.controller;

import com.ffish.spring_boot_reids.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * @Auther: shup
 * @Date: 2019/3/17 12:11
 * @Description: TODO
 */
@Controller
public class StudentController {
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    StringRedisTemplate stringRedisTemplate;

	//测试存入字符串
    @GetMapping("/testRedis")
    public String testRedis(String test){
        ValueOperations<String,String> ops = stringRedisTemplate.opsForValue();
        ops.set("test",test);
        return "测试数据test:"+ops.get("test");
    }
    //存入对象
    @GetMapping("/save")
    public String save(Student student){
        ValueOperations ops = redisTemplate.opsForValue();
        ops.set("student",student);
        System.out.println(((Student) ops.get("student")).toString());
        return "redirect:/get";
    }

	//取出对象
    @GetMapping("/get")
    public String get(Model model){
        ValueOperations ops = redisTemplate.opsForValue();
        Student student = (Student) ops.get("student");
        System.out.println(student.toString());
        model.addAttribute("student",student);
        return "show";
    }
}

static文件夹下添加edit.html

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
        <form action="/save" method="get">
            <label>name:</label>
            <input type="text" name="name" >
            <label>gender:</label>
            <input type="text" name="gender" >
            <label>hometown:</label>
            <input type="text" name="hometown" >
            <button type="submit">save</button>
        </form>
    </body>
</html>

templates文件夹下添加show.html

<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
        show:
        <label>name:</label>
        <input type="text" th:value="${student.name}" >
        <label>gender:</label>
        <input type="text" th:value="${student.gender}" >
        <label>hometown:</label>
        <input type="text" th:value="${student.hometown}" >
    </body>
</html>

4.测试
testRedis方法,存储字符串
Redis安装与SpringBoot项目整合详细教程
查看Redis上有无test数据
Redis安装与SpringBoot项目整合详细教程
可以发现test 已经存入redis了

下面测试往redis 存储对象
Redis安装与SpringBoot项目整合详细教程