๐Ÿ’ป Programming (358)

MyBatis์—์„œ ์ „๋‹ฌ๋ฐ›์€ ๊ฐ์ฒด(MyObject)์˜ ํ•„๋“œ์— ํŠน์ • ๊ฐ’์„ ์„ธํŒ…ํ•  ๋•Œ selectKey ํƒœ๊ทธ๋ฅผ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.


์˜ˆ๋ฅผ๋“ค์–ด MyObject๊ฐ€ ์•„๋ž˜ ๋‘ ๊ฐœ์˜ ํ•„๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ํด๋ž˜์Šค๋ผ๊ณ  ๊ฐ€์ •ํ•˜์ž.

String id;

String name;


MyBatis๋ฅผ ์ด์šฉํ•˜์—ฌ MyObject๋ฅผ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ž…๋ ฅํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์ด๋ฆ„๊ฐ’๋งŒ ํ™”๋ฉด์—์„œ ์ „๋‹ฌ๋ฐ›๊ณ  ID๋ฅผ ์ž๋™์œผ๋กœ sequence๋ฅผ ๋”ฐ์„œ ์ž…๋ ฅ ํ•ด์ค„ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด๋•Œ  MyObject์˜ idํ•„๋“œ์— ๊ฐ’ ์„ธํŒ…


<insert id="insert" parameterType="MyObject">

        <selectKey keyProperty="id" resultType="int" order="BEFORE">

            select nextval('sequence_id')

        </selectKey>

        <![CDATA[

        INSERT INTO person(id, name)

        VALUES (#{id}, #{name});

        ]]>

    </insert>


order="BEFORE" ์˜ต์…˜์€ insert ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ์ „์— keyProperty์— ๊ฐ’์„ ์„ธํŒ…ํ•˜๋ผ๋Š” ์˜๋ฏธ์ด๋‹ค.


์ž๋ฐ” ์†Œ์Šค๋‹จ์—์„œ ๊ฐ์ฒด์˜ id์— ๊ฐ’์„ ์„ธํŒ…ํ•˜์ง€ ์•Š๊ณ ์žˆ๋Š”๋ฐ ์ฟผ๋ฆฌ ์‹คํ–‰ ํ›„์— id๊ฐ’์ด ์„ธํŒ…๋˜์–ด์ ธ์„œ ๋‚˜์˜ค๋Š” ์ผ€์ด์Šค๊ฐ€ ์žˆ์—ˆ๋Š”๋ฐ ์ด๊ฒŒ ๋ญ”๊ฐ€ ์‹ถ์–ด ์ฐพ์•„๋ณด๋‹ค๊ฐ€ ์•Œ๊ฒŒ๋จ.


mybatis์„ค์ • ํŒŒ์ผ์—๋Š” userGeneratedKeys ์„ค์ •์ด ์žˆ์–ด์•ผ ํ•œ๋‹ค.

<configuration>

    <settings>

        <setting name="cacheEnabled" value="false"/>

        <setting name="useGeneratedKeys" value="true"/>

        <setting name="defaultExecutorType" value="REUSE"/>

    </settings>

</configuration>




์œ„ ์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์œผ๋‚˜ ์ €๋ ‡๊ฒŒ ์‚ฌ์šฉํ•˜๋Š” ์ผ€์ด์Šค๊ฐ€ ๋งŽ์ง€ ์•Š๋‹ค๋ฉด ์ฟผ๋ฆฌ์— ์ง์ ‘ ์˜ต์…˜์œผ๋กœ ์ค„ ์ˆ˜๋„ ์žˆ๋‹ค.


<insert
  id="insertAuthor"
  parameterType="domain.blog.Author"
  flushCache="true"
  statementType="PREPARED"
  keyProperty=""
  keyColumn=""
  useGeneratedKeys=""
  timeout="20">

์†์„ฑ์— ๋Œ€ํ•œ ์„ค๋ช…์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

์†์„ฑ์„ค๋ช…
id๊ตฌ๋ฌธ์„ ์ฐพ๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๋Š” ๋„ค์ž„์ŠคํŽ˜์ด์Šค๋‚ด ์œ ์ผํ•œ ๊ตฌ๋ถ„์ž
parameterType๊ตฌ๋ฌธ์— ์ „๋‹ฌ๋  ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ํŒจํ‚ค์ง€ ๊ฒฝ๋กœ๋ฅผ ํฌํ•จํ•œ ์ „์ฒด ํด๋ž˜์Šค๋ช…์ด๋‚˜ ๋ณ„์นญ
parameterMap์™ธ๋ถ€ parameterMap ์„ ์ฐพ๊ธฐ ์œ„ํ•œ ๋น„๊ถŒ์žฅ๋œ ์ ‘๊ทผ๋ฐฉ๋ฒ•. ์ธ๋ผ์ธ ํŒŒ๋ผ๋ฏธํ„ฐ ๋งคํ•‘๊ณผ parameterType์„ ๋Œ€์‹  ์‚ฌ์šฉํ•˜๋ผ.
flushCache์ด ๊ฐ’์„ true ๋กœ ์…‹ํŒ…ํ•˜๋ฉด ๊ตฌ๋ฌธ์ด ํ˜ธ์ถœ๋ ๋•Œ๋งˆ๋‹ค ์บ์‹œ๊ฐ€ ์ง€์›์งˆ๊ฒƒ์ด๋‹ค(flush). ๋””ํดํŠธ๋Š” false ์ด๋‹ค.
timeout์˜ˆ์™ธ๊ฐ€ ๋˜์ ธ์ง€๊ธฐ ์ „์— ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ์š”์ฒญ ๊ฒฐ๊ณผ๋ฅผ ๊ธฐ๋‹ค๋ฆฌ๋Š” ์ตœ๋Œ€์‹œ๊ฐ„์„ ์„ค์ •ํ•œ๋‹ค. ๋””ํดํŠธ๋Š” ์…‹ํŒ…ํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ด๊ณ  ๋“œ๋ผ์ด๋ฒ„์— ๋”ฐ๋ผ ๋‹ค์†Œ ์ง€์›๋˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ๋‹ค.
statementTypeSTATEMENT, PREPARED ๋˜๋Š” CALLABLE์ค‘ ํ•˜๋‚˜๋ฅผ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋‹ค. ๋งˆ์ด๋ฐ”ํ‹ฐ์Šค์—๊ฒŒ Statement, PreparedStatement ๋˜๋Š” CallableStatement๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ํ•œ๋‹ค. ๋””ํดํŠธ๋Š” PREPARED ์ด๋‹ค.
useGeneratedKeys(์ž…๋ ฅ(insert, update)์—๋งŒ ์ ์šฉ) ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ๋‚ด๋ถ€์ ์œผ๋กœ ์ƒ์„ฑํ•œ ํ‚ค (์˜ˆ๋ฅผ๋“ค์–ด MySQL๋˜๋Š” SQL Server์™€ ๊ฐ™์€ RDBMS์˜ ์ž๋™ ์ฆ๊ฐ€ ํ•„๋“œ)๋ฅผ ๋ฐ›๋Š” JDBC getGeneratedKeys๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ์„ค์ •ํ•˜๋‹ค. ๋””ํดํŠธ๋Š” false ์ด๋‹ค.
keyProperty(์ž…๋ ฅ(insert, update)์—๋งŒ ์ ์šฉ) getGeneratedKeys ๋ฉ”์†Œ๋“œ๋‚˜ insert ๊ตฌ๋ฌธ์˜ selectKey ํ•˜์œ„ ์—˜๋ฆฌ๋จผํŠธ์— ์˜ํ•ด ๋ฆฌํ„ด๋œ ํ‚ค๋ฅผ ์…‹ํŒ…ํ•  ํ”„๋กœํผํ‹ฐ๋ฅผ ์ง€์ •. ๋””ํดํŠธ๋Š” ์…‹ํŒ…ํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ด๋‹ค. ์—ฌ๋Ÿฌ๊ฐœ์˜ ์นผ๋Ÿผ์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ํ”„๋กœํผํ‹ฐ๋ช…์— ์ฝค๋งˆ๋ฅผ ๊ตฌ๋ถ„์ž๋กœ ๋‚˜์—ดํ• ์ˆ˜ ์žˆ๋‹ค.
keyColumn(์ž…๋ ฅ(insert, update)์—๋งŒ ์ ์šฉ) ์ƒ์„ฑํ‚ค๋ฅผ ๊ฐ€์ง„ ํ…Œ์ด๋ธ”์˜ ์นผ๋Ÿผ๋ช…์„ ์…‹ํŒ…. ํ‚ค ์นผ๋Ÿผ์ด ํ…Œ์ด๋ธ”์ด ์ฒซ๋ฒˆ์งธ ์นผ๋Ÿผ์ด ์•„๋‹Œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค(PostgreSQL ์ฒ˜๋Ÿผ)์—์„œ๋งŒ ํ•„์š”ํ•˜๋‹ค. ์—ฌ๋Ÿฌ๊ฐœ์˜ ์นผ๋Ÿผ์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ํ”„๋กœํผํ‹ฐ๋ช…์— ์ฝค๋งˆ๋ฅผ ๊ตฌ๋ถ„์ž๋กœ ๋‚˜์—ดํ• ์ˆ˜ ์žˆ๋‹ค.
databaseId์„ค์ •๋œ databaseIdProvider๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ ๋งˆ์ด๋ฐ”ํ‹ฐ์Šค๋Š” databaseId ์†์„ฑ์ด ์—†๋Š” ๋ชจ๋“  ๊ตฌ๋ฌธ์„ ๋กœ๋“œํ•˜๊ฑฐ๋‚˜ ์ผ์น˜ํ•˜๋Š” databaseId์™€ ํ•จ๊ป˜ ๋กœ๋“œ๋  ๊ฒƒ์ด๋‹ค. ๊ฐ™์€ ๊ตฌ๋ฌธ์—์„œ databaseId๊ฐ€ ์žˆ๊ฑฐ๋‚˜ ์—†๋Š” ๊ฒฝ์šฐ ๋ชจ๋‘ ์žˆ๋‹ค๋ฉด ๋’ค์— ๋‚˜์˜จ ๊ฒƒ์ด ๋ฌด์‹œ๋œ๋‹ค.


Git์—์„œ  ํƒœ๊ทธ ์‚ญ์ œํ•˜๊ธฐ


git tag -d {name of tag}

git push origin :refs/tags/{name of tag}


git tag -d 12345
git push origin :refs/tags/12345




์˜ค๋žœ๋งŒ์— ํฌ์ŠคํŒ…์„ ํ•ฉ๋‹ˆ๋‹ค.


๋ญ, ๊ทธ๋ƒฅ ์ข…์ข… ์‚ฌ์šฉํ•˜๋Š” ๊ฑด๋ฐ ๋งจ๋‚  ๊นŒ๋จน์–ด์„œ ๊ณ„์† ๊ตฌ๊ธ€๋งํ•˜๊ฒŒ ๋งŒ๋“œ๋Š” vi์—์„œ ๋ฌธ์ž์—ด ์น˜ํ™˜ํ•˜๊ธฐ์— ๋Œ€ํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.


*nix ํ™˜๊ฒฝ์—์„œ ์ž‘์—…์„ ํ•˜๊ฒŒ๋˜๋ฉด ์„ค์ • ํŒŒ์ผ์„ ์ˆ˜์ •ํ•  ์ผ์ด ๋งŽ์ด ์ƒ๊น๋‹ˆ๋‹ค.


์ด๋•Œ ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ ๊ฒฝ๋กœ๋‚˜, ์ด๋Ÿฐ ์ €๋Ÿฐ ๊ฒฝ๋กœ๋“ค์„ config ํŒŒ์ผ ์•ˆ์— ์„ค์ •ํ•ด๋†“๊ณ  ์‚ฌ์šฉํ•˜๊ธฐ๋„ ํ•˜๋Š”๋ฐ, ์ด๋•Œ ๊ฒฝ๋กœ ๊ฐœ์ˆ˜๊ฐ€ ๋งŽ๊ณ  ์•ž๋ถ€๋ถ„์˜ ๊ฒฝ๋กœ๋Š” ๋™์ผํ•œ ๊ฒƒ๋“ค์ด ๋งŽ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


์˜ˆ๋ฅผ๋“ค์–ด, /aaa/bbb/myapp/conf/config.properties ํŒŒ์ผ์— ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋‚ด์šฉ๋“ค์ด ์žˆ์„ ์ˆ˜ ์žˆ์ฃ .


.

.

myapp.root=/aaa/bbb/myapp

myapp.log=/aaa/bbb/myapp/log

myapp.modules=/aaa/bbb/myapp/modules

.

.


์ €๋Ÿฐ์‹์œผ๋กœ ๊ฒฝ๋กœ๊ฐ€ ์ €์žฅ์ด ๋˜์–ด์žˆ๋Š”๋ฐ, ์•ž๋ถ€๋ถ„์˜ ์ ˆ๋Œ€๊ฒฝ๋กœ๋ฅผ ๋‹ค๋ฅธ ๊ฒฝ๋กœ๋กœ ๋ณ€๊ฒฝํ•ด์ฃผ๊ณ  ์‹ถ์„ ๋•Œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ํŒŒ์ผ ์‚ฌ์ด์ฆˆ๊ฐ€ ์ž‘๊ณ  4~5๊ฐœ ์ •๋„๋ฉด ํ•˜๋‚˜ํ•˜๋‚˜ ์ˆ˜์ •์„ ํ•ด์ค˜๋„ ๋˜๊ฒ ์ง€๋งŒ, ํŒŒ์ผ ์‚ฌ์ด์ฆˆ๊ฐ€ ํฌ๊ณ  ๊ฒฝ๋กœ ์„ค์ •ํ•˜๋Š” ๋ถ€๋ถ„์ด ์—ฌ๊ธฐ์ €๊ธฐ ํฉ์–ด์ ธ ์žˆ์œผ๋ฉด ์ผ์ผ์ด ์ฐพ์•„์„œ ์ˆ˜์ •ํ•˜๊ธฐ๊ฐ€ ์‰ฝ์ง€ ์•Š์Šต๋‹ˆ๋‹ค.


์ด๋Ÿด ๋•Œ ์‚ฌ์šฉํ•˜๋ผ๊ณ  vi์—์„œ๋Š” ๊ฐ•๋ ฅํ•œ ์น˜ํ™˜ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๊ณ ์žˆ์Šต๋‹ˆ๋‹ค.


%s/์น˜ํ™˜ํ•˜๋ ค๋Š”๋ฌธ์ž์—ด์ •๊ทœ์‹/๊ฒฐ๊ณผ๋ฌธ์ž์—ด์ •๊ทœ์‹/์˜ต์…˜


vi๋กœ ๋ฌธ์„œ๋ฅผ ์—ด์–ด๋†“์€ ์ƒํƒœ์—์„œ :์„ ์ž…๋ ฅํ•˜์—ฌ ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•œ ๋’ค ์œ„ ํ˜•์‹์— ๋งž๊ฒŒ ๋ฌธ์ž์—ด ๋ฐ ์˜ต์…˜์„ ์ž…๋ ฅํ•ด์ฃผ๋ฉด ํŒŒ์ผ ์ „์ฒด์—์„œ ํŠน์ • ๋ฌธ์ž์—ด์„ ์ฐพ์•„์„œ ๊ฒฐ๊ณผ๋ฌธ์ž์—ด๋กœ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


์œ„ ์˜ˆ๋ฅผ๋“ค์–ด /aaa/bbb/myapp์„ /ccc/ddd/app ์œผ๋กœ ๋ณ€๊ฒฝ์„ ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด


%s/\/aaa\/bbb\/myapp/\/ccc\/ddd\/app/g


๋ผ๊ณ  ์ž…๋ ฅํ•ด์ฃผ๋ฉด ์ „์ฒด ํŒŒ์ผ์˜ ๋‚ด์šฉ์„ ๊ฒ€์ƒ‰ํ•˜์—ฌ /aaa/bbb/myapp ๋ฌธ์ž์—ด์„ ์ฐพ์€ ๋’ค /ccc/ddd/app ์œผ๋กœ ์น˜ํ™˜ํ•ด์ค๋‹ˆ๋‹ค.


๋ช…๋ น์–ด๋ฅผ ์‚ดํŽด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.


%s : ๋ฌธ์„œ ์ „์ฒด

g : ํ•œ์ค„์— ๋™์ผ ํŒจํ„ด์ด ์—ฌ๋Ÿฌ๊ฐœ ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ ๋ชจ๋‘ ์ ์šฉ

i : ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„ ์•ˆํ•จ




์ด์ƒ์œผ๋กœ ๊ฐ„๋งŒ์˜ ํฌ์ŠคํŒ…์„ ๋งˆ์นฉ๋‹ˆ๋‹ค~

์•ˆ๋…•ํ•˜์„ธ์š”, ์ผ€์ด์น˜์ž…๋‹ˆ๋‹ค.


์˜ค๋Š˜๋„ ์‰ฝ๊ฒŒ ๋„˜์–ด๊ฐ€์ง€๋ฅผ ์•Š๊ณ  ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋„ค์š”. ์˜ค๋Š˜์€ ์œˆ๋„์šฐ์šฉ ๋„์ปค๋ฅผ ์„ค์น˜๋ฅผ ํ–ˆ๋‹ค๊ฐ€ jre 8 ์ด ์ •์ƒ์ ์œผ๋กœ ๋™์ž‘์„ ํ•˜์ง€ ๋ชปํ•˜๊ฒŒ ๋œ ์ผ€์ด์Šค์ž…๋‹ˆ๋‹ค.


jdk7, 8, jre7, 8์„ ์œˆ๋„์šฐ10 ์— ์„ค์น˜ํ•ด์„œ ์‚ฌ์šฉ์ค‘์ธ ํ™˜๊ฒฝ์ธ๋ฐ์š” jdk8์„ ์„ค์น˜ํ•ด๋ณด์‹  ๋ถ„๋“ค์€ ์•„์‹œ๊ฒ ์ง€๋งŒ 7๋ฒ„์ „ ๊นŒ์ง€๋Š” c:\Program Files\Java ๋ฐ‘์— ์„ค์น˜๊ฐ€ ๋˜๊ณ  ํ™˜๊ฒฝ์„ค์ • ๋ณ€์ˆ˜์—๋„ ํ•ด๋‹น ๊ฒฝ๋กœ์˜ bin๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์„ค์ •ํ•ด์คฌ์—ˆ๋Š”๋ฐ(C:\Program Files\Java\jre7\bin), 8๋ฒ„์ „๋ถ€ํ„ฐ๋Š” C:\ProgramData\Oracle\Java\javapath\java.exe ๋ฅผ ํ™˜๊ฒฝ๋ณ€์ˆ˜์— ๊ฐ€์ง€๋„๋ก ํ•ด๋†จ๋”๊ตฐ์š”. ๋ฌผ๋ก  ์„ค์น˜ ๊ฒฝ๋กœ๋Š” Program Files\Java\jdk1.8 ์ด์—ˆ๋Š”๋ฐ๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ๋ง์ด์ฃ . ์–ด์ฐŒ๋˜์—ˆ๊ฑด ์ž˜ ์“ฐ๊ณ ์žˆ์—ˆ๋Š”๋ฐ, ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธด๊ฑด ์ฑ—๋ด‡ ์˜คํ”ˆ์†Œ์Šค ํ…Œ์ŠคํŠธ๋ฅผ ํ•˜๋ ค๊ณ  ์œˆ๋„์šฐ์šฉ ๋„์ปค๋ฅผ ์„ค์น˜ํ•ด์„œ ํ…Œ์ŠคํŠธ๋ฅผ ํ•˜๊ณ ๋‚œ ๋’ค์— ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ฐ‘์ž๊ธฐ ์ดํด๋ฆฝ์Šค(Oxygen)๊ฐ€ ์‹คํ–‰์ด ์•ˆ๋˜๋”๊ตฐ์š”. 

๋ญ์ง€? ๋ญ์ง€? ํ•˜๋‹ค๊ฐ€, ์ดํด๋ฆฝ์Šค๋ฅผ ๋‹ค์‹œ ์„ค์น˜ํ•ด๋ณด๊ณ  ์‹คํ–‰ํ–ˆ๋”๋‹ˆ ์ฒ˜์Œ ํ•œ๋ฒˆ์€ ์ž˜ ๋ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ดํด๋ฆฝ์Šค๋ฅผ ์ข…๋ฃŒํ–ˆ๋‹ค๊ฐ€ ์‹คํ–‰์‹œํ‚ค๋ฉด ๋˜ ์•ˆ๋˜๊ณ  ์ฐธ ํฌํ•œํ•œ ํ˜„์ƒ์ด ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค. ใ… ใ… 


์•„๋ฌดํŠผ ํ•œ์ฐธ์„ ํ•ด๋ฉ”๋‹ค๊ฐ€ neon ๋ฒ„์ „์œผ๋กœ ๋‹ค์‹œ ์„ค์น˜ํ•ด์„œ๋„ ํ•ด๋ณด๊ณ  ํ•˜๋‹ค๊ฐ€ java๋ฅผ ์‹คํ–‰ํ•ด๋ดค๋Š”๋ฐ ์•„๋ž˜์™€ ๊ฐ™์€ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋”๊ตฐ์š”.


Error occurred during initialization of VM

java/lang/NoClassDefFoundError: java/lang/Object


ํ—ˆํ—ˆ, ์ด๋Ÿฐ ๋ฉ”์‹œ์ง€๋Š” ๋˜ ์ฒ˜์Œ ๋ด์„œ ๊ตฌ๊ธ€๋ง์„ ํ•ด๋ดค๋”๋‹ˆ jdk, jre ์„ค์น˜๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ์•ˆ๋œ ์ผ€์ด์Šค์—์„œ ๋ฐœ์ƒํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์€ ๊ฒƒ ๊ฐ™๋”๊ตฐ์š”.


์กฐ๊ธˆ ์ „๊นŒ์ง€ ์•„๋ฌด ๋ฌธ์ œ ์—†๋˜๊ฒŒ ๋„์ปค๋ฅผ ์„ค์น˜ํ–ˆ๋”๋‹ˆ ์ž๋ฐ”๊ฐ€ ์‹คํ–‰์ด ์•ˆ๋œ๋‹ค? ์ข€ ์ดํ•ด๊ฐ€ ์•ˆ๋˜๋Š” ๋ถ€๋ถ„์ด๋ผ์„œ java๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•  ๋•Œ Program Files ํ•˜์œ„์˜ java๋ฅผ ์“ฐ๋Š”๊ฑด์ง€ ProgramDataํ•˜์œ„์˜ java๋ฅผ ์“ฐ๋Š”๊ฑด์ง€ ํ™•์ธํ•ด๋ณด๊ณ  ์‹ถ์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋•Œ ์‚ฌ์šฉํ•œ ๋ช…๋ น์–ด๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.


 for %i in (java.exe) do @echo. %~$PATH:i


์œˆ๋„์šฐ์—์„œ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณตํ•ด์ฃผ๋Š” for ๋ช…๋ น์–ด๋ฅผ ์ด์šฉํ•œ ๊ฒƒ์œผ๋กœ ๋„์›€๋ง์„ ์ถœ๋ ฅํ•ด๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.



C:\Users\dev>for /?

ํŒŒ์ผ ์ง‘ํ•ฉ์—์„œ ๊ฐ ํŒŒ์ผ์— ๋Œ€ํ•ด ์ง€์ •๋œ ๋ช…๋ น์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.


FOR %๋ณ€์ˆ˜ IN (์ง‘ํ•ฉ) DO ๋ช…๋ น์–ด [๋ช…๋ น์–ด ๋งค๊ฐœ ๋ณ€์ˆ˜]


  %๋ณ€์ˆ˜      ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋Š” ๋งค๊ฐœ ๋ณ€์ˆ˜๋ฅผ ํ•œ ๋ฌธ์ž๋กœ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.

  (์ง‘ํ•ฉ)      ํ•˜๋‚˜ ์ด์ƒ์˜ ํŒŒ์ผ์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ์™€์ผ๋“œ์นด๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  ๋ช…๋ น์–ด      ๊ฐ ํŒŒ์ผ์— ๋Œ€ํ•ด ์ˆ˜ํ–‰ํ•  ๋ช…๋ น์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.

  ๋ช…๋ น์–ด-๋งค๊ฐœ ๋ณ€์ˆ˜

              ์ง€์ •๋œ ๋ช…๋ น์˜ ๋งค๊ฐœ ๋ณ€์ˆ˜๋‚˜ ์Šค์œ„์น˜๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.


์ผ๊ด„ ํ”„๋กœ๊ทธ๋žจ์—์„œ FOR ๋ช…๋ น์„ ์“ฐ๋ ค๋ฉด, '%๋ณ€์ˆ˜' ๋Œ€์‹  '%%๋ณ€์ˆ˜'๋ฅผ ์ง€์ •ํ•˜์‹ญ์‹œ์˜ค.

๋ณ€์ˆ˜ ์ด๋ฆ„์—์„œ๋Š” ๋Œ€๋ฌธ์ž์™€ ์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ณ„ํ•˜๋ฏ€๋กœ %i์™€ %I๋Š” ๋‹ค๋ฆ…๋‹ˆ๋‹ค.


๋ช…๋ น ํ™•์žฅ์„ ์‚ฌ์šฉํ•˜๋ฉด FOR ๋ช…๋ น์— ์•„๋ž˜์™€ ๊ฐ™์€ ์ถ”๊ฐ€์ ์ธ ํ˜•ํƒœ๊ฐ€

์ง€์›๋ฉ๋‹ˆ๋‹ค.


FOR /D %๋ณ€์ˆ˜ IN (์ง‘ํ•ฉ) DO ๋ช…๋ น [๋ช…๋ น-๋งค๊ฐœ ๋ณ€์ˆ˜]


   ์ง‘ํ•ฉ์— ๋Œ€ํ‘œ ๋ฌธ์ž๊ฐ€ ์žˆ์œผ๋ฉด ํŒŒ์ผ ์ด๋ฆ„ ๋Œ€์‹  ๋””๋ ‰ํ„ฐ๋ฆฌ ์ด๋ฆ„๊ณผ

   ์ผ์น˜ํ•˜๋„๋ก ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.


FOR /R [[๋“œ๋ผ์ด๋ธŒ:]๊ฒฝ๋กœ] %๋ณ€์ˆ˜ IN (์ง‘ํ•ฉ) DO ๋ช…๋ น [๋ช…๋ น-๋งค๊ฐœ ๋ณ€์ˆ˜]


   [๋“œ๋ผ์ด๋ธŒ:]๊ฒฝ๋กœ๋ฅผ ๋ฃจํŠธ๋กœ ํ•˜์—ฌ ๋””๋ ‰ํ„ฐ๋ฆฌ ํŠธ๋ฆฌ๋ฅผ ๋”ฐ๋ผ ๋‚ด๋ ค๊ฐ€๋ฉฐ

   FOR ๊ตฌ๋ฌธ์„ ํŠธ๋ฆฌ์˜ ๊ฐ ๋””๋ ‰ํ„ฐ๋ฆฌ์—์„œ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. /R ์Šค์œ„์น˜ ๋’ค์—

   ๋””๋ ‰ํ„ฐ๋ฆฌ๊ฐ€ ์ง€์ •๋˜์ง€ ์•Š์œผ๋ฉด ํ˜„์žฌ ๋””๋ ‰ํ„ฐ๋ฆฌ๊ฐ€ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

   ์ง‘ํ•ฉ์— ๋งˆ์นจํ‘œ(.)๊ฐ€ ์‚ฌ์šฉ๋˜๋ฉด ๋””๋ ‰ํ„ฐ๋ฆฌ ํŠธ๋ฆฌ๋งŒ ๋‚˜์—ดํ•ฉ๋‹ˆ๋‹ค.


FOR /L %๋ณ€์ˆ˜ IN (์‹œ์ž‘,๋‹จ๊ณ„,๋) DO ๋ช…๋ น [๋ช…๋ น-๋งค๊ฐœ ๋ณ€์ˆ˜]


   ์ง‘ํ•ฉ์€ ๋‹จ๊ณ„๋ณ„๋กœ ์ฆ๊ฐ€/๊ฐ์†Œํ•˜๋Š” ์‹œ์ž‘๋ถ€ํ„ฐ ๋๊นŒ์ง€์˜ ์ผ๋ จ์˜ ์ˆซ์ž์ž…๋‹ˆ๋‹ค.

   ๋”ฐ๋ผ์„œ (1,1,5)๋Š” 1 2 3 4 5๋ฅผ ๋‚˜ํƒ€๋‚ด๋ฉฐ (5,-1,1)์€ 5 4 3 2 1์„

   ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.


FOR /F ["์˜ต์…˜"] %๋ณ€์ˆ˜ IN (ํŒŒ์ผ-์ง‘ํ•ฉ) DO ๋ช…๋ น [๋ช…๋ น-๋งค๊ฐœ ๋ณ€์ˆ˜]

FOR /F ["์˜ต์…˜"] %๋ณ€์ˆ˜ IN ("๋ฌธ์ž์—ด") DO ๋ช…๋ น์–ด [๋ช…๋ น-๋งค๊ฐœ ๋ณ€์ˆ˜]

FOR /F ["์˜ต์…˜"] %๋ณ€์ˆ˜ IN ('๋ช…๋ น์–ด') DO ๋ช…๋ น์–ด [๋ช…๋ น-๋งค๊ฐœ ๋ณ€์ˆ˜]


    ๋˜๋Š” usebackq ์˜ต์…˜์ด ์žˆ๋Š” ๊ฒฝ์šฐ:


FOR /F ["์˜ต์…˜"] %๋ณ€์ˆ˜ IN (ํŒŒ์ผ-์ง‘ํ•ฉ) DO ๋ช…๋ น [๋ช…๋ น-๋งค๊ฐœ ๋ณ€์ˆ˜]

FOR /F ["์˜ต์…˜"] %๋ณ€์ˆ˜ IN ('๋ฌธ์ž์—ด') DO ๋ช…๋ น์–ด [๋ช…๋ น-๋งค๊ฐœ ๋ณ€์ˆ˜]

FOR /F ["์˜ต์…˜"] %๋ณ€์ˆ˜ IN (`๋ช…๋ น์–ด`) DO ๋ช…๋ น์–ด [๋ช…๋ น-๋งค๊ฐœ ๋ณ€์ˆ˜]


   ํŒŒ์ผ-์ง‘ํ•ฉ์€ ํ•˜๋‚˜ ์ด์ƒ์˜ ํŒŒ์ผ ์ด๋ฆ„์ž…๋‹ˆ๋‹ค. ํŒŒ์ผ-์ง‘ํ•ฉ์˜ ๊ฐ ํŒŒ์ผ์€

   ๋‹ค์Œ ํŒŒ์ผ๋กœ ์ด๋™ํ•˜๊ธฐ ์ „์— ์—ด๊ธฐ ๋˜๋Š” ์ฝ๊ธฐ ๋“ฑ์˜ ์ž‘์—…์ด ์ง„ํ–‰๋ฉ๋‹ˆ๋‹ค.

   ํŒŒ์ผ์„ ์ฝ์–ด์„œ ๋ฌธ์ž์—ด์„ ํ•œ ํ–‰์”ฉ ๋ถ„๋ฆฌํ•˜๊ณ  ๊ฐ ํ–‰์„ 0๊ฐœ ์ด์ƒ์˜

   ํ† ํฐ์œผ๋กœ ๊ตฌ๋ฌธ ๋ถ„์„ํ•˜๋Š” ๊ณผ์ •์œผ๋กœ ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. For ๋ฃจํ”„์˜ ๋ณธ๋ฌธ์€๋ฐœ๊ฒฌ๋œ ํ† ํฐ ๋ฌธ์ž์—ด์— ์„ค์ •๋œ ๋ณ€์ˆ˜ ๊ฐ’(๋“ค)๊ณผ ํ•จ๊ป˜ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.

   ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ /F๋Š” ํŒŒ์ผ์˜ ๊ฐ ํ–‰์œผ๋กœ๋ถ€ํ„ฐ ๋ถ„๋ฆฌ๋œ ํ† ํฐ์„ ์ฒซ ๋ฒˆ์งธ ๊ณต๋ฐฑ์—์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. ๋นˆ ํ–‰์€ ๊ฑด๋„ˆ๋œ๋‹ˆ๋‹ค. "์˜ต์…˜" ๋งค๊ฐœ ๋ณ€์ˆ˜๋ฅผ ์ง€์ •ํ•˜์—ฌ

   ๊ธฐ๋ณธ ๊ตฌ๋ฌธ ๋ถ„์„ ๋™์ž‘์„ ๋ฌด์‹œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ๋‹ค๋ฅธ ๊ตฌ๋ฌธ ๋ถ„์„

   ๋งค๊ฐœ ๋ณ€์ˆ˜๋ฅผ ์ง€์ •ํ•˜๋Š” ํ•˜๋‚˜ ์ด์ƒ์˜ ํ‚ค์›Œ๋“œ๋ฅผ ๊ฐ–๋Š” ์ธ์šฉ ๋ถ€ํ˜ธ๋กœ

   ๋ฌถ์ธ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค.

   ํ‚ค์›Œ๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.


   eol=c  - ํ–‰ ๋ ์„ค๋ช… ๋ฌธ์ž๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค (ํ•˜๋‚˜๋งŒ)

   skip=n  - ํŒŒ์ผ์˜ ์‹œ์ž‘ ๋ถ€๋ถ„์—์„œ ๋ฌด์‹œํ•  ํ–‰์˜ ๊ฐœ์ˆ˜๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.

   delims=xxx  - ๊ตฌ๋ถ„ ๋ฌธ์ž ์ง‘ํ•ฉ์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.  ์ด๊ฒƒ์€ ๊ณต๋ฐฑ ๋˜


๊ณ„์†ํ•˜๋ ค๋ฉด ์•„๋ฌด ํ‚ค๋‚˜ ๋ˆ„๋ฅด์‹ญ์‹œ์˜ค . . .


๋„ˆ๋ฌด ๊ธธ์–ด์„œ ์งง๊ฒŒ ๋Š์—ˆ์Šต๋‹ˆ๋‹ค. ์œ„์—์„œ ์‚ฌ์šฉํ•œ ๋ช…๋ น์–ด๋Š” ์œ ๋‹‰์Šค์˜ which java์™€ ๊ฐ™์ด PATHํ™˜๊ฒฝ๋ณ€์ˆ˜์— ์žˆ๋Š” ๋””๋ ‰ํ† ๋ฆฌ ๊ฒฝ๋กœ๋ฅผ ํ›‘์œผ๋ฉด์„œ ์ œ์ผ ์ฒ˜์Œ ๋งŒ๋‚˜๋Š” java์œ„์น˜๋ฅผ ์ถœ๋ ฅํ•ด์ค๋‹ˆ๋‹ค. ์ฆ‰, ์œˆ๋„์šฐ์šฉ which ๋ช…๋ น์–ด๋ผ๊ณ  ์ƒ๊ฐํ•˜์‹œ๋ฉด ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.



์ด์ƒ์ž…๋‹ˆ๋‹ค.




์•„๋ž˜๋Š” ๊ธฐ๋ก์„ ์œ„ํ•ด์„œ ๊ฐœ์ธ์ ์œผ๋กœ ์ž‘์„ฑํ–ˆ๋˜ ๋…ธํŠธ์ž…๋‹ˆ๋‹ค.

์˜คํ”ˆ์†Œ์Šค ์ฑ—๋ด‡ ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•ด์„œ Docker ์„ค์น˜ํ•˜๊ณ  ํ…Œ์ŠคํŠธ ํ•œ ์ดํ›„๋ถ€ํ„ฐ ์ดํด๋ฆฝ์Šค ์‹คํ–‰์ด ์•ˆ๋˜๋Š” ํ˜„์ƒ ๋ฐœ์ƒ
    - ๋‹ค์‹œ ๋™์ผํ•œ ๋””๋ ‰ํ† ๋ฆฌ์— Oxygen๋ฒ„์ „ ์ธ์Šคํ†จ๋Ÿฌ๋ฅผ ์ด์šฉํ•ด์„œ ์žฌ์„ค์น˜ ํ•œ ๋’ค launchํ•˜๋ฉด ๊ธฐ๋™์ด ๋˜๋‚˜, ์žฌ๋ถ€ํŒ… ํ›„์—๋Š” ์—ญ์‹œ ๋˜ ์•ˆ๋จ
    - Neon ๋ฒ„์ „์ฒ˜๋Ÿผ ํŒจํ‚ค์ง€๋กœ ๋ฐฐํฌ๋˜๋Š” ์ดํด๋ฆฝ์Šค๋Š” ์ƒˆ๋กœ ๋‹ค์šด๋ฐ›์•„์„œ ์‹คํ–‰ํ•ด๋„ ์‹คํ–‰ ์•ˆ๋จ
    - Java๋ฅผ ์‹คํ–‰ํ•ด๋ณด๋‹ˆ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์—๋Ÿฌ ๋ฉ”์‹œ์ง€ ๋ฐœ์ƒ
        - Error occurred during initialization of VM
          java/lang/NoClassDefFoundError: java/lang/Object
    - ํ˜„์žฌ jdk1.7๊ณผ 1.8 ๋ฒ„์ „์ด ์„ค์น˜๋˜์–ด์žˆ๊ณ  jre์—ญ์‹œ 7, 8 ๋ฒ„์ „์ด ์„ค์น˜๋˜์–ด์žˆ์Œ
    - ์•„๋ž˜ ๋ช…๋ น์–ด๋ฅผ ์ด์šฉํ•˜์—ฌ ์–ด๋Š ์œ„์น˜์˜ ์ž๋ฐ”๊ฐ€ ์‹คํ–‰๋˜๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธ
        for %i in (java.exe) do @echo. %~$PATH:i
        - C:\ProgramData\Oracle\Java\javapath\java.exe
    - jdk 1.8์„ ์„ค์น˜ํ•˜๋ฉด ํ™˜๊ฒฝ๋ณ€์ˆ˜์— ์œ„ ๊ฒฝ๋กœ๋กœ ์„ธํŒ…์ด ๋จ
    - Docker๋ฅผ ์„ค์น˜ํ•˜๊ณ ๋‚˜์„œ ์œ„ java๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•จ
    - jdk1.7, jdk1.8 ์˜ java.exe๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์ •์ƒ์ ์œผ๋กœ  ์‹คํ–‰์ด ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธ
    - jdk1.8์„ ์žฌ์„ค์น˜ํ•˜์—ฌ ์ •์ƒ์ ์œผ๋กœ java๊ฐ€ ์‹คํ–‰์ด ๋˜๋„๋ก ํ•  ์ˆ˜๋„ ์žˆ์œผ๋‚˜ ์ผ๋‹จ์€ ํ™˜๊ฒฝ๋ณ€์ˆ˜ ํ™•์ธ
    - ํ™˜๊ฒฝ๋ณ€์ˆ˜์—๋Š” JAVA_HOME์— jdk1.8์˜ ํŒจ์Šค๊ฐ€ ์žกํ˜€์žˆ๊ณ  PATH์— %JAVA_HOME%\bin์ด ์žˆ์Œ, ๊ทธ๋ฆฌ๊ณ  ProgramData\OracleJava\javapath๋„ ์žกํ˜€์žˆ์Œ
    - ํ™˜๊ฒฝ๋ณ€์ˆ˜์—์„œ ProgramData\OracleJava\javapath ๊ฒฝ๋กœ๋Š” ์ œ๊ฑฐํ•จ
    - ๋‹ค์‹œ ์ดํด๋ฆฝ์Šค๋ฅผ ์žฌ๊ธฐ๋™ ํ•ด๋ณด๋‹ˆ ์ •์ƒ์ ์œผ๋กœ ๊ธฐ๋™ํ•จ

    - jre1.8์˜ java๋Š” ์ •์ƒ์ ์œผ๋กœ ์‹คํ–‰์ด ์•ˆ๋จ -> ์žฌ์„ค์น˜






์•ˆ๋…•ํ•˜์„ธ์š” ์ผ€์ด์น˜์ž…๋‹ˆ๋‹ค.


์˜ค๋Š˜์€ ์œˆ๋„์šฐ์—์„œ ํŠน์ • ํฌํŠธ๋ฅผ ์‚ฌ์šฉ์ค‘์ธ ํ”„๋กœ์„ธ์Šค๋ฅผ ์ฃฝ์ด๋Š” ๋ฒ•์„ ๊ฐ€์ง€๊ณ  ์™”์Šต๋‹ˆ๋‹ค. 


์ตœ๊ทผ์— Node.js ์•ฑ์„ ํ…Œ์ŠคํŠธํ•ด๋ณผ ์ผ์ด ์žˆ์—ˆ๋Š”๋ฐ ๊ณ ์ •์ ์œผ๋กœ ํŠน์ • ํฌํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์•ฑ์ด์—ˆ์Šต๋‹ˆ๋‹ค. ์œˆ๋„์šฐ์—์„œ cmd ์ฐฝ์„ ๋„์›Œ์„œ ์‹คํ–‰์‹œ์ผฐ๋‹ค๊ฐ€ Ctrl + C๋กœ  ํ”„๋กœ๊ทธ๋žจ์„ ์ค‘์ง€์‹œ์ผฐ๋Š”๋ฐ ๋‹ค์‹œ ๊ธฐ๋™์‹œ์ผฐ๋”๋‹ˆ ํฌํŠธ๊ฐ€ ์ด๋ฏธ ์‚ฌ์šฉ์ค‘์ด๋ผ๋ฉด์„œ ์—๋Ÿฌ๊ฐ€ ๋‚˜๋”๋ผ๊ตฌ์š”. ๊ทธ๋ž˜์„œ ์ด๋†ˆ์„ ์–ด๋–ป๊ฒŒ ์ฃฝ์—ฌ์•ผ ํ•˜๋‚˜ ์ฐพ์•„๋ดค์Šต๋‹ˆ๋‹ค.


์šฐ์„  ์ œ ํ™˜๊ฒฝ์€ Windows 10์ด๊ตฌ์š” CMD์ฐฝ์„ ๋„์šฐ๋ฉด ์•„๋ž˜์ฒ˜๋Ÿผ ๋‚˜์˜ต๋‹ˆ๋‹ค.


์ œ๊ฐ€ ๋ดค๋˜ ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๋Š” ์•„๋ž˜์™€ ๊ฐ™๊ตฌ์š”


Error: listen EADDRINUSE :::2002


2002๋ฒˆ ํฌํŠธ๊ฐ€ ์ด๋ฏธ ์‚ฌ์šฉ์ค‘์ด๋ผ๋Š” ๋ฉ”์‹œ์ง€์ž…๋‹ˆ๋‹ค.


๊ทธ๋Ÿผ ๋ˆ„๊ฐ€ ์ด ํฌํŠธ๋ฅผ ์‚ฌ์šฉ์ค‘์ธ์ง€ ์ฐพ์•„๋ณด์ฃ .


cmd ์ฐฝ์— ์•„๋ž˜ ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค.


C:\Users\dev>netstat -ano|find "2002"

  TCP    0.0.0.0:2002         0.0.0.0:0           LISTENING       5096

  TCP    [::]:2002              [::]:0                 LISTENING       5096


์ถœ๋ ฅ๋œ ๋‚ด์šฉ์€ TCP ํ”„๋กœํ† ์ฝœ์—์„œ 2002๋ฒˆ ํฌํŠธ๋กœ ๋ฆฌ์Šค๋‹ํ•˜๊ณ ์žˆ๋Š” ํ”„๋กœ์„ธ์Šค์˜ ID๊ฐ€ 5096๋ฒˆ์ด๋ผ๋Š” ๊ฑธ ๋งํ•ด์ค๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” 5096 ํ”„๋กœ์„ธ์Šค๋ฅผ ์ฃฝ์ด๋ฉด ๋˜๋Š”๊ฑฐ๊ฒ ์ฃ . ์ฃฝ์ด๊ธฐ ์ „์— ์œ„ ๋ช…๋ น์–ด์— ๋Œ€ํ•ด์„œ ์„ค๋ช…์„ ์ข€ ๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค.


netstat -ano๋Š” netstat ๋ช…๋ น์–ด๋ฅผ a, n, o ์˜ต์…˜์œผ๋กœ ์‹คํ–‰์‹œํ‚ค๋ผ๋Š” ์˜๋ฏธ์ฃ . ์—ฌ๊ธฐ์„œ a, n, o์— ๋Œ€ํ•œ ์„ค๋ช…์€ netstat /? ๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


C:\Users\dev>netstat /?


ํ”„๋กœํ† ์ฝœ ํ†ต๊ณ„์™€ ํ˜„์žฌ TCP/IP ๋„คํŠธ์›Œํฌ ์—ฐ๊ฒฐ์„ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.


NETSTAT [-a] [-b] [-e] [-f] [-n] [-o] [-p proto] [-r] [-s] [-x] [-t] [interval]


  -a            ๋ชจ๋“  ์—ฐ๊ฒฐ๊ณผ ์ˆ˜์‹  ๋Œ€๊ธฐ ํฌํŠธ๋ฅผ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.

  -b            ๊ฐ ์—ฐ๊ฒฐ ๋˜๋Š” ์ˆ˜์‹  ๋Œ€๊ธฐ ํฌํŠธ ์ƒ์„ฑ๊ณผ ๊ด€๋ จ๋œ ์‹คํ–‰ ํŒŒ์ผ์„

                ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค. ์ž˜ ์•Œ๋ ค์ง„ ์‹คํ–‰ ํŒŒ์ผ์ด ์—ฌ๋Ÿฌ ๋…๋ฆฝ ๊ตฌ์„ฑ ์š”์†Œ๋ฅผ

                ํ˜ธ์ŠคํŒ…ํ•  ๊ฒฝ์šฐ ์—ฐ๊ฒฐ ๋˜๋Š” ์ˆ˜์‹  ๋Œ€๊ธฐ ํฌํŠธ ์ƒ์„ฑ๊ณผ ๊ด€๋ จ๋œ

                ๊ตฌ์„ฑ ์š”์†Œ์˜ ์‹œํ€€์Šค๊ฐ€ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค.

                ์ด๋Ÿฌํ•œ ๊ฒฝ์šฐ์—๋Š” ์‹คํ–‰ ํŒŒ์ผ ์ด๋ฆ„์ด ๋Œ€๊ด„ํ˜ธ๋กœ ์•„๋ž˜์—

                ํ‘œ์‹œ๋˜๊ณ  ์œ„์—๋Š” TCP/IP์— ๋„๋‹ฌํ•  ๋•Œ๊นŒ์ง€

                ํ˜ธ์ถœ๋œ ๊ตฌ์„ฑ ์š”์†Œ๊ฐ€ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. ์ด ์˜ต์…˜์€ ์‹œ๊ฐ„์ด ์˜ค๋ž˜

                ๊ฑธ๋ฆด ์ˆ˜ ์žˆ์œผ๋ฉฐ ์‚ฌ์šฉ ๊ถŒํ•œ์ด ์—†์œผ๋ฉด ์‹คํŒจํ•ฉ๋‹ˆ๋‹ค.

  -e            ์ด๋”๋„ท ํ†ต๊ณ„๋ฅผ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค. ์ด ์˜ต์…˜์€ -s ์˜ต์…˜๊ณผ ํ•จ๊ป˜

                ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  -f            ์™ธ๋ถ€ ์ฃผ์†Œ์˜ FQDN(์ •๊ทœํ™”๋œ ๋„๋ฉ”์ธ ์ด๋ฆ„)์„

                ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.

  -n            ์ฃผ์†Œ์™€ ํฌํŠธ ๋ฒˆํ˜ธ๋ฅผ ์ˆซ์ž ํ˜•์‹์œผ๋กœ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.

  -o            ๊ฐ ์—ฐ๊ฒฐ์˜ ์†Œ์œ ์ž ํ”„๋กœ์„ธ์Šค ID๋ฅผ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.

  -p proto      proto๋กœ ์ง€์ •ํ•œ ํ”„๋กœํ† ์ฝœ์˜ ์—ฐ๊ฒฐ์„ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค. proto๋Š”

                TCP, UDP, TCPv6 ๋˜๋Š” UDPv6 ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค. -s ์˜ต์…˜๊ณผ ํ•จ๊ป˜

                ์‚ฌ์šฉํ•˜์—ฌ ํ”„๋กœํ† ์ฝœ๋ณ„ ํ†ต๊ณ„๋ฅผ ํ‘œ์‹œํ•  ๊ฒฝ์šฐ proto๋Š” IP, IPv6, ICMP,

                ICMPv6, TCP, TCPv6, UDP ๋˜๋Š” UDPv6 ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค.

  -q            ๋ชจ๋“  ์—ฐ๊ฒฐ, ์ˆ˜์‹  ๋Œ€๊ธฐ ํฌํŠธ ๋ฐ ๋ฐ”์ธ๋”ฉ๋œ ๋น„์ˆ˜์‹  ๋Œ€๊ธฐ TCP

                ํฌํŠธ๋ฅผ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค. ๋ฐ”์ธ๋”ฉ๋œ ๋น„์ˆ˜์‹  ๋Œ€๊ธฐ ํฌํŠธ๋Š” ํ™œ์„ฑ ์—ฐ๊ฒฐ๊ณผ ์—ฐ๊ฒฐ๋˜๊ฑฐ๋‚˜

                ์—ฐ๊ฒฐ๋˜์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

  -r            ๋ผ์šฐํŒ… ํ…Œ์ด๋ธ”์„ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.

  -s            ํ”„๋กœํ† ์ฝœ๋ณ„ ํ†ต๊ณ„๋ฅผ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ IP, IPv6, ICMP,

                ICMPv6, TCP, TCPv6, UDP ๋ฐ UDPv6์— ๋Œ€ํ•œ ํ†ต๊ณ„๋ฅผ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.

                -p ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ธฐ๋ณธ๊ฐ’์˜ ์ผ๋ถ€ ์ง‘ํ•ฉ์— ๋Œ€ํ•œ ํ†ต๊ณ„๋งŒ

                ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  -t            ํ˜„์žฌ ์—ฐ๊ฒฐ ์˜คํ”„๋กœ๋“œ ์ƒํƒœ๋ฅผ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.

  -x            NetworkDirect ์—ฐ๊ฒฐ, ์ˆ˜์‹ ๊ธฐ ๋ฐ ๊ณต์œ  ๋์ ์„

                ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.

  -y            ๋ชจ๋“  ์—ฐ๊ฒฐ์— ๋Œ€ํ•œ TCP ์—ฐ๊ฒฐ ํ…œํ”Œ๋ฆฟ์„ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.

                ๋‹ค๋ฅธ ์˜ต์…˜๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

  interval      ๋‹ค์Œ ํ™”๋ฉด์œผ๋กœ ์ด๋™ํ•˜๊ธฐ ์ „์— ์ง€์ •ํ•œ ์‹œ๊ฐ„(์ดˆ) ๋™์•ˆ ์„ ํƒํ•œ ํ†ต๊ณ„๋ฅผ ๋‹ค์‹œ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.

                ํ†ต๊ณ„ ๋‹ค์‹œ ํ‘œ์‹œ๋ฅผ ์ค‘์ง€ํ•˜๋ ค๋ฉด <Ctrl+C>๋ฅผ ๋ˆ„๋ฅด์‹ญ์‹œ์˜ค.

                ์ด ๊ฐ’์„ ์ƒ๋žตํ•˜๋ฉด ํ˜„์žฌ ๊ตฌ์„ฑ ์ •๋ณด๊ฐ€

                ํ•œ ๋ฒˆ๋งŒ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.


์ด๋ ‡๊ฒŒ ์‹คํ–‰๋œ ๊ฒฐ๊ณผ๋ฅผ ํŒŒ์ดํ”„( | )๋ฅผ ํ†ตํ•ด find ๋ช…๋ น์–ด๋กœ ์ „๋‹ฌํ•˜์—ฌ "2002"๋ฅผ ์ฐพ์œผ๋ผ๊ณ  ํ•˜๊ณ ์žˆ์ฃ . find ๋ช…๋ น์–ด๋Š” ๋ฆฌ๋ˆ…์Šค์˜ grep๊ณผ ๋น„์Šทํ•œ ๊ธฐ๋Šฅ์„ ํ•ฉ๋‹ˆ๋‹ค.


find "2002"๋ฅผ ์ œ์™ธํ•œ ์•ž ๋ถ€๋ถ„๋งŒ ์‹คํ–‰์‹œํ‚ค๋ฉด 2002ํฌํŠธ ๋ง๊ณ ๋„  ๋‹ค๋ฅธ ์—ฌ๋Ÿฌ ํฌํŠธ๋“ค์— ๋Œ€ํ•œ ๋ชฉ๋ก์ด ํ•จ๊ป˜ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. 


์ž, ๊ทธ๋Ÿผ 5096๋ฒˆ ํ”„๋กœ์„ธ์Šค๋ฅผ ์ฃฝ์—ฌ๋ณผ๊นŒ์š”?


์œˆ๋„์šฐ์—์„œ ํ”„๋กœ์„ธ์Šค๋ฅผ ์ฃฝ์ผ ๋•Œ๋Š” "์ž‘์—…๊ด€๋ฆฌ์ž"๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ์ง€๋งŒ ๊ฐœ๋ฐœ์„ ํ•˜๋‹ค๋ณด๋ฉด ๊ฐ„ํ˜น ์ž‘์—…๊ด€๋ฆฌ์ž์—์„œ ์ฐพ๊ธฐ ํž˜๋“  ๋…€์„๋“ค์ด ์žˆ์ฃ . ๊ทธ๋Ÿด ๊ฒฝ์šฐ cmd์ฐฝ์—์„œ ์ž‘์—…์„ ํ•˜๋ฉด ํ›จ์”ฌ ์ˆ˜์›”ํ•˜๊ฒŒ ์ž‘์—…์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


ํ”„๋กœ์„ธ์Šค๋ฅผ ์ฃฝ์ผ ๋•Œ๋Š” taskkill ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ ์ด๋…€์„์— ๋Œ€ํ•œ ์„ค๋ช…์„ ํ•œ๋ฒˆ ๋ณผ๊นŒ์š”?


C:\Users\dev>taskkill /?


TASKKILL [/S ์‹œ์Šคํ…œ [/U ์‚ฌ์šฉ์ž ์ด๋ฆ„ [/P [์•”ํ˜ธ]]]]

         { [/FI ํ•„ํ„ฐ] [/PID ํ”„๋กœ์„ธ์Šค id | /IM ์ด๋ฏธ์ง€ ์ด๋ฆ„] } [/T] [/F]


์„ค๋ช…:

    ์ด ๋„๊ตฌ๋Š” ํ”„๋กœ์„ธ์Šค ID(PID) ๋˜๋Š” ์ด๋ฏธ์ง€ ์ด๋ฆ„์œผ๋กœ ์ž‘์—…์„ ์ข…๋ฃŒํ•˜๋Š” ๋ฐ

    ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.


๋งค๊ฐœ ๋ณ€์ˆ˜ ๋ชฉ๋ก:

    /S    ์‹œ์Šคํ…œ           ์—ฐ๊ฒฐํ•  ์›๊ฒฉ ์‹œ์Šคํ…œ์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.


    /U    [๋„๋ฉ”์ธ\]์‚ฌ์šฉ์ž  ๋ช…๋ น์„ ์‹คํ–‰ํ•ด์•ผ ํ•˜๋Š” ์‚ฌ์šฉ์ž ์ปจํ…์ŠคํŠธ๋ฅผ

                           ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.


    /P    [์•”ํ˜ธ]           ํ•ด๋‹น ์‚ฌ์šฉ์ž ์ปจํ…์ŠคํŠธ์˜ ์•”ํ˜ธ๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.

                           ์ƒ๋žตํ•œ ๊ฒฝ์šฐ์—๋Š” ๋ฌผ์–ด๋ด…๋‹ˆ๋‹ค.


    /FI   ํ•„ํ„ฐ             ์ž‘์—… ์ง‘ํ•ฉ์„ ์„ ํƒํ•˜๋Š” ํ•„ํ„ฐ๋ฅผ ์ ์šฉํ•ฉ๋‹ˆ๋‹ค.

                           "*"๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ: imagename eq acme*


    /PID  ํ”„๋กœ์„ธ์Šค_ID      ์ข…๋ฃŒํ•  ํ”„๋กœ์„ธ์Šค์˜ PID๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.

                           TaskList๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ PID๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


    /IM   ์ด๋ฏธ์ง€ ์ด๋ฆ„      ์ข…๋ฃŒํ•  ํ”„๋กœ์„ธ์Šค์˜ ์ด๋ฏธ์ง€ ์ด๋ฆ„์„

                           ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ์™€์ผ๋“œ์นด๋“œ ๋ฌธ์ž '*'๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ

                           ๋ชจ๋“  ์ž‘์—… ๋˜๋Š” ์ด๋ฏธ์ง€ ์ด๋ฆ„์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


    /T                     ์ง€์ •๋œ ํ”„๋กœ์„ธ์Šค์™€ ๊ทธ ํ”„๋กœ์„ธ์Šค๋กœ๋ถ€ํ„ฐ ์‹œ์ž‘๋œ

                           ๋ชจ๋“  ์ž์‹ ํ”„๋กœ์„ธ์Šค๋ฅผ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.


    /F                     ํ”„๋กœ์„ธ์Šค๋ฅผ ๊ฐ•์ œ๋กœ ์ข…๋ฃŒํ•˜๋„๋ก ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.


    /?                     ์ด ๋„์›€๋ง ๋ฉ”์‹œ์ง€๋ฅผ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.


ํ•„ํ„ฐ:

    ํ•„ํ„ฐ ์ด๋ฆ„     ์œ ํšจํ•œ ์—ฐ์‚ฐ์ž             ์œ ํšจํ•œ ๊ฐ’

    -----------   ---------------           -------------------------

    STATUS        eq, ne                    RUNNING |

                                            NOT RESPONDING | UNKNOWN

    IMAGENAME     eq, ne                    ์ด๋ฏธ์ง€ ์ด๋ฆ„

    PID           eq, ne, gt, lt, ge, le    PID ๊ฐ’

    SESSION       eq, ne, gt, lt, ge, le    ์„ธ์…˜ ๋ฒˆํ˜ธ.

    CPUTIME       eq, ne, gt, lt, ge, le    CPU ์‹œ๊ฐ„ ํ˜•์‹

                                            hh:mm:ss

                                            hh - ์‹œ๊ฐ„,

                                            mm - ๋ถ„, ss - ์ดˆ

    MEMUSAGE      eq, ne, gt, lt, ge, le    ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ(KB)

    USERNAME      eq, ne                    ์‚ฌ์šฉ์ž ์ด๋ฆ„([domain\]user

                                            ํ˜•์‹)

    MODULES       eq, ne                    DLL ์ด๋ฆ„

    SERVICES      eq, ne                    ์„œ๋น„์Šค ์ด๋ฆ„

    WINDOWTITLE   eq, ne                    ์ฐฝ ์ œ๋ชฉ


    ์ฐธ๊ณ 

    ----

    1) /IM ์Šค์œ„์น˜์— ๋Œ€ํ•œ ์™€์ผ๋“œ์นด๋“œ ๋ฌธ์ž '*'๋Š” ํ•„ํ„ฐ๊ฐ€ ์ ์šฉ๋  ๋•Œ๋งŒ

    ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    2) ์›๊ฒฉ ํ”„๋กœ์„ธ์Šค๋Š” ํ•ญ์ƒ ๊ฐ•์ œ์ ์œผ๋กœ(/F) ์ข…๋ฃŒ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    3) ์›๊ฒฉ ์ปดํ“จํ„ฐ๊ฐ€ ์ง€์ •๋˜๋ฉด "WINDOWTITLE"  ๋ฐ "STATUS" ํ•„ํ„ฐ๋Š”

       ์ง€์›๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.


