반응형

1. 시험환경

    ˙ SpringBoot

    ˙ Redis

    ˙ Docker

 

2. 목적

    ˙ Docker를 이용하여 Redis를 설치 및 실행한다.

    ˙ SpringBoot에서 Redis 사용을 위한 라이브러리 설치 및 설정하는 방법을 알아보자.

    ˙ SpringBoot를 이용하여 Redis에 Sample 데이터를 저장하고 불러오는 테스트를 진행한다.

 

3. 적용

    ① Redis가 설치 및 실행되어 있어야 한다. ( 여기서는 Docker를 이용하여 Redis를 설치하고 실행한다. )

        - 참고 : https://languagestory.tistory.com/303

 

Redis Docker 설치 및 Cli 초기 접속

1. 시험환경 ˙ 윈도우 ˙ Docker ˙ Redis 2. 목적 ˙ Redis Docker를 다운(pull)받아 설치한다. ˙ Redis 버전을 확인하고 Redis-Cli에 접속하여 CRUD 기본 사용법을 확인한다. 3. 적용 ① docker hub로부터 redis docker im

languagestory.tistory.com

 

    ② SpringBoot에서 Redis 라이브러리를 설치한다.

        - 파일명 : build.gradle

1
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
cs

 

    ③ redis 접속 정보 등을 설정한다.

        - 파일명 : application.yml

1
2
3
4
5
6
7
8
9
  redis:
    lettuce:
      pool:
        max-active: 10
        max-idle: 10
        min-idle: 2
    port: 6379
    host: 127.0.0.1
    password: 1q2w3e
cs

 

    ④ RedisConfig 파일을 설정한다.

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
package com.example.demo.config;
 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
 
@Configuration
public class RedisConfig {
 
  @Value("${spring.redis.host}")
  private String redisHost;
 
  @Value("${spring.redis.port}")
  private String redisPort;
 
  @Value("${spring.redis.password}")
  private String redisPassword;
 
  @Bean
  public RedisConnectionFactory redisConnectionFactory() {
    RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
    redisStandaloneConfiguration.setHostName(redisHost);
    redisStandaloneConfiguration.setPort(Integer.parseInt(redisPort));
    redisStandaloneConfiguration.setPassword(redisPassword);
    LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory(redisStandaloneConfiguration);
    return lettuceConnectionFactory;
  }
 
  @Bean
  public RedisTemplate<String, Object> redisTemplate() {
    RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
    redisTemplate.setConnectionFactory(redisConnectionFactory());
    redisTemplate.setKeySerializer(new StringRedisSerializer());
    redisTemplate.setValueSerializer(new StringRedisSerializer());
    return redisTemplate;
  }
}
 
cs

 

    ⑤ 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
26
27
28
29
30
31
32
package com.example.demo.ctrl;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
 
@RestController
public class RedisController {
  @Autowired
  private RedisTemplate<StringString> redisTemplate;
 
  @GetMapping("/set")
  public ResponseEntity<?> setKeyValue() {
    ValueOperations<StringString> vop = redisTemplate.opsForValue();
    vop.set("Korea""Seoul");
    vop.set("America""NewYork");
    vop.set("Italy""Rome");
    vop.set("Japan""Tokyo");
    return new ResponseEntity<>( HttpStatus.CREATED);
  }
 
  @GetMapping("/get/{key}")
  public ResponseEntity<?> getValueFromKey(@PathVariable String key) {
    ValueOperations<StringString> vop = redisTemplate.opsForValue();
    String value = vop.get(key);
    return new ResponseEntity<>(value, HttpStatus.OK);
  }
 
}
cs

 

    ⑥ 스프링 프로젝트를 시작한다.

 

4. 결과

    ˙ Redis에 데이터 저장하기

        - 테스트 URL : http://localhost:8080/set

 

    ˙ Redis에 저장된 데이터 불러오기

        - 테스트 URL : http://localhost:8080/get/Korea

        - 테스트 URL : http://localhost:8080/get/Italy

 

 

반응형
반응형

1. 시험환경

    ˙ 윈도우

    ˙ Docker

    ˙ Redis

 

2. 목적

    ˙ Redis Docker를 다운(pull)받아 설치한다.

    ˙ Redis 버전을 확인하고 Redis-Cli에 접속하여 CRUD 기본 사용법을 확인한다.

 

3. 적용

    ① docker hub로부터 redis docker image 최신버전을 다운로드(pull) 받는다.

        - > docker  pull  redis

        - > docker  images

 

    ② Local 저장소에 저장된 Redis Docker 이미지를 실행한다.

        - > docker  run  -d  -p  6379:6379  [Image-id]

 

    ③ 실행중인 Redis Container 내부로 진입하여 설치 버전을 확인한다.

        -> docker  exec  -it  [container-id]  /bin/bash

 

    ④ Redis Cli에 접속하여 CRUD 명령어를 확인한다.

        -> redis-cli

 

반응형

+ Recent posts