一、安装Redis
在redis官网上找到最新稳定版本redis,当前最新版本是5.0.5,复制链接然后在Ubuntu上使用curl命令直接下载
ouyp@ouyp-VirtualBox:~/redis$ curl -O http://download.redis.io/releases/redis-5.0.5.tar.gz
ouyp@ouyp-VirtualBox:~/redis$ tar -zxvf redis-5.0.5.tar.gz
ouyp@ouyp-VirtualBox:~/redis/redis-5.0.5$ make
二、配置Redis
编辑~/redis-5.0.5/redis.conf文件,做以下修改
- 注释bind 127.0.0.1这样其他机器才能访问
#bind 127.0.0.1
- 将protected-mode 改为no
protected-mode no
- 以配置文件方式启动redis
root@ouyp-VirtualBox:/home/ouyp/redis-5.0.5# ./src/redis-server redis.conf
三、springboot+redis配置
- pom.xml配置
<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>
- application.properties配置
spring.redis.host=10.6.3.148
spring.redis.port=6379
spring.redis.password=
四、springboot+redis测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class AApplicationTests {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Test
public void contextLoads() throws Exception {
Map<String, Object> map = new HashMap<>();
map.put("name", "kk-cock");
map.put("age", 10);
ObjectMapper objectMapper = new ObjectMapper();
String value = objectMapper.writeValueAsString(map);
stringRedisTemplate.opsForValue().set("obj", value);
}
}