1213 - 1219


# 1213 - 1219

# 1213 - StopWatch

Spring์˜ StopWatch ํด๋ž˜์Šค๋Š” ์—ฌ๋Ÿฌ๊ฐœ ์ž‘์—…์˜ ์‹œ๊ฐ„์„ ์ธก์ •ํ•ด์ฃผ๋ฉฐ, ์ด ์†Œ์š”์‹œ๊ฐ„๊ณผ ๊ฐ๊ฐ์˜ ์ด๋ฆ„๋ถ™์ธ ์ž‘์—…๋“ค์— ์†Œ์š”๋œ ์‹œ์‚ฐ์„ ํ‘œ์‹œํ•ด์ค€๋‹ค. System.nanoTime()์˜ ์‚ฌ์šฉ์„ ๊ฐ์ถ”๊ณ  ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ์„ ๋†’์ผ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๊ณ„์‚ฐ ์˜ค๋ฅ˜์˜ ๊ฐ€๋Šฅ์„ฑ๋„ ์ค„์—ฌ์ค€๋‹ค.

  • ์˜ˆ์‹œ ์ฝ”๋“œ
import org.springframework.util.StopWatch;
 
public class StopWatchClass {
    public static void main(String[] args) {
 
        // StopWatch ์ƒ์„ฑ
        StopWatch stopwatch = new StopWatch();
 
        // ํƒ€์ด๋จธ ์‹œ์ž‘
        stopwatch.start();
 
        // ์ž‘์—…
        fibonacci(45);
 
        // ํƒ€์ด๋จธ ์ข…๋ฃŒ
        stopwatch.stop();
 
        // ๊ฒฐ๊ณผ ๋ถ„์„
        System.out.println(stopwatch.prettyPrint());
 
    }
 
    public static int fibonacci(int n) {
        if (n == 0)
            return 0;
        if (n == 1)
            return 1;
        return fibonacci(n - 1) + fibonacci(n - 2);
    }
 
}    

// 1 sec = 1,000 ms = 1,000,000 ฮผs = 1,000,000,000 ns

// 45๊ฐœ์˜ ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜์—ด์„ ๊ณ„์‚ฐํ•˜๋Š”๋ฐ ์ด 3.82 ์ดˆ.
  • Recursive, Dynamic Programming
import org.springframework.util.StopWatch;
 
public class StopWatchClass2 {
    static final int number = 45;
    static long[] memo= new long[number+1];
 
    public static void main(String[] args) {
 
        StopWatch stopwatch = new StopWatch("fibonacci");
 
        // 1๋ฒˆ ์ž‘์—…
        stopwatch.start("recursive");
        fibonacci(number);
        stopwatch.stop();
 
        // 2๋ฒˆ ์ž‘์—…
        stopwatch.start("DP");
        fibonacciDp(number);
        stopwatch.stop();
 
        // ๊ฒฐ๊ณผ ๋ถ„์„
        System.out.println(stopwatch.prettyPrint());
 
    }
 
    public static long fibonacci(int n) {
        if (n == 0)
            return 0;
        if (n == 1)
            return 1;
        return fibonacci(n - 1) + fibonacci(n - 2);
    }
 
    public static long fibonacciDp(int n){
        if(n==0)
            return 0;
        if(n==1)
            return 1;
        if(memo[n] != 0) {
            return memo[n];
        }else {
            return memo[n] = fibonacciDp(n-1) + fibonacciDp(n-2);
        }
    }
 
}

# 1215 - .map()๊ณผ .flatMap()

# .map()

.map()์€ ๋‹จ์ผ ์ŠคํŠธ๋ฆผ ์›์†Œ๋ฅผ ๋งคํ•‘์‹œํ‚จ ํ›„ ๋งคํ•‘์‹œํ‚จ ๊ฐ’์„ ๋‹ค์‹œ ์ŠคํŠธ๋ฆผ์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ์ค‘๊ฐ„ ์—ฐ์‚ฐ์„ ๋‹ด๋‹นํ•œ๋‹ค. ๊ฐ์ฒด์—์„œ ์›ํ•˜๋Š” ์›์†Œ๋ฅผ ์ฃผ์ถœํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.

# .flatMap()

.flatMap()์€ Array๋‚˜ Object๋กœ ๊ฐ์‹ธ์ ธ ์žˆ๋Š” ๋ชจ๋“  ์›์†Œ๋ฅผ ๋‹จ์ผ ์›์†Œ ์ŠคํŠธ๋ฆผ์œผ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค. .map()์€ ์ž…๋ ฅํ•œ ์›์†Œ๋ฅผ ๊ทธ๋Œ€๋กœ ์ŠคํŠธ๋ฆผ์œผ๋กœ ๋ฐ˜ํ™˜ํ•˜์ง€๋งŒ, .flatMap()์€ ์ž…๋ ฅํ•œ ์›์†Œ๋ฅผ ๊ฐ€์žฅ ์ž‘์€ ๋‹จ์œ„์˜ ๋‹จ์ผ ์ŠคํŠธ๋ฆผ์œผ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

