基于Spring Boot框架读写Redis

一、部署Redis

1.虚拟机设置为桥接模式

首先关闭正在运行的虚拟机

然后打开虚拟机设置

点击下图所示

按照下图勾选

点击确定,启动虚拟机

2.Ubuntu更改静态IP

首先查看自己本机的IP地址,比如我的是

接下来,在Ubuntu中输入

sudo nano /etc/netplan/00-installer-config.yaml

按照下图修改,请注意两处圈红的地方(第一处要用你的IP地址前缀,比如我的本机IP地址是192.168.1.2,所以在此我给虚拟机设为了192.168.1.126;第二处设置为了192.168.1.1)

比如上方查询到你的IP地址是147.119.1.5那么你的圈红处应为147.119.1.X(随意)和147.119.1.1

退出编辑文件,输入

sudo netplan apply

然后可以尝试ping一下百度,试一下有没有成功

3.部署redis

首先更新安装包

sudo apt update
sudo apt upgrade -y

然后安装

sudo apt install redis-server -y

我们要确保能监听到所有端口,所以输入

sudo systemctl restart redis

找到bind 127.0.0.1 ::1这一行,把他改为bind 0.0.0.0

完成之后保存退出,再重启redis服务

sudo systemctl restart redis

二、编写api接口

1.配置Redis

首先在pom.xml文件里添加依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

再在application.properties文件里配置连接(这里的IP指的是我们在上文中配置的虚拟机的静态IP,以我的为例就是192.168.1.126)

spring.redis.host=IP
spring.redis.port=6379

2.编写api

首先创建一个redis服务类

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class RedisService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    public void setValue(String key,String value)
    {
        stringRedisTemplate.opsForValue().set(key,value);
    }
    public String getValue(String key)
    {
        return stringRedisTemplate.opsForValue().get(key);
    }
}

然后创建一个控制器类

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/redis")
public class RedisController {
    @Autowired
    private RedisService redisService;
    @PostMapping("/set")
    public String setValue(@RequestParam String key,@RequestParam String value)
    {
        redisService.setValue(key,value);
        return "成功写入";
    }
    @GetMapping("/get")
    public String getValue(@RequestParam String key)
    {
        String value= redisService.getValue(key);
        return value !=null?value:"查找失败";
    }
}

最后编写一个配置类(无此配置类,程序会优先连接本地redis,我也不清楚为什么)

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;

@Configuration
public class RedisConfig {

    @Bean
    public LettuceConnectionFactory redisConnectionFactory() {
        return new LettuceConnectionFactory(new RedisStandaloneConfiguration("192.168.1.126", 6379));
    }
}

三、测试

首先运行DemoApplication程序

然后打开postman

我们先post一下,你可以像下图一样手动添加值,也可以直接输入url:http://localhost:8080/api/redis/set?key=mykey&value=myvalue

如果成功会显示“成功写入”

然后再get一下,也可以直接输入url:http://localhost:8080/api/redis/get?key=mykey

如果成功会显示“查找成功”

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值