반응형

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. 시험환경

    ˙ Window

    ˙ Spring Initializr

    ˙ Gradle

    ˙ IntelliJ Community

 

2. 목적

    ˙ Spring Initializer를 이용하여 Spring Boot 프로젝트 패키지를 구성한다.

    ˙ Spring Initializer를 이용하여 구성된 패키지를 IntelliJ에 임포트(import)하여 시작한다.

 

3. 적용

    ① Spring Initializer 사이트에 접속한다.

       - URL : https://start.spring.io/

 

    ② 최소한의 의존성 패키지 구성으로 Spring Boot 프로젝트를 생성한다.

        - "GENERATE" 버튼을 클릭하여 프로젝트 압축파일을 다운로드 받는다.

 

    ③ 다운받은 압축파일을 workspace(프로젝트 작업공간)에서 압축해제한다.

template.zip
0.06MB

 

    ④ IntelliJ에서 template 폴더를 임포트(import) 하면 자동으로 빌드(build) 작업이 진행된다.

        - build.gradle에 추가된 Dependency는 다음과 같다.

1
2
3
4
5
6
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
cs

 

    ⑤ 테스트를 위한 Controller를 작성한다.

        - 패키지 : com.boot.template.controller

        - 테스트용 컨트롤러 : com.boot.template.controller.TestController.java

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.boot.template.controller;
 
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class TestController {
 
    @GetMapping("/test")
    public String test() {
        System.out.println("test...");
        return "Hello World";
    }
}
 
cs

 

 

    Main()에서 프로젝트를 실행한다.

 

4. 결과

    ˙ SpringBoot 실행 결과를 확인한다.

반응형
반응형

1. 시험환경

    ˙ JDK
    ˙ Visual Studio Code

 

2. 목적

    ˙ Visual Studio Code를 이용하여 Spring Boot 프로젝트 개발을 위한 프로젝트 환경을 구축한다.

    ˙ 리눅스

 

3. 적용

    ① Visual Studio Code를 다운 받아 설치한다.

        - URL : https://code.visualstudio.com/

 

Visual Studio Code - Code Editing. Redefined

Visual Studio Code is a code editor redefined and optimized for building and debugging modern web and cloud applications.  Visual Studio Code is free and available on your favorite platform - Linux, macOS, and Windows.

code.visualstudio.com

Visual Studio Code 사이트

 

    ② 공식 가이드에 Spring Boot를 시작하기 위한 사전 설치 프로그램과 VSCode 확장팩에 관한 정보를 확인한다.

사전 설치 프로그램 및 VSCode 확장팩

 

    ③ 사전 필수 프로그램을 설치한다.

        - JDK(JAVA Development Kit) 설치 : https://languagestory.tistory.com/11

 

윈도우 환경에서 JDK(JAVA Development Kit) 설치

1. 시험환경 ˙ 윈도우 ˙ JDK 17 Win x64 2. 목적 ˙ 윈도우 환경에서 JDK를 설치한다. ˙ 설치한 JDK 환경을 설정한다. 3. 적용 ① 오라클 다운로드 센터에서 JDK for Developers를 클릭한다. - URL : https://www.oracl

languagestory.tistory.com

 

        - Apache Maven 설치 (옵션) : https://languagestory.tistory.com/69

 

Apache Maven 설치

1. 시험환경 - 윈도우 10 2. 목적 - 윈도우 운영체제에 Apache Maven을 설치한다. 3. 적용 ① Apache Maven 사이트로 이동한다. - https://maven.apache.org/ Maven – Welcome to Apache Maven Welcome to Apache Maven Apache Maven is a

languagestory.tistory.com

 

    ④ VSCode 확장팩을 설치한다.

        - Java Extension Pack
        - Spring Boot Tools
        - Spring Initializr
        - Spring Boot Dashboard

Java Extension Pack 설치
Spring Boot Tools 설치
Spring Initializr Java Support 설치
Spring Boot Dashboard 설치

 

반응형

+ Recent posts