본문 바로가기
Java

[Java] Vert.x 클러스터링 예제

by teamnova 2024. 4. 4.
728x90

오늘은 Vert.x의 클러스터링을 해보겠습니다.

 

파일 구조는 아래와 같습니다.

 

// Main.java
public class Main {
    public static void main(String[] args) {
        // 기존 Hazelcast 클러스터 매니저와 Vert.x 옵션 설정
        HazelcastClusterManager mgr = new HazelcastClusterManager();
        VertxOptions options = new VertxOptions().setClusterManager(mgr);


        // 클러스터링된 Vert.x 인스턴스 생성
        Vertx.clusteredVertx(options, res -> {
            if (res.succeeded()) {
                Vertx vertx = res.result();

                // 기존 버티클 배포
                vertx.deployVerticle(new Verticle1());
                vertx.deployVerticle(new Verticle2());

                // REST API 버티클 배포
                vertx.deployVerticle(new RestApiVerticle());

            } else {
                System.out.println("Cluster up failed: " + res.cause());
            }
        });
    }
}

 

// Verticle1.java
public class Verticle1 extends AbstractVerticle {
    @Override
    public void start(Promise<Void> startPromise) throws Exception {
        vertx.eventBus().consumer("cluster-message", message -> {
            System.out.println(" Verticle1 says : Received message: " + message.body());
        });
        startPromise.complete();
    }
}

 

// Verticle2.java
public class Verticle2 extends AbstractVerticle {
    @Override
    public void start(Promise<Void> startPromise) throws Exception {
        vertx.setPeriodic(1000, id -> {
            vertx.eventBus().send("cluster-message", "Hello from Verticle 2");
        });
        startPromise.complete();
    }
}

 

 

// RestApiVerticle.java
public class RestApiVerticle extends AbstractVerticle {

    @Override
    public void start(Promise<Void> startPromise) throws Exception {
        Router router = Router.router(vertx);
        router.route().handler(BodyHandler.create());

        router.get("/api/status").handler(this::handleClusterStatus);

        vertx.createHttpServer()
                .requestHandler(router)
                .listen(8080, result -> {
                    if (result.succeeded()) {
                        startPromise.complete();
                    } else {
                        startPromise.fail(result.cause());
                    }
                });
    }

    private void handleClusterStatus(RoutingContext context) {
        // 클러스터 상태 정보를 반환하는 로직 구현
        context.response().end("Cluster status: OK");
    }
}

 

아래 파일은 pom.xml입니다.

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.yourcompany</groupId>
    <artifactId>your-artifact</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <vertx.version>4.4.4</vertx.version> <!-- Vert.x 버전 -->
    </properties>

    <dependencies>
        <!-- Vert.x core dependency -->
        <dependency>
            <groupId>io.vertx</groupId>
            <artifactId>vertx-core</artifactId>
            <version>${vertx.version}</version>
        </dependency>

        <dependency>
            <groupId>io.vertx</groupId>
            <artifactId>vertx-web</artifactId>
            <version>4.4.4</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>io.vertx</groupId>
            <artifactId>vertx-hazelcast</artifactId>
            <version>4.3.3</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
        </plugins>
    </build>
</project>

 

클러스터링과 상태체크도 잘 되고 있음을 확인할 수 있습니다.