์˜ˆ:

    TASKKILL /IM notepad.exe

    TASKKILL /PID 1230 /PID 1241 /PID 1253 /T

    TASKKILL /F /IM cmd.exe /T

    TASKKILL /F /FI "PID ge 1000" /FI "WINDOWTITLE ne untitle*"

    TASKKILL /F /FI "USERNAME eq NT AUTHORITY\SYSTEM" /IM notepad.exe

    TASKKILL /S ์‹œ์Šคํ…œ /U domain\username /FI "USERNAME ne NT*" /IM *

    TASKKILL /S ์‹œ์Šคํ…œ /U ์‚ฌ์šฉ์ž ์ด๋ฆ„ /P ์•”ํ˜ธ /FI "IMAGENAME eq note*"


์„ค๋ช…์„ ์ฝ์–ด๋ณด๋‹ˆ ์šฐ๋ฆฌ์—๊ฒŒ ํ•„์š”ํ•œ ๋‚ด์šฉ์ด ๋„ˆ๋ฌด๋‚˜๋„ ์ž˜ ์„ค๋ช…๋˜์–ด์žˆ๋„ค์š”.


๊ทธ๋Ÿผ ์ด์ œ ํ”„๋กœ์„ธ์Šค๋ฅผ ์ฃฝ์—ฌ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.


C:\Users\dev>netstat -ano|find "2002"

  TCP    0.0.0.0:2002           0.0.0.0:0              LISTENING       5096

  TCP    [::]:2002              [::]:0                 LISTENING       5096


C:\Users\dev>taskkill /pid 5096

์˜ค๋ฅ˜: ํ”„๋กœ์„ธ์Šค(PID 5096)๋ฅผ ์ข…๋ฃŒํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

์›์ธ: ์ด ํ”„๋กœ์„ธ์Šค๋Š” /F ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ•์ œ๋กœ ์ข…๋ฃŒํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.


C:\Users\dev>taskkill /pid 5096 /f /t

์„ฑ๊ณต: PID 5096์ธ ํ”„๋กœ์„ธ์Šค(PID 11172์ธ ์ž์‹ ํ”„๋กœ์„ธ์Šค)๊ฐ€ ์ข…๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.


๋ณด์‹œ๋ฉด ์ค‘๊ฐ„์— ์˜ค๋ฅ˜๊ฐ€ ํ•œ๋ฒˆ ๋‚˜๋ฉด์„œ /F์˜ต์…˜์„ ์“ฐ๋ผ๊ณ  ๋‚˜์˜ค๋„ค์š”. ์ฐธ๊ณ ์— ์›๊ฒฉ ํ”„๋กœ์„ธ์Šค๋Š” /F ์˜ต์…˜์„ ์ค˜์•ผ ์ข…๋ฃŒ๋œ๋‹ค๊ณ  ๋‚˜์™€์žˆ๋Š”๋ฐ ๊ทธ๊ฑฐ ๋•Œ๋ฌธ์ธ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ F์˜ต์…˜์„ ์ถ”๊ฐ€ํ•˜๊ณ  ํ˜น์‹œ๋‚˜ ํ•ด์„œ ์ž์‹ํ”„๋กœ์„ธ์Šค๊นŒ์ง€ ์ฃฝ์ด๋Š” T์˜ต์…˜์„ ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค. ์•„!! ๊ทธ๋ฆฌ๊ณ   ์˜ต์…˜์€ ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ฌธ์„ ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์œˆ๋„์šฐ๋Š” ์˜์–ด ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„์„ ํ•˜์ง€ ์•Š์ฃ . 


์ด์ƒ์ž…๋‹ˆ๋‹ค. ์˜ค๋Š˜์€ ์—ฌ๊ธฐ๊นŒ์ง€ ์“ธ๊ฒŒ์š”. ์˜ค๋Š˜๋„ ์ฆ์ฝ”ํ•˜์„ธ์š”~








์•ˆ๋…•ํ•˜์„ธ์š”, ์ผ€์ด์น˜์ž…๋‹ˆ๋‹ค.


์˜ค๋Š˜์€ ๋ฆฌ๋ˆ…์Šค ์„œ๋ฒ„์˜ ์žฌ๋ถ€ํŒ…/์žฌ๊ธฐ๋™ ๋˜๋Š” ์–ธ์ œ shutdown์ด ๋์—ˆ๋Š”์ง€๋ฅผ ํ™•์ธํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ฐฐ์› ์Šต๋‹ˆ๋‹ค.


๊ทธ๋‹ค์ง€ ๋งŽ์ด ์“ธ ์ผ์€ ์—†๋Š” ๋ช…๋ น์–ด์ด๊ธด ํ•œ๋ฐ ์›น์‚ฌ์ดํŠธ์— ์ ‘์†์ด ์•ˆ๋˜์„œ ํ™•์ธํ•ด๋ณด๋‹ค๊ฐ€ ์•Œ๊ฒŒ๋˜์—ˆ๋„ค์š”.


