redis的安装及springboot配置redis

Nosql :redis , mongodb是当前主流的非关系型数据库,nosql型数据库没有sql查询语句,

阅读本教程前,你需要了解基本的数据结构,例如以下几种:

  • String: 字符串
  • Hash: 散列
  • List: 列表
  • Set: 集合
  • Sorted Set: 有序集合

下面我们开始在linux下安装redis

$ wget http://download.redis.io/releases/redis-2.8.17.tar.gz
$ tar xzf redis-2.8.17.tar.gz
$ cd redis-2.8.17
$ sudo su  #提升为管理员权限执行以下操作
$ make install

下面运行会出现一个图标,表示保护模式

$redis-server redis.conf

进入操作redis,这个时候如果要进入操作模式需要重新打开终端

$redis-cli 

$ping#如果出现Pong表示安装成功了

Pong

$set name "cyc"

ok

扫描二维码关注公众号,回复: 5775837 查看本文章

$get name

"cyc"

可以在控制台SHUTDOWN然后exit退出

我们可以修改redis.conf配置文件

daemonize yes        //配置允许redis启动在后台

#bind 127.0.0.1        //表示允许连接该redis,默认情况下只允许本地连接,将默认配置注释掉,外面就可以了解redis

requirepass 123456   //设置密码

proteced-mode no      //配置了密码登录,就可以关闭保护模式了,关闭了保护模式后运行就没有小图标了

在linux下配置redis要外面机器访问还需要关闭防火墙

开启防火墙:

[root@centos6 ~]# service iptables start

关闭防火墙:

[root@centos6 ~]# service iptables stop

=======================分割线================================

spring boot配置linux下的redis

<!--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>
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
</dependency>

配置.yml

spring:
  redis:
    database: 0        //使用redis的编号,redis中提供了16个database,编号0-15
    host: *.*.*.*      //连接地址,linux下ifconfig查看ip地址
    post: 6379         //对外的端口
    password: 123456   //设置的密码
    jedis:
      pool:
        max-active: 8    //最大的连接数
        max-idle: 8      //最大的空闲连接数
        max-wait: -1ms   //最大堵塞等待时间,默认为-1表示没有限制
        min-idle: 0      //最小空闲连接数
public class Book implements Serializable {
    private int id;
    private String author;
    private String name;
//省略get/set方法
}
测试一下
@Autowired
StringRedisTemplate stringRedisTemplate;
@RequestMapping("/book")
public void book() {
    ValueOperations<String ,String> ops1 = stringRedisTemplate.opsForValue();
    ops1.set("name","三国演义");
    System.out.println(ops1.get("name"));
}

成功,我们再到虚拟机中查看一下key为name的值

已经就name格式了,但是我们在控制台输出是“三国演义”,

猜你喜欢

转载自blog.csdn.net/qq_41426326/article/details/88571910
今日推荐