nats server and client

4 minute read

Written By KJ Jang, VCANUS

Nats 서버는 심플하고 안전한 고성능 오픈소스 메세징 시스템이다. 기본적으로 높은 성능과 확장성, 간단한 사용성을 고려하여 설계되었다.

Server Installation

Ubuntu18.04 기준

download reference

Shell server

Nats Server

modify version, ex) v2.0.0 => v2.x.x

$ sudo apt-get update
$ sudo apt-get -y upgrade
$ sudo snap install curl
$ curl -L https://github.com/nats-io/nats-server/releases/download/v2.0.0/nats-server-v2.0.0-linux-amd64.zip -o nats-server.zip
$ unzip nats-server-v2.0.0-linux-amd64.zip
$ sudo cp nats-server-v2.0.0-linux-amd64/nats-server /usr/bin

docker

Nats port :

  • 4222 is for clients.
  • 8222 is an HTTP management port for information reporting.
  • 6222 is a routing port for clustering.

1. docker Install and run

$ sudo docker pull nats
$ sudo docker run -p 4222:4222 -p 8222:8222 -p 6222:6222 --name nats-server -ti nats:latest

2. docker Nats swarm install

node1 $ sudo docker swarm init
To add a worker to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-3cn52uehfphwp2fe6wxijkd91st7ucgtrzzfanx4flwqurf2vy-1evrmzm310mxliwx02ykunmnp host(node1):2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

nood2 $ sudo docker swarm join --token SWMTKN-1-3cn52uehfphwp2fe6wxijkd91st7ucgtrzzfanx4flwqurf2vy-1evrmzm310mxliwx02ykunmnp host(node1):2377
node3 $ sudo docker swarm join --token SWMTKN-1-3cn52uehfphwp2fe6wxijkd91st7ucgtrzzfanx4flwqurf2vy-1evrmzm310mxliwx02ykunmnp host(node1):2377
node1 $ sudo docker network create --driver overlay nats-cluster-example
node1 $ sudo docker service create --network nats-cluster-example --name nats-cluster-node-1 -p 4222:4222 -p 8222:8222 nats:1.0.0 -cluster nats://0.0.0.0:6222 -DV

3. docker Nats Network host Cluster

(option) centos8 firewall config

# Allows container to container communication, the solution to the problem
firewall-cmd --zone=public --add-masquerade --permanent

# standard http & https stuff
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --zone=public --add-port=443/tcp --permanent
firewall-cmd --zone=public --add-port=6222/tcp --permanent
firewall-cmd --zone=public --add-port=4222/tcp --permanent

# reload the firewall
firewall-cmd --reload
node1 $ sudo docker run --name nats-node-1 --net host nats
node2 $ sudo docker run --name nats-node-2 --net host nats --cluster nats://0.0.0.0:6222  --routes=nats://ruser:T0pS3cr3t@(node1 ip):6222
node3 $ sudo docker run --name nats-node-2 --net host nats --cluster nats://0.0.0.0:6222  --routes=nats://ruser:T0pS3cr3t@(node1 ip):6222
##cluster 확인
node1 $ crul http://127.0.0.1:8222/routez

Client Installation

Linux

1. install protobuf and protobuf-c

1.1 install with yum or apt
$ sudo yum install -y protobuf protobuf-c
$ sudo apt install -y libprotobuf-dev libprotobuf-c-dev
1.2 install with source (option)
1.2.1 install protobuf

reference: https://github.com/protocolbuffers/protobuf/tree/master/src

$ git clone https://github.com/protocolbuffers/protobuf.git
$ cd protobuf
$ git submodule update --init --recursive
$ ./autogen.sh
$ ./configure
$ make
$ make check
$ sudo make install
$ sudo ldconfig # refresh shared library cache.
1.2.2 install protobuf-c

update ~/.bash_profile for protobuf configuration

    export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig

reference: https://github.com/protobuf-c/protobuf-c

$ git clone https://github.com/protobuf-c/protobuf-c.git
$ cd protobuf-c
$ ./autogen.sh
$ ./configure
$ make
$ sudo make install

(option) install protobuf-c

reference: http://google.github.io/proto-lens/installing-protoc.html

$ PROTOC_ZIP=protoc-3.7.1-linux-x86_64.zip
$ curl -OL https://github.com/protocolbuffers/protobuf/releases/download/v3.7.1/$PROTOC_ZIP
$ sudo unzip -o $PROTOC_ZIP -d /usr/local bin/protoc
$ sudo unzip -o $PROTOC_ZIP -d /usr/local 'include/*'
$ rm -f $PROTOC_ZIP