์šฐ์„  ์›น์„œ๋ฒ„๋กœ ์ ‘์†์„ ํ•˜๋Š”๋ฐ ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฉ”์‹œ์ง€๊ฐ€ ๋–ด์Šต๋‹ˆ๋‹ค.

Service Temporarily Unavailable
The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later.


๋ญ”์†Œ๋ฆฐ๊ฐ€ ๋ดค๋”๋‹ˆ ์ ๊ฒ€ ์ค‘์ด๊ฑฐ๋‚˜ ์šฉ๋Ÿ‰๋ฌธ์ œ๋กœ ์ธํ•ด์„œ ์ ‘์†ํ•  ์ˆ˜ ์—†๋‹ค๋ผ๋Š” ๋ง์ด๋„ค์š”.


๊ทธ๋ž˜์„œ ์„œ๋ฒ„์— ์ ‘์†ํ•ด์„œ ํ˜น์‹œ ๋””๋ ‰ํ† ๋ฆฌ ์šฉ๋Ÿ‰์ด ๋ถ€์กฑํ•œ๊ฐ€ ๋ดค๋Š”๋ฐ ์•„๋‹ˆ์—ˆ์Šต๋‹ˆ๋‹ค.


๊ทธ๋Ÿผ ์™œ ์•ˆ๋˜๋Š”์ง€ ์—๋Ÿฌ๋กœ๊ทธ๋ถ€ํ„ฐ ํ™•์ธํ•ด์•ผ๊ฒ ์ฃ .


์›น์„œ๋ฒ„์˜ ์—๋Ÿฌ๋กœ๊ทธ๋ฅผ ๋ดค๋Š”๋ฐ ์—๋Ÿฌ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค ใ„ทใ„ท;;;;; ๋ญ์ง€ ์ด๊ฑด;;;


์ด์ œ ์‹ค์ œ ์›น์•ฑ์ด ์˜ฌ๋ผ๊ฐ€์žˆ๋Š” WAS ๊ธฐ๋™์ƒํƒœ๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.


๋‘๋‘ฅ!!!! WAS๊ฐ€ ์ฃฝ์—ˆ๋„ค์š” ใ…œใ…œ


๋ญ์ง€ ๋‚œ ์ฃฝ์ธ์ ์ด ์—†๋Š”๋ฐ....๊ทธ๋ž˜์„œ ๋‹ค์‹œ WAS์˜ ์—๋Ÿฌ๋กœ๊ทธ๋ฅผ ํ™•์ธํ•˜๋Š”๋ฐ ํ—;;;; ์—ฌ๊ธฐ๋„ ์—๋Ÿฌ๋กœ๊ทธ๋Š” ์—†์Šต๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰๊นŒ์ง€ ์—ด์‹ฌํžˆ ์‚ฌ๋žŒ๋“ค์˜ ์š”์ฒญ์— ๋ถ€์‘ํ•œ ํ”์ ๋ฐ–์— ์—†๋„ค์š”.


๋ญ์ง€? ์„œ๋ฒ„๊ฐ€ ์žฌ๋ถ€ํŒ… ๋๋‚˜? ์–ด๋–ป๊ฒŒ ํ™•์ธํ•˜์ง€?


๋ฆฌ๋ˆ…์Šค ์„œ๋ฒ„์˜ ์žฌ๊ธฐ๋™ ์ƒํƒœ๋ฅผ ํ™•์ธํ•˜๋Š” ๋ช…๋ น์–ด๋Š” ๋‘ ๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.


์ฒซ์งธ, last ๋ช…๋ น์–ด.

๋‘˜์งธ, who ๋ช…๋ น์–ด.


[keichee@server1 ~]$ last reboot

reboot   system boot  3.10.0-514.16.1. Thu Jun 22 17:13 - 11:32 (140+18:19)

reboot   system boot  3.10.0-327.36.3. Wed Jan  4 22:14 - 17:11 (168+18:57)

reboot   system boot  3.10.0-327.36.3. Thu Nov 17 14:41 - 17:11 (217+02:30)

reboot   system boot  3.10.0-327.36.3. Thu Nov 17 15:06 - 17:11 (217+02:04)

reboot   system boot  3.10.0-327.36.3. Thu Nov 17 11:01 - 17:11 (217+06:09)


last reboot ๋ช…๋ น์–ด๋Š” ๋งˆ์ง€๋ง‰์œผ๋กœ ์žฌ๊ธฐ๋™ํ•œ ์‹œ๊ฐ„์ด ์–ธ์ œ์ธ์ง€๋ฅผ desc ์ •๋ ฌํ•˜์—ฌ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ์ œ์ผ ์œ„์— ์žˆ๋Š”๊ฒŒ ๊ฐ€์žฅ ์ตœ๊ทผ์— ์žฌ๊ธฐ๋™ํ•œ ์‹œ๊ฐ„์„ ๋งํ•ฉ๋‹ˆ๋‹ค.


last reboot ๋ง๊ณ ๋„ last shutdown ๋ช…๋ น์–ด๋กœ ์–ธ์ œ ์…ง๋‹ค์šด๋์—ˆ๋Š”์ง€ ํ™•์ธ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. 


last -x ๋Š” ์–ด๋–ค ์‚ฌ์šฉ์ž๊ฐ€ ์„œ๋ฒ„์— ์ ‘์†ํ•ด์„œ ์–ผ๋งˆ๋‚˜ ์žˆ์—ˆ๋Š”์ง€ ํ™•์ธ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. run level์ด ์–ด๋–ป๊ฒŒ ๋ฐ”๋€Œ์—ˆ๋Š”์ง€๋„ ํ™•์ธ์ด ๊ฐ€๋Šฅํ•˜์ฃ .


[keichee@server1 ~]$ last -x

keichee    pts/0        210.145.229.14   Fri Nov 10 11:09   still logged in

keichee    pts/0        210.145.229.14   Tue Nov  7 09:17 - 14:14  (04:57)

keichee    pts/0        210.145.229.143   Mon Nov  6 09:19 - 18:27  (09:07)

keichee    pts/2        204.130.143.176  Thu Nov  2 22:04 - 00:26  (02:21)

keichee    pts/1        103.170.103.116  Thu Nov  2 22:04 - 00:26  (02:22)

runlevel (to lvl 3)   3.10.0-514.16.1. Thu Jun 22 17:13 - 11:35 (140+18:22)
reboot   system boot  3.10.0-514.16.1. Thu Jun 22 17:13 - 11:35 (140+18:22)
shutdown system down  3.10.0-327.36.3. Thu Jun 22 17:11 - 17:13  (00:01)


๊ทธ๋Ÿผ ๋‘ ๋ฒˆ์งธ ๋ช…๋ น์–ด์˜€๋˜ who๋Š” ์–ด๋–ป๊ฒŒ ์“ธ๊นŒ์š”?


๊ธฐ๋ณธ์ ์œผ๋กœ who๋ฅผ ์“ธ ๋•Œ๋Š” ํ˜„์žฌ ๋‚ด๊ฐ€ ์–ด๋–ค ์‚ฌ์šฉ์ž๋กœ ์ ‘์†์„ ํ–ˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์ฃ .


[keichee@server1 ~]$ who

keichee    pts/0        2017-11-10 11:09 (118.145.269.124)


help ์˜ต์…˜์œผ๋กœ ์–ด๋–ค ์˜ต์…˜์„ ์ถ”๊ฐ€๋กœ ์ค„ ์ˆ˜ ์žˆ๋Š”์ง€ ํ•œ๋ฒˆ ๋ณผ๊นŒ์š”?


[keichee@server1 ~]$ who  --help

Usage: who [OPTION]... [ FILE | ARG1 ARG2 ]

Print information about users who are currently logged in.


  -a, --all         same as -b -d --login -p -r -t -T -u

  -b, --boot        time of last system boot

  -d, --dead        print dead processes

  -H, --heading     print line of column headings

  -l, --login       print system login processes

      --lookup      attempt to canonicalize hostnames via DNS

  -m                only hostname and user associated with stdin

  -p, --process     print active processes spawned by init

  -q, --count       all login names and number of users logged on

  -r, --runlevel    print current runlevel

  -s, --short       print only name, line, and time (default)

  -t, --time        print last system clock change

  -T, -w, --mesg    add user's message status as +, - or ?

  -u, --users       list users logged in

      --message     same as -T

      --writable    same as -T

      --help     display this help and exit

      --version  output version information and exit


์˜ต์…˜ ์ค‘์— -b ์˜ต์…˜์ด ๋งˆ์ง€๋ง‰ ์‹œ์Šคํ…œ ๋ถ€ํŒ… ์‹œ๊ฐ„์„ ์ถœ๋ ฅํ•œ๋‹ค๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


๊ทธ๋Ÿผ ํ•œ ๋ฒˆ ์จ๋ณด์ฃ .

[keichee@server1 ~]$ who -b

         system boot  2017-06-22 17:13

17๋…„ 6์›” 22์ผ 17์‹œ 13๋ถ„์— ์‹œ์Šคํ…œ์ด ๊ธฐ๋™๋์Œ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


์ด์ƒ์ž…๋‹ˆ๋‹ค.


์˜ค๋Š˜์€ ์—ฌ๊ธฐ๊นŒ์ง€~ 

๋ฆฌ๋ˆ…์Šค ํ™˜๊ฒฝ์— ๋ฐฐํฌํ•œ WAR ๋˜๋Š” JAR ํŒŒ์ผ์— ์ƒˆ๋กœ์šด ํŒŒ์ผ์„ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ๊ธฐ์กด ํŒŒ์ผ์„ ์—…๋ฐ์ดํŠธํ•˜๋Š” ๋ฐฉ๋ฒ•


์•ˆ๋…•ํ•˜์„ธ์š”, ์ผ€์ด์น˜์ž…๋‹ˆ๋‹ค. 


์˜ค๋Š˜์€ ๋ฆฌ๋ˆ…์Šค ํ™˜๊ฒฝ์— ๋ฐฐํฌํ•œ WAR ๋˜๋Š” JAR ํŒŒ์ผ์— ์ƒˆ๋กœ์šด ํŒŒ์ผ์„ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ๊ธฐ์กด ํŒŒ์ผ์„ ์—…๋ฐ์ดํŠธํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ๋ ค๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ทธ๋‹ฅ ์ด๋ ‡๊ฒŒ ํ•  ์ผ์ด ์—†๋‹ค๋ณด๋‹ˆ ํ•  ๋•Œ๋งˆ๋‹ค ๊ณ„์† ๊นŒ๋จน์–ด์„œ ๊ทธ๋ƒฅ ํ˜ผ์ž ๋„์ ์ผ๊ฒธ ์ ์–ด๋ด…๋‹ˆ๋‹ค.


์šฐ์„  /mydir/myapp.war ํŒŒ์ผ์ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค.


์šฐ์„  ์ด ํŒŒ์ผ์•ˆ์— ์–ด๋–ค ํŒŒ์ผ์ด ์žˆ๋Š”์ง€๋ถ€ํ„ฐ ํ™•์ธํ•ด๋ณด์ฃ .


$ cd /mydir

$ ll    (๋˜๋Š” ls -l)

-rw-rw-r--. 1 keichee keichee 63378 Nov  6 11:37 myapp.war


$ unzip -l myapp.war


Archive:  myapp.war

  Length      Date    Time    Name

---------  ----------   -----    ----

        0  11-06-2017 11:28   META-INF/

     128  11-06-2017 11:28   META-INF/MANIFEST.MF

        0  11-06-2017 11:28   css/

        0  11-06-2017 11:28   images/

        0  11-06-2017 11:28   WEB-INF/

        0  11-06-2017 11:28   WEB-INF/classes/

        0  11-06-2017 11:28   WEB-INF/classes/com/

        0  11-06-2017 11:28   WEB-INF/classes/com/my/

.
.....์ค‘๋žต.....
.
  85308  08-14-2017 17:37   WEB-INF/lib/spring-oxm-4.3.0.RELEASE.jar
 587278  08-14-2017 17:37   WEB-INF/lib/spring-test-4.3.0.RELEASE.jar
 266993  08-14-2017 17:37   WEB-INF/lib/spring-tx-4.3.0.RELEASE.jar
 808847  08-14-2017 17:37   WEB-INF/lib/spring-web-4.3.0.RELEASE.jar
 905320  08-14-2017 17:37   WEB-INF/lib/spring-webmvc-4.3.0.RELEASE.jar
---------                           -------
 44109477                        393 files

WARํŒŒ์ผ์•ˆ์— ์–ด๋–ค ํŒŒ์ผ๋“ค์ด ๋“ค์–ด๊ฐ€์žˆ๋Š”์ง€ ๋””๋ ‰ํ† ๋ฆฌ ๊ฒฝ๋กœ๊นŒ์ง€ ์ „๋ถ€ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


์ด์ œ ํŒŒ์ผ ํ•˜๋‚˜๋ฅผ ์ถ”์ถœํ•ด๋ณด๋„๋ก ํ•˜์ฃ .


์ถ”์ถœํ•  ๋Œ€์ƒ์€ /META-INF/MANIFEST.MF ์ž…๋‹ˆ๋‹ค. ์‚ฌ์ด์ฆˆ๊ฐ€ 128์ด๊ตฐ์š”.


$ unzip myapp.war META-INF/MANIFEST.MF

Archive:  myapp.war

  inflating: META-INF/MANIFEST.MF

$ ll   (๋˜๋Š” ls -l )

-rw-rw-r--. 1 keichee keichee 44109477 Nov  6 11:37 myapp.war

drwxrwxr-x. 3 keichee keichee       17 Nov  9 14:42 META-INF

META-INF ๋””๋ ‰ํ† ๋ฆฌ๊ฐ€ ์ƒ์„ฑ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


$ cd META-INF

$ ls -l

-rw-rw-r--. 1 keichee keichee 128 Nov  6 11:28 MANIFEST.MF


ํŒŒ์ผ์ด ์žˆ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์ด์ฆˆ๊ฐ€ 128๋กœ ์œ„์—์„œ ์กฐํšŒํ•œ ๊ฒƒ๊ณผ ๋™์ผํ•˜๋„ค์š”.


๊ทธ๋Ÿผ ํŒŒ์ผ์„ ์ˆ˜์ •ํ•ด๋ณผ๊นŒ์š”?


$ vi MANIFEST.MF


๋งˆ์Œ๋Œ€๋กœ ๋‚ด์šฉ์„ ์ˆ˜์ •ํ•ด์„œ ํŒŒ์ผ ์‚ฌ์ด์ฆˆ๊ฐ€ ๋‹ฌ๋ผ์ง€๋„๋ก ํ•ด์ฃผ์„ธ์š”.


$ ls -l

-rw-rw-r--. 1 keichee keichee 145 Nov  9 13:38 MANIFEST.MF


ํŒŒ์ผ์˜ ์‚ฌ์ด์ฆˆ์™€ ์‹œ๊ฐ„์ด ๋ณ€๊ฒฝ๋˜์—ˆ๋„ค์š”. 128 -> 145


๊ทธ๋Ÿผ ์ด ํŒŒ์ผ์„ ์ด์ œ ๋‹ค์‹œ WAR ํŒŒ์ผ์•ˆ์œผ๋กœ ์—…๋ฐ์ดํŠธํ•ด์„œ ๋„ฃ์–ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.


$ jar uvf myapp.war META-INF/MANIFEST.MF

adding: META-INF/MANIFEST.MF(in = 145) (out= 120)(deflated 17%)


ํŒŒ์ผ์„ ์ถ”๊ฐ€ํ•œ๋‹ค๊ณ  ๋‚˜์˜ค๋ฉด์„œ ์••์ถ•์ด ์–ผ๋งˆ๋‚˜ ๋๋Š”์ง€๊ฐ€ ๋‚˜์˜ค๋„ค์š”. 17%๊ฐ€ ์••์ถ•๋์Šต๋‹ˆ๋‹ค. ์ฐธ๊ณ ๋กœ ์—ฌ๊ธฐ์„œ in์€ ์‹ค์ œ ํŒŒ์ผ ์‚ฌ์ด์ฆˆ๊ณ  out์€ ์••์ถ•๋œ ํŒŒ์ผ์‚ฌ์ด์ฆˆ ์ž…๋‹ˆ๋‹ค.


๋งŒ์•ฝ jar ๋ช…๋ น์–ด๊ฐ€ ์—†์„ ๊ฒฝ์šฐ์—๋Š” zip ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

$ zip -r myapp.war META-INF/MANIFEST.MF

adding: META-INF/MANIFEST.MF(in = 145) (out= 120)(deflated 17%)



์ด์ œ ๋‹ค์‹œ myapp.war ํŒŒ์ผ์— ์–ด๋–ค ํŒŒ์ผ๋“ค์ด ์žˆ๋Š”์ง€ ์กฐํšŒํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.


$ unzip -l myapp.war


Archive:  myapp.war

  Length      Date    Time    Name

---------  ----------   -----    ----

        0  11-06-2017 11:28   META-INF/

     145  11-09-2017 13:38   META-INF/MANIFEST.MF

        0  11-06-2017 11:28   css/

        0  11-06-2017 11:28   images/

        0  11-06-2017 11:28   WEB-INF/

        0  11-06-2017 11:28   WEB-INF/classes/

        0  11-06-2017 11:28   WEB-INF/classes/com/

        0  11-06-2017 11:28   WEB-INF/classes/com/my/

.
.....์ค‘๋žต.....
.
  85308  08-14-2017 17:37   WEB-INF/lib/spring-oxm-4.3.0.RELEASE.jar
 587278  08-14-2017 17:37   WEB-INF/lib/spring-test-4.3.0.RELEASE.jar
 266993  08-14-2017 17:37   WEB-INF/lib/spring-tx-4.3.0.RELEASE.jar
 808847  08-14-2017 17:37   WEB-INF/lib/spring-web-4.3.0.RELEASE.jar
 905320  08-14-2017 17:37   WEB-INF/lib/spring-webmvc-4.3.0.RELEASE.jar
---------                           -------
 44109494                        393 files


ํŒŒ์ผ ์‚ฌ์ด์ฆˆ์™€ ๋‚ ์งœ๊ฐ€ ๋ณ€๊ฒฝ์ด ๋˜์—ˆ๋„ค์š”.

์ฐธ๊ณ ๋กœ ์—ฌ๊ธฐ์— ์กฐํšŒ๋˜๋Š” ํŒŒ์ผ์‚ฌ์ด์ฆˆ๋Š” ์••์ถ•๋˜๊ธฐ ์ „์˜ ํŒŒ์ผ ์‚ฌ์ด์ฆˆ์ž…๋‹ˆ๋‹ค.


๋ฆฌ๋ˆ…์Šค์—์„œ ๋ช…๋ น์–ด ํžˆ์Šคํ† ๋ฆฌ ์กฐํšŒ ํ•  ๋•Œ ๋‚ ์งœ ๋ฐ ์‹œ๊ฐ„ ํ•จ๊ป˜ ํ‘œ์‹œํ•˜๊ธฐ



๋ฆฌ๋ˆ…์Šค ํ™˜๊ฒฝ์—์„œ ์–ด๋–ค ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ๊ทธ ๋ช…๋ น์–ด ๋ชฉ๋ก์ด history์— ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. 


๊ทธ๋Ÿฐ๋ฐ ๊ทธ ๋ชฉ๋ก์ด ๋‹จ์ˆœํžˆ ๋ช…๋ น์–ด ๋ชฉ๋ก๋งŒ ์ˆœ์„œ๋Œ€๋กœ ๋‚˜์˜ฌ ๋ฟ ์ด๊ฒŒ ์–ธ์ œ ์‹คํ–‰์ด ๋˜์—ˆ๋Š”์ง€๋Š” ์•Œ ์ˆ˜๊ฐ€ ์—†์ฃ .


์ผ๋ฐ˜์ ์œผ๋กœ history๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์•„๋ž˜์ฒ˜๋Ÿผ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.



  421  ls -l

  422  ./listAlgorithms.sh

  423  cat ./listAlgorithms.sh

  424  cd ..

  425  ls -l

  426  cd lib

  427  ls -l

  428  ../bin/listAlgorithms.sh

  429  history


์—ฌ๊ธฐ์„œ ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ–ˆ๋˜ ๋ช…๋ น์–ด๋ฅผ ์กฐํšŒํ• ๋•Œ ์–ธ์ œ์‹คํ–‰ ํ–ˆ๋Š”์ง€๋ฅผ ์•Œ์•„์•ผ ํ•  ๋•Œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.


  ๊ทธ๋Ÿด ๊ฒฝ์šฐ ์‚ฌ์šฉ์ž ๊ณ„์ •์˜ ~/.bash_profile ํŒŒ์ผ์ด๋‚˜ /etc/profile์— HISTTIMEFORMAT ๋ณ€์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•ด ์ฃผ๋ฉด history๋ฅผ ์กฐํšŒํ•  ๋•Œ ๊ฐ ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•œ ๋‚ ์งœ์™€ ์‹œ๊ฐ„์ด ํ‘œ์‹œ ๋ฉ๋‹ˆ๋‹ค.


1. ~/.bash_profile์ด๋‚˜ /etc/profile์— ์•„๋ž˜ ๋‚ด์šฉ์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.


  $ vi /etc/profile

.

.  

HISTTIMEFORMAT="%F %T - "

export HISTTIMEFORMAT

.

.



2. ์ ์šฉ


  $ . /etc/profile

๋˜๋Š”

  $ source /etc/profile


3. ํ™•์ธ


  $ history

  421  2017-11-02 18:48:16 - vi /etc/profile
  422  2017-11-02 18:48:22 - . /etc/profile
  423  2017-11-02 18:48:25 - history


์—ฌ๊ธฐ์„œ %F๋Š” ๋…„์›”์ผ์„ ํ‘œ์‹œํ•˜๋Š” ๊ฑฐ๊ณ  %T๋Š” ์‹œ๊ฐ„์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.


์ด ์™ธ์—๋„ history ๋ชฉ๋ก ๊ฐœ์ˆ˜๋ฅผ ์ง€์ •ํ•˜๊ฑฐ๋‚˜ history๋ฅผ ์ €์žฅํ•  ํŒŒ์ผ์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ๋„ ์žˆ์Šต๋‹ˆ๋‹ค.


์—ญ์‹œ .bash_profile ์„ ์ˆ˜์ •ํ•ด์„œ ์•„๋ž˜ ๋‚ด์šฉ์„ ๋„ฃ์–ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

๋ฌผ๋ก  ์ ์šฉ๋„ ์‹œ์ผœ์ค˜์•ผ๊ฒ ์ฃ .

export HISTSIZE=450      -> ํžˆ์Šคํ† ๋ฆฌ ๋ช…๋ น์–ด๋กœ ์กฐํšŒ์‹œ ๋ชฉ๋ก์— ์กฐํšŒ๋  ๊ฐœ์ˆ˜ export HISTFILESIZE=450    -> ํžˆ์Šคํ† ๋ฆฌ ํŒŒ์ผ์— ์ €์žฅํ•  ๋ชฉ๋ก ๊ฐœ์ˆ˜ export HISTFILE=~/history    -> ํžˆ์Šคํ† ๋ฆฌ๋ฅผ ์ €์žฅํ•  ํŒŒ์ผ

ํžˆ์Šคํ† ๋ฆฌ ๋ชฉ๋ก์—์„œ ์—ฐ์†์œผ๋กœ ์ž…๋ ฅํ•œ ๋™์ผํ•œ ๋ช…๋ น์–ด๋Š” ์ €์žฅํ•˜์ง€ ์•Š๋Š” ๊ธฐ๋Šฅ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. 

export HISTCONTROL=ignoredups


๐Ÿ’ป Programming

[ Node.js in Action ] 13์žฅ ์š”์•ฝ

/**
 *  ์›น์„œ๋ฒ„ ๊ทธ ์ด์ƒ์˜ ๊ฒƒ๋“ค
 * - Socket IO
 * - TCP/IP ๋„คํŠธ์›Œํ‚น
 * - OS์™€ ์ƒํ˜ธ์ž‘์šฉํ•˜๊ธฐ ์œ„ํ•œ ํˆด
 * - ์ปค๋งจ๋“œ๋ผ์ธ ํˆด ๊ฐœ๋ฐœํ•˜๊ธฐ
 */


13.1. Socket.io


Socket IO ๋Š” WebSocket๊ณผ ์œ ์‚ฌํ•œ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๋ฉด์„œ๋„, broadcasting, volatile ๋ฉ”์‹œ์ง•์„ ํŽธ๋ฆฌํ•˜๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” API๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

WebSocket์€ WebSocket ํ”„๋กœํ† ์ฝœ์„ ์‚ฌ์šฉํ•˜๋Š”๋ฐ ์•„์ง ์™„์„ฑ๋œ ํ”„๋กœํ† ์ฝœ์ด ์•„๋‹ˆ๋ผ์„œ ๋ธŒ๋ผ์šฐ์ € ๋งˆ๋‹ค ์ง€์›์—ฌ๋ถ€์—๋„ ์ฐจ์ด๊ฐ€ ์žˆ๋‹ค๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. Socket IO๋ฅผ ์ด์šฉํ•  ๊ฒฝ์šฐ ์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, Socket IO๋Š” WebSocket์„ ์ง€์›ํ•˜์ง€ ์•Š๋Š” ๋ธŒ๋ผ์šฐ์ €์˜ ๊ฒฝ์šฐ์— ๊ฐ ๋ธŒ๋ผ์šฐ์ €๋ณ„๋กœ ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์„ ํ†ตํ•ด ๋‚ด๋ถ€์ ์œผ๋กœ ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋‚ผ ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.


๏‚ก A minimal Socket.IO application that pushes the serverโ€™s time to connected clients
๏‚ก A Socket.IO application that triggers page refreshes when CSS files are edited


13.1.1 ์ดˆ๊ฐ„๋‹จ Socket.IO ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋งŒ๋“ค๊ธฐ


<< ๊ฐ„๋‹จํ•œ Socket IO clock-server ํ”„๋กœ๊ทธ๋žจ >> ( ์‹œ๊ฐ„์ด ํ‘œ์‹œ๊ฐ€ ์•ˆ๋˜๋Š”๋ฐ ์ด์œ ๋ฅผ ๋ชจ๋ฅด๊ฒ ์Œ ;;;; )


13.1.2 Socket.IO๋ฅผ ์ด์šฉํ•œ ํŽ˜์ด์ง€์™€ CSS reloading ํ•˜๊ธฐ


์›นํŽ˜์ด์ง€ ๋””์ž์ธ์„ ํ•  ๋•Œ ์ผ๋ฐ˜์ ์ธ ์ˆœ์„œ

1 Open the web page in multiple browsers.
2 Look for styling on the page that needs adjusting.
3 Make changes to one or more stylesheets.
4 Manually reload all the web browsers.
5 Go back to step 2.


<< ์œ„ 4๋ฒˆ ๊ณผ์ •์„ ์‰ฝ๊ฒŒ ๋งŒ๋“ค์–ด์ฃผ๋Š” ํ”„๋กœ๊ทธ๋žจ ์˜ˆ์ œ >> ( ์ด๋†ˆ๋„ ์ œ๋Œ€๋กœ ๋™์ž‘์„ ํ•˜์ง€ ์•Š์Œ...;;;)


13.1.3 Socket.IO์˜ ๋˜ ๋‹ค๋ฅธ ์‚ฌ์šฉ๋ฒ•


Socket.IO๋Š” ํ”„๋กœ๊ทธ๋ ˆ์Šค ๋ฐ”๋ฅผ ๊ณ„์† ์—…๋ฐ์ดํŠธํ•ด์„œ ์ˆ˜์น˜๊ฐ€ ์˜ฌ๋ผ๊ฐ€๋Š” ๊ฒƒ ๋ณด์—ฌ์ฃผ๋Š” ๊ธฐ๋Šฅ์— ์ ํ•ฉํ•จ




13.2 TCP/IP ๋„คํŠธ์›Œํ‚น


๋…ธ๋“œ๋Š” ๋„คํŠธ์›Œํ‚น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— ๋งค์šฐ ์ ํ•ฉ. ๋„คํŠธ์›Œํ‚น์€ ๋งŽ์€ I/O๋ฅผ ์š”๊ตฌํ•˜๊ธฐ ๋•Œ๋ฌธ.


๏‚ก Working with buffers and binary data
๏‚ก Creating a TCP server
๏‚ก Creating a TCP client


13.2.1 Buffers์™€ Binary Data ๋‹ค๋ฃจ๊ธฐ


Buffer ๋ฐ์ดํƒ€ ํƒ€์ž…์€ ๋…ธ๋“œ์—์„œ ์ œ๊ณตํ•˜๋Š” ํŠน๋ณ„ํ•œ ๋ฐ์ดํƒ€ ํƒ€์ž…์ด๋‹ค.

๊ณ ์ • ๊ธธ์ด์˜ raw binary data์ฒ˜๋Ÿผ ๋™์ž‘ํ•˜๋ฉฐ, C์—์„œ์˜ malloc()์ด๋‚˜ C++, JAVA์—์„œ์˜ new ํ‚ค์›Œ๋“œ๋ผ๊ณ  ์ƒ๊ฐํ•ด๋„ ๋œ๋‹ค.

๊ธฐ๋ณธ์ ์œผ๋กœ Stream ํด๋ž˜์Šค์— ์˜ํ•ด ๋ฐ์ดํƒ€ ์ด๋ฒคํŠธ ๋‚ด์— ๋ฐ˜ํ™˜๋œ๋‹ค.

Buffer๋Š” ์ˆซ์ž 0~ 255๊นŒ์ง€๋งŒ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” non-resizable ๋ฐฐ์—ด์ด๋‹ค.


TEXT DATA VS. BINARY DATA

1. -----------------------------------

var b = new Buffer("121234869");
console.log(b.length);
9
console.log(b);
<Buffer 31 32 31 32 33 34 38 36 39>


2. -----------------------------------

var b = new Buffer(4);
b.writeInt32LE(121234869, 0);
console.log(b.length);
4
console.log(b);
<Buffer b5 e5 39 07>

-----------------------------------


13.2.2 TCP ์„œ๋ฒ„ ๋งŒ๋“ค๊ธฐ 


Socket ํด๋ž˜์Šค๋Š” client์™€ server ์–‘์ชฝ์—์„œ ๋ชจ๋‘ ์‚ฌ์šฉ๋จ

Itโ€™s a Stream subclass thatโ€™s both readable and writable (bidirectional).

That is, it emits data events when input data has been read from the socket,

and it has write() and end() functions for sending output data.


WRITING DATA


var net = require('net');
net.createServer(function (socket) {
socket.write('Hello World!\r\n');
socket.end();
}).listen(1337);
console.log('listening on port 1337');


$ telnet localhost 1337


READING DATA


socket.on('data', function (data) {
console.log('got "data"', data);
});


data ์ธ์ž๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ Buffer์ธ์Šคํ„ด์Šค์ด๋‹ค.

setEncoding()์„ ์ด์šฉํ•ด์„œ decoded String์„ ์ธ์ž๋กœ ๋„˜๊ธธ ์ˆ˜๋„ ์žˆ๋‹ค.


socket.on('end', function () {
console.log('socket has ended');
});


end ์ด๋ฒคํŠธ๋ฅผ ๋ฆฌ์Šค๋‹ํ•จ์œผ๋กœ์จ ๋ถˆํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ ์ „์†ก์„ ํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ๋‹ค.


SAMPLE TEST  


var net = require('net');
var socket = net.connect({ host: process.argv[2], port: 22 });
socket.setEncoding('utf8');
socket.once('data', function (chunk) {
console.log('SSH server version: %j', chunk.trim());
socket.end();
});


$ node client.js github.com


SOCKET.PIPE() ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋‘ ๊ฐœ์˜ ์ŠคํŠธ๋ฆผ ์—ฐ๊ฒฐํ•˜๊ธฐ


The pipe() ํ•จ์ˆ˜๋Š” readable ์ŠคํŠธ๋ฆผ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด์™€์„œ ์“ฐ๊ธฐ๊ฐ€๋Šฅํ•œ ์ŠคํŠธ๋ฆผ์— ๋ฐ์ดํ„ฐ๋ฅผ ์“ฐ๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค.


๋น„์ •์ƒ ์—ฐ๊ฒฐ๋Š๊น€ ํ•ธ๋“ค๋งํ•˜๊ธฐ

