说明:
1.在ubantu系统(ip:192.168.1.4)安装了redis,安装步骤很简单:
a>到官网中下载最新稳定release版:
redis-stable.tar.gz
b>将文件放在/home/young/software/下解压,得到redis-4.0.6
c>到redis-4.0.6下make
d>启动:
cd src
./redis-server
2.在springboot项目 (所在ip:192.168.1.3)中使用redis:
a>在pom.xml中加入依赖:
<
dependency
>
< groupId >org.springframework.boot </ groupId > < artifactId >spring-boot-starter-data-redis </ artifactId >
</dependency>
|
该依赖里默认包含了spring-data-redis和Jedis依赖 |
b>在application.properties中加入redis的配置:
# Redis 配置
# Redis服务器地址
spring.redis.host=192.168.1.3
# Redis服务器连接端口
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
|
c>java使用redis:
@RestController public class RedisController { @Autowired private RedisTemplate<String,String> redisTemplate; @RequestMapping(value = "/redis/string",method = RequestMethod. GET) public void insertString(){ redisTemplate.opsForValue().set( "hei", "reddddis"); } } |
d>启动调用localhost:8080/redis/string
出现错误:
(error) DENIED Redis is running in protected mode because protected mode is enabled
|
3.解决办法:
修改redis.conf的配置内容:
a>protected
-mode
no
b>注释bind:127.0.0.1(或者修改为bind 0.0.0.0 ->>代表允许任意ip的请求)
c>重新启动:./src/redis-server redis.conf
说明注意事项:
a>本来按照官方的说明,只要先将protected-mode改为no,并且在bind后面加上我们需要的客户端网络的ip,但是没有用,只能选择注释bind或者改为0.0.0.0
b>在springboot项目中使用redistemplate调用set,这个redistemplate需要用@Autowired引入,而不能用@Resource引用。
这样就能在springboot中使用redis了,其实redis很强大,还有需要后续更多的学习,这里只是掌握基本的配置,能使其运行。