4. install openssl and openssl-devel

$ sudo yum install -y openssl openssl-devel

5. install nats client (C version)

reference: https://nats.io/download/nats-io/nats.c/

$ git clone https://github.com/nats-io/nats.c.git
$ cd nats.c
$ mkdir build
$ cd build
$ cmake ..
$ make
$ sudo make install

Mac

1. install protobuf protobuf-c

$ brew install protobuf protobuf-c

2. install openssl and openssl-devel

$ brew install openssl

3. add environment variable “OPENSSL_HOME” to .bash_profile

OPENSSL_HOME=/usr/local/opt/openssl@1.1
export PATH=$OPENSSL_HOME/bin:$PATH

4. install nats clienht (C version)

$ git clone https://github.com/nats-io/nats.c.git
$ cd nats.c
$ mkdir build
$ cd build
$ cmake .. -DOPENSSL_ROOT_DIR=/usr/local/opt/openssl@1.1 -DOPENSSL_LIBRARIES=/usr/loca/opt/openssl@1.1/lib
$ make
$ sudo make install

Windows

1. vcpkg integrate

visual studio 랑 vcpkg 통합 시켜주는 기능

C:\vcpkg > vcpkg integrate install

2. protobuf (for streaming) install

vcpkg install protobuf:x64-windows

3. install openssl, zlib, curl with vcpkg

vcpkg install openssl:x64-windows zlib:x64-windows curl:x64-windows
  • 시스템 속성->환경 변수->시스템 변수 추가 후 재부팅
  • 변수명: OPENSSL_ROOT_DIR, 경로명: C:\vcpkg-master\installed\x64-windows (depends on your system)

    4. install cmake install

  • install cmake with windows installer (binary)
  • 모든 유저에 환경 변수 등록 적용
  • 적용된 환경 변수 사용을 위해, cmake 설치 후 새로운 콘솔창 띄우고 사용

    5. install Boost [install] (https://boost.org)

  • go to boost homepage and download boost_version.zip
  • unzip boost_version.zip and put it on proper place
  • go to boost folder and run the below
    $ bootstrap.bat
    $ .\b2
    

    6. install NATS C client install

  • download .zip file and decompress it
  • 압축 푼 디렉토리 밑에 build 디렉토리 생성
  • 윈도우즈에서 protobuf-c 설치 실패 시, cmake 실행 때 -DNATS_BUILD_STREAMING=OFF 옵션 사용 (NATS Streaming API 지원 불가)
  • 참고로, 2020년 8월 현재 vcpkg install protobuf-c 설치 지원하지 않음
    mkdir build
    cd build
    cmake .. -DNATS_BUILD_STREAMING=OFF
    

    7. NATS C Client 설치 결과

  • 5번 build 디텍토리에 cnats.sln 파일 생성됨
  • Visual Studio로 cnats.sln 로딩 후 All Build
  • 빌드 경로 디렉터리에 lib, dll 파일이 생성된다. ex) C:\nats.c-master\build\src\Release
  • include 파일 ex) C:\nats.c-master\src
    C
    └── nats.c-master
      └── src
           └── adapters
           └── nats.h
           └── status.h
           └── version.h
    

    8. Visual C++ Project Setting

  • define winsock2.h before windows.h
  • put the below in the top of your main.cpp
    #define _WINSOCKAPI_    // stops winsock.h including windows.h
    #include <winsock2.h>
    #include <windows.h>
    
  • 코드 맨 위에 #pragma comment(lib,”ws2_32.lib”) 추가하거나, or 프로젝트 우클릭 -> 속성 -> 링커 -> 입력 -> 추가 종속성에 ws2_32.lib 추가
  • include setting . properties -> General -> C/C++ -> Additional Include Directories . include directories of vcx-project, vcpkg, and boost, etc. . example ``` C:\Users\user_id\Documents\workspace\vcx-project\include; C:\vcpkg-master\installed\x64-windows\include; C:\opt\boost_1_74_0\boost
- lib setting
 . properties -> Linker -> General -> Additional Lib Directories
 . lib directories of vcx-project, vcpkg, and boost, etc.
 . example

C:\Users\user_id\Documents\workspace\vcx-project\lib_windows; C:\vcpkg-master\installed\x64-windows\lib; C:\opt\boost_1_74_0\stage\lib ```

Leave a comment