ํด๋ผ์ด์–ธํŠธ๊ฐ€ netcat์„ ์ผ์„ ๋•Œ Ctrl-D ๋Œ€์‹  Ctrl-C๋กœ ์ปค๋„ฅ์…˜์„ ์ข…๋ฃŒ์‹œํ‚ค๋ฉด ์„œ๋ฒ„์ชฝ ์†Œ์ผ“์ด ๊น”๋”ํ•˜๊ฒŒ ์ข…๋ฃŒ๊ฐ€ ๋˜์ง€ ์•Š๋Š”๋ฐ, ์ด ๋ถ€๋ถ„์— ๋Œ€ํ•œ ๋’ท์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋ ค๋ฉด ์•„๋ž˜์ฒ˜๋Ÿผ close ์ด๋ฒคํŠธ๋ฅผ ๋ฆฌ์Šค๋‹ํ•˜๋ฉด ๋œ๋‹ค.


socket.on('close', function () {
console.log('client disconnected');
});


FINAL TEST APP


server


var net = require('net');
net.createServer(function (socket) {
console.log('socket connected!');
socket.on('data', function (data) {
console.log('"data" event', data);
});
socket.on('end', function () {
console.log('"end" event');
});
socket.on('close', function () {
console.log('"close" event');
});
socket.on('error', function (e) {
console.log('"error" event', e);
});
socket.pipe(socket);
}).listen(1337);


client


var net = require('net');
var host = process.argv[2];
var port = Number(process.argv[3]);
var socket = net.connect(port, host);
socket.on('connect', function () {
process.stdin.pipe(socket);
socket.pipe(process.stdout);
process.stdin.resume();
});
socket.on('end', function () {
process.stdin.pause();
});



13.3 Tools for interacting with the operating system


๏‚ก The global process objectโ€”Contains information about the current process, such
as the arguments given to it and the environment variables that are currently set
๏‚ก The fs moduleโ€”Contains the high-level ReadStream and WriteStream classes
that youโ€™re familiar with by now, but also houses low-level functions that weโ€™ll
look at
๏‚ก The child_process moduleโ€”Contains both low-level and high-level interfaces
for spawning child processes, as well as a special way to spawn node instances
with a two-way message-passing channel


13.3.1 The process global singleton


Every Node process has a single global process object that every module shares access
to. Useful information about the process and the context itโ€™s running in can be found
in this object.


the most interesting feature of the process object is that itโ€™s an EventEmitter instance, which emits very special events,
such as exit and uncaughtException.


USING PROCESS.ENV TO GET AND SET ENVIRONMENT VARIABLES


SPECIAL EVENTS EMITTED BY PROCESS


๏‚ก exit gets emitted right before the process exits.


process.on('exit', function (code) {
console.log('Exiting...');
});


๏‚ก uncaughtException gets emitted any time an unhandled error is thrown.


process.on('uncaughtException', function (err) {
console.error('got uncaught exception:', err.message);
process.exit(1);
});
throw new Error('an uncaught exception');


CATCHING SIGNALS SENT TO THE PROCESS 


three signals that Node handles by default


๏‚ก SIGINTโ€”Sent by your shell when you press Ctrl-C. Nodeโ€™s default behavior is to
kill the process, but this can be overridden with a single listener for SIGINT on
process.
๏‚ก SIGUSR1โ€”When this signal is received, Node will enter its built-in debugger.
๏‚ก SIGWINCHโ€”Sent by your shell when the terminal is resized. Node resets
process.stdout.rows and process.stdout.columns and emits a resize event
when this is received.


13.3.2 Using the filesystem module


MOVING A FILE 


fs.rename()

์›๊ฒฉ์œผ๋กœ ์ด๋™์‹œํ‚ค๋ ค๋ฉด stream์„ ์ด์šฉํ•ด์„œ ๋ณต์‚ฌํ•ด์•ผ ํ•จ. rename์€ ์›๊ฒฉ์ง€๋กœ ์ด๋™ํ•˜๋Š” ๊ฒƒ์„ ์ง€์›ํ•˜์ง€ ์•Š์Œ.


WATCHING A DIRECTORY OR FILE FOR CHANGES 


fs.watch() - ํ”Œ๋žซํผ์˜ ๋„ค์ดํ‹ฐ๋ธŒ ํŒŒ์ผ ๋ณ€๊ฒฝ ์•Œ๋ฆผ API๋ฅผ ์ด์šฉํ•จ. (ํ”Œ๋žซํผ ์ฐจ์ด๋กœ ์ธํ•ด unreliable ํ•จ)

fs.watchFile()


USING COMMUNITY MODULES: FSTREAM AND FILED 


fstream์„ ์ด์šฉํ•˜๋ฉด cp -rp srcDir destDir ๋ช…๋ น์–ด๋ฅผ ์‰ฝ๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.


filed ์ธ์Šคํ„ด์Šค๋Š” req, res ๊ฐ์ฒด์— ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

http.createServer(function (req, res) {
req.pipe(filed('path/to/static/files')).pipe(res);
});

์œ„ ์ฝ”๋“œ๋Š” ํŒŒ์ผ์ด ์บ์‰ฌ๋˜์—ˆ์„ ๋•Œ 304 Not Modified ์˜ค๋ฅ˜๋ฅผ ๋ฑ‰์–ด๋‚ด๊ณ  ํŒŒ์ผ์„ ๋””์Šคํฌ์—์„œ ์—ด๊ฑฐ๋‚˜ ์ฝ๋Š” ํ–‰์œ„๋Š” ํ•˜์ง€ ์•Š๋Š”๋‹ค.



13.3.3 Spawning external processes


๏‚ก cp.exec()โ€”A high-level API for spawning commands and buffering the result
in a callback
๏‚ก cp.spawn()โ€”A low-level API for spawning single commands into a Child-
Process object
๏‚ก cp.fork()โ€”A special way to spawn additional Node processes with a built-in
IPC channel


์ž์‹ ํ”„๋กœ์„ธ์Šค์˜ ์žฅ๋‹จ์  (PROS AND CONS TO CHILD PROCESSES)

 

cons : child process ๋กœ ์‹คํ–‰๋˜๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์ด๋ฏธ ํด๋ผ์ด์–ธํŠธ์— ์„ค์น˜๊ฐ€ ๋˜์–ด์žˆ์–ด์•ผ ํ•œ๋‹ค.

pros : ๋‹ค๋ฅธ ์–ธ์–ด๋กœ ์“ฐ์—ฌ์ง„ ์• ํ”Œ๋ฆฌ์ผ€์ž‡์…˜์„ ์ด์šฉํ•ด์„œ ์ข€ ๋” richํ•œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ฐœ๋ฐœ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.


cp.exec()๋กœ ๋ช…๋ น์–ด ๊ฒฐ๊ณผ ๋ฒ„ํผ๋งํ•˜๊ธฐ(BUFFERING COMMAND RESULTS USING CP.EXEC())


cp.exec(), is useful for when you want to invoke a command, and you only care about the final result. This API allows you to enter full sequences of commands, including multiple processes piped to one another.


SPAWNING COMMANDS WITH A STREAM INTERFACE USING CP.SPAWN()


cp.spawn() returns a ChildProcess object that you can interact with.


ex)

var child = cp.spawn('ls', [ '-l' ]);
// stdout is a regular Stream instance, which emits 'data',
// 'end', etc.
child.stdout.pipe(fs.createWriteStream('ls-result.txt'));
child.on('exit', function (code, signal) {
// emitted when the child process exits
});


DISTRIBUTING THE WORKLOAD USING CP.FORK()


Like cp.spawn(), cp.fork() returns a ChildProcess object.

The major difference is the API added by the IPC channel: the child process now has a child.send (message) function, and the script being invoked by fork() can listen for process.on('message') events.


ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜์—ด ๊ณ„์‚ฐ ์˜ˆ์ œ


13.4 ์ปค๋งจ๋“œ๋ผ์ธ ํˆด ๊ฐœ๋ฐœํ•˜๊ธฐ(Developing command-line tools )


๏‚ก Parsing command-line arguments
๏‚ก Working with stdin and stdout streams
๏‚ก Adding pretty colors to the output using ansi.js


13.4.1 Parsing command-line arguments


Node provides you with the process.argv property, which is an array of strings, which are the arguments that were used when Node was invoked.

The first entry of the array is the Node executable, and the second entry is the name of your script.


ex)

var args = process.argv.slice(2);
console.log(args);


$ node args.js
[]


$ node args.js hello world
[ 'hello', 'world' ]


$ node args.js "tobi is a ferret"
[ 'tobi is a ferret' ]


์–ด๋–ค ์˜ต์…˜์ด ๋“ค์–ด์™”๋Š๋ƒ์— ๋”ฐ๋ผ usageํ”„๋ฆฐํŠธํ•˜๋Š” ์˜ˆ์ œ


13.4.2 stdin, stdout ๊ฐ€์ง€๊ณ  ๋†€๊ธฐ (Working with stdin and stdout)


๏‚ก process.stdinโ€”A ReadStream to read input data from
๏‚ก process.stdoutโ€”A WriteStream to write output data to


WRITING OUTPUT DATA WITH PROCESS.STDOUT


console.log() ๋‚ด์—์„œ process.stdout.write()์ด ์‚ฌ์šฉ๋จ.


(์ดํด๋ฆฝ์Šค์—์„œ argument ์„ค์ •ํ•ด์ฃผ๊ณ  ์‹คํ–‰ํ•œ๋‹ค๊ณ  ๊ฐ€์ •)

var target = process.argv[2];
process.stdout.write('Entered value is : ' + target);


PROCESS.STDIN ๋กœ ์ž…๋ ฅ๊ฐ’ ์ฝ์–ด์˜ค๊ธฐ (READING INPUT DATA WITH PROCESS.STDIN )

 

Before you can read from stdin, you must call process.stdin.resume() to indicate that your script is interested in data from stdin.

After that, stdin acts like any other readable stream, emitting data events as data is received from the output of another process, or as the user enters keystrokes into the terminal window.


๋‚˜์ด ๊ฒ€์ฆํ•˜๋Š” ์˜ˆ์ œ


DIAGNOSTIC LOGGING WITH PROCESS.STDERR 


 process.stderr์„ ์ง์ ‘ ์‚ฌ์šฉํ•˜์ง€๋ง๊ณ  console.error()๋ฅผ ์‚ฌ์šฉํ•˜์ž.


13.4.3 ์ถœ๋ ฅ์— ์ƒ‰ ์ž…ํžˆ๊ธฐ (Adding colored output)


ANSI ์ด์Šค์ผ€์ดํ”„ ์ฝ”๋“œ ์ž‘์„ฑํ•˜๊ธฐ (CREATING AND WRITING ANSI ESCAPE CODES )


(cmd ์ฐฝ์—์„œ ansi.js ์‹คํ–‰)


console.log('\033[43m\033[35mhello\033[39m\033[49m');

var ansi = require('ansi');
var cursor = ansi(process.stdout);

cursor
.fg.green()
.write('This should be green.')
.fg.reset()
.write('\n');
 



๐Ÿ’ป Programming

[ Node.js in Action ] 3์žฅ ์š”์•ฝ

3์žฅ์—์„œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ๋‚ด์šฉ์„ ๋‹ค๋ฃน๋‹ˆ๋‹ค.


๏‚ก Organizing your code into modules
๏‚ก Coding conventions
๏‚ก Handling one-off events with callbacks
๏‚ก Handling repeating events with event emitters
๏‚ก Implementing serial and parallel flow control
๏‚ก Leveraging flow-control tools


3.1 ๋ชจ๋“ˆ์„ ์ด์šฉํ•œ ์ฝ”๋“œ ์žฌ์‚ฌ์šฉ ๋ฐ ์ •๋ฆฌ


PHP๋‚˜ Ruby๊ฐ™์€ ์–ธ์–ด์—์„œ importํ•˜๋ฉด global scope์— ์˜ํ–ฅ์„ ๋ฏธ์นœ๋‹ค.

์ด๋Ÿฐ ๊ฒƒ์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด์„œ PHP๋Š” namespaces๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , Ruby๋Š” modules๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

ํ•˜์ง€๋งŒ namespaces๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ํ•ด๋„ overwrite์˜ ๊ฐ€๋Šฅ์„ฑ์„ ์™„์ „ํžˆ ๋ฐฐ์ œํ•  ์ˆ˜๋Š” ์—†๋‹ค.

Node์—์„œ๋Š” ์ด๋Ÿฐ ์œ„ํ—˜์„ ์™„์ „ํžˆ ๋ฐฐ์ œํ•œ๋‹ค.

์šฐ์„  Node์˜ module์€ gloabl scope๋ฅผ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š๋Š”๋‹ค. ๋˜ํ•œ, module์„ ๋งŒ๋“ค ๋•Œ ๊ฐœ๋ฐœ์ž๊ฐ€ ์–ด๋–ค ๊ธฐ๋Šฅ, ๋ณ€์ˆ˜๋ฅผ ์™ธ๋ถ€์—์„œ ์‚ฌ์šฉ๊ฐ€๋Šฅ ํ•˜๊ฒŒ ํ• ์ง€ ์„ ํƒ ํ•  ์ˆ˜ ์žˆ๋‹ค. ( ์ด๊ฑด ์ž๋ฐ”์—์„œ ์ ‘๊ทผ์ œํ•œ์ž, public ๋˜๋Š” private,๋ฅผ ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค. )

Node์—์„œ๋Š” ์™ธ๋ถ€์— ๋…ธ์ถœ์‹œํ‚ค๋Š” ๊ฒƒ์„ exportํ•œ๋‹ค๊ณ  ํ•œ๋‹ค. ( public์œผ๋กœ ๋งŒ๋“ ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•จ )

export์˜ ๋ฐฉ๋ฒ•์€ ์ฒซ์งธ, ๋‘˜ ์ด์ƒ์˜ ๊ธฐ๋Šฅ ๋ฐ ๋ณ€์ˆ˜๋ฅผ exportํ•  ๋•Œ์—๋Š” exports๊ฐ์ฒด์˜ ์†์„ฑ์„ ์„ธํŒ…ํ•ด์ฃผ๋ฉด ๋œ๋‹ค. ๋‘˜์งธ, ํ•˜๋‚˜์˜ ๊ธฐ๋Šฅ์ด๋‚˜ ๋ณ€์ˆ˜๋งŒ exportํ•  ๋•Œ์—๋Š” module.exports ์†์„ฑ์„ ์ด์šฉํ•œ๋‹ค.


3.1.1 ๋ชจ๋“ˆ ์ƒ์„ฑํ•˜๊ธฐ


๋ชจ๋“ˆ์€ a.ํ•˜๋‚˜์˜ ํŒŒ์ผ์ด๊ฑฐ๋‚˜, b.๋””๋ ‰ํ† ๋ฆฌ๋‚ด์— ํ•˜๋‚˜ ์ด์ƒ์˜ ํŒŒ์ผ๋กœ ๊ตฌ์„ฑ๋  ์ˆ˜ ์žˆ๋‹ค.

๋””๋ ‰ํ† ๋ฆฌ ๊ตฌ์กฐ์ผ ๊ฒฝ์šฐ์—๋Š” ๋””๋ ‰ํ† ๋ฆฌ ๋‚ด๋ถ€์— index.jsํŒŒ์ผ์„ mainํŒŒ์ผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค. ( ์ด ํŒŒ์ผ ๋ช…์€ overridden ๊ฐ€๋Šฅํ•˜๋‹ค. )


ํ•˜๋‚˜์˜ ํŒŒ์ผ์„ ๋ชจ๋“ˆ๋กœ ๋งŒ๋“ค ๋•Œ์—๋Š” ์•„๋ž˜์ฒ˜๋Ÿผ ์ •์˜ํ•œ๋‹ค.

--------------- convert.js -------------

var num = 0.5;

exports.convertNum = function(myNum) {
     return Math.round(myNum / num);
}

----------------------------------------

์œ„ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด exports์˜ ์†์„ฑ์œผ๋กœ convertNum ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ–ˆ๋‹ค. ์ด๋Š” ์™ธ๋ถ€์—์„œ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•œ public ํ•จ์ˆ˜์ด๋‹ค. ํ•˜์ง€๋งŒ num ๋ณ€์ˆ˜๋Š” exports์†์„ฑ์œผ๋กœ ์ •์˜ํ•˜์ง€ ์•Š์•˜์œผ๋ฏ€๋กœ convert.jsํŒŒ์ผ ๋‚ด์—์„œ๋งŒ ์ ‘๊ทผ ๊ฐ€๋Šฅํ•œ private ๋ณ€์ˆ˜์ด๋‹ค.


์ด๋ ‡๊ฒŒ ๋งŒ๋“ค์–ด์ง„ ๋ชจ๋“ˆ์„ test-convert.js ํŒŒ์ผ์—์„œ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์•„๋ž˜์ฒ˜๋Ÿผ ํ•  ์ˆ˜ ์žˆ๋‹ค.


--------------- test-convert.js -------------

var convert = require('./convert');

console.log(convert.convertNum(5));

-----------------------------------------

์—ฌ๊ธฐ์„œ require()์— ๋ช…์‹œํ•œ "./convert"๋Š” ํ˜„์žฌ ๋””๋ ‰ํ† ๋ฆฌ์˜ convert.jsํŒŒ์ผ์„ importํ•œ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค. "./"๋ฅผ ๋ถ™์ด์ง€ ์•Š์œผ๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋‚œ๋‹ค. requireํ•  ๋•Œ core module์— ๋Œ€ํ•ด์„œ๋Š” ์ƒ๋Œ€๊ฒฝ๋กœ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๋ฐ”๋กœ ๋ชจ๋“ˆ๋ช…๋งŒ ๋ช…์‹œํ•˜๋„๋ก ๋˜์–ด์žˆ๋Š”๋ฐ convert.js๋Š” core module์ด ์•„๋‹ˆ๋ผ ๋ฐฉ๊ธˆ ์šฐ๋ฆฌ๊ฐ€ ๋งŒ๋“  module์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

require()๋Š” convert.js์— ๋ช…์‹œํ•œ exports์˜ ์†์„ฑ์„ returnํ•œ๋‹ค. ๋”ฐ๋ผ์„œ var convert์—๋Š” convert.js(๋ชจ๋“ˆ)์—์„œ ์ •์˜ํ•œ exports ๊ฐ์ฒด์˜ ๋‚ด์šฉ,์ฆ‰, convertNum ํ•จ์ˆ˜๊ฐ€ ํ• ๋‹น๋˜์—ˆ๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค. 


3.1.2 module.exports ์‚ฌ์šฉํ•˜๊ธฐ ( single ๋ณ€์ˆ˜, ํ•จ์ˆ˜, ๊ฐ์ฒด export ์‹œ ์‚ฌ์šฉ ) 


module์„ ๊ฐ์ฒด์ง€ํ–ฅ ๊ด€์ ์—์„œ ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•  ๋•Œ์—๋Š” ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ returnํ•˜๋„๋ก ํ•œ ๋’ค ์•„๋ž˜์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์„ ๊ฒƒ์ด๋‹ค.


---------------- convert.js --------------

var Convert = function (num){

     this.num = num;

}

Convert.prototype.convertNum = function(myNum) {
     return Math.round(myNum / num);
}

// exports = Convert; ( ์ด๋ ‡๊ฒŒ ์‚ฌ์šฉ ๊ธˆ์ง€ )

module.exports = Convert; 

---------------------------------------

------------- test-convert2.js ------------

var Convert = require('./convert');

var num = 0.5

var convert = new Convert(num);

console.log(convert.convertNum(5));

---------------------------------------

์œ„ ์ฝ”๋“œ์—์„œ ์ค‘์š”ํ•œ ๋ถ€๋ถ„์€ module.exports = Convert; ๋ผ์ธ์ด๋‹ค.


์ฒซ ์˜ˆ์ œ์—์„œ exports๋ฅผ ์ด์šฉํ•ด์„œ property๊ฐ’์„ ์„ค์ •ํ•จ์œผ๋กœ์จ convert๋ชจ๋“ˆ์—์„œ ์ •์˜ํ•œ convertNum() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค. ์ด๋Š” exports๊ฐ€ module.exports๋ฅผ ์ฐธ์กฐํ•˜๋Š” global๋ณ€์ˆ˜์˜€๊ธฐ์— ๊ฐ€๋Šฅํ–ˆ๋‹ค. ๋”ฐ๋ผ์„œ exports = Convert; ๋ผ๊ณ  ํ•ด๋ฒ„๋ฆฌ๋ฉด exports๋Š” ๋”์ด์ƒ module.exports๋ฅผ ์ฐธ์กฐํ•˜๋Š” ๋ณ€์ˆ˜๊ฐ€ ์•„๋‹Œ๊ฒƒ์ด ๋œ๋‹ค. 


3.1.3 node_modules ๋””๋ ‰ํ† ๋ฆฌ


์•ž์—์„œ module์„ requireํ•  ๋•Œ core module์ด ์•„๋‹ˆ๋ฉด ์ƒ๋Œ€ ๊ฒฝ๋กœ๋ฅผ ์ ์–ด์ฃผ๊ณ , core module์ด๋ฉด ๊ทธ๋ƒฅ ์ด๋ฆ„์„ ์จ๋„ ๋œ๋‹ค๊ณ  ํ–ˆ๋‹ค.

๊ทธ๋Ÿฌ๋ฉด ๋‚ด๋ถ€์ ์œผ๋กœ ์–ด๋–ค ๋กœ์ง์„ ํƒ€๊ธธ๋ž˜ core module์€ ์ด๋ฆ„๋งŒ ์ ์–ด๋„ ๋˜๋Š” ๊ฑธ๊นŒ? Node๊ฐ€ module์„ ์ฐพ๋Š” ๋กœ์ง์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.



 

์ž, ๊ทธ๋Ÿผ ๋‚ด๊ฐ€ ๋งŒ๋“  ๋ชจ๋“ˆ์„ ์ƒ๋Œ€๊ฒฝ๋กœ ์ง€์ •์„ ํ•˜์ง€ ์•Š๊ณ  ์“ฐ๋ ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•˜๋ฉด ๋ ๊นŒ?

๊ทธ๋ƒฅ node_modules ์•ˆ์— ๋„ฃ์–ด๋ฒ„๋ฆฌ๋ฉด ๋œ๋‹ค. ์‹ค์ œ๋กœ npm(Node Package Manager)๋ฅผ ์ด์šฉํ•˜์—ฌ install์„ ํ•˜๋ฉด node_modules ๋””๋ ‰ํ† ๋ฆฌ์— ์„ค์น˜๊ฐ€ ๋œ๋‹ค.


3.1.4 ์ฃผ์˜์‚ฌํ•ญ

์ฒซ๋ฒˆ์งธ ์ฃผ์˜์‚ฌํ•ญ์€ ๋””๋ ‰ํ† ๋ฆฌ module ์‚ฌ์šฉ์ƒ ์ฃผ์˜์‚ฌํ•ญ์ด๋‹ค.

๋””๋ ‰ํ† ๋ฆฌ ๋ฌถ์Œ์œผ๋กœ module์„ ๋งŒ๋“ค์—ˆ์„ ๋•Œ์—๋Š” package.json์— ๋ช…์‹œํ•˜์ง€ ์•Š๋Š”ํ•œ index.jsํŒŒ์ผ์ด ์กด์žฌํ•ด์•ผํ•œ๋‹ค.

์•„๋ž˜ ๊ทธ๋ฆผ์€ connect module์„ ์„ค์น˜ํ–ˆ์„ ๋•Œ ๋ณผ ์ˆ˜ ์žˆ๋Š” ํŒจํ‚ค์ง€ ํŠธ๋ฆฌ์ด๋‹ค.


connect ๋””๋ ‰ํ† ๋ฆฌ ์•ˆ์— index.js๋ผ๋Š” ํŒŒ์ผ์ด ์žˆ๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. 

์ด์ œ ์ด ํŒŒ์ผ๋ช…์„ index.js ๊ฐ€ ์•„๋‹Œ index_2.js ๋กœ ๋ฐ”๊ฟ”๋ณด์ž.

๊ทธ๋ฆฌ๊ณ  ํ…Œ์ŠคํŠธ ํ”„๋กœ๊ทธ๋žจ์„ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด์„œ require('connect'); ํ•œ ์ค„๋งŒ ์ž…๋ ฅํ•ด๋ณด๊ณ  ์‹คํ–‰ํ•ด๋ณด์ž. 

๊ทธ๋Ÿผ ์˜ค๋ฅ˜๊ฐ€ ๋‚  ๊ฒƒ์ด๋‹ค.

์ผ๋ฐ˜์ ์œผ๋กœ ๋””๋ ‰ํ† ๋ฆฌ ๊ตฌ์กฐ์˜ module์—์„œ๋Š” index.js ๋ผ๋Š” ํŒŒ์ผ๋ช…์„ ์‚ฌ์šฉํ•˜์ง€๋งŒ, ๊ตณ์ด ๋ฐ”๊พธ๊ณ  ์‹ถ๋‹ค๋ฉด package.json ํŒŒ์ผ์— ๋ช…์‹œํ•ด์ฃผ๋ฉด ๋œ๋‹ค.


package.json ํŒŒ์ผ์„ ์—ด์–ด์„œ "main" ์„ ์ฐพ์•„๋ณด์ž. ์•„๋งˆ ์—†์„ ๊ฒƒ์ด๋‹ค. ์•„๋ž˜์ฒ˜๋Ÿผ main์†์„ฑ์„ ๋„ฃ์–ด๋ณด์ž.

{  // package.json 1๋ฒˆ์งธ ๋ผ์ธ

"main":"index_2.js"

--์ƒ๋žต--

} // package.json ๋งˆ์ง€๋ง‰ ๋ผ์ธ


์ด์ œ ๋‹ค์‹œ ์‹คํ–‰ํ•ด๋ณด๋ฉด ์—๋Ÿฌ์—†์ด ์‹คํ–‰๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ( core module์ด๋‹ˆ ํŒŒ์ผ๋ช…์€ ๋‹ค์‹œ index.js๋กœ ๋ฐ”๊ฟ”์ฃผ์ž. Now~~ )


์ฐธ๊ณ ๋กœ ์•„๋ž˜ ๊ทธ๋ฆผ์€ directory ํ˜•ํƒœ์˜ module์„ requireํ–ˆ์„ ๋•Œ ์ฒ˜๋ฆฌ๋˜๋Š” ๋กœ์ง์ด๋‹ค.



๋‘ ๋ฒˆ์งธ ์ฃผ์˜์‚ฌํ•ญ์€ "monkey patching"์ด๋‹ค. ์›์ˆญ์ด ํŒจ์น˜? ์ด๋ฆ„์ด ์ฐธ ํฌํ•œํ•˜๋‹ค.

monkey patching์€ module์„ ๊ฐ์ฒด๋กœ cachingํ•˜๋Š” Node์˜ ๋Šฅ๋ ฅ ๋•Œ๋ฌธ์— ๊ฐ€๋Šฅํ•œ ๊ฒƒ์œผ๋กœ, ๋งŒ์•ฝ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ์„œ๋กœ ๋‹ค๋ฅธ ๋‘ ํŒŒ์ผ์ด ํ•˜๋‚˜์˜ module์„ requireํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ณด์ž. ์ด ๋•Œ ๋™์ผํ•œ module์„ ๋‘ ๋ฒˆ requireํ–ˆ๋‹ค๊ณ ํ•ด์„œ moduleํŒŒ์ผ์„ ๋‘ ๋ฒˆ ์ฝ์–ด๋“ค์ผ ํ•„์š”๊ฐ€ ์—†๋„๋ก Node์—์„œ ์บ์‹œ์— ๋„ฃ์–ด๋‘”๋‹ค. ํ•˜์ง€๋งŒ, ์ด ๋‘๋ฒˆ ์งธ require์—์„œ ์ด ์บ์‹œ์— ๋“ค์–ด์žˆ๋Š” module์ •๋ณด๋ฅผ ๋ณ€๊ฒฝ(alter)ํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐํšŒ๋ฅผ ๊ฐ–๊ฒŒ ๋œ๋‹ค. ์ด๋ ‡๊ฒŒ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒƒ์„ monkey patching์ด๋ผ๊ณ  ํ•˜๋Š”๋ฐ, ์ด๊ฒƒ์„ ์ž˜ ์ด์šฉํ•˜๋ฉด module์ด ๋‹ค๋ฅธ module์˜ ๋™์ž‘(behavior)๋ฅผ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ์„ ๋ฟ ์•„๋‹ˆ๋ผ ๊ฐœ๋ฐœ์ž๊ฐ€ ๋‹ค๋ฅธ ๋ฒ„์ „์˜ module์„ ๋งŒ๋“ค ํ•„์š”๊ฐ€ ์—†๋„๋ก ํ•ด์ค€๋‹ค. ( ์ด๊ฑด ๋™์  BCI๋ผ๊ณ  ์ดํ•ดํ–ˆ๋‹ค. )


3.2 ๋น„๋™๊ธฐ์‹ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ํ…Œํฌ๋‹‰


3.2.1 Callbackํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•œ one-off ์ด๋ฒคํŠธ ํ•ธ๋“ค๋ง


var fs = require('fs');
fs.readFile('./blah.txt', function(err, data) {
     if (err) { throw err };
     // do something
});


3.2.2 EventEmitter๋ฅผ ์ด์šฉํ•œ ๋ฐ˜๋ณต ์ด๋ฒคํŠธ ํ•ธ๋“ค๋ง 


var net = require('net');
var server = net.createServer(function(socket) {
     socket.on('data', function(data) {
          socket.write(data);
     });
});
server.listen(8080);


1ํšŒ์„ฑ ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ์‹œ once ์‚ฌ์šฉ.


var net = require('net');
var server = net.createServer(function(socket) {
     socket.once ('data', function(data) {
          socket.write(data);
     });
});
server.listen(8080);


EventEmitter ํ™•์žฅํ•˜๊ธฐ


function Watcher(watchDir, processedDir) {
     this.watchDir = watchDir;
     this.processedDir = processedDir;
}

var events = require('events'), util = require('util');
util.inherits(Watcher, events.EventEmitter);     // == Watcher.prototype = new events.EventEmitter();


var fs = require('fs'), watchDir = './watch', processedDir = './done';
Watcher.prototype.watch = function() {
     var watcher = this;
     fs.readdir(this.watchDir, function(err, files) {
          if (err) throw err;
          for(var index in files) {
               watcher.emit('process', files[index]);
          }
     })
}
Watcher.prototype.start = function() {
     var watcher = this;
     fs.watchFile(watchDir, function() {
          watcher.watch();
     });
}

var watcher = new Watcher(watchDir, processedDir);


watcher.on('process', function process(file) {
     var watchFile = this.watchDir + '/' + file;
     var processedFile = this.processedDir + '/' + file.toLowerCase();
     fs.rename(watchFile, processedFile, function(err) {
          if (err) throw err;
     });
});


watcher.start();



3.2.3 ๋น„๋™๊ธฐ์‹ ์–ดํ”Œ ๊ฐœ๋ฐœ์‹œ ์ฃผ์˜ํ•  ์ 


๋™๊ธฐ์‹์—์„œ๋Š” ํ”„๋กœ๊ทธ๋žจ์˜ ์‹คํ–‰ ์ˆœ์„œ๊ฐ€ ๋ช…ํ™•ํ•˜์ง€๋งŒ ๋น„๋™๊ธฐ์‹์—์„œ๋Š” ์‹คํ–‰ ์ˆœ์„œ๊ฐ€ ๋ช…ํ™•ํ•˜์ง€ ์•Š๋‹ค. 

Node์˜ event loop๋Š” ๋น„๋™๊ธฐ ๋กœ์ง์ด ์™„์ „ํžˆ ์™„๋ฃŒ๋˜์—ˆ๋Š”์ง€ ์ถ”์ ์„ ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ฌด๋ฆฌ ๋น„๋™๊ธฐ ๋กœ์ง์ด๋ผ๋„ ์™„์ „ํžˆ ๋๋‚˜์ง€ ์•Š์•˜๋‹ค๋ฉด ํ”„๋กœ์„ธ์Šค๋Š” ์ข…๋ฃŒ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.


[ Javascript ์˜ Closure์— ๋Œ€ํ•œ ๊ฐœ๋… ๋งํฌ ]



3.3 ๋น„๋™๊ธฐ์‹ ๋กœ์ง์„ ์ˆœ์„œ๋Œ€๋กœ ์ฒ˜๋ฆฌํ•˜๊ธฐ