flatMap

  • witheoutFlatMap.java
String[][] sample = new String[][]{
  {"a", "b"}, {"c", "d"}, {"e", "a"}, {"a", "h"}, {"i", "j"}
};

//without .flatMap()
Stream<String> stream = sample.stream()
  .filter(alpha -> "a".equals(alpha[0].toString() || "a".equals(alpha[1].toString())))
stream.forEach(alpha -> System.out.println("{"+alpha[0]+", "+alpha[1]+"}"));

// output
{a, b}
{e, a}
{a, h}
  • withFlatMap.java
String[][] sample = new String[][]{
  {"a", "b"}, {"c", "d"}, {"e", "a"}, {"a", "h"}, {"i", "j"}
};

//without .flatMap()
Stream<String> stream = sample.stream()
  .flatMap(array -> Arrays.stream(array))
  .filter(x-> "a".equals(x));

stream.forEach(System.out::println);

// output
a
a
a

# 1218 - AWS ์ธํ”„๋ผ ๊ตฌ์ถ•

# ์ „ํ†ต์ธํ”„๋ผ์™€ ๋น„๊ต

์ „ํ†ต์ธํ”„๋ผ AWS
๋„คํŠธ์›Œํฌ VPC
๋ณด์•ˆ์ •์ฑ… SECURITY CROUP
L4 ELB
์„œ๋ฒ„ EC2
DNS ROUTE 53
DB RDS
์Šคํ† ๋ฆฌ์ง€ S3
CDN CloudFront
NAT NAT
๋ฐฐํฌ์‹œ์Šคํ…œ OpsWorks
์žฅ์• ์•Œ๋ฆผ SNS
์„œ๋ฒ„์ ‘๊ทผ๋ฐฉ์‹ VPN

# vpc๋ž€

VPC(Virtual Private Cloud)๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ์ •์˜ํ•˜๋Š” ๊ฐ€์žฅ์˜ ๋„คํŠธ์›Œํฌ ์ด๋‹ค. VPC๋ฅผ ํ†ตํ•ด ์ธ์Šคํ„ด์Šค๊ฐ€ ์†ํ•˜๋Š” ๋„คํŠธ์›Œํฌ๋ฅผ ๊ตฌ๋ถ„ํ•˜์—ฌ ๊ฐ ๋„คํŠธ์›Œํฌ์— ๋งž๋Š” ์„ค์ •์„ ๋ถ€์—ฌํ•  ์ˆ˜ ์žˆ๋‹ค. AWS๋Š” VPC์˜ ์ค‘์š”์„ฑ์„ ๊ฐ•์กฐํ•˜์—ฌ 2019๋…„๋ถ€ํ„ฐ ๋ชจ๋“  ์„œ๋น„์Šค์— VPC๋ฅผ ์ ์šฉํ•˜๋„๋ก ํ–ˆ๊ณ , ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ์‹ฑ ์ž๋™์œผ๋กœ default VPC์— ์ธ์Šคํ„ด์Šค๊ฐ€ ๋ฐฐ์น˜๋œ๋‹ค.

์‚ฌ์„ค IP ๋Œ€์—ญ

  • 10.0.0.0 ~ 10.255.255.255(10/8 prefix)
  • 172.16.0.0 ~ 172.31.255.255(182.16/12 prefix)
  • 192.168.0.0 ~ 192.168.255.255(192.168/16 prefix)

VPC์—์„œ ํ•œ๋ฒˆ ์„ค์ •๋œ IP ๋Œ€์—ญ์€ ์ˆ˜์ •ํ•  ์ˆ˜ ์—†์œผ๋ฉฐ ๊ฐ๊ฐ์˜ VPC๋Š” ๋…๋ฆฝ์ ์ด๊ธฐ ๋•Œ๋ฌธ์— ์„œ๋กœ ํ†ต์‹ ํ•  ์ˆ˜ ์—†๋‹ค. ๋งŒ์•ฝ ํ†ต์‹ ์„ ์›ํ•œ๋‹ค๋ฉด VPCํ”ผ์–ด๋ง ์„œ๋น„์Šค๋ฅผ ํ†ตํ•ด VPC๊ฐ„์— ํŠธ๋ž˜ํ”ฝ์„ ๋ผ์šฐํŒ…ํ•  ์ˆ˜ ์žˆ๋˜๋ก ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

Last update: September 13, 2022 21:44
Contributors: ahnjs