-
Notifications
You must be signed in to change notification settings - Fork 140
[DOC] Understanding ManagedServiceBuilder to create Microservices in QBit that support Docker, Heroku, Swagger, Consul, and StatsD with near 0 config
QBit integrates easily with Consul, StatsD and Swagger. In addition QBit has its own health system, stats engine, and meta-data engine.
Swagger is for code generations of REST clients in Python, Java, Ruby, Scala, etc. Consul is for health monitoring and service discovery (among other things). StatsD is for microservice health monitoring.
To make configuring QBit easier we did two things: 1) we added support for Spring Boot (which we have not released yet), and 2) we created the ManagedServiceBuilder
, which we will show below.
The ManagedServiceBuilder
simplifies construction of QBit endpoints by registering all services and endpoints with the system service so that they are shut down correctly when you CTRL-C or kill
an app gracefully.
In addition ManagedServiceBuilder
allows enabling of StatsD, Swagger, and Consul in one simple step. By default, ManagedServiceBuilder
is configured to run in an environment like Docker or Heroku.
Let's show a simple example:
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();
/* Start the admin builder which exposes health end-points and meta data. */
managedServiceBuilder.getAdminBuilder().build().startServer();
System.out.println("Servers started");
}
}
The above is a simple REST service. It has one REST method.
@RequestMapping("/hello")
public class HelloWorldService {
@RequestMapping("/hello")
public String hello() {
return "hello " + System.currentTimeMillis();
}
QBit uses the same style REST methods as Spring MVC REST support. QBit only supports JSON as body params and return types.
The gradle file to compile this is as follows:
group 'qbit-ex'
version '1.0-SNAPSHOT'
apply plugin: 'java'
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.8.16-RC2-SNAPSHOT'
compile group: 'io.advantageous.qbit', name: 'qbit-vertx', version: '0.8.16-RC2-SNAPSHOT'
}
Change the version to a release after 0.8.16 or build the snapshot.
The main method starts up the end point on port 8080 and then starts up an Admin Server on PORT 7777.
...
public static void main(final String... args) {
final ManagedServiceBuilder managedServiceBuilder =
ManagedServiceBuilder.managedServiceBuilder().setRootURI("/root");
/* Start the service. */
managedServiceBuilder.addEndpointService(new HelloWorldService())
.getEndpointServerBuilder()
.build().startServer();
/* Start the admin builder which exposes health end-points and meta data. */
managedServiceBuilder.getAdminBuilder().build().startServer();
System.out.println("Servers started");
}
Since this class has a main method, you should be able to run it from your IDE.
The admin server exposes health end-points and meta-data.
The default port for an end point is 8080, but you can override it by setting the environment variable
PORT
or WEB_PORT
.
By default, we enable endpoints to manage the server for health, and stats that works in a Heroku or Docker environment. There are methods on ManagedServiceBuilder
to disable health checks, etc.
There are also methods on ManagedServiceBuilder
to turn on StatsD and Consul support.
Let's look at the endpoints we have exposed so far.
$ curl http:https://localhost:8080/root/hello/hello
"hello 1438470713526"
$ curl http:https://localhost:7777/__admin/meta/
{
"swagger": "2.0",
"info": {
"title": "application title goes here",
"description": "Description not set",
"contact": {
"name": "ContactName not set",
"url": "Contact URL not set",
"email": "[email protected]"
},
"version": "0.1-NOT-SET",
"license": {
"name": "licenseName not set",
"url": "http:https://www.license.url.com/not/set/"
}
},
"host": "localhost:8888",
"basePath": "/root",
"schemes": [
"http",
"https",
"wss",
"ws"
],
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"paths": {
"/hello/hello": {
"get": {
"operationId": "hello",
"produces": [
"application/json"
],
"responses": {
"200": {
"description": "returns",
"schema": {
"type": "string"
}
}
}
}
}
}
}
You can import the above into the Swagger editor and generate clients in Python, Perl, PHP, Ruby, Java, C# and more. By the way, there are ways to configure all the parameters that say "set me" or some variation of the above.
There is a health endpoint to make working in Docker and Heroku easy or other similar cloud environments (EC2, VMWare cloud, OpenStack).
$ curl http://localhost:8080/__health
"ok"
This deceptively easy end-point will check every endpoint server, service, service queue, etc. to see if they are healthy and you can register your own health checks. This is not just your REST services but all of the IO services, nano services, etc. that they depend on. We could write a whole article on just the HealthService
, which is preconfigured with ManagedServiceBuilder
.
$ curl http:https://localhost:7777/__admin/ok
The above Returns true
if all registered health systems are healthy.
A node is a service, service bundle, queue, or server endpoint that is being monitored.
$ curl http:https://localhost:7777/__admin/all-nodes/
$ curl http:https://localhost:7777/__admin/healthy-nodes/
$ curl http:https://localhost:7777/__admin/load-nodes/
$ curl http:https://localhost:8080/__stats/instance
Let's say you want to use Consul. There is one method to enable it.
managedServiceBuilder.enableConsulServiceDiscovery("dc1", "localhost", 8500);
If you want to use the default host and port:
managedServiceBuilder.enableConsulServiceDiscovery("dc1");
Just do the above before you create your first endpoint server or service queue. The main endpoint will automatically register with Consul and periodically check-in health. It will even check with the internal health system to see if all of the nodes (service queues, endpoints etc.) are healthy and pass that information to Consul.
Enabling StatsD is also easy
managedServiceBuilder.getStatsDReplicatorBuilder()
.setHost("somehost").setPort(9000);
managedServiceBuilder.setEnableStats(true);
Just do the above before you create your first endpoint server or service queue. There are default stats gathered for all Service Queues and Endpoint servers.
ManagedServiceBuilder
is one stop shopping to writing a cloud friendly microservices in QBit.
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:https://www.mammatustech.com/Microservice-Service-Discovery-with-Consul)
Microservices Service Discovery Tutorial with Consul
[Reactive Microservices] (http:https://www.mammatustech.com/reactive-microservices)
[High Speed Microservices] (http:https://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