Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Info

https://docs.microsoft.com/ko-kr/azure/developer/java/spring-framework/configure-spring-boot-initializer-java-app-with-redis-cache

https://docs.microsoft.com/ko-kr/azure/azure-cache-for-redis/cache-java-get-started

Info

Table of Contents

00. 사전조건

Info

01. Spring Boot Application 준비

Spring Boot Application 생성

Spring Boot Application Workspace 준비

02. Create Azure Cache for Redis

03. Coding Spring Boot Application for Azure Redis

Code Block
titleC:\workspace\redisdemo\src\main\resources\application.properties
linenumberstrue
# Specify the DNS URI of your Redis cache.
spring.redis.host=redis20201230.redis.cache.windows.net

# Specify the port for your Redis cache.
spring.redis.port=6379
spring.redis.ssl=true

# Specify the access key for your Redis cache.
spring.redis.password=4pgNlZPhbsla7tgi1s2qONb9YgxttV1zlecnkBJ4uxg=
Code Block
languagejava
linenumberstrue
package com.example.redisdemo;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

@RestController
public class HelloController {

   @Autowired
   private StringRedisTemplate template;

   @RequestMapping("/")
   // Define the Hello World controller.
   public String hello() {

      ValueOperations<String, String> ops = this.template.opsForValue();

      // Add a Hello World string to your cache.
      String key = "greeting";
      if (!this.template.hasKey(key)) {
          ops.set(key, "Hello World!");
      }

      // Return the string from your cache.
      return ops.get(key);
   }
}

04. Execute Spring Boot Application for Azure Redis

Code Block
C:\workspace\redisdemo>mvnw clean package
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.example:redisdemo >------------------------
[INFO] Building redisdemo 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
~~~~~~~~~~~~~~~~ 중략 ~~~~~~~~~~~~~~~~~
[INFO] Replacing main artifact with repackaged archive
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  21.121 s
[INFO] Finished at: 2020-12-30T22:08:19+09:00
[INFO] ------------------------------------------------------------------------

C:\workspace\redisdemo>mvnw spring-boot:run
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.example:redisdemo >------------------------
[INFO] Building redisdemo 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
~~~~~~~~~~~~~~~~ 중략 ~~~~~~~~~~~~~~~~~~~
2020-12-30 22:10:54.930  INFO 7284 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-12-30 22:10:54.941  INFO 7284 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-12-30 22:10:54.942  INFO 7284 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.41]
2020-12-30 22:10:55.045  INFO 7284 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-12-30 22:10:55.046  INFO 7284 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1340 ms
2020-12-30 22:10:55.526  INFO 7284 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-12-30 22:10:55.807  INFO 7284 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-12-30 22:10:55.820  INFO 7284 --- [           main] c.e.redisdemo.RedisdemoApplication       : Started RedisdemoApplication in 2.782 seconds (JVM running for 3.
388)


C:\workspace\redisdemo>curl localhost:8080
Hello World!
C:\workspace\redisdemo>