Skip to content

[Quick Start] Using QBit microservice lib's REST support with URI Params

fadihub edited this page Jun 8, 2015 · 8 revisions

##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.

Using QBit microservice lib's REST support with URI Params

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.

What you will build

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

How to complete this guide

In order to complete this example successfully you will need the following installed on your machine:

Now that your machine is all ready let's get started:

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:

The process will be explained in more detail under [[Detailed Tutorial] Using QBit microservice lib's REST support with URI Params ] (https://github.com/advantageous/qbit/wiki/%5BDetailed-Tutorial%5D-Using-QBit-microservice-lib's-REST-support-with-URI-Params)

EmployeeEventExampleUsingChannelsToSendEvents.java Listing

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;


        }

    }




}

Test The Service

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

Summary

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!

Tutorials

__

Docs

Getting Started

Basics

Concepts

REST

Callbacks and Reactor

Event Bus

Advanced

Integration

QBit case studies

QBit 2 Roadmap

-- Related Projects

Kafka training, Kafka consulting, Cassandra training, Cassandra consulting, Spark training, Spark consulting

Clone this wiki locally