Distributor에서 마이크로서비스들의 정보를 받아 와 처리하는 로직을 만들겠습니다. 다음과 같이 코드를 추가합니다.

    코드 8-2 Distributor 통신 기능

    예제 파일 : gate.js

    ......
    const tcpClient = require('./client');        // ➊ Client 클래스 참조
    
    var mapClients = {};
    var mapUrls = {};
    var mapResponse = {};
    var mapRR = {};
    var index = 0;
    
    var server = http.createServer((req, res) => {
            
        ......
    }).listen(8000, () => {
        console.log('listen', server.address());
    
        var packet = {                           // ➋ Distributor 전달 패킷
            uri: "/distributes",
            method: "POST",
            key: 0,
            params: {
                port: 8000,
                name: "gate",
                urls: []
            }
        };
        var isConnectedDistributor = false;
            
        this.clientDistributor = new tcpClient(  // ➌ Distributor 접속
            "127.0.0.1", 9000, (options) => {    // ➍ istributor 접속 완료 이벤트
                isConnectedDistributor = true;
                this.clientDistributor.write(packet);
            }, (options, data) => { onDistribute(data); } // ➎ Distributor 데이터 수신 이벤트
            , (options) => { isConnectedDistributor = false; } // ➏ Distributor 접속 종료 이벤트
            , (options) => { isConnectedDistributor = false; } // ➐ Distributor 에러 이벤트
        );
        setInterval(() => {                      // ➑ istributor 재접속
            if (isConnectedDistributor != true) {
                this.clientDistributor.connect();
            }
        }, 3000);
    });
    
    function onRequest(res, method, pathname, params) {
    ......
    }
    
    function onDistribute(data) {                // ➒ istributor 데이터 수신 처리
        for (var n in data.params) {
            var node = data.params[n];
            var key = node.host + ":" + node.port;
            if (mapClients[key] == null && node.name != "gate") {
                var client = new tcpClient(node.host, node.port, onCreateClient, onReadClient, onEndClient, onErrorClient);
                mapClients[key] = {              // ➓ 마이크로서비스 연결 정보 저장
                    client: client,
                    info: node
                };
                for (var m in node.urls) {       // ⓫ 마이크로서비스 URL 정보 저장
                    var key = node.urls[m];
                    if (mapUrls[key] == null) {
                        mapUrls[key] = [];
                    }
                    mapUrls[key].push(client);
                }
                client.connect();
            }
        }
    }
    
    function onCreateClient(options) {
        console.log("onCreateClient");
    }
    
    function onReadClient(options, packet) {
            
    }
    
    function onEndClient(options) {              // ⓬ 마이크로서비스 접속 종료 처리
        var key = options.host + ":" + options.port;
        console.log("onEndClient", mapClients[key]);
        for (var n in mapClients[key].info.urls) {
            var node = mapClients[key].info.urls[n];
            delete mapUrls[node];
        }
        delete mapClients[key];
    }
    
    function onErrorClient(options) {
        console.log("onErrorClient");
    }
    신간 소식 구독하기
    뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.