ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 앱 플랫폼 개발 : Vert.x 개념, Listen 프로젝트 예제
    vert.x 2021. 3. 26. 14:24
    728x90
    반응형

    Vert.x

    Eclipse Vert.x는 JVM에서 반응성 애플리케이션을 개발하기 위한 이벤트 기반 툴킷으로, 비동기식 통신을 위해 설계되었습니다.

     

    Vert.x의 가장 기본적인 이점은 이벤트 기반 및 비차단 접근 방식으로 소수의 커널 스레드를 사용하여 많은 처리를 하는 것입니다. 이러한 방식으로 최소한의 하드웨어로 애플리케이션 확장 수행할 수 있습니다.

     

    동기식 I/O 스레드 모델에서 알 수 있듯이 네트워크 통신은 각 클라이언트에 스레드를 할당하여 관리됩니다. 즉, 클라이언트에 할당된 스레드가 연결이 끊길 때까지 클라이언트를 처리합니다. 그러나 이벤트 기반 프로그래밍 모델을 제공하기 때문에 Vert.x를 사용하여 코드를 단일 스레드 애플리케이션으로 쓸 수 있습니다.

     

    Vert.x 코어 라이브러리는 비동기 네트워크 애플리케이션을 쓰기 위한 기본 API를 정의하고 폴리글롯 애플리케이션도 지원합니다. 즉, 여러 언어를 애플리케이션에 포함할 수 있습니다.

     

    또한 Vert.x에는 여러 구성 요소가 포함되어 있습니다. 원하는 경우 모든 일반 라이브러리에 구성 요소를 사용할 수 있습니다. Vert.x는 이와 관련하여 사용자를 제한하지 않습니다.

    Vert.x Listen Project

     

     

    우선 Gradle 프로젝트를 생성합니다. 

     

     

    임의의 프로젝트명을 설정합니다.

     

     

     

     

    다음을 눌러줍니다.

     

     

     

     

    설정을 마치고 프로젝트를 생성합니다.

     

     

     

    build.gradle 수정

    /*

    * This build file was generated by the Gradle 'init' task.

    *

    * This generated file contains a sample Java Library project to get you started.

    * For more details take a look at the Java Libraries chapter in the Gradle

    * user guide available at https://docs.gradle.org/4.3/userguide/java_library_plugin.html

    */

     

    //-------- 추가됨---------------------------------------

    buildscript {

    repositories {

    jcenter()

    }

    dependencies {

    classpath "com.github.jengelman.gradle.plugins:shadow:4.0.2"

    }

    }

    //--------------------------------------------------



    // Apply the java-library plugin to add support for Java Library

    apply plugin: 'java-library'

     

    //-------- 추가됨 ------------------------------

    apply plugin: 'java'

    apply plugin: 'eclipse'

    apply plugin: "com.github.johnrengelman.shadow"

    //--------------------------------------------------

     

     

    // In this section you declare where to find the dependencies of your project

    repositories {

    // Use jcenter for resolving your dependencies.

    // You can declare any Maven/Ivy/file repository here.

    jcenter()

        //-------- 추가됨 ------------------------------

       mavenCentral()

        maven {

         url = 'http://oss.sonatype.org/content/repositories/snapshots/'

        }

        //--------------------------------------------------

    }

     

    dependencies {

    // This dependency is exported to consumers, that is to say found on their compile classpath.

    api 'org.apache.commons:commons-math3:3.6.1'

     

    // This dependency is used internally, and not exposed to consumers on their own compile classpath.

    implementation 'com.google.guava:guava:23.0'

     

    // Use JUnit test framework

    testImplementation 'junit:junit:4.12'

     

    }

     

    //-------- 추가됨---------------------------------------

    dependencies {

    compile "io.vertx:vertx-core:3.6.3"

    }

     

    jar {

    manifest {

    attributes 'Main-Class': 'yonsei.app.hw1.Launcher'

    }

    }

     

    version = '1.0.0'

    shadowJar {

    classifier = 'fat'

    mergeServiceFiles {

    include 'META-INF/services/io.vertx.core.spi.VerticleFactory'

    }

    }

    //----------------------------------------------------------------------------

     

    build.gradle를 위와 같이 수정해줍니다. gradle을 활용하여 빌드에 필요한 dependency를 추가합니다.

     

    다음과 같이 그래들 프로젝트를 새로고침해줍니다. 그 후 이클립스를 껐다 켜줍니다. 

     

     

     

     

    다음과 같이 Launcher.java를 생성합니다. 

     

     

     

     

    Launcher.java 생성 및 입력

    import io.vertx.core.Vertx;

     

    public class Launcher {

        public static void main(String[] args) {

     

            //학번을 인자로 넣지 않을 경우 사용법을 알려주고 끝낸다.

            if (args.length < 1) {

                System.out.println("java -jar hw1-1.0.0-fat.jar 학번");

                args = new String[] {"12345"};

            }

     

            //학번 = 첫번째 인자

            String id = args[0];

            

            //서버 실행

            Vertx.vertx().createHttpServer()

            .requestHandler(req -> {

                req.response().end("Hello "+" @" +id);

            })

            .listen(8080, handler -> {

                //서버 실행 상태

                if (handler.succeeded()) {

                    System.out.println("http://127.0.0.1:8080/ 또는 http://ServerIP:8080/");

                    System.out.println("학번 : " + id);

                } else {

                    System.err.println("Failed to listen on port 8080");

                }

            });

        }

    }

     

     

     

     

     

    다음과 같이 실행 파일을 export 하겠습니다. 

     

     

     

     

    Runnable JAR file을 클릭합니다. 

     

     

     

     

    Launch configuration의 프로젝트와 해당하는 파일을 선택합니다. 그 다음 실행 파일을 생성할 곳과 이름을 입력합니다. 저는 바탕화면으로 지정하였습니다. 

     

     

     

     

     

    실행 파일이 나왔으니 이제 콘솔창에서 실행을 하겠습니다. 콘솔창에서 데스크 탑으로 경로를 이동합니다. 각자 본인의 실행파일이 있는 경로로 이동하시면 됩니다. 그 후 java -jar "프로젝트이름" "학번" 을 입력합니다. 실행 파일이 존재하고 컴파일이 잘 되었다면 다음과 같이 출력됩니다.

     

     

     

     

    브라우저에서도 확인해보겠습니다. 127.0.0.1:8080 을 입력합니다. 다음과 같이 잘 출력되는 것을 확인하실 수 있습니다. 

    728x90
    반응형

    'vert.x' 카테고리의 다른 글

    대규모 트래픽 분산처리를 위한 서버개발 지식  (0) 2021.03.26
    Restful API 디자인 가이드  (0) 2021.03.26
Designed by Tistory.