Redis的安装配置及IDEA中使用

目录

一、安装redis,配置redis.conf

1.安装gcc

2.将redis的压缩包放到指定位置解压 [如下面放在 /opt 目录下]

3.编译安装

4.配置redis.conf文件

5.开机自启

二、解决虚拟机本地可以连接redis但是主机不能连接redis

1.虚拟机网络适配器网络连接设置为桥接模式

2.控制面板查看主机的网络连接信息

3.手动配置虚拟机网络连接,输入与主机同一网络段地址

三、配置远程连接

1.设置防火墙端口

2.使用windows图形化界面

四、IDEA连接redis

1.新建项目

2.引入redis依赖和连接池依赖

3.配置文件

4.注入redisTemplate

5.编写测试(所以代码)

6.查看winsows GUI 界面


一、安装redis,配置redis.conf

1.安装gcc

# Redis是基于C语言编写的,因此首先需要安装Redis所需要的gcc依赖:
yum install -y gcc tcl
gcc -v # 查看gcc版本

2.将redis的压缩包放到指定位置解压 [如下面放在 /opt 目录下]

# 安装reids
ll # 查看目录内容
cd /opt # 进入 /opt 目录,将redis压缩包放在该目录下
tar xzf redis-7.0.0.tar.gz # 解压
cd redis-7.0.0 # 进入redis文件夹
make && make install # 编译安装redis

# 出现以下语句则安装成功
# Hint: It's a good idea to run 'make test' ;)
# - redis-cli:是redis提供的命令行客户端
# - redis-server:是redis的服务端启动脚本
# - redis-sentinel:是redis的哨兵启动脚本

redis-server # 启动redis

3.编译安装

4.配置redis.conf文件

# 更改配置 redis.conf
pwd # 查看当前所在路劲信息
mkdir /myredis # 创建一个鑫目录文件夹用来存放副本reids.conf配置文件
cp redis.conf /myredis # 将redis.conf复制到/myredis 文件目录下
vim redis.conf # 编辑redis.conf配置文件

# redis.conf 的配置更改,使用 /bind 回车找到位置
# 允许访问的地址,默认是127.0.0.1,会导致只能在本地访问。修改为0.0.0.0则可以在任意IP访问,生产环境不要设置为0.0.0.0
bind 0.0.0.0
# 守护进程,修改为yes后即可后台运行
daemonize yes 
# 密码,设置后访问Redis必须输入密码
requirepass 123456

5.开机自启

# 开机自启 
vi /etc/systemd/system/redis.service
# 内容如下
[Unit]
Description=redis-server
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/bin/redis-server /usr/local/src/redis-6.2.6/redis.conf
PrivateTmp=true

[Install]
WantedBy=multi-user.target


# 然后重载系统服务
systemctl daemon-reload

# 启动
systemctl start redis
# 停止
systemctl stop redis
# 重启
systemctl restart redis
# 查看状态
systemctl status redis

# 运行命令实现开机自启
systemctl enable redis
# 其他配置(可选)
# 监听的端口
port 6379
# 工作目录,默认是当前目录,也就是运行redis-server时的命令,日志、持久化等文件会保存在这个目录
dir .
# 数据库数量,设置为1,代表只使用1个库,默认有16个库,编号0~15
databases 1
# 设置redis能够使用的最大内存
maxmemory 512mb
# 日志文件,默认为空,不记录日志,可以指定日志文件名
logfile "redis.log"

二、解决虚拟机本地可以连接redis但是主机不能连接redis

(先关闭虚拟机再进行以下配置:)

1.虚拟机网络适配器网络连接设置为桥接模式

2.控制面板查看主机的网络连接信息

3.手动配置虚拟机网络连接,输入与主机同一网络段地址

4.主机与虚拟机网络地址对应

ifconfig # 查看网络地址 192.168.43.180
redis-cli -h 192.168.43.180 -p 6379 -a 123456 # 使用网络地址连接服务器

三、配置远程连接

1.设置防火墙端口

sudo firewall-cmd --add-port=6379/tcp --permanent
sudo firewall-cmd --reload
redis-cli -h c -p 6379 -a 123456
ps aux | grep redis

2.使用windows图形化界面

四、IDEA连接redis

1.新建项目

2.引入redis依赖和连接池依赖

<!--redis依赖-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--common-pool-->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
</dependency>

3.配置文件

spring:
  redis:
    host: 192.168.43.180 # 更换成自己的地址
    port: 6379
    password: 123456
    lettuce:
      pool:
        max-active: 8
        max-idle: 8
        min-idle: 0
        max-wait: 100ms

4.注入redisTemplate

@Autowired
private RedisTemplate<String, Object> redisTemplate;

5.编写测试(所以代码)

import com.heima.redis.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;

@SpringBootTest
class RedisDemoApplicationTests {

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @Test
    void testString() {
        // 写入一条String数据
        redisTemplate.opsForValue().set("name", "柯迪耐");
        // 获取string数据
        Object name = redisTemplate.opsForValue().get("name");
        System.out.println("name = " + name);
    }

    @Test
    void testSaveUser() {
        // 写入数据
        redisTemplate.opsForValue().set("user:100", new User("虎哥", 21));
        // 获取数据
        User o = (User) redisTemplate.opsForValue().get("user:100");
        System.out.println("o = " + o);
    }
}

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.RedisSerializer;

@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory){
        // 创建RedisTemplate对象
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        // 设置连接工厂
        template.setConnectionFactory(connectionFactory);
        // 创建JSON序列化工具
        GenericJackson2JsonRedisSerializer jsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
        // 设置Key的序列化
        template.setKeySerializer(RedisSerializer.string());
        template.setHashKeySerializer(RedisSerializer.string());
        // 设置Value的序列化
        template.setValueSerializer(jsonRedisSerializer);
        template.setHashValueSerializer(jsonRedisSerializer);
        // 返回
        return template;
    }
}

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
    private String name;
    private Integer age;
}

6.查看winsows GUI 界面

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CodingKnight

永远开源

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值