Nimble ์„ ์ด์šฉํ•œ sequencing ์ฒ˜๋ฆฌ

Nimble ์ด์™ธ์—๋„ Step, Seq์™€ ๊ฐ™์€ module์ด ์ด๋ฏธ ์กด์žฌํ•จ. ๊ฐ€์ ธ๋‹ค ์“ฐ๋ฉด ๋จ.





[[๊ถ๊ธˆ์ฆ ๋“ค.....]]

1. ์˜์กด์„ฑ๋ชจ๋“ˆ package.json ํŒŒ์ผ๋ช…์„ ๋‹ค๋ฅธ๊ฑธ๋กœ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋‚˜??

npmjs.com์—์„œ ๋งํ•˜๋Š” npm์—์„œ ๋งํ•˜๋Š” package๋ผ ํ•จ์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.


A package is any of:

  • a) a folder containing a program described by a package.json file
  • b) a gzipped tarball containing (a)
  • c) a url that resolves to (b)
  • d) a <name>@<version> that is published on the registry with (c)
  • e) a <name>@<tag> that points to (d)
  • f) a <name> that has a latest tag satisfying (e)
  • g) a git url that, when cloned, results in (a).

๋˜ํ•œ module์ด๋ผ ํ•จ์€


  • A folder with a package.json file containing a main field.
  • A folder with an index.js file in it.
  • A JavaScript file.

๋กœ ์ •์˜๋ฅผ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. 

๋”ฐ๋ผ์„œ, package.json ํŒŒ์ผ์„ ๋‹ค๋ฅธ ํŒŒ์ผ๋ช…์œผ๋กœ ์‚ฌ์šฉํ•œ ๋‹ค๋Š” ๊ฒƒ์€ ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.


์ถœ์ฒ˜ : https://docs.npmjs.com/how-npm-works/packages


2. ์ด๋ฒคํŠธ ๋ฃจํ”„ = ๋ฉ€ํ‹ฐ์“ฐ๋ ˆ๋“œ????, ๋น„๋™๊ธฐ์™€ ๋ฉ€ํ‹ฐ์“ฐ๋ ˆ๋“œ์˜ ์ฐจ์ด์ ??

์ด๋ฒคํŠธ ๋ฃจํ”„๋Š” ๊ฒฐ๊ตญ ๋ฉ€ํ‹ฐ์“ฐ๋ ˆ๋“œ, ํด๋ง๊ณผ ๊ฐ™์€ ๊ธฐ์ˆ ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋ผ๋Š” ๊ฒฐ๋ก ์ž…๋‹ˆ๋‹ค. 

์•„๋ž˜๋Š” ์ฐธ์กฐํ•  ๋งŒํ•œ ๋งํฌ๋“ค์ž…๋‹ˆ๋‹ค.


http://jeremyko.blogspot.kr/2012/12/nodejs-thread-poollibuv.html    ( ํ•œ๊ธ€ ๋ธ”๋กœ๊ทธ )


http://nikhilm.github.io/uvbook/basics.html#event-loops


http://stackoverflow.com/questions/21485920/single-threaded-event-loop-vs-multi-threaded-non-blocking-worker-in-node-js



3. monkey patching - ์™œ ์ฒซ ๋ฒˆ์งธ requireํ•  ๋•Œ ์ˆ˜์ •๊ฐ€๋ŠฅX ?

์ด๊ฑด ์‚ฌ์‹ค ์งˆ๋ฌธ ์ž์ฒด๊ฐ€ ์ž˜๋ชป๋˜์—ˆ๋˜ ๊ฒƒ ๊ฐ™๋‹ค. monkey patching์— ๋Œ€ํ•ด์„œ๋Š” ์•„๋ž˜ ๋งํฌ์— ์„ค๋ช…์ด ๋‚˜์™€์žˆ๋‹ค.


http://fredkschott.com/post/2014/09/nodejs-dangerous-module-design-patterns/


๊ฒฐ๊ตญ monkey patching ์ด๋ผ๋Š” ๊ฒƒ์€ ๊ธฐ์กด์— ์žˆ๋˜ ๊ฒƒ์„ overwriteํ•œ๋‹ค๋Š” ๊ฒƒ์ธ๋ฐ require๋ฅผ ์ฒ˜์Œ ํ•  ๋•Œ์—๋Š” overwriteํ•  ๊ฒŒ ์—†์œผ๋‹ˆ๊นŒ ๋™์ผํ•œ ๋ชจ๋“ˆ์„ ๋‹ค๋ฅธ ํŒŒ์ผ์—์„œ ๋˜ require ํ•  ๋•Œ์— ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐํšŒ๋ฅผ ์ค€๋‹ค๊ณ  ์ฑ…์—์„œ ์„ค๋ช…์„ ํ•œ ๊ฒƒ ๊ฐ™๋‹ค. ์ด "์›์ˆญ์ด ํŒจ์น˜"์— ๋Œ€ํ•ด์„œ๋Š” ์ฑ…์—์„œ ์ž์„ธํ•˜๊ฒŒ ์„ค๋ช…์„ ์•ˆํ•˜๊ณ  ์žˆ๋Š”๋ฐ ์œ„ ๋งํฌ์—์„œ ์„ค๋ช…ํ•˜๋Š” ๊ฒƒ์„ ๋ณด๋ฉด ๊ทธ ์œ„ํ—˜์„ฑ ๋•Œ๋ฌธ์— ๊ทธ๋Ÿฐ๊ฒŒ ์•„๋‹Œ๊ฐ€ ์‹ถ๋‹ค. ๊ฐ€๋…์„ฑ๋„ ๋–จ์–ด์งˆ ๊ฒƒ ๊ฐ™๊ณ ..

๐Ÿ’ป Programming

[ Node.js In Action ] 1์žฅ ์š”์•ฝ

1์žฅ์€ ์ •๋ง ๊ฐ„๋‹จํ•˜๊ฒŒ ๋…ธ๋“œ๋ฅผ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค.


Node.js ๋Š” asynchronousํ•˜๊ณ  event-driven์ด๋‹ค.


asynchronous๋Š” ๋น„๋™๊ธฐ์‹์ด๋ผ๋Š” ๋ง์ด๊ณ  ๋น„๋™๊ธฐ์‹์ด๋ผ ํ•จ์€ ์–ด๋–ค ์š”์ฒญ์ด ๋“ค์–ด์™”์„ ๋•Œ ํ•ด๋‹น ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•ด์•ผํ•˜๋Š” ๋…€์„์„ ํ˜ธ์ถœํ•˜๊ณ  ๊ทธ ๋…€์„์ด ์ผ์„ ๋๋‚ด๊ธฐ๋ฅผ ๊ธฐ๋‹ค๋ฆฌ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋‚˜๋Š” ๋˜ ๋‹ค๋ฅธ ์š”์ฒญ์„ ๊ธฐ๋‹ค๋ฆฐ๋‹ค๋Š” ๋ง์ด๋‹ค.

Event-driven์ด๋ผ๋Š” ๊ฒƒ์€ ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๊ธฐ๋ฅผ ๊ธฐ๋‹ค๋ฆฌ๊ณ  ์žˆ๋‹ค๊ฐ€ ํ•ด๋‹น ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•  ๋•Œ ์–ด๋–ค ์ผ์„ ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ ์ด๋ฒคํŠธ๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋Š” ๊ฒƒ์ด ๋  ์ˆ˜๋„ ์žˆ๊ณ , ์Šคํฌ๋กค๋ฐ”๊ฐ€ ์ œ์ผ ๋ฐ‘์œผ๋กœ ๋‚ด๋ ค๊ฐ”๋‹ค๊ฑฐ๋‚˜, ์‚ฌ์šฉ์ž๊ฐ€ ๋งˆ์šฐ์Šค๋ฅผ ํŠน์ • ์˜์—ญ์— ์˜ฌ๋ ค๋†“๋Š”๋‹ค๊ฑฐ๋‚˜ ํ•˜๋Š” ๋“ฑ์˜ ๋™์ž‘์„ ๋งํ•œ๋‹ค.


Node.js๋Š” DIRTy ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์œ„ํ•œ ํ”Œ๋žซํผ์ด๋‹ค. ( ํ”„๋ ˆ์ž„์›์ด ์•„๋‹ˆ๋‹ค )


์—ฌ๊ธฐ์„œ DIRTy๋ผ ํ•จ์€ Data-Intense Real-Time ์„ ๋งํ•œ๋‹ค. 


๋์— y๋Š” ์™œ ๋ถ™์˜€์„๊นŒ? ๊ถ๊ธˆํ•ด์กŒ๋‹ค.  

"DIRT์• ํ”Œ๋ฆฌ์ผ€์ด์…˜"๊ณผ "DIRTy์• ํ”Œ๋ฆฌ์ผ€์ด์…˜"์ด๋ผ๋Š” ๋‹จ์–ด๋ฅผ ๋น„๊ตํ•ด๋ณด๋ฉด, dirt๋ผ๋Š” ๋‹จ์–ด๋Š” ๋จผ์ง€, ๋–ผ, ์ง„ํ™ ๋“ฑ์„ ์˜๋ฏธํ•˜๋Š” ๋ช…์‚ฌ์ด๊ณ  dirty๋Š” dirt์˜ ํ˜•์šฉ์‚ฌ๋กœ์„œ ๋”๋Ÿฌ์šด, ์ง€์ €๋ถ„ํ•œ ์ด๋ผ๋Š” ์˜๋ฏธ๋ฅผ ๊ฐ–๊ณ ์žˆ๋‹ค. ๋ญ๊ฐ€ ๋” ๋“ฃ๊ธฐ ์ข‹์€์ง€ ํ•œ๊ตญ๋ง๋กœ ๋น„๊ตํ•ด๋ณด์ž. "๋จผ์ง€ ์–ดํ”Œ", "์ง„ํ™ ์–ดํ”Œ", "๋–ผ ์–ดํ”Œ"์ด๋ผ๋Š” ๋ง๊ณผ "๋”๋Ÿฌ์šด ์–ดํ”Œ", "์ง€์ €๋ถ„ํ•œ ์–ดํ”Œ"์ด๋ผ๋Š” ๋ง, ์–ด๋–ค ์ด๋ฆ„์ด ๋” ์ž์—ฐ์Šค๋Ÿฝ๊ณ  ์ดํ•ดํ•˜๊ธฐ ์‰ฌ์šด๊ฐ€? ์‹ค์‹œ๊ฐ„์œผ๋กœ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ๋Ÿ‰์ด ๋งŽ์€ ์–ดํ”Œ์„ ์–˜๊ธฐํ•  ๋•Œ "๋จผ์ง€ ์–ดํ”Œ"๊ณผ "์ง€์ €๋ถ„ํ•œ ์–ดํ”Œ", ๋‘˜ ์ค‘ ํ•˜๋‚˜๋ฅผ ์„ ํƒํ•œ๋‹ค๋ฉด ๋‹น์—ฐํžˆ ์ง€์ €๋ถ„ํ•œ ์–ดํ”Œ์„ ์„ ํƒํ•˜๋Š”๊ฒŒ ์‚ฌ๋žŒ๋“ค์ด ์ดํ•ดํ•˜๊ธฐ๊ฐ€ ํ›จ์”ฌ ์‰ฝ๋‹ค. ๊ทธ๋ž˜์„œ ๋์— y๋ฅผ ๋ถ™์—ฌ์„œ ๋Š๋‚Œ์„ ์‚ด๋ฆฐ๊ฑฐ๋‹ค.

๋ญ ์ด๊ฑด ์–ด๋””๊นŒ์ง€๋‚˜ ๋‚˜์˜ ๊ฐœ์ธ์ ์ธ ์ƒ๊ฐ์ด๋‹ค.


์ € ๋‘ ๊ฐ€์ง€ ๋‚ด์šฉ์ด 1์žฅ์—์„œ ์–˜๊ธฐํ•˜๋Š” ๋‚ด์šฉ์ด๋‹ค.


๊ทธ๋Ÿผ 2์žฅ์œผ๋กœ ๋„˜์–ด๊ฐ€๋ณผ๊นŒ? 

์•„๋ž˜ ๋งํฌ๋Š” ๋…ธ๋“œ ํŠœํ† ๋ฆฌ์–ผ์ž…๋‹ˆ๋‹ค.


๋…ธ๋“œ ์„ค์น˜๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ Express, connect ๋ชจ๋“ˆ ์‚ฌ์šฉํ•ด์„œ ์ •๋ง ๊ฐ„๋‹จํ•œ ์›น์‚ฌ์ดํŠธ ๋งŒ๋“œ๋Š” ๊ฒƒ ๊นŒ์ง€ ์งง๊ณ  ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์„ค๋ช…ํ•ด์ฃผ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.


๋น„๋ก ์˜๋ฌธ์ด๊ธด ํ•˜์ง€๋งŒ Node.js๋ฅผ ๊นŠ์ด ๊ณต๋ถ€ํ•˜๊ธฐ์ „์— ๋…ธ๋“œ๊ฐ€ ๋ญ์ง€? ํ•˜์‹œ๋Š” ๋ถ„๋“ค์ด ๋ณด๋ฉด ์ƒ๋‹นํžˆ ์ข‹์€ ์˜์ƒ์ธ๊ฒƒ ๊ฐ™์•„์„œ ๊ณต์œ ํ•ฉ๋‹ˆ๋‹ค.


์œ ํˆฝ์— ํ•œ๊ธ€ํŒ ๋…ธ๋“œ ๊ฐ•์ขŒ๊ฐ€ ์—†์–ด์„œ ์˜๋ฌธํŒ์ด๋ผ๋„ ๋ด๋ณผ๊นŒ ํ•ด์„œ ๋ณธ๊ฑด๋ฐ ์ƒ๊ฐ๋ณด๋‹ค ์ƒ๋‹นํžˆ ๋„์›€์ด ๋งŽ์ด ๋œ ํŠœํ† ๋ฆฌ์–ผ์ด์—ˆ์Šต๋‹ˆ๋‹ค.


https://www.youtube.com/watch?v=-u-j7uqU7sI&list=PL6gx4Cwl9DGBMdkKFn3HasZnnAqVjzHn_&index=1

https://www.youtube.com/watch?v=-u-j7uqU7sI&list=PL6gx4Cwl9DGBMdkKFn3HasZnnAqVjzHn_&index=1




CSS position ์†์„ฑ ์ •๋ณตํ•˜๊ธฐ

 

์•ˆ๋…•ํ•˜์„ธ์š”, ์˜ค๋Š˜์€ ์ž๊พธ ํ—ท๊ฐˆ๋ฆฌ๊ฒŒ ๋งŒ๋“œ๋Š” ํฌ์ง€์…˜ ์†์„ฑ์— ๋Œ€ํ•ด์„œ ํฌ์ŠคํŒ…์„ ํ•˜๋ คํ•ฉ๋‹ˆ๋‹ค.

 

ํ”„๋ก ํŠธ ๊ฐœ๋ฐœ์ž๊ฐ€ ์•„๋‹ˆ๋‹ค๋ณด๋‹ˆ ์ข…์ข… css ๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋ ๋•Œ๋งˆ๋‹ค ์ œ์ผ ํ—ท๊ฐˆ๋ฆฌ๋Š”๊ฒŒ ๋ฐ”๋กœ ์ด ํฌ์ง€์…˜ ์†์„ฑ์ž…๋‹ˆ๋‹ค.

 

ํฌ์ง€์…˜ ์†์„ฑ์€ ํ™”๋ฉด์ƒ์—์„œ ์–ด๋–ค ์—˜๋ฆฌ๋จผํŠธ(์š”์†Œ)์˜ ์œ„์น˜๋ฅผ ์–ด๋””๋‹ค๊ฐ€ ์ค„์ง€๋ฅผ ๊ฒฐ์ •ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ๋Š” ์†์„ฑ์ž…๋‹ˆ๋‹ค.

 

์ฆ‰, ํฌ์ง€์…”๋‹์„ ํ•  ๋ฐฉ๋ฒ•์„ ์ง€์ •ํ•ด์ฃผ๋Š” ์†์„ฑ์ž…๋‹ˆ๋‹ค.

 

ํ™”๋ฉด์„ ๊พธ๋ฐ€ ๋•Œ htmlํŒŒ์ผ์— ํƒœ๊ทธ๋ฅผ ์จ์ฃผ๋Š”๋ฐ ์ด๋•Œ ์ ํžŒ ์ˆœ์„œ๋Œ€๋กœ ์œ„์—์„œ ์•„๋ž˜๋กœ ๊ทธ๋ƒฅ ๋ณด์—ฌ์ค„ ์ง€, ์•„๋‹ˆ๋ฉด ์ƒ์œ„ ์—˜๋ฆฌ๋จผํŠธ์™€ ๊ฒน์ณ์„œ ๋ณด์—ฌ์ค„ ์ง€, ๋˜๋Š” ์•„์˜ˆ ๋™๋–จ์–ด์ ธ์„œ ์ €~~~~~์•„๋ž˜์ชฝ์— ์œ„์น˜์‹œํ‚ฌ์ง€๋„ ๊ฒฐ์ •์„ ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ค๋‹ˆ๋‹ค.

 

์ง์ ‘์ ์œผ๋กœ ์œ„์น˜๊ฐ’์„ ์ง€์ •ํ•ด์ฃผ๋Š” ์†์„ฑ์€ ์•„๋‹ˆ์ง€๋งŒ ์ด ์†์„ฑ๊ฐ’์— ๋”ฐ๋ผ ์œ„์น˜๊ฐ’์ด ์ ์šฉ์ด ๋ ์ง€ ์•ˆ๋ ์ง€๊ฐ€ ๊ฒฐ์ •๋˜๊ธฐ ๋•Œ๋ฌธ์— ์•„์ฃผ ์ค‘์š”ํ•œ ์†์„ฑ์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ž, ๊ทธ๋Ÿผ ์ด ํฌ์ง€์…˜ ์†์„ฑ์˜ ์†์„ฑ๊ฐ’์— ์–ด๋–ค ๊ฒƒ๋“ค์ด ์žˆ๋Š”์ง€ ๋จผ์ € ์‚ดํŽด๋ณผ๊นŒ์š”?

 

ํฌ์ง€์…˜ ์†์„ฑ์˜ ์†์„ฑ๊ฐ’์€ ๋‹ค์Œ ๋„ค ๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

  • static
  • relative
  • fixed
  • absolute

์ด๋Ÿฐ ์†์„ฑ์„ ๊ฐ–๋Š” ์—˜๋ฆฌ๋จผํŠธ๋“ค์€ ๋ถ€์ˆ˜์ ์œผ๋กœ top, bottom, left, ๊ทธ๋ฆฌ๊ณ  right ์†์„ฑ์„ ๊ฐ–๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

๋ฌผ๋ก  ๋ถ€์ˆ˜์ ์ธ ๊ฐ’๋“ค์„ ์ฃผ์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. 

 

์–ด์จŒ๋“  ์ด๋Ÿฐ ๋ถ€์ˆ˜์ ์ธ ์œ„์น˜๊ฐ’๋“ค์€ position ์†์„ฑ์ด ์—†์œผ๋ฉด ์•„๋ฌด๋Ÿฐ ์˜ํ–ฅ๋„ ๋ฏธ์น˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. 

 

๋˜ํ•œ ํฌ์ง€์…˜ ๊ฐ’์— ๋”ฐ๋ผ ๋‹ค๋ฅด๊ฒŒ ๋™์ž‘ํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค.

 

๊ทธ๋Ÿผ ์ œ์ผ ์ฒซ๋ฒˆ์งธ ์†์„ฑ๋ถ€ํ„ฐ ํ•œ๋ฒˆ ์‚ดํŽด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

1. static 

 

static ์†์„ฑ์€ ๊ถ‚์ด ์šฐ๋ฆฌ๊ฐ€ ์ž…๋ ฅํ•ด์ฃผ์ง€ ์•Š์•„๋„ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ ์šฉ์ด ๋˜์–ด์žˆ๋Š” ์†์„ฑ์ž…๋‹ˆ๋‹ค.

 

์˜ˆ๋ฅผ ๋“ค์–ด, ์•„๋ž˜ ์ฒ˜๋Ÿผ div ํƒœ๊ทธ์— position ์†์„ฑ์„ ์ฃผ์ง€ ์•Š์•˜๋‹ค๊ณ  ํ•ด๋ณด์ฃ .

 

<div>์ด๊ฑด static ํฌ์ง€์…˜ ์†์„ฑ์ด ์ ์šฉ๋œ๊ฒ๋‹ˆ๋‹ค.</div>

 

์•„๋ฌด๋Ÿฐ ๊ฐ’๋„ ์ฃผ์ง€ ์•Š์•˜์ง€๋งŒ ์ € div ํƒœ๊ทธ์—๋Š” position:static ์ด๋ผ๋Š” ์†์„ฑ์ด ๊ธฐ๋ณธ์ ์œผ๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ์ฆ‰,

 

<div style="position:static;">์ด๊ฑด static ํฌ์ง€์…˜ ์†์„ฑ์ด ์ ์šฉ๋œ๊ฒ๋‹ˆ๋‹ค.</div>

 

์™€ ๋™์ผํ•œ ํƒœ๊ทธ๊ฐ€ ๋˜๋Š” ๊ฒƒ์ด์ฃ .

 

static ์†์„ฑ์€ ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ์œ„์—์„œ๋ถ€ํ„ฐ ์•„๋ž˜์ชฝ์œผ๋กœ ์ˆœ์„œ๋Œ€๋กœ ๋ฐฐ์—ด์‹œ์ผœ์ฃผ๋Š” ์†์„ฑ์ž…๋‹ˆ๋‹ค.

 

๋„ค ๊ฐ€์ง€ ์†์„ฑ ์ค‘์—์„œ ์œ ์ผํ•˜๊ฒŒ top, right, bottom, left ์†์„ฑ์˜ ๊ฐ’์„ ๋ฌด์‹œํ•˜๋Š” ์†์„ฑ์ž…๋‹ˆ๋‹ค.

 

 

2. relative

 

 position: relative; ์†์„ฑ์„ ๊ฐ–๋Š” ์—˜๋ฆฌ๋จผํŠธ๋“ค์€ ์›๋ž˜ ๊ธฐ๋ณธ์ ์ธ ํฌ์ง€์…˜์—์„œ ์ƒ๋Œ€์ ์ธ ํฌ์ง€์…˜์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ค๋‹ˆ๋‹ค.

 

์œ„์—์„œ ์–˜๊ธฐํ–ˆ๋˜ ๋ถ€์ˆ˜์ ์ธ ์˜ต์…˜๋“ค(top, right, bottom, ๊ทธ๋ฆฌ๊ณ  left)์„ ์ด์šฉํ•ด์„œ ์›๋ž˜์˜ ์œ„์น˜์—์„œ ์ƒ๋Œ€์ ์ธ ์œ„์น˜๋ฅผ ์ง€์ •ํ•ด์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

 

์—ฌ๊ธฐ์„œ ๋งํ•˜๋Š” "์›๋ž˜์˜ ์œ„์น˜"๋ผ๋Š” ๊ฒƒ์€  position์†์„ฑ์— ์•„๋ฌด๋Ÿฐ ๊ฐ’์„ ์ฃผ์ง€ ์•Š์•˜์„ ๋•Œ, ์ฆ‰, static ์ผ๋•Œ์˜ ์œ„์น˜๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. 

 

๋˜ํ•œ, ๋‹ค๋ฅธ ์ปจํ…ํŠธ๊ฐ€ ์ด ์—˜๋ฆฌ๋จผํŠธ์— ์˜ํ•ด ์ƒ๊ธด ๋นˆ ๊ณต๊ฐ„์— ์œ„์น˜ํ•  ์ˆ˜๋Š” ์—†์Šต๋‹ˆ๋‹ค.

 

๋นˆ๊ณต๊ฐ„์ด๋ž€ ์›๋ž˜ ์œ„์น˜ํ•ด์•ผํ•  ๊ณณ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

 

relative ์†์„ฑ๊ณผ static ์†์„ฑ์˜ ์ฐจ์ด๋Š” 

static ์†์„ฑ์˜ ๊ฒฝ์šฐ top, right, bottom, left ์†์„ฑ์œผ๋กœ ์œ„์น˜๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†์ง€๋งŒ, 

relative๋Š” ๊ทธ๊ฒƒ์ด ๊ฐ€๋Šฅํ•˜๋‹ค๋ผ๋Š” ์ ์ž…๋‹ˆ๋‹ค. 

 

์˜ˆ์ œ๋ฅผ ํ†ตํ•ด์„œ static๊ณผ relative์˜ ์ฐจ์ด๋ฅผ ํ•œ๋ฒˆ ๋ณผ ๊นŒ์š”??

 

