Spring Boot Redis with docker

Redis는 데이터 저장에 최적화된 인메모리 Key-Value 스토어로 RDBMS를 제외한 데이터 저장소 중 가장 유명하고 널리 쓰이고 있다.

https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-nosql

1
Redis is a cache, message broker, and richly-featured key-value store. Spring Boot offers basic auto-configuration for the Lettuce and Jedis client libraries and the abstractions on top of them provided by Spring Data Redis.

즉 레디스는 키와 밸류 스토어다가 핵심이다.

pom.xml dependency 추가

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

도커를 활용하여 redis를 써보장

1
2
3
4
5
6
7
8
왼쪽이 호스트주소:6379
내로컬호스트의 포트를 오른쪽 컨테이너 포트에 맵핑시키겠다.

docker run -p 6379:6379 --name pkj-redis -d redis

위와 같이 진행한 후 docker ps를 치면 아래와 같이 나온다.

30151fd1866 redis "docker-entrypoint.s…" 11 seconds ago Up 9 seconds 0.0.0.0:6379->6379/tcp pkj-redis

위의 디펜던시와 도커 또는 로컬에 redis스가 설치되었다면 소스를 짜자 아래의 소스를 복사하자

RedisApplication

1
2
3
4
5
6
7
@SpringBootApplication
public class DemoredisApplication {

public static void main(String[] args) {
SpringApplication.run(DemoredisApplication.class, args);
}
}

Trip

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
38
39
40
41
42
43
44
45

@RedisHash("Trips")
public class Trip {


@Id
private String id;
private String title;
private Date startedAt;


public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public Date getStartedAt() {
return startedAt;
}

public void setStartedAt(Date startedAt) {
this.startedAt = startedAt;
}


@Override
public String toString() {
return "Trip{" +
"id='" + id + '\'' +
", title='" + title + '\'' +
", startedAt=" + startedAt +
'}';
}
}

TripRepository

1
2
3
4
5

public interface TripRepository extends CrudRepository<Trip, String>{


}

DefaultDataPopulator

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

@Component
public class DefaultDataPopulator implements ApplicationRunner {

@Autowired TripRepository tripRepository;



@Override
public void run(ApplicationArguments args) throws Exception {
Trip trip = new Trip();
trip.setTitle("Test Trip");
trip.setStartedAt(new Date());
tripRepository.save(trip);

tripRepository.findAll().forEach(m ->{
System.out.println("=============");
System.out.println(m.toString());

}
);
}
}

어플리케이션 실행 정상작동, 콘솔에 자기가 만든 Trip Object가 정상적으로 잘 toString되어 나온다면 정상작동

레디스에 어떻게 담겼는지 궁금하지 않은가?

내가 만든 도커에 cli를 붙여보자

1
docker run -it --link pkj-redis:redis --rm redis redis-cli -h redis -p 6379

그리고 keys *을 치게 될경우 아래와 같은 결과를 얻을 수 있다.

1
2
3
redis:6379> keys *
1) "Trips"
2) "Trips:6e032b7c-bb38-456b-87fe-be016dfeb37e"

담겨져있는 정확한 데이터를 가져와보자

1
2
3
4
5
6
7
8
9
10
11
12
13
1.
redis:6379> hget Trips:6e032b7c-bb38-456b-87fe-be016dfeb37e title

"Test Trip"

2.
redis:6379> hget Trips:6e032b7c-bb38-456b-87fe-be016dfeb37e id

"6e032b7c-bb38-456b-87fe-be016dfeb37e"

3.
redis:6379> hget Trips:6e032b7c-bb38-456b-87fe-be016dfeb37e startedAt
"1533380993871"

hget all

1
2
3
4
5
6
7
8
9
hgetall Trips:6e032b7c-bb38-456b-87fe-be016dfeb37e
1) "_class"
2) "me.jdredis.demoredis.Trip"
3) "id"
4) "6e032b7c-bb38-456b-87fe-be016dfeb37e"
5) "title"
6) "Test Trip"
7) "startedAt"
8) "1533380993871"

<백기선 스프링부트 유튜브 강의 참고>