-
Notifications
You must be signed in to change notification settings - Fork 139
[Quick Start] Building a simple Rest web microservice server with QBit
##overview WHAT IS REST? REST stands for Representational State Transfer. It relies on a stateless, client-server, cacheable communications protocol. In virtually all cases, the HTTP protocol is used. Restful applications use HTTP requests to post data (create and/or update), read data, and delete data. Thus, REST uses HTTP for all four CRUD (Create/Read/Update/Delete) operations. The World Wide Web itself is based on HTTP, can be viewed as a REST-based architecture.
This wiki will walk you through the process for building a simple Rest web server with QBit.
You will build a service that will accept HTTP GET/POST (CREATE/READ) requests at:
curl http://localhost:6060/services/myservice/ping
when a ping is sent by a client, a response of a greeting and a confirmation that the server is working will be posted; the JSON greeting representation is as follows:
["Hello; My REST server is working"]
In order to complete this example successfully you will need the following installed on your machine:
- Gradle; if you need help installing it, visit Installing Gradle.
- Your favorite IDE or text editor (we recommend [Intellig IDEA ] (https://www.jetbrains.com/idea/) latest version).
- [JDK ] (http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html) 1.8 or later.
- Build and install QBit on your machine click [Building QBit ] (https://github.com/advantageous/qbit/wiki/%5BQuick-Start%5D-Building-QBit-the-microservice-lib-for-Java) for instrutions.
Now that your machine is all ready let's get started:
- [Download ] (https://github.com/fadihub/simple-rest-server/archive/master.zip) and unzip the source repository for this guide, or clone it using Git:
git clone https://github.com/fadihub/simple-rest-server.git
Once this is done you can test the service, let's first explain the process:
This service will handle GET/POST requests. Once the client pings the server, a greeting and a confirmation message will be posted at:
curl http://localhost:6060/services/myservice/ping
The content of the message is the following:
["Hello: This REST server is working!"]
####SimpleRestServer Listing
src/main/java/io.advantageous.qbit.examples/SimpleRestServer
package io.advantageous.qbit.examples;
import io.advantageous.qbit.annotation.RequestMapping;
import io.advantageous.qbit.queue.QueueBuilder;
import io.advantageous.qbit.server.ServiceEndpointServer
ServiceEndpointServer
ServiceEndpointServer;
import io.advantageous.qbit.server.EndpointServerBuilder;
import org.boon.Boon;
import java.util.Collections;
import java.util.List;
/**
*
$ ./wrk -c 200 -d 10s http://localhost:6060/services/myservice/ping -H "X_USER_ID: RICK" --timeout 100000s -t 8
Running 10s test @ http://localhost:6060/services/myservice/ping
8 threads and 200 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 2.63ms 360.02us 4.41ms 68.14%
Req/Sec 10.17k 1.32k 12.89k 55.97%
766450 requests in 10.00s, 76.75MB read
Requests/sec: 76651.84
Transfer/sec: 7.68MB
*/
public class SimpleRestServer {
public static class MyService {
/*
curl http://localhost:6060/services/myservice/ping -H "X_USER_ID: RICK"
*/
@RequestMapping
public List ping() {
return Collections.singletonList("Hello: This REST server is working!");
}
}
public static void main(String... args) throws Exception {
final ServiceEndpointServer
ServiceEndpointServer
ServiceEndpointServer serviceServer = new EndpointServerBuilder().setPort(6060).setQueueBuilder(
new QueueBuilder().setLinkTransferQueue().setTryTransfer(true).setBatchSize(10).setPollWait(10)
).setNumberOfOutstandingRequests(1000000).setTimeoutSeconds(40)
.build();
serviceServer.initServices(new MyService());
serviceServer.start();
Boon.gets();
}
}
As you can see the code for this simple rest server is very straight forward, we are just creating the server and setting the port to 6060, and setting a few other parameters like PollWait etc.. will talk about these in later tutorials.
####build.gradle Listing
~/simple-rest-server/build.gradle
group = 'io.advantageous.qbit.examples'
apply plugin: 'idea'
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'application'
version = '0.1-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
sourceSets {
main {
java {
srcDir 'src/main/java'
}
resources {
srcDir 'src/main/resources'
}
}
}
mainClassName = "io.advantageous.qbit.examples.SimpleRestServer"
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
compile group: 'io.advantageous.qbit', name: 'qbit-vertx', version: '0.6.1'
compile "org.slf4j:slf4j-api:[1.7,1.8)"
compile 'ch.qos.logback:logback-classic:1.1.2'
testCompile group: 'junit', name: 'junit', version: '4.10'
}
idea {
project {
jdkName = '1.8'
languageLevel = '1.8'
}
}
This is where all the dependencies are set up for the simple-rest-server code.
Now we are ready to test the localhost server.
##Test The Service
With your terminal cd into ~/simple-rest-sever
run gradle clean build
then gradle run
open up your favorite browser then visit http://localhost:6060/services/myservice/ping or open up another terminal and type the following:
curl http://localhost:6060/services/myservice/ping
you should get this response:
["Hello: This REST server is working!"]
##Summary You have just built a simple REST server with QBit and tested it, see you in the next tutorial!
QBit Website What is Microservices Architecture?
QBit Java Micorservices lib tutorials
The Java microservice lib. QBit is a reactive programming lib for building microservices - JSON, HTTP, WebSocket, and REST. QBit uses reactive programming to build elastic REST, and WebSockets based cloud friendly, web services. SOA evolved for mobile and cloud. ServiceDiscovery, Health, reactive StatService, events, Java idiomatic reactive programming for Microservices.
Reactive Programming, Java Microservices, Rick Hightower
Java Microservices Architecture
[Microservice Service Discovery with Consul] (http://www.mammatustech.com/Microservice-Service-Discovery-with-Consul)
Microservices Service Discovery Tutorial with Consul
[Reactive Microservices] (http://www.mammatustech.com/reactive-microservices)
[High Speed Microservices] (http://www.mammatustech.com/high-speed-microservices)
Reactive Microservices Tutorial, using the Reactor
QBit is mentioned in the Restlet blog
All code is written using JetBrains Idea - the best IDE ever!
Kafka training, Kafka consulting, Cassandra training, Cassandra consulting, Spark training, Spark consulting
Tutorials
- QBit tutorials
- Microservices Intro
- Microservice KPI Monitoring
- Microservice Batteries Included
- RESTful APIs
- QBit and Reakt Promises
- Resourceful REST
- Microservices Reactor
- Working with JSON maps and lists
__
Docs
Getting Started
- First REST Microservice
- REST Microservice Part 2
- ServiceQueue
- ServiceBundle
- ServiceEndpointServer
- REST with URI Params
- Simple Single Page App
Basics
- What is QBit?
- Detailed Overview of QBit
- High level overview
- Low-level HTTP and WebSocket
- Low level WebSocket
- HttpClient
- HTTP Request filter
- HTTP Proxy
- Queues and flushing
- Local Proxies
- ServiceQueue remote and local
- ManagedServiceBuilder, consul, StatsD, Swagger support
- Working with Service Pools
- Callback Builders
- Error Handling
- Health System
- Stats System
- Reactor callback coordination
- Early Service Examples
Concepts
REST
Callbacks and Reactor
Event Bus
Advanced
Integration
- Using QBit in Vert.x
- Reactor-Integrating with Cassandra
- Using QBit with Spring Boot
- SolrJ and service pools
- Swagger support
- MDC Support
- Reactive Streams
- Mesos, Docker, Heroku
- DNS SRV
QBit case studies
QBit 2 Roadmap
-- Related Projects
- QBit Reactive Microservices
- Reakt Reactive Java
- Reakt Guava Bridge
- QBit Extensions
- Reactive Microservices
Kafka training, Kafka consulting, Cassandra training, Cassandra consulting, Spark training, Spark consulting