htmlํŒŒ์ผ์„ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด์„œ ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

 

 

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
<!DOCTYPE html>
<html>
<head>
<style>
div.static {position: static;border: 3px solid #00f;}
div.relative {position: relative;border: 3px solid #f00;}
</style>
</head>
<body>
 
<div class="static">
This div element has position: static;
</div>
<div class="relative">
This div element has position: relative;
</div>
<div class="static">
This div element has position: static;
</div>
<div class="relative">
This div element has position: relative;
</div>
 
</body>
</html>
cs
 

์œ„ ํŽ˜์ด์ง€๋ฅผ ์—ด์–ด๋ณด๋ฉด ์•„๋ž˜์ฒ˜๋Ÿผ ํ™”๋ฉด์— ์ถœ๋ ฅ์ด ๋ฉ๋‹ˆ๋‹ค. 

 

์ž, ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด static ํด๋ž˜์Šค์™€ relative ํด๋ž˜์Šค ๋ชจ๋‘ ๋™์ผํ•œ ์†์„ฑ์„ ๊ฐ–๊ณ ์žˆ๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์ฃ . top, right, bottom, left์˜ ๊ฐ’์€ ๋‘˜ ๋‹ค ์—†์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ relative์™€ static์€ ์•„๋ฌด๋Ÿฐ ์ฐจ์ด๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ๊ทธ์ €, ํƒœ๊ทธ ์ˆœ์„œ๋Œ€๋กœ ํ™”๋ฉด์— ์ถœ๋ ฅ๋˜์ฃ .

 

์ด์ œ static ํด๋ž˜์Šค์˜ ์†์„ฑ์— left:15px ์„ ํ•œ๋ฒˆ ์ค˜๋ณผ๊นŒ์š”?

 

1
2
3
4
<style>
div.static {position: static;border: 3px solid #00f;left: 15px;}
div.relative {position: relative;border: 3px solid #f00;}
</style>
cs

 

๊ฒฐ๊ณผ๊ฐ€ ์–ด๋–ป๊ฒŒ ๋‚˜์˜ฌ๊นŒ์š”?

 

์•„๋ฌด๋Ÿฐ ์ฐจ์ด๊ฐ€ ์—†์„ ๊ฒ๋‹ˆ๋‹ค. static์€ top, right, bottom, left ์†์„ฑ๊ฐ’์„ ๋ฌด์‹œํ•˜๊ธฐ ๋•Œ๋ฌธ์ด์ฃ .

 

๊ทธ๋Ÿผ ์ด๋ฒˆ์—๋Š” relative์— left:15px; ์†์„ฑ์„ ์ค˜ ๋ด…์‹œ๋‹ค.

 

 

1
2
3
4
<style>
div.static {position: static;border: 3px solid #00f;}
div.relative {position: relative;border: 3px solid #f00;left: 15px;}
 
</style>
cs

 

 

์ด๋ฒˆ์—๋Š” ์•„๋ž˜ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ relative ํด๋ž˜์Šค์— ํ•ด๋‹นํ•˜๋Š” div ์—˜๋ฆฌ๋จผํŠธ๊ฐ€ ์›€์ง์ธ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

 

์ด์ œ ์•„๋ž˜์ฒ˜๋Ÿผ relativeํด๋ž˜์Šค์— top:15px; ์†์„ฑ์„ ์ถ”๊ฐ€ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

 

1
2
3
4
<style>
div.static {position: static;border: 3px solid #00f;}
div.relative {position: relative;border: 3px solid #f00;left: 15px;top: 15px;}
 
</style>
cs

 

๊ทธ๋Ÿผ ์–ด๋–ป๊ฒŒ ๋‚˜์˜ฌ์ง€ ์ƒ์ƒ์ด ๊ฐ€์‹œ๋‚˜์š”?

 

๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์ฒ˜๋Ÿผ ๋‚˜์˜ต๋‹ˆ๋‹ค.

 

 

 

relative ํด๋ž˜์Šค์— ์†ํ•œ div ์—˜๋ฆฌ๋จผํŠธ๋“ค์ด ์•„๋ž˜์ชฝ์œผ๋กœ 15px๋งŒํผ ๋‚ด๋ ค์˜จ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ด์ œ top์†์„ฑ์˜ ๊ฐ’์„ 100px๋กœ ์„ธํŒ…ํ•ด๋ณด์„ธ์š”. relative ํด๋ž˜์Šค์˜ div ์—˜๋ฆฌ๋จผํŠธ๋“ค์ด ์•„๋ž˜์ชฝ์œผ๋กœ ํ•œ์ฐธ ๋‚ด๋ ค๊ฐ€๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์ค‘์š”ํ•œ ์ ์€, relative ํด๋ž˜์Šค์˜ ์—˜๋ฆฌ๋จผํŠธ๋“ค์ด ์•„๋ž˜๋กœ ๋‚ด๋ ค๊ฐ”๋‹ค๊ณ  ํ•ด์„œ static ํด๋ž˜์Šค์˜ ์—˜๋ฆฌ๋จผํŠธ๋“ค์ด ์œ„์ชฝ์œผ๋กœ ์ด๋™ํ•˜๊ฑฐ๋‚˜ ํ•˜์ง€๋Š” ์•Š๋Š” ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

 

3. fixed

 

fixed ์†์„ฑ์€ ํ™”๋ฉด์— ๋ณด์ด๋Š” ์œ„์น˜๊ฐ€ ๊ธฐ์ค€์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. 

 

ํ•œ๋ฒˆ ์œ„์น˜๊ฐ€ ๊ฒฐ์ •๋˜๋ฉด ํ™”๋ฉด์—์„œ ์Šคํฌ๋กค์ด ์ƒ๊ธด๋‹คํ•ด๋„ ์‚ฌ๋ผ์ง€์ง€ ์•Š๊ณ  ์ฒ˜์Œ ํ™”๋ฉด์— ์ถœ๋ ฅ๋˜์—ˆ๋˜ ๊ทธ์ž๋ฆฌ์— ๊ณ ์ •์ ์œผ๋กœ ์œ„์น˜ํ•ฉ๋‹ˆ๋‹ค. 

 

๊ทธ๋ž˜์„œ ์†์„ฑ ์ด๋ฆ„์ด fixed ์ธ๊ฒ๋‹ˆ๋‹ค. 

 

์ด ์†์„ฑ์˜ ๊ฒฝ์šฐ relative์ฒ˜๋Ÿผ top, right, bottom, left ์†์„ฑ์œผ๋กœ ์œ„์น˜๊ฐ’์„ ์ง€์ •ํ•ด์ค„ ์ˆ˜ ์žˆ์ง€๋งŒ, 

relative ์†์„ฑ๊ณผ๋Š” ๋‹ฌ๋ฆฌ ์›๋ž˜ ์žˆ์–ด์•ผ ํ•  ์œ„์น˜, ์ฆ‰, static์ผ ๊ฒฝ์šฐ์— ์œ„์น˜ํ•  ๊ณณ์— ๋นˆ๊ณต๊ฐ„์„ ๋งŒ๋“ค์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

 

์—ฌ๊ธฐ์„œ ๋นˆ๊ณต๊ฐ„์„ ๋งŒ๋“ ๋‹ค๋Š” ๊ฒƒ์€ ๋‹ค๋ฅธ ์—˜๋ฆฌ๋จผํŠธ๊ฐ€ ๊ทธ ์ž๋ฆฌ์— ์œ„์น˜ํ•  ์ˆ˜ ์—†๋„๋ก ํ•œ๋‹ค๋Š” ๋ง๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

์ด ์†์„ฑ์€ ๋ง ๊ทธ๋Œ€๋กœ ํ™”๋ฉด์„ ๊ธฐ์ค€์œผ๋กœ ์–ด์ฐŒ๋ณด๋ฉด ์ ˆ๋Œ€์ ์ธ ์œ„์น˜๋ฅผ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ด๋ฏ€๋กœ ์•„๋งˆ ๊ฐ€์žฅ ์‰ฝ๊ฒŒ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋Š” ์†์„ฑ์ด๋ผ ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์˜ˆ์ œ๋Š” ์ƒ๋žตํ•ฉ๋‹ˆ๋‹ค.

 

 

4. absolute

 

์ด ์†์„ฑ์€ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด positioned ์กฐ์ƒ ์—˜๋ฆฌ๋จผํŠธ์— ์ƒ๋Œ€์ ์ธ ์œ„์น˜๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ค๋‹ˆ๋‹ค.

 

fixed ์†์„ฑ์ด ํ™”๋ฉด์—์„œ ์ƒ๋Œ€์ ์ธ ์œ„์น˜๋ฅผ ๊ฒฐ์ •ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ๋Š” ์†์„ฑ์ด๋ผ๋ฉด,

 

absolute๋Š” positioned ์กฐ์ƒ ์—˜๋ฆฌ๋จผํŠธ์—์„œ ์ƒ๋Œ€์ ์ธ ์œ„์น˜๋ฅผ ๊ฒฐ์ •ํ•œ๋‹ค๋Š” ์ ์—์„œ ์ฐจ์ด๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๋งŒ์•ฝ, ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด positioned ์กฐ์ƒ ์—˜๋ฆฌ๋จผํŠธ ๊ฐ€ ์—†๋‹ค๋ฉด body๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํ•˜๋ฉฐ ์œ„์น˜๊ฐ’์— ๋”ฐ๋ผ ์Šคํฌ๋กค์ด ์ƒ๊ธฐ๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค.

 

์—ฌ๊ธฐ์„œ, positioned ์—˜๋ฆฌ๋จผํŠธ๋ผ๋Š” ๊ฒƒ์€ position ์†์„ฑ ๊ฐ’์ด static์ธ ๊ฒƒ์„ ์ œ์™ธํ•˜๊ณ  position ์†์„ฑ์„ ๊ฐ–๊ณ ์žˆ๋Š” ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

 

์ด ์†์„ฑ์€ ์‡ผํ•‘๋ชฐ ๊ฐ™์€ ๊ณณ์—์„œ ์‚ฌ์ง„ ์œ„์— ์™ผ์ชฝ, ์˜ค๋ฅธ์ชฝ ํ™”์‚ดํ‘œ๋ฅผ ์˜ค๋ฒ„๋žฉ ์‹œํ‚ค๊ณ  ํ•ด๋‹น ํ™”์‚ดํ‘œ๋ฅผ ํด๋ฆญํ•  ๋•Œ๋งˆ๋‹ค ์‚ฌ์ง„์„ ๋ฐ”๊ฟ”์ฃผ๋Š” ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•  ๋•Œ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

๊ทธ๊ฒŒ ์–ด๋–ป๊ฒŒ ๊ฐ€๋Šฅํ•˜๋ƒ๊ตฌ์š”? ์ด์ œ๋ถ€ํ„ฐ ์ €์™€ ํ•จ๊ป˜ ์‚ดํŽด๋ณด์‹œ์ฃ .

 

์šฐ์„   html ํŽ˜์ด์ง€๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“ค๊ณ  ์•„๋ž˜์ฒ˜๋Ÿผ ์†Œ์Šค๋ฅผ ์ ์–ด์ฃผ์„ธ์š”.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!DOCTYPE html>
<html>
<head>
<style>
div.relative {width: 360px;height: 200px;border: 3px solid #00f;position: relative;}
div.absolute {width: 200px;height: 100px;border: 3px solid #f00;position: absolute;}
</style>
</head>
<body>
 
<div class="relative">position: relative;
  <div class="absolute">position: absolute;</div>
</div>
 
</body>
</html>
cs

 

์œ„ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด, relative ํด๋ž˜์Šค์˜ div ์—˜๋ฆฌ๋จผํŠธ ์•ˆ์— absolute ํด๋ž˜์Šค์˜ div ์—˜๋ฆฌ๋จผํŠธ๊ฐ€ ์กด์žฌํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

 

absolute ํฌ์ง€์…”๋‹์€ ์ƒ์œ„์— positioned ์—˜๋ฆฌ๋จผํŠธ๊ฐ€ ํ•„์š”ํ•˜๋‹ค๋Š” ๊ฒƒ์„ ๊ธฐ์–ตํ•ด์ฃผ์„ธ์š”.

 

์—ฌ๊ธฐ์„œ absolute ํด๋ž˜์Šค div ์—˜๋ฆฌ๋จผํŠธ(12๋ผ์ธ)์˜ positioned ์กฐ์ƒ ์—˜๋ฆฌ๋จผํŠธ๋Š” relative ํด๋ž˜์Šค div ์—˜๋ฆฌ๋จผํŠธ(11๋ผ์ธ)๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

 

์œ„ ์ฝ”๋“œ๋ฅผ ํ™”๋ฉด์— ์ถœ๋ ฅํ•ด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

absolute ๋ฐ•์Šค๊ฐ€ relative ๋ฐ•์Šค ์•ˆ์— ์žˆ๋Š” ๋ชจ์–‘์ด์ฃ .

 

์ด์ œ absolute์— right:0; ์†์„ฑ์„ ์ถ”๊ฐ€ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

1
2
3
4
<style>
div.relative {width: 360px;height: 200px;border: 3px solid #00f;position: relative;}
 
div.absolute {width: 200px;height: 100px;border: 3px solid #f00;position: absolute;right:0;}
</style>
cs

 

๊ทธ๋ฆฌ๊ณ  ํ™”๋ฉด์„ ์ƒˆ๋กœ๊ณ ์นจ ํ•ด๋ณด๋ฉด ???

 

absolute ๋ฐ•์Šค๊ฐ€ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๋ถ™์€ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

์ด์ œ relative ํด๋ž˜์Šค์— left:150px; ์†์„ฑ์„ ์ถ”๊ฐ€ํ•ด ๋ณด์ฃ .

 

1
2
3
4
<style>
div.relative {width:360px;height:200px;border:3px solid #00f;position:relative;left:150px;}
 
div.absolute {width:200px;height:100px;border:3px solid #f00;position:absolute;right:0;}
</style>
cs

 

์–ด๋–ค ๋ชจ์–‘์ด ๋‚˜์˜ฌ๊นŒ์š”? absolute ๋ฐ•์Šค๋Š” relative ๋ฐ•์Šค ์•ˆ์— ์žˆ๋Š”๋ฐ relative ๋ฐ•์Šค๋ฅผ ์šฐ์ธก์œผ๋กœ ์›€์ง์˜€์Šต๋‹ˆ๋‹ค.

 

relative ๋ฐ•์Šค์˜ ์ขŒ์ธก์— 150px๋งŒํผ ๊ณต๊ฐ„์„ ์ถ”๊ฐ€ํ–ˆ๊ณ , ๊ทธ ์•ˆ์—์žˆ๋Š” absolute ๋ฐ•์Šค๋Š” relative ๋ฐ•์Šค ์˜ค๋ฅธ์ชฝ์— ๊ผญ ๋ถ™์–ด์žˆ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ฆ‰, absolute ๋ฐ•์Šค๋Š” relative ๋ฐ•์Šค์— ์ƒ๋Œ€์ ์ธ ์œ„์น˜์— ๊ณ ์ •๋œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์ฃ .

 

์ด ์†์„ฑ์„ ์‘์šฉํ•˜๋ฉด relative ๋ฐ•์Šค์— ์ด๋ฏธ์ง€๊ฐ€ ๋กœ๋”ฉ ๋˜๋„๋ก ํ•˜๊ณ , absolute ๋ฐ•์Šค์— ํ™”์‚ดํ‘œ ์ด๋ฏธ์ง€๊ฐ€ ๋กœ๋”ฉ๋˜๋„๋ก ํ•ด์„œ ๋‘ ๊ฐœ์˜ ์ด๋ฏธ์ง€๋ฅผ ์˜ค๋ฒ„๋ž˜ํ•‘ ์‹œํ‚ฌ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ด๊ฑด ์ง์ ‘ ํ•œ๋ฒˆ ํ•ด๋ณด์„ธ์š” ~~

 

 

 

 

์ž, ์–ด๋–ค๊ฐ€์š”? ์ด์ œ CSS๋ฅผ ์ด์šฉํ•œ ํฌ์ง€์…”๋‹์„ ๋งˆ์Œ๋Œ€๋กœ ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๋‚˜์š”?

 

๊ทธ๋ ‡๋‹ค๋ฉด ๋‹คํ–‰์ด๋„ค์š”~ ^-^ ์ œ๊ฐ€ ์„ค๋ช…์„ ์ž˜ ํ•ด๋“œ๋ฆฐ ๊ฒƒ์ด๋‹ˆ๊นŒ์š” ใ…Žใ…Žใ…Ž

 

์ด๋งŒ ์˜ค๋Š˜์˜ ํฌ์ŠคํŒ…์„ ๋งˆ์น˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

๊ถ๊ธˆํ•œ ์ ์ด ์žˆ์œผ์‹œ๋ฉด ๋Œ“๊ธ€๋‚จ๊ฒจ์ฃผ์„ธ์š” ์•„๋Š” ํ•œ ์—ด์‹ฌํžˆ ๋‹ต๋ณ€๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค ^-^

 

 

์•ˆ๋…•ํ•˜์„ธ์š”, ์ผ€์ด์น˜์ž…๋‹ˆ๋‹ค.


์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” ์ž๋ฐ”๋กœ ํ…์ŠคํŠธ ํŒŒ์ผ์˜ ๋‚ด์šฉ์„ ์ฝ์–ด์„œ ์ถœ๋ ฅํ•ด๋ณด๋Š” ์†Œ์Šค๋ฅผ ๋ณด์—ฌ๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค.


์šฐ์„  CLI ํ™˜๊ฒฝ์ด๋“  ๋‹ค๋ฅธ IDE ํˆด ํ™˜๊ฒฝ์ด๋“  Java ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ์‹คํ–‰ํ•˜๋ ค๋ฉด ๋ฉ”์ธ ํ•จ์ˆ˜๊ฐ€ ํ•„์š”ํ•˜์ฃ .


๊ทธ๋ž˜์„œ ํด๋ž˜์Šค ํŒŒ์ผ์„ ๋งŒ๋“ค๋ฉด public void main(String[] args) {} ํ•จ์ˆ˜๋ฅผ ์ƒ์„ฑํ•ด์„œ ์“ฐ์ฃ .


์ด๋•Œ  main๋ฉ”์„œ๋“œ์˜ ์ธ์ž๋กœ ๋ฐ›๋Š” ์ € String[] args ๊ฐ€ ํŒŒ์ผ๋ช…์„ ์ž…๋ ฅ๋ฐ›๊ธฐ ์œ„ํ•œ ํ†ต๋กœ์ž…๋‹ˆ๋‹ค.


์ปค๋งจ๋“œ ๋ผ์ธ ๋ช…๋ น์œผ๋กœ java myapp a.txt  ๋ผ๊ณ  ์ž…๋ ฅ์„ ํ•ด์„œ ์‹คํ–‰ํ•˜๋ฉด ์ € args์— a.txt๋ผ๋Š” ์ŠคํŠธ๋ง ๊ฐ’์ด ๋“ค์–ด๊ฐ€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.


์ฆ‰, ํŒŒ์ผ๋ช…์„ ์ž…๋ ฅ๋ฐ›๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด์ฃ .


ํŒŒ์ผ๋ช…์„ ์ž…๋ ฅ๋ฐ›์•˜์œผ๋ฉด, ์šฐ๋ฆฌ๋Š” ๊ทธ ํŒŒ์ผ์„ ์—ด์–ด์„œ ์•ˆ์— ์žˆ๋Š” ๋‚ด์šฉ์„ ์ถœ๋ ฅํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋˜๋Š” ๊ฒ๋‹ˆ๋‹ค.


๊ทธ๋ ‡๋‹ค๊ณ  ๋ฌด์ž‘์ • ํŒŒ์ผ์„ ์—ด ์ˆ˜๋Š” ์—†์„ ๊ฒ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๊ทธ ํŒŒ์ผ์ด ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด?? ์—๋Ÿฌ ์ฒ˜๋ฆฌ๋„ ํ•ด์ค˜์•ผ ๊ฒ ์ฃ ?


์ž, ๊ทธ๋Ÿผ ํ•˜๋‚˜ํ•˜๋‚˜ ์ฐจ๊ทผ์ฐจ๊ทผ ํ•ด ๋ณผ๊นŒ์š”?


์šฐ์„  ํด๋ž˜์Šค๋ฅผ ํ•˜๋‚˜ ์ƒ์„ฑํ•˜๊ณ  ๋ฉ”์ธ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด ๋ด…์‹œ๋‹ค.


public class ReadFileAndPrint {     public static void main(String[] args) {     } }


ReadFileAndPrint๋ผ๋Š” ์ด๋ฆ„์˜ ์ž๋ฐ”ํŒŒ์ผ์„ ํ•˜๋‚˜ ์ƒ์„ฑํ•ด์„œ ๋ฉ”์ธํ•จ์ˆ˜๋ฅผ ์ •์˜ํ–ˆ์Šต๋‹ˆ๋‹ค.


์ž, ๊ทธ๋Ÿผ ์ด์ œ ๋ญ˜ ํ•ด์•ผ ํ•  ๊นŒ์š”? ์ € ์œ„์—์„œ ๋ง์”€๋“œ๋ฆฐ ๊ฒƒ์ฒ˜๋Ÿผ ํŒŒ์ผ๋ช…์„ ์ž…๋ ฅ๋ฐ›์•„์„œ args๋กœ ์ „๋‹ฌํ•ด์ฃผ๋Š” ๊ฒƒ์€ ์ž๋ฐ”์—์„œ ์•Œ์•„์„œ ์ž๋™์œผ๋กœ ํ•ด์ค„ํ…Œ๋‹ˆ


์šฐ๋ฆฌ๋Š” ๊ทธ ์ „๋‹ฌ๋ฐ›์€ ํŒŒ์ผ๋ช…์„ ๊ฐ€์ง€๊ณ  ์ง€์ง€๊ณ  ๋ณถ๊ณ  ํ•˜๋ฉด ๋˜๊ฒ ์ฃ ?


์ผ๋‹จ ์ž…๋ ฅ ๋ฐ›์€ ํŒŒ์ผ๋ช…์ด ์กด์žฌํ•˜๋Š”์ง€๋ฅผ ํ™•์ธํ•ด์„œ ์—๋Ÿฌ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ค˜์•ผ๊ฒ ์Šต๋‹ˆ๋‹ค.



public static void main(String[] args) {
		
	// 1. ์ž…๋ ฅ๋ฐ›์€ ํŒŒ์ผ์ด ์žˆ๋Š”์ง€ ๊ฒ€์‚ฌ
	if ( args.length == 0 ){
		System.out.println("์ž…๋ ฅ๋ฐ›์€ ํŒŒ์ผ์ด ์—†์Šต๋‹ˆ๋‹ค.");
		System.exit(0);
	}
}


์œ„ ์ฝ”๋“œ๋Š” args๋กœ ์ „๋‹ฌ๋ฐ›์€ ๊ฐ’์ด ์žˆ๋Š”์ง€ ์ŠคํŠธ๋ง ๋ฐฐ์—ด์˜ ๊ธธ์ด๋ฅผ ๊ฒ€์‚ฌํ•ด์„œ ์ž…๋ ฅ๋ฐ›์€ ๊ฐ’์ด ์—†์œผ๋ฉด ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•˜๋„๋ก ํ•ด๋†“์•˜์Šต๋‹ˆ๋‹ค.


์ž, ๊ทธ๋Ÿผ ์‹ค์ œ๋กœ ์ž…๋ ฅ ๋ฐ›์€ ๊ฐ’์ด ์žˆ์„ ๋•Œ๋Š” ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ•  ๊นŒ์š”?


๊ทธ ์ž…๋ ฅ๋ฐ›์€ ๊ฐ’์œผ๋กœ ํŒŒ์ผ์„ ์ฝ์–ด๋“ค์—ฌ์•ผ ๊ฒ ์ฃ ?


์ด์ œ ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.



// 2. ์ž…๋ ฅ๋ฐ›์€ ํŒŒ์ผ์ด ์กด์žฌํ•˜๋Š”์ง€ ๊ฒ€์‚ฌ
File file = new File(args[0]);

if ( file.exists() ){

}


์ž…๋ ฅ๋ฐ›์€ ํŒŒ์ผ์ด ํ•œ๊ฐœ๋ผ๋Š” ๊ฐ€์ • ํ•˜์— args๋ฐฐ์—ด์˜ 0๋ฒˆ์งธ ์ธ๋ฑ์Šค์— ์žˆ๋Š” ๊ฐ’๋งŒ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.


ํŒŒ์ผ์„ ์ž๋ฐ”์†Œ์Šค๋กœ ์ฝ์–ด๋“ค์ผ ๋•Œ๋Š” File ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. File ์ƒ์„ฑ์ž์— ํŒŒ์ผ๋ช…์„ ๋‹ด๊ณ ์žˆ๋Š” args[0] ์„ ์ „๋‹ฌํ•˜์—ฌ new ํ‚ค์›Œ๋“œ๋กœ ์ƒ์„ฑํ•˜๋ฉด ํ•ด๋‹น ์ด๋ฆ„์˜ ํŒŒ์ผ์„ File ๊ฐ์ฒด๋กœ ๋งŒ๋“ค์–ด ์ค๋‹ˆ๋‹ค.


๊ทธ๋ฆฌ๊ณ  if ๋ฌธ์„ ํ†ตํ•ด์„œ ํ•ด๋‹น ํŒŒ์ผ๋ช…์„ ๊ฐ–๋Š” ํŒŒ์ผ์ด ์‹ค์ œ๋กœ ์‹œ์Šคํ…œ ์ƒ์— ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ์„ ํ•ฉ๋‹ˆ๋‹ค.


if ๋ฌธ ์•ˆ์—์„œ๋Š” ํ•ด๋‹น ํŒŒ์ผ๋ช…์„ ๊ฐ–๋Š” ํŒŒ์ผ์ด ์‹ค์ œ๋กœ ์กด์žฌํ–ˆ์„ ๋•Œ ํ•ด์•ผํ•  ์ผ์„ ์ฝ”๋“œ๋กœ ์ ์–ด์ฃผ๋ฉด ๋˜๊ฒ ์ฃ .


์šฐ๋ฆฌ๋Š” File  ๊ฐ์ฒด๋ฅผ BufferedReader ๋ฅผ ์ด์šฉํ•ด์„œ ์ข€๋” ๋‹ค๋ฃจ๊ธฐ ์‰ฌ์šด ํ˜•ํƒœ๋กœ ๋ฐ”๊ฟ€ ๊ฒ๋‹ˆ๋‹ค.


์ด์ œ if ๋ฌธ ์•ˆ์— ๋‹ค์Œ ์†Œ์Šค๋ฅผ ์ถ”๊ฐ€ํ•ด ๋ณผ๊นŒ์š”?



// 3. ํ…์ŠคํŠธ ํŒŒ์ผ์˜ ๋‚ด์šฉ์„ ์ฝ์–ด์„œ ์ถœ๋ ฅ BufferedReader br = new BufferedReader(new FileReader(file)); while( br.ready() ){ System.out.println(br.readLine()); } br.close();


์œ„ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด File ๊ฐ์ฒด์ธ file์„ FileReader๋กœ ๊ฐ์‹ผ ๋‹ค์Œ ๋‹ค์‹œ BufferedReader๋กœ ๊ฐ์ŒŒ์Šต๋‹ˆ๋‹ค.


์ด๋ ‡๊ฒŒ ํ•˜๋ฉด BufferedReader ์˜ readLine() ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ์†์‰ฝ๊ฒŒ ๋ผ์ธ ๋‹จ์œ„๋กœ ํŒŒ์ผ์„ ์ฝ์–ด๋“ค์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


๋‚ด์šฉ์ด ์กด์žฌํ•˜๋Š”์ง€๋Š” br.reader() ๋ฅผ ์ด์šฉํ•ด์„œ ๊ฒ€์‚ฌ๋ฅผ ํ•˜๊ณ  true๊ฐ€ ๋ฐ˜ํ™˜๋  ๊ฒฝ์šฐ์— ๊ณ„์†ํ•ด์„œ ํ•œ์ค„์”ฉ ์ฝ์–ด๋‚ด๋ ค๊ฐ€๋ฉด์„œ ์ถœ๋ ฅํ•˜๋„๋ก ๋กœ์ง์ด ๊ตฌ์„ฑ๋˜์–ด์žˆ์Œ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


์ž, ๊ทธ๋Ÿผ ์ „์ฒด ์†Œ์Šค๋ฅผ ๋‹ค์‹œ ํ•œ๋ฒˆ ๋ณด๋„๋ก ํ•˜์ฃ .



public class ReadFileAndPrint {     public static void main(String[] args) throws IOException {

// 1. ์ž…๋ ฅ๋ฐ›์€ ํŒŒ์ผ์ด ์žˆ๋Š”์ง€ ๊ฒ€์‚ฌ if ( args.length == 0 ){ System.out.println("์ž…๋ ฅ๋ฐ›์€ ํŒŒ์ผ์ด ์—†์Šต๋‹ˆ๋‹ค."); System.exit(0); } // 2. ์ž…๋ ฅ๋ฐ›์€ ํŒŒ์ผ์ด ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ File file = new File(args[0]); if ( file.exists() ){     // 3. ํŒŒ์ผ์ด ์กด์žฌํ•˜๋ฉด, ํ…์ŠคํŠธ ํŒŒ์ผ์˜ ๋‚ด์šฉ์„ ์ฝ์–ด์„œ ์ถœ๋ ฅ     BufferedReader br = new BufferedReader(new FileReader(file));     while( br.ready() ){ System.out.println(br.readLine());     }     br.close(); }

}



์ด์ œ ์ž๋ฐ” ํด๋ž˜์Šค ํŒŒ์ผ์„ ์ปดํŒŒ์ผํ•˜๊ณ  ์‹คํ–‰ํ•  ๋•Œ ์ธ์ž๋กœ ์›ํ•˜๋Š” ํ…์ŠคํŠธ ํŒŒ์ผ์˜ ์ ˆ๋Œ€๊ฒฝ๋กœ๋ช…์„ ๋„ฃ์–ด์ฃผ์„ธ์š”. 


์ƒ๋Œ€๊ฒฝ๋กœ๋ช…์„ ๋„ฃ์„ ๊ฒฝ์šฐ ํ™˜๊ฒฝ์— ๋”ฐ๋ผ ํŒŒ์ผ์„ ๋ชป ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


ํŒŒ์ผ์˜ ๋‚ด์šฉ์ด ์ž˜ ์ถœ๋ ฅ๋œ๋‹ค๋ฉด ์ž˜ ๋”ฐ๋ผ์˜ค์‹ ๊ฒ๋‹ˆ๋‹ค.


์–ด๋•Œ์š” ? ๋ณ„๋กœ ์–ด๋ ต์ง€ ์•Š์ฃ ??


์ด์ƒ์œผ๋กœ ์ž๋ฐ”๋กœ ํ…์ŠคํŠธ ํŒŒ์ผ ์ฝ์–ด์„œ ์ถœ๋ ฅํ•˜๊ธฐ ํฌ์ŠคํŒ…์„ ๋งˆ์น˜๊ฒ ์Šต๋‹ˆ๋‹ค.


๊ถ๊ธˆํ•˜์‹  ์  ์žˆ์œผ๋ฉด ๋Œ“๊ธ€๋‹ฌ์•„์ฃผ์„ธ์š” ^-^

์Šคํ”„๋ง ์›น ๊ฐœ๋ฐœ์„ ํ•˜๋‹ค๋ณด๋ฉด ์ƒ๋‹นํžˆ ๋งŽ์€ ์—๋Ÿฌ ๋ฉ”์‹œ์ง€์™€ ๋งˆ์ฃผํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.


ํŠนํžˆ ๊ฐœ๋ฐœ ํ™˜๊ฒฝ์„ ๊ตฌ์„ฑํ•  ๋•Œ ์ž์ฃผ ๋ณด๊ฒŒ ๋˜์ฃ .


์ตœ๊ทผ์— ๊ฐœ์ธ์ ์œผ๋กœ ์Šคํ”„๋ง ์›น ๊ฐœ๋ฐœ ํ™˜๊ฒฝ์„ ๊ตฌ์„ฑ์„ ํ•ด๋ณด๋‹ค๊ฐ€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.


No converter found for return value of type: class java.util.ArrayList


์Šคํ”„๋ง ๋ฉ”์ด๋ธ ํ”„๋กœ์ ํŠธ๋กœ ๊ตฌ์„ฑ์„ ํ–ˆ๊ตฌ์š”, ์Šคํ”„๋ง ๋ฒ„์ „์€ 4.3.2 ๋ฒ„์ „์ด์—ˆ์Šต๋‹ˆ๋‹ค.


pom.xml ์— jackson-core ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋””ํŽœ๋˜์‹œ ๋ชฉ๋ก์— ์ถ”๊ฐ€ํ•ด๋†“์€ ์ƒํƒœ์—ฌ์„œ ๋‹น์—ฐํžˆ ๋  ๊ฑฐ๋ผ๊ณ  ์ƒ๊ฐํ–ˆ์—ˆ๋Š”๋ฐ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋”๊ตฐ์š”.


์Šคํ”„๋ง ์ปจํŠธ๋กค๋Ÿฌ์—์„œ @ResponseBody ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•ด์„œ List<Dto> ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ ค๊ณ  ํ•  ๋•Œ ๋ฐœ์ƒํ•œ ์—๋Ÿฌ์ž…๋‹ˆ๋‹ค.


๊ตฌ๊ธ€๋ง์„ ํ•ด๋ณด๋‹ˆ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์€ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค.


jackson-bind ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ๋Š” ๊ฒ๋‹ˆ๋‹ค.


๊ทธ๋ž˜์„œ ์•„๋ž˜ ๋””ํŽœ๋˜์‹œ ๋ถ€๋ถ„์„ ์ถ”๊ฐ€๋ฅผ ํ•ด์„œ ํ•ด๊ฒฐํ–ˆ์Šต๋‹ˆ๋‹ค.


<dependency>

<groupId>com.fasterxml.jackson.core</groupId>

  <artifactId>jackson-databind</artifactId>

<version>2.5.4</version>

</dependency>



@ResponseBody ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด jackson-bind ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ํ•„์š”ํ•˜๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ๋˜์—ˆ๋„ค์š”~


์—ญ์‹œ ๋ˆ„๊ฐ€ ๊ตฌ์„ฑํ•ด๋†“์€ ํ™˜๊ฒฝ์—์„œ๋งŒ ๊ฐœ๋ฐœ์„ ํ•˜๋‹ค๊ฐ€ ํ˜ผ์ž์„œ ํ•˜๋ ค๋‹ˆ ์ด๋Ÿฐ์ €๋Ÿฐ ๋‹ค์–‘ํ•œ ์ผ€์ด์Šค๋“ค์„ ๋งŽ์ด ๋ณด๊ฒŒ ๋˜๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.


์˜ค๋žœ๋งŒ์— ํฌ์ŠคํŒ… ํ•˜๋„ค์š”.


์ž, ์˜ค๋Š˜์€ ์œˆ๋„์šฐ์—์„œ ๋„์ปค๋ฅผ ์ด์šฉํ•ด์„œ PostgreSQL ์„ค์น˜ํ•˜๊ณ  ์ ‘์†ํ•ด๋ณด๋„๋ก ํ•  ๊ฒ๋‹ˆ๋‹ค.


์™œ ๋„์ปค์— ์„ค์น˜ํ•˜๋ƒ๊ตฌ์š”?


docker๋ฅผ ์ด์šฉํ•˜๋ฉด ์ •๋ง ์‰ฝ๊ณ  ๋น ๋ฅด๊ฒŒ ๋‹ค์–‘ํ•œ ํ™˜๊ฒฝ์„ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ •๋ง ์ •๋ง ํฐ ์žฅ์ ์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด์ฃ .


๊ฐ€์žฅ ๋จผ์ € PC์— ๋„์ปค๋ฅผ ์„ค์น˜ํ•ด์•ผ ๊ฒ ์ง€์š”.


์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” ๋„์ปค ์„ค์น˜ ๋ถ€๋ถ„์€ ๊ฑด๋„ˆ๋›ฐ๋Š” ๊ฒƒ์œผ๋กœ ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. 


๊ตฌ๊ธ€๋งํ•˜๋ฉด ์—„์ฒญ ๋งŽ์ด ๋‚˜์˜ฌ๊ฑฐ๊ฑฐ๋“ ์š”. ๊ทธ๊ฑธ ์ฐธ๊ณ  ํ•˜์‹œ๋ฉด ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.


์œˆ๋„์šฐ์—๋Š” ์œˆ๋„์šฐ์šฉ ์ธ์Šคํ†จ๋Ÿฌ๋ฅผ ์ด์šฉํ•˜๋ฉด ๊ธˆ๋ฐฉ ์„ค์น˜๋ฅผ ํ•  ์ˆ˜ ์žˆ์œผ๋‹ˆ๊นŒ์š”.


์ผ๋‹จ ์ œ ํ™˜๊ฒฝ์€ ์œˆ๋„์šฐ8์—๋‹ค๊ฐ€ ๋„์ปค๋ฅผ ์œˆ๋„์šฐ ์ธ์Šคํ†จ๋Ÿฌ๋ฅผ ์ด์šฉํ•ด์„œ ์„ค์น˜๋ฅผ ํ•œ ์ƒํƒœ์ž…๋‹ˆ๋‹ค.


์‚ฌ์‹ค ๋„์ปค ์กฐ๊ธˆ ๋งŒ์ ธ๋ณด์‹  ๋ถ„๋“คํ•œํ…Œ๋Š” ์—„์ฒญ ์‰ฌ์šด ์ผ์ด ๋˜๊ฒ ์ง€๋งŒ ์ฒ˜์Œ ํ•ด๋ณด์‹œ๋Š” ๋ถ„๋“คํ•œํ…Œ๋Š” ์กฐ๊ธˆ ์–ด๋ ค์šธ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.


๋‹ค์Œ ์ˆœ์„œ์— ๋”ฐ๋ผ์„œ ์ง„ํ–‰ํ•ด๋ณด๋„๋ก ํ•˜์ฃ .


1. cmd ์ฐฝ์—์„œ docker ๊ฐ€ ์ œ๋Œ€๋กœ ์„ค์น˜๋˜์—ˆ๋Š”์ง€ ํ™•์ธ

2. docker๋ฅผ ์ด์šฉํ•ด์„œ postgreSQL ์„ค์น˜ ๋ฐ ๋ฐ๋ชฌ ํ˜•ํƒœ๋กœ ์‹คํ–‰

3. DBeaver ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค ๊ด€๋ฆฌ ํˆด์„ ์ด์šฉํ•˜์—ฌ ์ ‘์† ๋ฐ dml๋ฌธ ์‹คํ–‰


์ž, ๊ทธ๋Ÿผ 1๋ฒˆ ๋ถ€ํ„ฐ ํ•ด๋ณผ๊นŒ์š”?


์ผ๋‹จ cmd์ฐฝ์„ ๋„์›๋‹ˆ๋‹ค. ์–ด๋–ป๊ฒŒ ๋„์šฐ๋Š”์ง€ ๋ชจ๋ฅด์‹œ๋Š” ๋ถ„๋“ค์€ ์œˆ๋„์šฐํ‚ค๋ฅผ ํ•œ๋ฒˆ ๋ˆ„๋ฅธ ๋’ค์— cmd ์ž…๋ ฅํ•˜๊ณ  ์—”ํ„ฐ ์น˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

(์œˆ๋„์šฐ 7 ์ดํ•˜์ธ ๊ฒฝ์šฐ์—๋Š” ์œˆ๋„์šฐํ‚ค + c ๋ฅผ ๋ˆŒ๋Ÿฌ์„œ ์‹คํ–‰ ์ฐฝ์„ ๋„์šด๋’ค์— cmd ๋ผ๊ณ  ์ž…๋ ฅํ•ด์ฃผ์‹œ๋ฉด ๋ ๊ฒ๋‹ˆ๋‹ค.)



์œ„์™€ ๊ฐ™์€ ํ™”๋ฉด์ด ๋‚˜์™”์œผ๋ฉด ์ด์ œ docker ๋ผ๊ณ  ์น˜๊ณ  ์—”ํ„ฐ๋ฅผ ์นฉ๋‹ˆ๋‹ค.



์œ„ ์‚ฌ์ง„์ฒ˜๋Ÿผ ์˜ต์…˜ ๋ชฉ๋ก์ด ์ฃผ๋ฃจ๋ฃจ๋ฃฉ ๋‚˜์™€์ฃผ๋ฉด ์ œ๋Œ€๋กœ ์„ค์น˜๊ฐ€ ๋œ๊ฒ๋‹ˆ๋‹ค.


์ด์ œ ์•„๋ž˜ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰์‹œ์ผœ ์ฃผ์„ธ์š”


docker run --name myPostgreSQL -e POSTGRES_USER=GilDong -e POSTGRES_PASSWORD=1234 -d -p 5432:5432 postgres


์œ„ ๋ช…๋ น์–ด๋Š” postgresql ์ตœ์‹ ๋ฒ„์ „์„ ๋‹ค์šด๋กœ๋“œ ๋ฐ›์•„์„œ ๋กœ์ปฌ PC์˜ 5432 ํฌํŠธ์™€ postgresql ์ปจํ…Œ์ด๋„ˆ์˜ 5432 ํฌํŠธ๋ฅผ ์—ฐ๊ฒฐ์‹œ์ผœ์ฃผ๊ณ  myPostgreSQL ์ด๋ผ๋Š” ์ด๋ฆ„์˜ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์‹คํ–‰์‹œ์ผœ์ค๋‹ˆ๋‹ค.


-d ์˜ต์…˜์€ ๋ฐ๋ชฌ์œผ๋กœ ๋„์šฐ๋ผ๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค.

-e POSTGRES_USER=GilDong -e POSTGRES_PASSWORD=1234 ์˜ต์…˜์€ GilDong์ด๋ผ๋Š” ์ด๋ฆ„์˜ 1234 ๋น„๋ฒˆ์„ ๊ฐ–๋Š” ์ƒˆ๋กœ์šด ์‚ฌ์šฉ์ž๋ฅผ ์ƒ์„ฑํ•˜๋ผ๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค.


๊ฐ ์˜ต์…˜์— ๋Œ€ํ•œ ์„ค๋ช…์€ docker help run ์„ ์‹คํ–‰์‹œํ‚ค๋ฉด ํ™•์ธ ํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

docker run --help ๋ผ๊ณ  ํ•˜์…”๋„ ๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


๋งŒ์•ฝ, ๋‹ค์šด๋กœ๋“œ ๋ฐ›์ง€ ๋ชปํ•˜๊ณ  ์ด์ƒํ•œ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค๋ฉด ํŠธ๋Ÿฌ๋ธ”์ŠˆํŒ…์„ ํ•ด์ฃผ์…”์•ผ ํ•ฉ๋‹ˆ๋‹ค.


์ฐธ๊ณ ๋กœ, ์œˆ๋„์šฐ ํ™˜๊ฒฝ์—์„œ๋Š” CMD์ฐฝ์„ ๋„์šธ ๋•Œ๋งˆ๋‹ค docker-machine ์„ ์ด์šฉํ•ด์„œ ํ™˜๊ฒฝ๋ณ€์ˆ˜ ์„ค์ •์„ ํ•ด์ค˜์•ผ docker์˜ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋”๊ตฐ์š”.


์—๋Ÿฌ๋ฉ”์‹œ์ง€๋ฅผ ์ž˜ ์ฝ์–ด๋ณด๊ณ  ๊ตฌ๊ธ€๋ง ์กฐ๊ธˆ๋งŒ ํ•˜์‹œ๋ฉด ๊ธˆ๋ฐฉ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์„ ๊ฒ๋‹ˆ๋‹ค.


์ž, ๊ทธ๋Ÿผ ์—ฌ๊ธฐ๊นŒ์ง€ ์ž˜ ์„ค์น˜๊ฐ€ ๋˜์‹  ๋ถ„๋“ค์€ ์ด์ œ DBeaver์™€ ๊ฐ™์€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํˆด์„ ์‹คํ–‰์„ ์‹œ์ผœ์ฃผ์„ธ์š”.


๋ฌผ๋ก  postgreSQL ๋“œ๋ผ์ด๋ฒ„๋ฅผ ์ง€์›ํ•˜๋Š” ํˆด์„ ์‚ฌ์šฉํ•ด์•ผ๊ฒ ์ฃ .


DBeaver ๊ฐ€ ์‹คํ–‰์ด ๋์œผ๋ฉด ์•„๋ž˜ ์‚ฌ์ง„์ฒ˜๋Ÿผ ์ขŒ์ธก ์ƒ๋‹จ์˜ ์•„์ด์ฝ˜์„ ํด๋ฆญํ•ด์„œ ์ƒˆ๋กœ์šด ์ปค๋„ฅ์…˜์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.








์—ฌ๊ธฐ์„œ Host์—๋Š” docker-machine ip ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•ด์„œ ๋‚˜์˜ค๋Š” ์•„์ดํ”ผ๋ฅผ ์ ์–ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

๋งŒ์•ฝ docker-machine ์ด ์„ค์น˜๋˜์–ด์žˆ์ง€ ์•Š๋‹ค๋ฉด ๋ณ„๋„๋กœ ์„ค์น˜๋ฅผ ํ•ด์ฃผ์…”์•ผ ํ•ฉ๋‹ˆ๋‹ค.

(์ฐธ๊ณ ๋กœ docker-machine์€ docker ํ”„๋กœ์ ํŠธ ์ค‘ ํ•˜๋‚˜ ์ž…๋‹ˆ๋‹ค.)


๊ทธ๋ฆฌ๊ณ  database์™€ user์—๋Š” ์œ„์—์„œ ์‹คํ–‰ํ•  ๋•Œ POSTGRES_USER ์˜ต์…˜์— ์ฃผ์—ˆ๋˜ ์ด๋ฆ„์„ ๋™์ผํ•˜๊ฒŒ ์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

ํŒจ์Šค์›Œ๋“œ ์—ญ์‹œ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ POSTGRES_PASSWORD ์˜ต์…˜์— ์ฃผ์—ˆ๋˜ ๊ฐ’์„ ๊ทธ๋Œ€๋กœ ์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.


์ž…๋ ฅ์„ ๋‹ค ํ•˜๊ณ ๋‚œ ๋’ค ์šฐ์ธก ํ•˜๋‹จ์— ์žˆ๋Š” Test Connection ... ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜์—ฌ ์ •์ƒ์ ์œผ๋กœ ์ ‘์†์ด ๋˜๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.




์ •์ƒ์ ์œผ๋กœ ์—ฐ๊ฒฐ์ด ๋œ๋‹ค๋ฉด ์œ„์™€ ๊ฐ™์€ ํŒ์—…์ฐฝ์ด ๋œจ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.


๋ณ„๊ฑฐ ์—†์ฃ ?


์ด์ œ Next ํ•ด์„œ ์ปค๋„ฅ์…˜ ์ƒ์„ฑํ•˜๊ณ  DML๋ฌธ ์ž…๋ ฅํ•ด์„œ ์‹คํ–‰ํ•ด๋ณด์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค~


์ด์ƒ์œผ๋กœ ์˜ค๋Š˜ ํฌ์ŠคํŒ…์„ ๋งˆ์นฉ๋‹ˆ๋‹ค. ์ง‘์— ๊ฐˆ ์‹œ๊ฐ„์ด ๋‹ค ๋˜์–ด์„œ ๋„ˆ๋ฌด ๋นจ๋ฆฌ ์“ฐ๋‹ค๋ณด๋‹ˆ ๋Œ€์ถฉ ์“ฐ๊ฒŒ ๋˜๋„ค์š”.


๊ถ๊ธˆํ•œ ์ ์ด๋‚˜ ์ž˜ ์•ˆ๋˜์‹œ๋Š” ๋ถ„๋“ค ๋Œ“๊ธ€ ๋‚จ๊ฒจ์ฃผ์‹œ๋ฉด ๋น ๋ฅธ ๋‹ต๋ณ€ ๋‚จ๊ฒจ๋“œ๋ฆด๊ฒŒ์š”~ ใ…Žใ…Ž



Legend

Excellent Good Fair Bad Horrible

์ž๋ฃŒ๊ตฌ์กฐ๋ณ„ ์‹œ๊ฐ„๋ณต์žก๋„ ๋ฐ ๊ณต๊ฐ„๋ณต์žก๋„

์ž๋ฃŒ๊ตฌ์กฐ ์‹œ๊ฐ„ ๋ณต์žก๋„ ๊ณต๊ฐ„๋ณต์žก๋„
  Average Worst Worst
  Access Search Insertion Deletion Access Search Insertion Deletion  
Array O(1) O(n) O(n) O(n) O(1) O(n) O(n) O(n) O(n)
Stack O(n) O(n) O(1) O(1) O(n) O(n) O(1) O(1) O(n)
Singly-Linked List O(n) O(n) O(1) O(1) O(n) O(n) O(1) O(1) O(n)
Doubly-Linked List O(n) O(n) O(1) O(1) O(n) O(n) O(1) O(1) O(n)
Skip List O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(n) O(n) O(n) O(n) O(n log(n))
Hash Table - O(1) O(1) O(1) - O(n) O(n) O(n) O(n)
Binary Search Tree O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(n) O(n) O(n) O(n) O(n)
Cartesian Tree - O(log(n)) O(log(n)) O(log(n)) - O(n) O(n) O(n) O(n)
B-Tree O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(n)
Red-Black Tree O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(n)
Splay Tree - O(log(n)) O(log(n)) O(log(n)) - O(log(n)) O(log(n)) O(log(n)) O(n)
AVL Tree O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(n)

์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์‹œ๊ฐ„๋ณต์žก๋„ ๋ฐ ๊ณต๊ฐ„๋ณต์žก๋„

์•Œ๊ณ ๋ฆฌ์ฆ˜ ์‹œ๊ฐ„๋ณต์žก๋„ ๊ณต๊ฐ„๋ณต์žก๋„
  Best Average Worst Worst
Quicksort O(n log(n)) O(n log(n)) O(n^2) O(log(n))
Mergesort O(n log(n)) O(n log(n)) O(n log(n)) O(n)
Timsort O(n) O(n log(n)) O(n log(n)) O(n)
Heapsort O(n log(n)) O(n log(n)) O(n log(n)) O(1)
Bubble Sort O(n) O(n^2) O(n^2) O(1)
Insertion Sort O(n) O(n^2) O(n^2) O(1)
Selection Sort O(n^2) O(n^2) O(n^2) O(1)
Shell Sort O(n) O((nlog(n))^2) O((nlog(n))^2) O(1)
Bucket Sort O(n+k) O(n+k) O(n^2) O(n)
Radix Sort O(nk) O(nk) O(nk) O(n+k)

Graph Operations

Node / Edge Management Storage Add Vertex Add Edge Remove Vertex Remove Edge Query
Adjacency list O(|V|+|E|) O(1) O(1) O(|V| + |E|) O(|E|) O(|V|)
Incidence list O(|V|+|E|) O(1) O(1) O(|E|) O(|E|) O(|E|)
Adjacency matrix O(|V|^2) O(|V|^2) O(1) O(|V|^2) O(1) O(1)
Incidence matrix O(|V| ⋅ |E|) O(|V| ⋅ |E|) O(|V| ⋅ |E|) O(|V| ⋅ |E|) O(|V| ⋅ |E|) O(|E|)

Heap Operations

Type ์‹œ๊ฐ„๋ณต์žก๋„
  Heapify Find Max Extract Max Increase Key Insert Delete Merge
Linked List (sorted) - O(1) O(1) O(n) O(n) O(1) O(m+n)
Linked List (unsorted) - O(n) O(n) O(1) O(1) O(1) O(1)
Binary Heap O(n) O(1) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(m+n)
Binomial Heap - O(1) O(log(n)) O(log(n)) O(1) O(log(n)) O(log(n))
Fibonacci Heap - O(1) O(log(n)) O(1) O(1) O(log(n)) O(1)

 

์•ˆ๋…•ํ•˜์„ธ์š”, ์ผ€์ด์น˜์ž…๋‹ˆ๋‹ค.


์ง€๋‚œ ๋ ˆ๋””์Šค ๋ฐ์ดํƒ€ ํƒ€์ž… ํฌ์ŠคํŒ…์„ ๋ณด์…จ๋‹ค๋ฉด ์•„์‹œ๊ฒ ์ง€๋งŒ ๋ ˆ๋””์Šค๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ๋‹ค์„ฏ ๊ฐ€์ง€์˜ ๋ฐ์ดํƒ€ ํƒ€์ž…์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค.


์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” ๋ ˆ๋””์Šค ๋ฐ์ดํƒ€ ํƒ€์ž… ๋ณ„ ๋ช…๋ น์–ด ๋ชฉ๋ก ๋ฐ ๊ธฐ๋Šฅ ์ œ 1ํŽธ์œผ๋กœ ์ŠคํŠธ๋ง ํƒ€์ž…์— ๋Œ€ํ•œ ์„ค๋ช…์„ ํ•˜๋ คํ•ฉ๋‹ˆ๋‹ค.


Redis Strings

๋ ˆ๋””์Šค ์ŠคํŠธ๋ง ํƒ€์ž…์€ ์ œ์ผ ๊ธฐ๋ณธ์ ์ธ ํƒ€์ž…์ž…๋‹ˆ๋‹ค. Memcached์˜ ์œ ์ผํ•œ ๋ฐ์ดํ„ฐ ํƒ€์ž…์ธ ์ŠคํŠธ๋ง์„ ์ง€์›ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ ˆ๋””์Šค๋ฅผ ์ƒˆ๋กœ ๋ฐฐ์šฐ๋Š” ์‚ฌ๋žŒ๋“ค์—๊ฒŒ๋„ ์–ด๋ ต์ง€ ์•Š์ฃ . ๋ญ ์ŠคํŠธ๋ง์€ ๋ชจ๋“  ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ฐœ๋ฐœ์ž๋“ค์ด ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” ํƒ€์ž… ์ค‘์— ํ•˜๋‚˜๋‹ˆ๊นŒ์š”.

๋ ˆ๋””์Šค์˜ ํ‚ค๊ฐ€ ๋ฐ”๋กœ ์ŠคํŠธ๋ง์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ’์„ ์ŠคํŠธ๋ง ํƒ€์ž…์œผ๋กœ ์ €์žฅํ•œ๋‹ค๋Š” ๊ฒƒ์€ ํ•˜๋‚˜์˜ ์ŠคํŠธ๋ง์„ ๋˜๋‹ค๋ฅธ ์ŠคํŠธ๋ง์— ๋งคํ•‘ํ•œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ์ŠคํŠธ๋ง ๋ฐ์ดํƒ€ ํƒ€์ž…์€ ์ •๋ง ๋งŽ์€ ์šฉ๋„๋กœ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ๋“ค์–ด HTML ์ผ๋ถ€๋‚˜ ํŽ˜์ด์ง€ ์ „์ฒด๋ฅผ ์บ์‹ฑํ•  ์ˆ˜๋„ ์žˆ์ฃ .

์ŠคํŠธ๋ง ํƒ€์ž…์„ ๋‹ค๋ฃจ๋Š” ์˜ˆ์ œ๋ฅผ ํ•œ๋ฒˆ ๋ณด๋ฉด์„œ ๋” ์•Œ์•„๋ณด๋„๋ก ํ•˜์ฃ . 

๋ ˆ๋””์Šค ํ”„๋กฌํ”„ํŠธ ์ฐฝ์—์„œ ์•„๋ž˜์ฒ˜๋Ÿผ ์ž…๋ ฅํ•ด๋ณด์„ธ์š”.

> set mykey somevalue
OK
> get mykey
"somevalue"

์œ„ ์˜ˆ์ œ๋Š” SET ๋ช…๋ น์–ด๋ฅผ ์ด์šฉํ•˜์—ฌ mykey๋ผ๋Š” ํ‚ค์— somevalue๋ผ๋Š” ๊ฐ’์„ ๋งคํ•‘์‹œํ‚ต๋‹ˆ๋‹ค.  ์—ฌ๊ธฐ์„œ ์ค‘์š”ํ•œ ์ ์€ SET ๋ช…๋ น์–ด๋Š”  ๋™์ผํ•œ ํ‚ค๊ฐ€ ์ด๋ฏธ ์กด์žฌํ•œ๋‹ค๋ฉด ๊ทธ ํ‚ค์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’์„ ๋ฎ์–ด์“ด๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ ๊ฐ’์ด ์–ด๋–ค ํƒ€์ž…์ด๋ƒ๋Š” ์ƒ๊ด€์—†์ด ๋ฌด์กฐ๊ฑด ๋ฎ์–ด์“ฐ๋‹ˆ ์ฃผ์˜ํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. ๋‹ค์‹œ๋งํ•˜๋ฉด SET ๋ช…๋ น์–ด๋Š” ์ž๋ฐ” ํ”„๋กœ๊ทธ๋ž˜๋ฐ์˜ = ๊ณผ ๊ฐ™์ด ํŠน์ • ํ‚ค์— ํŠน์ • ๊ฐ’์„ ํ• ๋‹นํ•ด์ฃผ๋Š” ๊ธฐ๋Šฅ์„ ํ•ฉ๋‹ˆ๋‹ค.

ํ‚ค์— ํ• ๋‹นํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ’์€ ์–ด๋– ํ•œ ์ŠคํŠธ๋ง์ด๋ผ๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๋ฐ”์ด๋„ˆ๋ฆฌ ๋ฐ์ดํƒ€ ์—ญ์‹œ ๊ฐ€๋Šฅํ•˜์ฃ . JPEG ์ด๋ฏธ์ง€์˜ ๋ฐ”์ด๋„ˆ๋ฆฌ ๋ฐ์ดํƒ€๋„ ์ €์žฅ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์—ฌ๊ธฐ์„œ ๋˜ ์ค‘์š”ํ•œ ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์–ด๋–ค ๊ฐ’์ด๋“  ๊ทธ ํฌ๊ธฐ๊ฐ€ 512MB๋ฅผ ๋„˜์„ ์ˆ˜๋Š” ์—†๋‹ค๋Š” ๊ฒƒ์ด์ฃ .

SET ๋ช…๋ น์–ด์— ์˜ต์…˜์„ ์ค„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ๋“ค์–ด ์ด๋ฏธ ํ•ด๋‹น ํ‚ค๊ฐ€ ์กด์žฌํ•˜๋ฉด ์‹คํŒจํ•˜๋„๋ก ํ•  ์ˆ˜๋„ ์žˆ๊ณ  ๊ทธ ๋ฐ˜๋Œ€๋กœ ์ด๋ฏธ ํ‚ค๊ฐ€ ์กด์žฌํ•  ๋•Œ์—๋งŒ SETํ•˜๋„๋ก ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์ด ๋ง์ด์ฃ .

> set mykey newval nx
(nil)
> set mykey newval xx
OK

์ŠคํŠธ๋ง์ด ๋ ˆ๋””์Šค์˜ ์ •๋ง ๊ธฐ๋ณธ์ ์ธ ๊ฐ’์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฐ์ดํ„ฐ ํ˜•์‹์ด์ง€๋งŒ ์—ฌ๋Ÿฌ ์‹ค์šฉ์ ์ธ ๊ธฐ๋Šฅ๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. atomic increment ๊ฐ€ ๊ทธ ํ•œ ์˜ˆ์ž…๋‹ˆ๋‹ค. atomic increment๊ฐ€ ๋ญ”์ง€ ์ž˜ ๋ชจ๋ฅด์‹ ๋‹ค๋ฉด atomic์ด ๋ญ˜ ์˜๋ฏธํ•˜๋Š”์ง€ ์ฐพ์•„๋ณด์‹œ๋ฉด ๋ ๊ฒ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๊ทธ์— ๋Œ€ํ•œ ์„ค๋ช…์€ ํ•˜์ง€ ์•Š๊ฒ ์Šต๋‹ˆ๋‹ค.

> set counter 100
OK
> incr counter
(integer) 101
> incr counter
(integer) 102
> incrby counter 50
(integer) 152

 INCR ๋ช…๋ น์–ด๋Š” ์ŠคํŠธ๋ง์œผ๋กœ ์ €์žฅ๋œ ๊ฐ’์„ ์ˆซ์žํ˜•ํƒœ๋กœ ๋ณ€ํ™˜ํ•œ ๋’ค 1์„ ์ฆ๊ฐ€์‹œํ‚ต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ทธ๋ ‡๊ฒŒ ์ฆ๊ฐ€๋œ ๊ฐ’์„ ๋‹ค์‹œ ์ŠคํŠธ๋ง ํ˜•ํƒœ๋กœ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ์ด์™€ ๋น„์Šทํ•˜๊ฒŒ INCRBYDECR ๊ทธ๋ฆฌ๊ณ  DECRBY ์™€ ๊ฐ™์€ ๋ช…๋ น์–ด๋“ค์„ ๋ ˆ๋””์Šค๋Š” ์ œ๊ณตํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ช…๋ น์–ด๋“ค์€ ๋‚ด๋ถ€์ ์œผ๋กœ ๋ชจ๋‘ ๋™์ผํ•˜๊ฒŒ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค. ์•„์ฃผ ๋ฏธ๋ฌ˜ํ•œ ์ฐจ์ด๋งŒ ์žˆ์„ ๋ฟ์ด์ฃ .

INCR ๊ฐ€ atomic์ด๋ผ๋Š”๊ฒŒ ๋ฌด์Šจ ์˜๋ฏธ๋ƒ๊ตฌ์š”? ์—ฌ๋Ÿฌ๋ช…์˜ ์‚ฌ์šฉ์ž๊ฐ€ ๋™์ผํ•œ ํ‚ค์— ๋Œ€ํ•ด์„œ INCR๋ช…๋ น์–ด๋ฅผ ์š”์ฒญํ•œ๋‹ค๊ณ  ํ–ˆ์„ ๋•Œ, ์š”์ฒญํ•œ ์‚ฌ์šฉ์ž๋“ค์ด ์ ˆ๋Œ€๋กœ race condition์— ๋น ์งˆ ์ˆ˜ ์—†๋‹ค๋ผ๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ์ฆ‰, ์‚ฌ์šฉ์ž 1 ์ด 10์ด๋ผ๋Š” ๊ฐ’์„ ์ฝ๊ณ , ์‚ฌ์šฉ์ž 2 ์—ญ์‹œ ๋™์ผํ•œ ํ‚ค์— ๋Œ€ํ•ด์„œ 10์ด๋ผ๋Š” ๊ฐ’์„ ์ฝ์€ ๋’ค ๋‘ ์‚ฌ์šฉ์ž๊ฐ€ 1์”ฉ ๊ฐ’์„ ์ฆ๊ฐ€์‹œ์ผฐ์„ ๋•Œ ์ตœ์ข… ๊ฒฐ๊ณผ๊ฐ€ 11์ด ๋  ์ˆ˜ ์—†๋‹ค๋Š” ๊ฒƒ์ด์ฃ . ์ตœ์ข… ๊ฒฐ๊ณผ๋Š” ํ•ญ์ƒ ๋ฌด์กฐ๊ฑด ์ ˆ๋Œ€๋กœ 12 ๊ฐ€ ๋œ๋‹ค๋Š” ๊ฒƒ์„ ๋ ˆ๋””์Šค๋Š” ๋ณด์žฅํ•ด์ฃผ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์‰ฝ๊ฒŒ๋งํ•˜๋ฉด ๊ฐ’์„ ์ฝ๊ณ , 1 ์ฆ๊ฐ€ํ•˜๊ณ , ๋‹ค์‹œ ์ €์žฅํ•˜๋Š” ๊ณผ์ •์ด ๋ชจ๋‘ ํ•œ๋ช…์˜ ์‚ฌ์šฉ์ž๊ฐ€ ์š”์ฒญํ–ˆ์„ ๋•Œ ์ด๋ฃจ์–ด์ง€๊ณ  ๊ทธ ์‚ฌ์ด์— ๋‹ค๋ฅธ ์‚ฌ์šฉ์ž๊ฐ€ ๋ผ์–ด๋“ค ์ˆ˜ ์—†๋‹ค๋Š” ๋ง์ด์ฃ .

์ด ์™ธ์—๋„ ์ŠคํŠธ๋ง์„ ๋‹ค๋ฃจ๊ธฐ ์œ„ํ•œ ์—ฌ๋Ÿฌ ๋ช…๋ น์–ด๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค.  GETSET ๋ช…๋ น์–ด๋Š” ํ‚ค์— ์กด์žฌํ•˜๋Š” ๊ฐ’์„ ์ฝ๊ณ  ์ƒˆ๋กœ์šด ๊ฐ’์„ ์„ธํŒ…ํ•œ๋’ค ์ฝ์–ด๋“ค์ธ ๊ธฐ์กด๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ช…๋ น์–ด๋Š” ๋ฐฉ๋ฌธ์ž์ˆ˜๋ฅผ ๊ธฐ๋กํ•  ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒ ์ฃ . ์ผ๋ณ„๋กœ ๋ฐฉ๋ฌธ์ž์ˆ˜๋ฅผ ๊ธฐ๋ก์„ ํ•ด์•ผํ•œ๋‹ค๋ฉด ์ž์ •์ด ๋˜์—ˆ์„ ๋•Œ GETSET ๋ช…๋ น์–ด๋กœ ์˜ค๋Š˜ ๋ฐฉ๋ฌธ์ž์ˆ˜๋ฅผ ์ฝ์–ด์˜ค๊ณ  0์œผ๋กœ ์„ธํŒ…ํ•ด์ฃผ๋ฉด ๋˜๋Š”๊ฑฐ์ฃ .

์—ฌ๋Ÿฌ๊ฐœ์˜ ํ‚ค-๊ฐ’ ํŽ˜์–ด๋ฅผ ์ €์žฅํ•˜๊ฑฐ๋‚˜ ์กฐํšŒํ•˜๊ณ  ์‹ถ์„ ๋•Œ์—๋Š” MSET ์™€ MGET ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

> mset a 10 b 20 c 30
OK
> mget a b c
1) "10"
2) "20"
3) "30" 

MSET์€ ํ•œ๋ฒˆ์— ์—ฌ๋Ÿฌ ํ‚ค-๊ฐ’ ํŽ˜์–ด๋ฅผ ์ €์žฅํ•˜๋Š” ๋ช…๋ น์–ด์ด๊ณ 

 MGET ์€ ๊ฐ’์˜ ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

Altering and querying the key space ( ํ‚ค ๋ณ€๊ฒฝ, ์กฐํšŒ, ์‚ญ์ œ ํ•˜๊ธฐ )

๋ ˆ๋””์Šค๋Š” ํ‚ค์™€ ๊ด€๋ จ๋œ ์—ฌ๋Ÿฌ ๋ช…๋ น์–ด๋“ค์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ํ‚ค ์ด๋ฆ„ ๋ณ€๊ฒฝ( ์ด๊ฒƒ์€ ์œ„์—์„œ ๋ณด์…จ์ฃ ? ), ํ‚ค ๋ชฉ๋ก ์กฐํšŒ ๋ฐ ์กด์žฌ์—ฌ๋ถ€ ํŒŒ์•…, ๋˜๋Š” ์‚ญ์ œํ•˜๊ธฐ์™€ ๊ฐ™์€ ๋ช…๋ น์–ด๋“ค์„ ๋ง์ด์ฃ .

 EXISTS ๋ช…๋ น์–ด๋Š” 1 ๋˜๋Š” 0 ์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ํ‚ค๊ฐ€ ์กด์žฌํ•˜๊ฑฐ๋‚˜ ์กด์žฌํ•˜์ง€ ์•Š์„๋•Œ ๋ง์ด์ฃ .

๊ทธ๋ฆฌ๊ณ  DEL ๋ช…๋ น์–ด๋Š” ํ•ด๋‹น ํ‚ค์™€ ๊ทธ ํ‚ค์— ํ• ๋‹น๋œ ๊ฐ’์„ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.

> set mykey hello
OK
> exists mykey
(integer) 1
> del mykey
(integer) 1
> exists mykey
(integer) 0

์œ„ ์˜ˆ์ œ๋ฅผ ๋ณด์‹œ๋ฉด ๊ฐ ๋ช…๋ น์–ด๊ฐ€ ์–ด๋–ป๊ฒŒ ๋™์ž‘ํ•˜๋Š”์ง€ ์‰ฝ๊ฒŒ ์ดํ•ด๊ฐ€ ๋˜์‹ค๊ฒ๋‹ˆ๋‹ค.

์ด ๋ช…๋ น์–ด๋“ค ์™ธ์—๋„ ๋ฌด์ˆ˜ํžˆ ๋งŽ์€ key space ๊ด€๋ จ ๋ช…๋ น์–ด๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„ ๋‘ ๋ช…๋ น์–ด์™€ ํ•จ๊ป˜ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ๋ช…๋ น์–ด ์ค‘ ํ•˜๋‚˜๊ฐ€ ๋ฐ”๋กœ  TYPE ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. ์ด ๋ช…๋ น์–ด๋Š” ํŠน์ • ํ‚ค์— ์ €์žฅ๋˜์–ด์žˆ๋Š” ๊ฐ’์˜ ํƒ€์ž…์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜ ์˜ˆ์ œ๋ฅผ ๋ณด์‹œ์ฃ .

> set mykey x
OK
> type mykey
string
> del mykey
(integer) 1
> type mykey
none

์ฒ˜์Œ์— set mykey x ๋กœ ํ‚ค mykey์— ๊ฐ’ x๋ฅผ ๋“ฑ๋กํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  type mykey ๋กœ mykey์— ์ €์žฅ๋œ ๊ฐ’ x์˜ ํƒ€์ž…์„ ์กฐํšŒํ•ด ๋ณด๋‹ˆ string์ด ๋‚˜์™”๋„ค์š”. ๊ทธ ๋‹ค์Œ์—” mykey๋ฅผ ์‚ญ์ œํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‹ค์‹œ ์กฐํšŒ๋ฅผ ํ•ด๋ณด๋‹ˆ none ์ด๋ผ๊ณ  ๋‚˜์™”๋„ค์š”. ํ•ด๋‹น ํ‚ค์— ์ €์žฅ๋œ ๊ฐ’์ด ์—†๋‹ค๋Š” ์–˜๊ธฐ๊ฒ ์ฃ .

Redis ํ‚ค์— TTL (Time To Live)์„ค์ •ํ•˜๊ธฐ 

์ด๊ฑด ๋‹จ์ง€ ์ŠคํŠธ๋ง ํƒ€์ž… ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋ชจ๋“  ํƒ€์ž…์— ์ ์šฉ๋˜๋Š” ๊ฒƒ์ด๊ธด ํ•ฉ๋‹ˆ๋‹ค๋งŒ ๋‹ค๋ฅธ ํƒ€์ž…์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์–˜๊ธฐํ•˜๊ธฐ ์ „์— ์งš๊ณ  ๋„˜์–ด๊ฐ€์•ผ ํ•  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ ํ‚ค์— ํƒ€์ž„์•„์›ƒ ์‹œ๊ฐ„์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์„ค์ •๋œ ํƒ€์ž„์•„์›ƒ ์‹œ๊ฐ„์ด ์ง€๋‚˜๋ฉด ์ž๋™์œผ๋กœ ํ‚ค๋Š” ์‚ญ์ œ๋ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ์‚ญ์ œ๋˜๋Š”๊ฒƒ์€ ์‚ฌ์šฉ์ž๊ฐ€ ์ง์ ‘ DEL ๋ช…๋ น์–ด๋กœ ์‚ญ์ œํ•˜๋Š” ๊ฒƒ๊ณผ ์™„์ „ํžˆ ๋™์ผํ•˜๊ฒŒ ์‚ญ์ œ๊ฐ€ ๋˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 Redis expires ์™€ ๊ด€๋ จํ•˜์—ฌ ์•„๋ž˜ ๋‚ด์šฉ์„ ์ˆ™์ง€ํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค.

  • ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ฐ€๋ฆฌ์ดˆ ๋‹จ์œ„๋กœ ๋™์ž‘ํ•œ๋‹ค.
  • ์ดˆ ๋‹จ์œ„ ๋˜๋Š” ๋ฐ€๋ฆฌ ์ดˆ ๋‹จ์œ„๋กœ ๋งŒ๋ฃŒ๋˜๊ธฐ ๊นŒ์ง€์˜ ์‹œ๊ฐ„์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๋งŒ๋ฃŒ์ผ์‹œ์™€ ๊ด€๋ จ๋œ ์ •๋ณด๋Š” ๋””์Šคํฌ์— ๋ณต์ œ๋˜์–ด ์ €์žฅ๋˜๊ณ  ๋ ˆ๋””์Šค ์„œ๋ฒ„๊ฐ€ ๋ฉˆ์ถฐ๋„ ์žฌ๊ธฐ๋™ ๋ ๋•Œ ํ˜„์žฌ ์ผ์‹œ์™€ ๋น„๊ตํ•˜์—ฌ ์‚ญ์ œํ•œ๋‹ค.

๋งŒ๋ฃŒ์ผ์‹œ๋ฅผ ์ง€์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์•„์ฃผ ์‰ฝ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜ ์˜ˆ์ œ๋ฅผ ํ•œ๋ฒˆ ๋ณด์‹œ์ฃ .

> set key some-value
OK
> expire key 5
(integer) 1
> get key (์œ„ ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•˜์ž๋งˆ์ž ๋ฐ”๋กœ ์‹คํ–‰)
"some-value"
> get key (5์ดˆ ๋’ค์— ์‹คํ–‰)
(nil)

์œ„ ์˜ˆ์ œ๋Š” ์–ด๋–ค ๊ฐ’์„ ์ €์žฅํ•œ ๋’ค 5์ดˆ๋กœ ๋งŒ๋ฃŒ์‹œ๊ฐ„์„ ์ง€์ •ํ•ด์ฃผ๊ณ  ๋‘ ๋ฒˆ์˜ GET ์„ ํ˜ธ์ถœํ•  ๋•Œ ๋งŒ๋ฃŒ์‹œ๊ฐ„์— ๋”ฐ๋ผ ๊ฐ’์ด ์กฐํšŒ๊ฐ€ ๋˜๋Š”์ง€ ์•ˆ๋˜๋Š”์ง€๋ฅผ ํ™•์ธํ•ด๋ณธ ๊ฒƒ์ž…๋‹ˆ๋‹ค. 5์ดˆ ๋’ค์— ๋งŒ๋ฃŒ๋˜๋ผ๊ณ  ์ง€์ •ํ•ด์ฃผ์—ˆ์œผ๋‹ˆ 5์ดˆ ๋’ค์—๋Š” nil์ด๋ผ๊ณ  ๋œจ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. 

์—ฌ๊ธฐ์„œ๋Š” EXPIRE ๋ช…๋ น์–ด๋ฅผ ์ด์šฉํ•ด์„œ ๋งŒ๋ฃŒ์ผ์‹œ๋ฅผ ์ง€์ •ํ•ด์ฃผ์—ˆ๋Š”๋ฐ ๋งŒ์•ฝ์— ์ด ๋งŒ๋ฃŒ์ผ์‹œ๋ฅผ ์—†์• ๊ธฐ ์œ„ํ•ด์„œ๋Š” PERSIST ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  EXPIRE๋ช…๋ น๊ณผ ๋™์ผํ•œ ๊ธฐ๋Šฅ์„ SET ๋ช…๋ น์–ด์˜ ์˜ต์…˜์„ ์ด์šฉํ•ด์„œ ์ ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

> set key 100 ex 10
OK
> ttl key
(integer) 9

์œ„ ์˜ˆ์ œ๋Š” ํ‚ค key์— ๊ฐ’ 100์„ ์ €์žฅํ•˜๋Š”๋ฐ 10์ดˆ ํ›„์— ๋งŒ๋ฃŒ๋˜์–ด ํŒŒ๊ธฐํ•˜๋ผ๋Š” ์˜ต์…˜์„ ๋ง๋ถ™์˜€์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ ๋‚˜์„œ  TTL ๋ช…๋ น์–ด๋กœ ํ‚ค key์˜ ๋งŒ๋ฃŒ๋˜๊ธฐ ์ „๊นŒ์ง€์˜ ์‹œ๊ฐ„(์ดˆ)์„ ์กฐํšŒ๋ฅผ ํ•ด๋ณด๋‹ˆ 9๊ฐ€ ๋‚˜์™”๋„ค์š”. (์ฐธ๊ณ ๋กœ TTL์€ Time To Live ์˜ ์•ฝ์ž์ž…๋‹ˆ๋‹ค.)

์ดˆ ๋‹จ์œ„๊ฐ€ ์•„๋‹Œ ๋ฐ€๋ฆฌ์ดˆ ๋‹จ์œ„๋กœ ์„ค์ •์„ ํ•˜๋ ค๋ฉด, PEXPIRE ์™€ PTTL ๋ช…๋ น์–ด๋ฅผ ์ฐธ๊ณ ํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋˜๋Š” PEXPIRE๋Œ€์‹  SET ๋ช…๋ น์–ด์˜ ์˜ต์…˜์„ ์ด์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.



์œ„ ๋‚ด์šฉ์€ ๋ ˆ๋””์Šค ๊ณต์‹ ๋ฌธ์„œ์— ๋‚˜์™€์žˆ๋Š” ๋‚ด์šฉ์„ ์Šคํ„ฐ๋”” ํ•  ๊ฒธ ๊ฐœ์ธ์ ์œผ๋กœ ๋ฒˆ์—ญํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค. 


Redis keys

Redis ์—์„œ ์‚ฌ์šฉํ•˜๋Š” key๋Š” binary safeํ•ฉ๋‹ˆ๋‹ค. 

๋ฐ”์ด๋„ˆ๋ฆฌ ์„ธ์ดํ”„ ํ•˜๋‹ค๋Š” ๋ง์€ ํ‚ค ์ด๋ฆ„์œผ๋กœ ์–ด๋– ํ•œ ๋ฐ”์ด๋„ˆ๋ฆฌ ์‹œํ€€์Šค๋ฅผ ์‚ฌ์šฉํ•ด๋„ ๋ฌด๋ฐฉํ•˜๋‹ค๋Š” ๋ง์ž…๋‹ˆ๋‹ค. 

์˜ˆ๋ฅผ๋“ค์–ด JPEGํŒŒ์ผ์„ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ ํ‚ค๋กœ "foo"๋ฅผ ์‚ฌ์šฉํ•ด๋„ ๋ฉ๋‹ˆ๋‹ค.

๋˜ํ•œ ๋นˆ ์ŠคํŠธ๋ง๋„ ์œ ํšจํ•œ ํ‚ค๋กœ ์ธ์‹ํ•ฉ๋‹ˆ๋‹ค.


ํ‚ค์™€ ๊ด€๋ จ๋œ ๋˜ ๋‹ค๋ฅธ ๊ทœ์น™์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค:

  • ๋„ˆ๋ฌด ๊ธด ์ด๋ฆ„์„ ๊ฐ€์ง„ ํ‚ค๋Š” ์ข‹์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ๋“ค์–ด 1024 ๋ฐ”์ดํŠธ ์งœ๋ฆฌ ํ‚ค๋Š” ๋งค์šฐ ์•ˆ์ข‹์€ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์™œ๋ƒ๋ฉด ๋ฉ”๋ชจ๋ฆฌ๋„ ๋งŽ์ด ์žก์•„๋จน์„ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ํ‚ค๋ฅผ ์ฐพ๋Š”๋ฐ๋„ ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ฆด ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋ฉ”๋ชจ๋ฆฌ ์ธก๋ฉด์ด๋‚˜ bandwidth ์ธก๋ฉด์—์„œ ๋ดค์„ ๋•Œ ๊ธด ํ‚ค๊ฐ’์ด ํ•ด๋‹น ํ‚ค์™€ ๊ด€๋ จ๋œ ๊ฐ’์ด๋‚˜ ์„œ๋น„์Šค์™€ ๊ด€๋ จ๋œ ์ข‹์€ ์ด๋ฆ„์ด๋ผ ํ• ์ง€๋ผ๋„ SHA1 ๋“ฑ์˜ ํ•ด์‹œ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ†ตํ•ด์„œ ๊ธธ์ด๋ฅผ ์ค„์—ฌ์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ์ถ”์ฒœํ•ฉ๋‹ˆ๋‹ค.
  • ๋„ˆ๋ฌด ์งง์€ ํ‚ค ์—ญ์‹œ ์ข‹์ง€ ์•Š์Šต๋‹ˆ๋‹ค. "user:1000:followers"๋ผ๋Š” ํ‚ค ๋Œ€์‹ ์— "u1000flw" ๋ผ๊ณ  ํ‚ค๋ฅผ ์ •ํ•˜๋ฉด ๋‚˜์ค‘์— ํ‚ค๋งŒ๋ณด๊ณ  ์ด๊ฒƒ์ด ๋ฌด์—‡์— ๊ด€๋ จ๋œ ํ‚ค์ธ์ง€ ํŒŒ์•…ํ•˜๊ธฐ๊ฐ€ ์‰ฝ์ง€ ์•Š๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ฐ€๋…์„ฑ์ด ๋–จ์–ด์ง€๋Š” ๊ฒƒ์ด์ฃ . ์งง์€ ํ‚ค๊ฐ€ ์•„๋ฌด๋ฆฌ ๋” ์ ์€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ• ์ง€๋ผ๋„ ํฐ ์ฐจ์ด๊ฐ€ ๋‚˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ์ ๋‹นํžˆ ๊ธธ์ด๋ฅผ ๋Š˜๋ ค์„œ ๊ฐ€๋…์„ฑ์žˆ๋Š” ํ‚ค๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ๋” ์ข‹์Šต๋‹ˆ๋‹ค.
  • ์Šคํ‚ค๋งˆ์— ์ง‘์ค‘ํ•˜์„ธ์š”. ์˜ˆ๋ฅผ๋“ค์–ด, "user:1000"์ฒ˜๋Ÿผ "object-type:id" ์‹์˜ ํ‚ค ๋ช…๋ช…๊ทœ์น™์€ ์ข‹์€ ์˜ˆ ์ž…๋‹ˆ๋‹ค. ๋งˆ์นจํ‘œ(.)๋‚˜ ๋Œ€์‰ฌ(-)๋ฅผ ์ด์šฉํ•˜์—ฌ ์—ฌ๋Ÿฌ ๋‹จ์–ด๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฒƒ๋„ ์ข‹์Šต๋‹ˆ๋‹ค. "comment:1234:reply.to" ๋˜๋Š” "comment:1234:reply-to" ์ฒ˜๋Ÿผ ๋ง์ด์ฃ .
  • ํ‚ค ํ•˜๋‚˜๋ฅผ ์ €์žฅํ•˜ํ•  ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ์‚ฌ์ด์ฆˆ๋Š” 512 MB ์ž…๋‹ˆ๋‹ค. ์„ค๋งˆ ํ‚ค ์ €์žฅํ•˜๋Š”๋ฐ ์ด๋ ‡๊ฒŒ ๋งŽ์€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•  ์ผ์€ ์—†๊ฒ ์ฃ ?


์ž ๊ทธ๋Ÿฌ๋ฉด ์ด์ œ Redis ์—์„œ ์ œ๊ณตํ•˜๋Š” ํ‚ค ๊ด€๋ จ ๋ช…๋ น์–ด์˜ ์‚ฌ์šฉ๋ฒ• ๋ฐ ์ข…๋ฅ˜์— ๋Œ€ํ•ด์„œ ๊ฐ„๋‹จํ•˜๊ฒŒ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

์šฐ์„  ๋ฌธ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

Syntax (๋ฌธ๋ฒ•)

redis 127.0.0.1:6379> {๋ช…๋ น์–ด} {ํ‚ค}

{๋ช…๋ น์–ด} ์ž๋ฆฌ์—๋Š” ํ‚ค ๊ด€๋ จ ๋ช…๋ น์–ด๊ฐ€ ๋“ค์–ด๊ฐ€๋ฉด๋ฉ๋‹ˆ๋‹ค. ์•„๋ž˜ ์˜ˆ์ œ๋ฅผ ๊ฐ™์ด ์‚ดํŽด๋ณผ๊นŒ์š”?

Example (์˜ˆ์ œ)

redis 127.0.0.1:6379> SET study:redis redis OK redis 127.0.0.1:6379> DEL study:redis (integer) 1

์œ„ ์˜ˆ์ œ์—์„œ DEL, SET ์€ ๋ช…๋ น์–ด ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  study:redis ๊ฐ€ ๋ฐ”๋กœ key์ž…๋‹ˆ๋‹ค. 

์ฒ˜์Œ์—๋Š” SET ๋ช…๋ น์–ด๋กœ study:redis๋ผ๋Š” ํ‚ค์— redis๋ผ๋Š” ๊ฐ’์„ ์ž…๋ ฅ์„ ํ–ˆ์Šต๋‹ˆ๋‹ค. 

๊ทธ๋ฆฌ๊ณ ๋‚˜์„œ DEL๋ช…๋ น์–ด๋กœ ํ•ด๋‹น ํ‚ค๋ฅผ ์‚ญ์ œ๋ฅผ ํ–ˆ์Šต๋‹ˆ๋‹ค. ํ‚ค๋ฅผ ์‚ญ์ œํ•˜๋ฉด ํ‚ค์— ๋งคํ•‘๋˜์–ด์žˆ๋Š” ๊ฐ’์— ์ ‘๊ทผํ•  ๋ฐฉ๋ฒ•์ด ์—†์–ด์ง€๊ฒ ์ฃ . 

ํ‚ค๋ฅผ ์‚ญ์ œํ•  ๋•Œ ํ‚ค๊ฐ€ ์กด์žฌํ•˜๊ณ  ์ •์ƒ์ ์œผ๋กœ ์‚ญ์ œ๊ฐ€ ๋˜์—ˆ๋‹ค๋ฉด (integer) 1 ๊ฐ€ ์ถœ๋ ฅ๋  ๊ฒƒ์ด๊ณ , ์•„๋‹ˆ๋ฉด (integer) 0 ๊ฐ€ ์ถœ๋ ฅ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค.

Redis keys commands ( ๋ ˆ๋””์Šค ํ‚ค ๋ช…๋ น์–ด )

์•„๋ž˜ ํ…Œ์ด๋ธ”์€ ํ‚ค์™€ ๊ด€๋ จ๋œ ๊ธฐ๋ณธ์ ์ธ ๋ช…๋ น์–ด๋ฅผ ๋ช‡ ๊ฐœ๋งŒ ๊ฐ„์ถ”๋ ค ๋ณธ ๊ฒƒ์ž…๋‹ˆ๋‹ค.:

No.

๋ช…๋ น์–ด ๋ฐ ์„ค๋ช…

1DEL key
์ด ๋ช…๋ น์–ด๋Š” ํ‚ค๊ฐ€ ์กด์žฌํ•œ๋‹ค๋ฉด ์‚ญ์ œ๋ฅผ ํ•ฉ๋‹ˆ๋‹ค
2

DUMP key 
ํ•ด๋‹น ํ‚ค์— ์ €์žฅ๋œ ๊ฐ’์˜ serialized version์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

3

EXISTS key 
ํ‚ค๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

4

EXPIRE key seconds
์ง€์ •ํ•œ ์‹œ๊ฐ„(์ดˆ)๊ฐ€ ์ง€๋‚˜๋ฉด ํ•ด๋‹น ํ‚ค๋ฅผ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ๋„๋ก ๋งŒ๋ฃŒ๊ธฐ๊ฐ„์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.

5

EXPIREAT key timestamp 
ํŠน์ •ํ•œ ์‹œ๊ฐ„์— ํ‚ค๊ฐ€ ์‚ญ์ œ๋  ์ˆ˜ ์žˆ๋„๋ก Unix timestamp ํฌ๋งท์œผ๋กœ ํŠน์ • ์‹œ๊ฐ์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.

6

PEXPIRE key milliseconds 
ํ‚ค์˜ ๋งŒ๋ฃŒ๊ธฐ๊ฐ„์„ ๋ฐ€๋ฆฌ์ดˆ ๋‹จ์œ„๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

7

PEXPIREAT key milliseconds-timestamp 
ํ‚ค์˜ ๋งŒ๋ฃŒ ์‹œ๊ฐ์„ ์œ ๋‹‰์Šค ๋ฐ€๋ฆฌ์ดˆ ํƒ€์ž„์Šคํƒฌํ”„ ํฌ๋งท์œผ๋กœ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.

8

KEYS pattern 
ํŠน์ • ํŒจํ„ด๊ณผ ์ผ์น˜ํ•˜๋Š” ๋ชจ๋“  ํ‚ค ๋ชฉ๋ก์„ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.

9

MOVE key db 
ํ•ด๋‹น ํ‚ค๋ฅผ ๋‹ค๋ฅธ db๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค.

10

PERSIST key 
ํ‚ค์— ์„ค์ •๋œ ๋งŒ๋ฃŒ์ผ์‹œ๋ฅผ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.

11

PTTL key 
ํ•ด๋‹น ํ‚ค๊ฐ€ ๋งŒ๋ฃŒ๋˜๊ธฐ๊นŒ์ง€์˜ ์‹œ๊ฐ„์„ ๋ฐ€๋ฆฌ์ดˆ ๋‹จ์œ„๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

12

TTL key 
ํ•ด๋‹น ํ‚ค๊ฐ€ ๋งŒ๋ฃŒ๋˜๊ธฐ ๊นŒ์ง€์˜ ์‹œ๊ฐ„์„ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.

13RANDOMKEY 
๋žœ๋คํ‚ค ํ•˜๋‚˜๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
14

RENAME key newkey 
ํ‚ค ์ด๋ฆ„์„ ์ƒˆ๋กœ์šด ํ‚ค๋กœ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค.

15RENAMENX key newkey 
ํ‚ค ์ด๋ฆ„์„ ์ƒˆ๋กœ์šด ํ‚ค๋กœ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค. (๋‹จ, ์ƒˆ๋กœ์šด ํ‚ค์™€ ๋™์ผํ•œ ์ด๋ฆ„์˜ ํ‚ค๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ์—๋งŒ)
16

TYPE key 
ํ‚ค์— ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ์˜ ํƒ€์ž…์„ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.


์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” ๊ฐ„๋žตํ•˜๊ฒŒ ๋ ˆ๋””์Šค์— ์–ด๋– ํ•œ ํƒ€์ž…์ด ์žˆ๊ณ  ์–ด๋–ค ๋ช…๋ น์–ด๋กœ ํ•ด๋‹น ํƒ€์ž…์˜ ๊ฐ’์„ ์ถ”๊ฐ€ ๋˜๋Š” ์กฐํšŒํ•˜๋Š” ์ง€ ๋“ฑ์˜ ์˜ˆ์ œ๋ฅผ ๋ณด์—ฌ๋“œ๋ฆด ๊ฒ๋‹ˆ๋‹ค.

๋ ˆ๋””์Šค๋Š” String, Hash, List, Set, Sorted Set ๋“ฑ์˜ ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค.


Strings (์ŠคํŠธ๋ง)

๋ ˆ๋””์Šค ์ŠคํŠธ๋ง์€ ์ˆœ์ฐจ์ ์ธ ๋ฐ”์ดํŠธ๋“ค์ž…๋‹ˆ๋‹ค. ๋ ˆ๋””์Šค์—์„œ ์ŠคํŠธ๋ง์€ binary safeํ•œ๋ฐ ์ด๊ฒƒ์€ ์ŠคํŠธ๋ง์ด ์•Œ์ˆ˜์žˆ๋Š” ๊ธธ์ด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๊ณ  ์–ด๋–ค ํŠน๋ณ„ํ•œ ์ข…๋ฃŒ๋ฌธ์ž์— ์˜ํ•ด ์ข…๋ฃŒ๋˜์ง€ ์•Š๋Š” ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ, ํ•œ ์ŠคํŠธ๋ง๋‹น ์ตœ๋Œ€ 512MB ๊ธธ์ด์˜ ๊ฐ’์„ ์ €์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Example (์˜ˆ์ œ)

redis 127.0.0.1:6379> SET name "keichee" OK redis 127.0.0.1:6379> GET name "keichee"

์œ„ ์˜ˆ์ œ์—์„œ ์‚ฌ์šฉ๋œ SET๊ณผ GET๋ช…๋ น์–ด๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ฑฐ๋‚˜ ์กฐํšŒํ•  ๋•Œ ์‚ฌ์šฉ๋˜๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  name์€ ๋ ˆ๋””์Šค์— ์ €์žฅํ•  ๋•Œ ํ‚ค ๊ฐ’์„ ์˜๋ฏธํ•˜๋ฉฐ "keichee"๋Š” name์ด๋ผ๋Š” ํ‚ค์— ๋Œ€์‘ํ•ด์„œ ์ €์žฅ๋  ๊ฐ’์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.


Hashes (ํ•ด์‰ฌ)

๋ ˆ๋””์Šค ํ•ด์‰ฌ๋Š” ํ‚ค-๊ฐ’ ํŽ˜์–ด์˜ ์ปฌ๋ ‰์…˜์ž…๋‹ˆ๋‹ค. ์ŠคํŠธ๋ง ํ•„๋“œ์™€ ์ŠคํŠธ๋ง ๊ฐ’์„ ๋งคํ•‘ํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ์ฒด๋ฅผ ํ‘œํ˜„ํ•  ๋•Œ ์ž์ฃผ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

Example

redis 127.0.0.1:6379> HMSET user:1 wow awesome keichee handsome blah handsome
OK
redis 127.0.0.1:6379> HGETALL user:1

1) "wow"
2) "awesome"
3) "keichee"
4) "handsome"
5) "blah"
6) "handsome"

์œ„ ์˜ˆ์ œ์—์„œ user:1 ์ด๋ผ๋Š” ํ‚ค์— wow, awesome, keichee, handsome, blah, handsome ์ด๋ผ๋Š” ๊ฐ’์„ ์„ธํŒ…ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ HMSET์€ ํ•ด์‰ฌ์— ์ €์žฅํ•  ๋•Œ, HGETALL ์€ ํ•ด์‰ฌ์— ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•  ๋•Œ ์‚ฌ์šฉ๋˜๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค.

ํ•ด์‰ฌ์— ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ํ‚ค-๊ฐ’ ํŽ˜์–ด์˜ ๊ฐœ์ˆ˜๋Š” ์ด (2^32) - 1 ๊ฐœ ์ž…๋‹ˆ๋‹ค.


Lists (๋ฆฌ์ŠคํŠธ)

๋ฆฌ์ŠคํŠธ๋Š” ๋ชฉ๋กํ˜•ํƒœ์˜ ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ์ŠคํŠธ๋ง ๋ชฉ๋ก์„ ๋งํ•˜๋ฉฐ ์‚ฝ์ž…ํ•œ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌ๋˜์–ด ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. 

๋ ˆ๋””์Šค ๋ฆฌ์ŠคํŠธ๋Š” ์ œ์ผ ์•ž์— ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ๊ณผ ์ œ์ผ ๋งˆ์ง€๋ง‰์— ์ถ”๊ฐ€ํ•˜๋Š” ๋ช…๋ น์–ด๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค..

Example

redis 127.0.0.1:6379> lpush dblist redis
(integer) 1
redis 127.0.0.1:6379> lpush dblist mongodb
(integer) 2 redis 127.0.0.1:6379> lpush dblist rabitmq
(integer) 3 redis 127.0.0.1:6379> lrange dblist 0 10
1) "rabitmq" 2) "mongodb" 3) "redis"

๋ฆฌ์ŠคํŠธ์˜ ์ตœ๋Œ€ ๊ธธ์ด๋Š” (2^32) - 1 ๊ฐœ ์ž…๋‹ˆ๋‹ค. (์ด 4294967295 ๊ฐœ).

lpush๋Š” ๋ฆฌ์ŠคํŠธ์˜ ์ œ์ผ ์•ž์— ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. 

lpush dblist redis ๋ช…๋ น์–ด๋Š” dblist๋ผ๋Š” ์ด๋ฆ„์˜ ๋ฆฌ์ŠคํŠธ์— redis๋ผ๋Š” ๊ฐ’์„ ์ €์žฅํ•˜๋ผ๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

์œ„ ์˜ˆ์ œ์—์„œ๋Š” redis, mongodb, rabitmq๋ฅผ dblist๋ผ๋Š” ๋ฆฌ์ŠคํŠธ์— ์ €์žฅํ•œ ๋’ค lrange ๋ช…๋ น์–ด๋ฅผ ์ด์šฉํ•˜์—ฌ 0๋ถ€ํ„ฐ 10๊นŒ์ง€์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.


Sets (์ง‘ํ•ฉ)

๋ ˆ๋””์Šค์—์„œ ์ง‘ํ•ฉ์€ ์ •๋ ฌ๋˜์ง€ ์•Š์€ ์ŠคํŠธ๋ง ๋ชฉ๋ก์„ ๋งํ•ฉ๋‹ˆ๋‹ค. 

์„ธํŠธ๋Š” ์ถ”๊ฐ€, ์‚ญ์ œ, ๊ทธ๋ฆฌ๊ณ  ์กด์žฌ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜๋Š” ๋ช…๋ น์–ด๋ฅผ O(1) ์‹œ๊ฐ„๋ณต์žก๋„๋กœ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

Example

redis 127.0.0.1:6379> sadd dbset redis
(integer) 1 redis 127.0.0.1:6379> sadd dbset mongodb (integer) 1 redis 127.0.0.1:6379> sadd dbset rabitmq (integer) 1 redis 127.0.0.1:6379> sadd dbset rabitmq (integer) 0 redis 127.0.0.1:6379> smembers dbset 1) "rabitmq" 2) "mongodb" 3) "redis"

