redis 查看连接ip redis查看ip地址 |
您所在的位置:网站首页 › redis查看链接ip › redis 查看连接ip redis查看ip地址 |
一、redis 1.首先打开VM虚拟机,输入个人账号密码后进行连接,进入系统主页面 2.在主页面的命令控制行输入命令查看ip地址,如果通过ifconfig命令在ens33中不能显示出ip地址,通过ifup ens33命令启动网卡,然后再输入ifconfig命令,查看ip地址。 3.打开xshell连接虚拟机,开启redis 4.查看redis服务进程是否正常开启 5.输入redis-cli命令进入redis命令输入行,redis-cli shutdown关闭redis服务,此时redis进程也全部关闭。 6.springboot集成redis时需要关闭虚拟机的防火墙。 查看防火墙状态:systemctl status firewalld 运行状态 关闭防火墙 关闭状态 打开防火墙 7. redis默认端口号6379在Linux防火墙中不允许进行远程连接,执行如下Linux命令,在防火墙中设置6379开启远程服务。 /sbin/iptables -I INPUT -p tcp --dport 6379 -j ACCEPT如果还是连接不成功可以vim redis.conf 修改redis配置文件后需要关闭redis服务,然后再重新启动redis服务。 二、springboot 项目结构
1.创建项目时导入redis的起步依赖 pom.xml文件 4.0.0 org.springframework.boot spring-boot-starter-parent 2.6.11 com.it 021-springboot-redis 0.0.1-SNAPSHOT 021-springboot-redis Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-data-redis org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin2.设置springboot核心配置文件application.proerties文件 连接redis远程服务时有密码加下密码,没有密码不要加,会导致报错。 默认是没有密码的 server.port=9001 server.servlet.context-path=/myredis #指定redis的信息 spring.redis.host=192.168.174.129 spring.redis.port=6379 #spring.redis.password=1234563.创建controller包下的RedisController类 package com.it.controller; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.ValueOperations; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @RestController public class RedisController { /** * 注入RedisTemplate * * RedisTemplate 泛型只有以下3种形式 * RedisTemplate *RedisTemplate *RedisTemPlate * * 注意:RedisTemplate对象名称:redisTemplate */ @Resource private RedisTemplate redisTemplate; @Resource private StringRedisTemplate stringRedisTemplate; //添加数据到redis @PostMapping("/redis/addString") public String addToRedis(String name,String value){ //操作redis中的String类型的数据,先获取ValueOperations对象 ValueOperations valueOperations = redisTemplate.opsForValue(); //添加数据到redis valueOperations.set("myname","lisi"); return "向redis添加string类型的数据"; } //从redis获取数据 @GetMapping("/redis/getk") public String getData(String k){ ValueOperations valueOperations = redisTemplate.opsForValue(); Object v = valueOperations.get(k); return "key是"+k+" value是"+v; } @PostMapping("/redis/{k}/{v}") public String addStringKV(@PathVariable String k, @PathVariable String v){ //使用StringRedisTemplate对象 stringRedisTemplate.opsForValue().set(k,v); return "使用的StringRedisTemplate对象"; } @GetMapping("/redis/getstr/{k}") public String getStringValue(@PathVariable String k){ String v = stringRedisTemplate.opsForValue().get(k); return "k的value:"+v; } }4.进入主函数类,运行入口函数 使用postman软件进行测试 测试结果的顺序与controller类中的方法顺序是一致的 测试结果 通过RedisTemplate设置key和value 进入xshell,查看redis中数据是否添加成功
通过RedisTemplate获取key和value 通过StringRedisTemplate设置key和value 进入xshell,查看redis中数据是否添加成功 通过StringRedisTemplate获取key和value |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |