-
Notifications
You must be signed in to change notification settings - Fork 140
[Detailed Tutorial] Using QBit microservice lib's REST support with URI Params
##Overview
QBit supports JSON, HTTP, REST and WebSocket. With QBit you can access a service via an HTTP client API, a high-speed WebSocket proxy and curl. As part of its REST support, it supports arguments to service methods which are URI params. You can also use request params.
You can invoke QBit services via a WebSocket proxy. The advantage of a WebSocket proxy is it allows you execute 1M RPC+ a second (1 million remote calls every second).
QBit uses the same style annotations as Spring MVC. We figure these are the ones that most people are familiar with. Since QBit focuses just on Microservices, it just supports JSON.
This wiki will walk you through a simple example to demonstrate to you how you can use a microservice remotely with WebSocket and how you can make a Rest call with URI params with QBit.
You will build a simple Rest server, call an AdderService
remotely via WebSocket, and call the AdderService
via URI params. when you run this example you will get the following:
Calling the adderService async via a WebSocket proxy interface = 3
Calling the adderService via the URI parameters = 4
also with your favorite browser you can visit http://localhost:7000/services/adder-service/add/3/987 to call the adderService
and add the last two params, in this case you will get:
990
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-with-uri-params/archive/master.zip) and unzip the source repository for this guide, or clone it using Git:
https://github.com/fadihub/simple-rest-server-with-uri-params.git
Once this is done you can test the service, let's first explain the process:
We have an `adderService that adds two numbers:
@RequestMapping("/adder-service")
public static class AdderService {
@RequestMapping("/add/{0}/{1}")
public int add(@PathVariable int a, @PathVariable int b) {
return a + b;
}
}
As you can see QBit uses the same style annotations as Spring MVC (@RequestMapping
and @PathVariable
).
We also have the AdderServiceClientInterface
:
interface AdderServiceClientInterface {
void add(Callback<Integer> callback, int a, int b);
}
To start the server:
QBitSystemManager systemManager = new QBitSystemManager();
/* Start Service server. */
final ServiceEndpointServer
ServiceEndpointServer
ServiceEndpointServer server = endpointServerBuilder()
.setSystemManager(systemManager)
.setPort(7000).build();
server.initServices(new AdderService());
server.start();
Create the proxy using the client interface to enable WebSocket calls to the AdderService
:
/* Create a proxy to the service. */
final AdderServiceClientInterface adderService =
client.createProxy(AdderServiceClientInterface.class, "adder-service");
Then build QBit's client for WebSocket calls and start the client:
final Client client = clientBuilder().setPort(7000).setRequestBatchSize(1).build();
client.start();
Now we can call the AdderService
remotely via WebSocket:
System.out.print("Calling the adderService async via a WebSocket proxy interface = ");
adderService.add(System.out::print, 1, 2);
this will give us:
Calling the adderService async via a WebSocket proxy interface = 3
Create a http client and start it:
HttpClient httpClient = httpClientBuilder()
.setHost("localhost")
.setPort(7000).build();
httpClient.start();
Here we get a hard coded URI (/services/adder-service/add/2/2
) using the httpClient
and this will call the AdderService
and add the params 2 + 2
:
String results = httpClient
.get("/services/adder-service/add/2/2")
.body();
System.out.print("Calling the adderService via the URI parameters = ");
System.out.println(results);
This will return the following:
Calling the adderService via the URI parameters = 4
This is it you can test the service now.
src/main/java/io.advantageous.qbit.example/SimpleRestServerWithURIParamsMain
/*
* Copyright (c) 2015. Rick Hightower, Geoff Chandler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* QBit - The Microservice lib for Java : JSON, WebSocket, REST. Be The Web!
*/
package io.advantageous.qbit.example;
import io.advantageous.boon.core.Sys;
import io.advantageous.qbit.annotation.PathVariable;
import io.advantageous.qbit.annotation.RequestMapping;
import io.advantageous.qbit.client.Client;
import io.advantageous.qbit.http.client.HttpClient;
import io.advantageous.qbit.server.ServiceEndpointServer
ServiceEndpointServer
ServiceEndpointServer;
import io.advantageous.qbit.reactive.Callback;
import io.advantageous.qbit.system.QBitSystemManager;
import static io.advantageous.qbit.client.ClientBuilder.clientBuilder;
import static io.advantageous.qbit.http.client.HttpClientBuilder.httpClientBuilder;
import static io.advantageous.qbit.server.EndpointServerBuilder.endpointServerBuilder;
/**
* @author rhightower
* on 2/17/15.
*/
public class SimpleRestServerWithURIParamsMain {
public static void main(String... args) throws Exception {
QBitSystemManager systemManager = new QBitSystemManager();
/* Start Service server. */
final ServiceEndpointServer
ServiceEndpointServer
ServiceEndpointServer server = endpointServerBuilder()
.setSystemManager(systemManager)
.setPort(7000).build();
server.initServices(new AdderService());
server.start();
/* Start QBit client for WebSocket calls. */
final Client client = clientBuilder().setPort(7000).setRequestBatchSize(1).build();
/* Create a proxy to the service. */
final AdderServiceClientInterface adderService =
client.createProxy(AdderServiceClientInterface.class, "adder-service");
client.start();
/* Call the service */
System.out.print("Calling the adderService async via a WebSocket proxy interface = ");
adderService.add(System.out::print, 1, 2);
HttpClient httpClient = httpClientBuilder()
.setHost("localhost")
.setPort(7000).build();
httpClient.start();
String results = httpClient
.get("/services/adder-service/add/2/2")
.body();
System.out.println("\n");
System.out.print("Calling the adderService via the URI parameters = ");
System.out.println(results);
// HttpResponse httpResponse = httpClient.get("/services/adder-service/foo/randomcrap/2");
// //System.out.println(httpResponse);
//
//
// httpResponse = httpClient.get("/services/adder-bs/foo/randomcrap/2");
// //System.out.println(httpResponse);
Sys.sleep(100);
//client.stop();
//httpClient.stop();
//systemManager.shutDown();
}
interface AdderServiceClientInterface {
void add(Callback<Integer> callback, int a, int b);
}
@RequestMapping("/adder-service")
public static class AdderService {
@RequestMapping("/add/{0}/{1}")
public int add(@PathVariable int a, @PathVariable int b) {
return a + b;
}
}
}
With your terminal cd simple-rest-server-with-uri-params
then gradle clean build
then gradle run
and you should get the following:
Calling the adderService async via a WebSocket proxy interface = 3
Calling the adderService via the URI parameters = 4
You can also access the AdderService
when you visit http://localhost:7000/services/adder-service/add/3/987
to add the last two params, in this case you will get:
990
You have built/tested a simple Rest server, called an AdderService
remotely via WebSocket and called an AdderService
via URI params using 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