NOTE: ์œ„ ์˜ˆ์ œ์—์„œ rabitmq๋Š” ๋‘ ๋ฒˆ ์ถ”๊ฐ€๊ฐ€ ๋˜์—ˆ์ง€๋งŒ ์„ธํŠธ์˜ ๊ธฐ๋ณธ ์†์„ฑ์ธ ์œ ๋‹ˆํฌ ์†์„ฑ ๋•Œ๋ฌธ์— ํ•˜๋‚˜์˜ rabitmq๋งŒ ์ €์žฅ๋ฉ๋‹ˆ๋‹ค.

sadd๋Š” ์ง‘ํ•ฉ์— ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•  ๋•Œ ์“ฐ์ด๋Š” ๋ช…๋ น์–ด์ด๋ฉฐ, ์ง‘ํ•ฉ์„ ์กฐํšŒํ•  ๋•Œ์—๋Š” smembers ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

์กฐํšŒ๋˜๋Š” ๋ฐ์ดํ„ฐ๋Š” ์ง‘ํ•ฉ์— ์ถ”๊ฐ€๋œ ์ˆœ์„œ์™€๋Š” ๊ด€๊ณ„์—†์ด ๋žœ๋คํ•˜๊ฒŒ ์กฐํšŒ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์ฆ‰, ์ˆœ์„œ๋ฅผ ๋ณด์žฅํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

