QBit开发微服务

一、QBit库介绍

QBit是一个开源的响应式开发库,用于构建微服务,支持JSON、HTTP、WebSocket和REST。QBit使用响应式编程建立弹性REST、基于云的WebSocket、Web服务。QBit是面向移动和云计算的SOA演进。QBit是一个很小的、轻量级的微服务开发库,提供了对服务发现、服务健康、响应式的状态服务、类型事件、以及Java惯用的反应式编程的支持。
QBit非常小且速度极快。

二、Gradle构建文件

group 'qbit-ex'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'application'

mainClassName = "com.mammatustech.HelloWorldService"

compileJava {
    sourceCompatibility = 1.8
}

repositories {
    mavenCentral()
    mavenLocal()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
    compile group: 'io.advantageous.qbit', name: 'qbit-admin', 
                                                 version: '0.9.0-M1'
    compile group: 'io.advantageous.qbit', name: 'qbit-vertx', 
                                                 version: '0.9.0-M1'

}

目前QBit微服务库最新版为0.9.0.M2版。

三、用QBit写示例代码

package com.mammatustech;

import io.advantageous.qbit.admin.ManagedServiceBuilder;
import io.advantageous.qbit.annotation.RequestMapping;

@RequestMapping("/hello")
public class HelloWorldService {

    @RequestMapping("/hello")
    public String hello() {
        return "hello " + System.currentTimeMillis();
    }

    public static void main(final String... args) {
        final ManagedServiceBuilder managedServiceBuilder =
                ManagedServiceBuilder.managedServiceBuilder()
                                     .setRootURI("/root");

        /* Start the service. */
        managedServiceBuilder.addEndpointService(new HelloWorldService())
                .getEndpointServerBuilder()
                .build().startServer();

    }
}

四、运行代码

# gradle run

五、用curl测试

$ curl http://localhost:8080/root/hello/hello
"hello 1440742489358"

六、用wrk测试

wrk是一个开源的HTTP基准测试工具。当wrk在单个多核CPU上运行时,能够产生显著的负载,它结合了多线程设计和可扩展的事件通知系统(如epoll和kqueue)。
wrk还提供了一个可选的LuaJIT脚本,可用于执行HTTP请求的产生、响应的处理,以及自定义报表。
主页: https://github.com/wg/wrk

$ wrk -d 5s -t 2 -c 1000 http://localhost:8080/root/hello/hello
Running 5s test @ http://localhost:8080/root/hello/hello
  2 threads and 1000 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    17.65ms   22.96ms 427.36ms   97.57%
    Req/Sec    33.33k     7.75k   43.10k    75.00%
  319154 requests in 5.06s, 28.00MB read
Requests/sec:  63083.97
Transfer/sec:      5.53MB

猜你喜欢

转载自www.linuxidc.com/Linux/2015-09/123343.htm