-
Notifications
You must be signed in to change notification settings - Fork 140
[Quick Start] Building a TODO web microservice server with QBit
This wiki will walk you through the process for building a Hello World TODO web microservice with QBit.
You will build a service that will accept HTTP GET/POST requests at:
curl http://localhost:8080/services/todo-service/todo/
Post a TODO item "Hello World" in this case using your terminal,
and respond with a JSON representation of a greeting or whatever you post:
[{"name":"Hello World"}
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/todo-service/archive/master.zip) and unzip the source repository for this guide, or clone it using Git:
git clone https://github.com/fadihub/todo-service.git
Once this is done you can test the service, let's first explain the process:
This service will handle GET/POST requests for /TodoService
. The POST request
curl -X POST -H "Content-Type: application/json" \
-d '{"name":"Hello World","description":"Greeting"}' \
http://localhost:8080/services/todo-service/todo
The GET request will return the following:
{"description":"greeting","name":"Hello World"}
The description
field is a unique identifier for the greeting in this case, and name
is the textual content representation of the greeting.
To model the greeting representation, provide a plain old java object with fields, constructors, and accessors for the description
and name
data:
/src/main/java/io.advantageous.qbit.examples/TodoItem.java
package io.advantageous.qbit.examples;
import java.util.Date;
public class TodoItem {
private final String description;
private final String name;
private final Date due;
public TodoItem(final String description, final String name, final Date due) {
this.description = description;
this.name = name;
this.due = due;
}
public String getDescription() {
return description;
}
public String getName() {
return name;
}
public Date getDue() {
return due;
}
}
The /TodoService
will handle GET/POST requests for /TodoItem
:
/src/main/java/io.advantageous.qbit.examples/TodoService.java
package io.advantageous.qbit.examples;
import io.advantageous.qbit.annotation.RequestMapping;
import io.advantageous.qbit.annotation.RequestMethod;
import java.util.ArrayList;
import java.util.List;
@RequestMapping("/todo-service")
public class TodoService {
List<TodoItem> items = new ArrayList<>();
@RequestMapping("/todo/count")
public int size() {
return items.size();
}
@RequestMapping("/todo/")
public List<TodoItem> list() {
return items;
}
@RequestMapping(value = "/todo", method = RequestMethod.POST)
public void add(TodoItem todoItem) {
items.add(todoItem);
}
}
To make and run your localhost server:
/src/main/java/io.advantageous.qbit.examples/TodoMain.java
package io.advantageous.qbit.examples;
import io.advantageous.qbit.server.ServiceEndpointServer
ServiceEndpointServer
ServiceEndpointServer;
import io.advantageous.qbit.server.EndpointServerBuilder;
public class TodoMain {
public static void main(String... args) {
ServiceEndpointServer
ServiceEndpointServer
ServiceEndpointServer server = new EndpointServerBuilder().setHost("localhost").setPort(8080).build();
server.initServices(new TodoService());
server.start();
}
}
now we are ready to test the service.
With your terminal cd into /todo-service
run gradle clean
then gradle run
open up another terminal, then
curl http://localhost:8080/services/todo-service/todo/
you should get the following:
[]
because you haven't posted anything yet, now post a greeting:
curl -X POST -H "Content-Type: application/json" \
-d '{"name":"Hello World","description":"greeting"}' \
http://localhost:8080/services/todo-service/todo
then visit http://localhost:8080/services/todo-service/todo/ with your browser or:
curl localhost:8080/services/todo-service/todo
you should get the following:
[{"description":"greeting","name":"Hello World"}]
You can post as many todo items as you want.
To query the size of the todo list run the following command:
curl localhost:8080/services/todo-service/todo/count
##Summary You have just built and tested a TODO web service with QBit. 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