set์— ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐ์ดํ„ฐ ๊ฐœ์ˆ˜ ์—ญ์‹œ (2^32) - 1 ๊ฐœ์ž…๋‹ˆ๋‹ค. 


Sorted Sets (์ •๋ ฌ ์ง‘ํ•ฉ)

์ •๋ ฌ๋œ ์ง‘ํ•ฉ์€ ๋ง๊ทธ๋Œ€๋กœ ์ง‘ํ•ฉ์ธ๋ฐ ์ •๋ ฌ๋œ ๊ฒƒ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ์œ ๋‹ˆํฌ ์†์„ฑ ์—ญ์‹œ ๊ทธ๋Œ€๋กœ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฉฐ. ์ผ๋ฐ˜ ์ง‘ํ•ฉ๊ณผ์˜ ์ฐจ์ด์ ์€ ์ •๋ ฌ์„ ์œ„ํ•œ score ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

Example

redis 127.0.0.1:6379> zadd sorted 0 redis (integer) 1 redis 127.0.0.1:6379> zadd sorted 0 mongodb (integer) 1 redis 127.0.0.1:6379> zadd sorted 0 rabitmq (integer) 1 redis 127.0.0.1:6379> zadd sorted 0 rabitmq (integer) 0 redis 127.0.0.1:6379> ZRANGEBYSCORE sorted 0 1000 1) "redis" 2) "mongodb" 3) "rabitmq"

์œ„ ์˜ˆ์ œ์—์„œ ์ฒ˜๋Ÿผ ์ •๋ ฌ์ง‘ํ•ฉ์— ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์„๋•Œ๋Š” zadd ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•˜๋ฉฐ ์ง‘ํ•ฉ๋ช… ๋‹ค์Œ์— score(์ ์ˆ˜)๋ฅผ ๋ช…์‹œํ•ด์ฃผ๋Š”๋ฐ,


์ด ์ ์ˆ˜๋Š” ๊ฐ ๋ฐ์ดํ„ฐ ๋งˆ๋‹ค ์ค‘๋ณตํ•˜์—ฌ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์œผ๋ฉฐ ์ค‘๋ณต๋œ ์ ์ˆ˜๋ฅผ ๊ฐ–๋Š” ๋ฐ์ดํ„ฐ๋Š” ์กฐํšŒ ์‹œ ์ˆœ์„œ๊ฐ€ ๋ณด์žฅ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.


ZRANGEBYSCORE ๋ช…๋ น์–ด๋Š” ์ ์ˆ˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ง‘ํ•ฉ์„ ์กฐํšŒ ํ•˜๋ผ๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค.




์ด ์™ธ์—๋„ ์ŠคํŠธ๋ง ๊ธฐ๋ฐ˜์˜ ๋น„ํŠธ๋งต(Bitmap)์ด๋‚˜ ํ•˜์ดํผ๋กœ๊ทธ๋กœ๊ทธ์Šค(HyperLogLogs)์™€ ๊ฐ™์€ ๋ฐ์ดํ„ฐ ํƒ€์ž…๋„ ์ง€์›ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. 


์ด๋Ÿฌํ•œ ํƒ€์ž…์— ๋Œ€ํ•ด์„œ ๋ ˆ๋””์Šค ๊ณต์‹ ๋ฌธ์„œ์—๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์„ค๋ช…์„ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

  • Bit arrays (or simply bitmaps): it is possible, using special commands, to handle String values like an array of bits: you can set and clear individual bits, count all the bits set to 1, find the first set or unset bit, and so forth.
์ŠคํŠธ๋ง ๊ฐ’์„ ๋น„ํŠธ์˜ ๋ฐฐ์—ด๋กœ ๋‹ค๋ฃจ๊ธฐ ์œ„ํ•œ ํŠน๋ณ„ํ•œ ๋ช…๋ น์–ด๋ฅผ ์ œ๊ณตํ•œ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ๊ฐ์˜ ๋น„ํŠธ๋ฅผ ์„ธํŒ…ํ•˜๊ฑฐ๋‚˜ 1๋กœ ์„ธํŒ…๋œ ๋ชจ๋“  ๋น„ํŠธ์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๊ฑฐ๋‚˜ ํ•˜๋Š” ๋“ฑ๋“ฑ์˜ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.
  • HyperLogLogs: this is a probabilistic data structure which is used in order to estimate the cardinality of a set. Don't be scared, it is simpler than it seems... See later in the HyperLogLog section of this tutorial.

ํ†ต๊ณ„ํ•™์  ๋ฐ์ดํƒ€ ๊ตฌ์กฐ๋กœ ์ง‘ํ•ฉ์˜ ์นด๋””๋‚ ๋ฆฌํ‹ฐ๋ฅผ ์ธก์ •ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์šฉ๋œ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ƒ๊ฐ๋ณด๋‹ค ์‰ฌ์šฐ๋‹ˆ๊นŒ ์‚ฌ์šฉํ•˜๊ธฐ๋ฅผ ๋‘๋ ค์›Œํ•˜์ง€ ๋ง๋ผ๊ณ  ํ•˜๋„ค์š” ใ…‹ใ…‹


์ž์„ธํ•œ ๋‚ด์šฉ์€ ๊ฐ๊ฐ์˜ ๋ฐ์ดํ„ฐ ํƒ€์ž…์— ๋Œ€ํ•œ ๊ฐœ๋ณ„ ํฌ์ŠคํŒ…์—์„œ ํ™•์ธํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. ^-^


์ด์ƒ ์ผ€์ด์น˜์˜€์Šต๋‹ˆ๋‹ค.


์ฆ๊ฒ๊ณ  ํ–‰๋ณตํ•œ ํ•˜๋ฃจ ๋˜์„ธ์š”~