Redis 설치

Redis 서버

Redis 설치 및 실행

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
[~]$ wget http://download.redis.io/redis-stable.tar.gz
[~]$ tar xvzf redis-stable.tar.gz
[~]$ cd redis-stable/src
[~]$ vim Makefile

PREFIX?=~/apps/redis-server <- 이부분 수정

[~ src]$ mkdir ~/apps/redis-server
[~ src]$ make PREFIX=~/apps/
[~ src]$ make test
[~ src]$ make install
[~ src]$ cd ~/apps/redis-server
[~ redis-server]$ mkdir conf
[~ redis-server]$ cp ~/redis-stable/redis.conf ./conf/
[~ redis-server]$ vim conf/redis.conf

bind x.x.x.x <- 이 부분을 현재 서버 IP로 설정
daemonize yes <- 이 부분 no를 yes로 수정

[~ redis-server]$ ./bin/redis-server ./conf/redis.conf
[~ redis-server]$ ps -ef | grep redis

- 0000 1 0 00:00 ? 00:00:00 ./bin/redis-server conf/redis.conf x.x.x.x:6379
- 0000 00000 0 00:00 pts/0 00:00:00 grep redis

실행 스크립트

1
2
3
4
5
6
#!/bin/bash
REDIS_DIR=~/apps/redis-server

#### Main #####
# apps/redis-server/bin/redis-server apps/redis-server/conf/redis.conf
$REDIS_DIR/bin/redis-server $REDIS_DIR/conf/redis.conf

Spring

pom.xml

1
2
3
4
5
6
7
8
9
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>biz.paluch.redis</groupId>
<artifactId>lettuce</artifactId>
<version>4.5.0.Final</version>
</dependency>

application.properties

1
2
3
4
5
spring.redis.lettuce.pool.max-active=10
spring.redis.lettuce.pool.max-idle=10
spring.redis.lettuce.pool.min-idle=2
spring.redis.port=6379
spring.redis.host=x.x.x.x

RedisConfiquration.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@Configuration    
public class RedisConfiguration {

@Bean
public RedisConnectionFactory redisConnectionFactory() {
return new LettuceConnectionFactory("x.x.x.x", 6379);
}
@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory());
redisTemplate.setKeySerializer(new StringRedisSerializer());

return redisTemplate;
}
}

RedisServiceTest.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisServiceTest {

@Autowired
RedisTemplate<String, Object> redisTemplate;

@Test
public void test() {
ValueOperations<String, Object> vop = redisTemplate.opsForValue();

vop.set("test1", "햇님");
assertEquals("햇님",vop.get("test1"));
}
}