더북(TheBook)

10.2.2 고가용 마이크로서비스 만들기

앞에서 배운 cluster 모듈을 우리가 만든 마이크로서비스에 적용하겠습니다. 이때 예제와 다른 점은 자식 프로세스에서 exit 이벤트가 감지되면 새로운 자식 프로세스를 실행하는 부분입니다. 상품 관리 마이크로서비스에 cluster 모듈을 이용해 Failover 기능을 만듭니다.

코드 10-2 상품 관리 마이크로서비스에 cluster 모듈 적용

예제 파일 : microservice_goods.js

‘use strict’;

const business = require(’../chapter5/monolithic_goods.js’); const cluster = require(‘cluster’); // ➊ cluster 모듈 로드

class goods extends require(’./server.js’) { constructor() { super(“goods” , process.argv[2] ? Number(process.argv[2]) : 9010 , [“POST/goods”, “GET/goods”, “DELETE/goods”]);

    <span class="k">this</span><span class="p">.</span><span class="nx">connectToDistributor</span><span class="p">(</span><span class="s2">"127.0.0.1"</span><span class="p">,</span> <span class="mi">9000</span><span class="p">,</span> <span class="p">(</span><span class="nx">data</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="p">{</span>
        <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"Distributor Notification"</span><span class="p">,</span> <span class="nx">data</span><span class="p">);</span>
    <span class="p">});</span>
<span class="p">}</span>

<span class="nx">onRead</span><span class="p">(</span><span class="nx">socket</span><span class="p">,</span> <span class="nx">data</span><span class="p">)</span> <span class="p">{</span>
    <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"onRead"</span><span class="p">,</span> <span class="nx">socket</span><span class="p">.</span><span class="nx">remoteAddress</span><span class="p">,</span> <span class="nx">socket</span><span class="p">.</span><span class="nx">remotePort</span><span class="p">,</span> <span class="nx">data</span><span class="p">);</span>
    <span class="nx">business</span><span class="p">.</span><span class="nx">onRequest</span><span class="p">(</span><span class="nx">socket</span><span class="p">,</span> <span class="nx">data</span><span class="p">.</span><span class="nx">method</span><span class="p">,</span> <span class="nx">data</span><span class="p">.</span><span class="nx">uri</span><span class="p">,</span> <span class="nx">data</span><span class="p">.</span><span class="nx">params</span><span class="p">,</span> <span class="p">(</span><span class="nx">s</span><span class="p">,</span> <span class="nx">packet</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="p">{</span>
                       <span class="nx">socket</span><span class="p">.</span><span class="nx">write</span><span class="p">(</span><span class="nx">JSON</span><span class="p">.</span><span class="nx">stringify</span><span class="p">(</span><span class="nx">packet</span><span class="p">)</span> <span class="o">+</span> <span class="s1">'¶'</span><span class="p">);</span>
    <span class="p">});</span>
<span class="p">}</span>

}

if (cluster.isMaster) { // ➋ 자식 프로세스 실행 cluster.fork();

    <span class="c1">// ➌ exit 이벤트가 발생하면 새로운 자식 프로세스 실행</span>
    <span class="nx">cluster</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s1">'exit'</span><span class="p">,</span> <span class="p">(</span><span class="nx">worker</span><span class="p">,</span> <span class="nx">code</span><span class="p">,</span> <span class="nx">signal</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="p">{</span>
        <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="sb">`worker </span><span class="si">${</span><span class="nx">worker</span><span class="p">.</span><span class="nx">process</span><span class="p">.</span><span class="nx">pid</span><span class="si">}</span><span class="sb"> died`</span><span class="p">);</span>
        <span class="nx">cluster</span><span class="p">.</span><span class="nx">fork</span><span class="p">();</span>
<span class="p">});</span>

}else { new goods(); }

cluster 모듈을 로드하고() 부모 프로세스에서 자식 프로세스를 하나 실행합니다(). 자식 프로세스에서 exit 이벤트가 발생하면 새로운 자식 프로세스를 실행합니다().

신간 소식 구독하기
뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.