springboot使用redis

项目创建

通过maven创建项目

添加依赖

pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.kite.springboot</groupId>
<artifactId>redis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>redis</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.7</java.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

添加配置

application.yml

1
2
3
4
5
6
7
8
9
spring.redis:
database: 0
host: 45.32.112.158
port: 7001
pool:
max-idle: 8
min-idle: 0
max-active: 8
max-wait: -1

测试使用

出现问题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.kite.springboot.redis;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Hello world!
*
*/
@SpringBootApplication
@RestController
public class App {
@Autowired
StringRedisTemplate stringRedisTemplate;
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
@GetMapping("/test")
public String test() {
Set<String> keys = stringRedisTemplate.keys("aa");
System.out.println(keys);
return "ok";
}
}
1
2
3
4
5
connet refersh
链接不上,注释掉bind(测试使用,生产请勿关闭)
DENIED Redis is running in protected mode because protected mode is enabled
保护模式开启,修改为关闭(测试使用,生产请勿关闭)
正常连接输出

注释bind后显示*
image