[Detailed Tutorial] Using QBit Microservice lib with Spring Boot

fadihub edited this page Jun 7, 2015 · 8 revisions

##overview

Spring Boot offers a fast way to build applications. for example if you were to to build an app using Spring MVC, Spring Boot will automatically embed a Tomcat for you, if you want to use Jetty Spring Boot will handle that for you, and if you want to use Thymeleaf there would be a few beans that need to be added to your application context Spring Boot will do that for you. These are just a few examples of the automatic configuration Spring Boot provides. Spring Boot is very helpful, and now you can use QBit and Spring Boot together, you can configure QBit to work with Spring MVC, and you can use QBit as a servlet that handles requests.

Using QBit Microservice lib with Spring Boot

This wiki will walk you through a simple example to show you that you can make QBit work with Spring Boot together to form a very powerful tool.

What you will build

You will build a very simple application using QBit and Spring Boot together, when you run it you will get the following:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.2.1.RELEASE)

Then with your favorite browser when you visit http://localhost:8080/services/myapp/helloservice/hello you will get the following:

"Hello from QBit"

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/qbit-with-spring-boot.git

Once this is done you can test the service, let's first explain the process:

First we define the Spring Boot Application. The Application.java file would declare the main method, along with the basic Configurations.

@configuration is used for java-based configuration.

@EnableAutoConfiguration is the annotation that tells Spring Boot to “guess” how to configure Spring based on the dependencies that you have added.

@PropertySource annotation is to Change the location of the external properties of this application:

@Configuration
@EnableAutoConfiguration
@PropertySource(value = {"classpath:default.properties",
        "file:${properties.location}"},
        ignoreResourceNotFound = true)

To run the application:

public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }

Declare the following beans for spring; dataDir() returns the data directory which is /tmp you can check it under resources/default.properties, helloService(), and dispatcherServlet():

 @Bean
    public String dataDir() {
        return environment.getProperty("data.location");
    }

    @Bean
    public HelloService helloService() {
        return new HelloService();
    }

    @Bean
    public DispatcherServlet dispatcherServlet() {
        return new DispatcherServlet();
    }

Finally configure the application:

@Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

Here is HelloService, it is a simple Hello World example, that will be added to a servlet as you will see later:

package io.advantageous.qbit.example;


import io.advantageous.qbit.annotation.RequestMapping;

@RequestMapping("helloservice")
public class HelloService {


    @RequestMapping("hello")
    public String helloWorld() {
        return "Hello from QBit";
    }
}

Now that we are done with Spring Boot configuration and we have a simple HelloService. In order to call its helloWorld()method when you request the following address http://localhost:8080/services/myapp/helloservice/hello we will make a DispatcherServlet that subclasses QBitHttpServlet:

public class DispatcherServlet extends QBitHttpServlet {

...........

wire in the helloService bean:

@Autowired
    private HelloService helloService;

The main job of a DispatcherServlet is to call a method when a browser requests the page, and to combine its results with the matching JSP file to make an html document. In this case once the the following url http://localhost:8080/services/myapp/helloservice/hello is hit, helloWorld() method will be called:

public static final String SERVICES_API_PROXY_URI_PREAMBLE = "/services/myapp/";

.............
@Override
    protected void wireHttpServer(final HttpTransport httpTransport,
                                  final ServletConfig servletConfig) {


        serviceServer = endpointServerBuilder().setHttpTransport(httpTransport)
                .setUri(SERVICES_API_PROXY_URI_PREAMBLE)
                .build().initServices(helloService).startServer();


    }

That is it now we have QBit working with Spring Boot together, we can test this service now but first let's display the full code listings

Application.java Listing

src/main/java/io.advantageous.qbit.example/Application

/*******************************************************************************
 * 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.
 *  ________ __________.______________
 *  \_____  \\______   \   \__    ___/
 *   /  / \  \|    |  _/   | |    |  ______
 *  /   \_/.  \    |   \   | |    | /_____/
 *  \_____\ \_/______  /___| |____|
 *         \__>      \/
 *  ___________.__                  ____.                        _____  .__                                             .__
 *  \__    ___/|  |__   ____       |    |____ ___  _______      /     \ |__| ___________  ____  ______ ______________  _|__| ____  ____
 *    |    |   |  |  \_/ __ \      |    \__  \\  \/ /\__  \    /  \ /  \|  |/ ___\_  __ \/  _ \/  ___// __ \_  __ \  \/ /  |/ ___\/ __ \
 *    |    |   |   Y  \  ___/  /\__|    |/ __ \\   /  / __ \_ /    Y    \  \  \___|  | \(  <_> )___ \\  ___/|  | \/\   /|  \  \__\  ___/
 *    |____|   |___|  /\___  > \________(____  /\_/  (____  / \____|__  /__|\___  >__|   \____/____  >\___  >__|    \_/ |__|\___  >___  >
 *                  \/     \/                \/           \/          \/        \/                 \/     \/                    \/    \/
 *  .____    ._____.
 *  |    |   |__\_ |__
 *  |    |   |  || __ \
 *  |    |___|  || \_\ \
 *  |_______ \__||___  /
 *          \/       \/
 *       ____. _________________    _______         __      __      ___.     _________              __           __      _____________________ ____________________
 *      |    |/   _____/\_____  \   \      \       /  \    /  \ ____\_ |__  /   _____/ ____   ____ |  | __ _____/  |_    \______   \_   _____//   _____/\__    ___/
 *      |    |\_____  \  /   |   \  /   |   \      \   \/\/   // __ \| __ \ \_____  \ /  _ \_/ ___\|  |/ // __ \   __\    |       _/|    __)_ \_____  \   |    |
 *  /\__|    |/        \/    |    \/    |    \      \        /\  ___/| \_\ \/        (  <_> )  \___|    <\  ___/|  |      |    |   \|        \/        \  |    |
 *  \________/_______  /\_______  /\____|__  / /\    \__/\  /  \___  >___  /_______  /\____/ \___  >__|_ \\___  >__| /\   |____|_  /_______  /_______  /  |____|
 *                   \/         \/         \/  )/         \/       \/    \/        \/            \/     \/    \/     )/          \/        \/        \/
 *  __________           __  .__              __      __      ___.
 *  \______   \ ____   _/  |_|  |__   ____   /  \    /  \ ____\_ |__
 *  |    |  _// __ \  \   __\  |  \_/ __ \  \   \/\/   // __ \| __ \
 *   |    |   \  ___/   |  | |   Y  \  ___/   \        /\  ___/| \_\ \
 *   |______  /\___  >  |__| |___|  /\___  >   \__/\  /  \___  >___  /
 *          \/     \/             \/     \/         \/       \/    \/
 *
 * QBit - The Microservice lib for Java : JSON, WebSocket, REST. Be The Web!
 *  http://rick-hightower.blogspot.com/2014/12/rise-of-machines-writing-high-speed.html
 *  http://rick-hightower.blogspot.com/2014/12/quick-guide-to-programming-services-in.html
 *  http://rick-hightower.blogspot.com/2015/01/quick-start-qbit-programming.html
 *  http://rick-hightower.blogspot.com/2015/01/high-speed-soa.html
 *  http://rick-hightower.blogspot.com/2015/02/qbit-event-bus.html
 ******************************************************************************/

package io.advantageous.qbit.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

/**
 * @author Geoff Chandler
 * @author  (Rick Hightower)
 */
@Configuration
@EnableAutoConfiguration
@PropertySource(value = {"classpath:default.properties",
        "file:${properties.location}"},
        ignoreResourceNotFound = true)
public class Application extends SpringBootServletInitializer {

    @Autowired
    private Environment environment;

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public String dataDir() {
        return environment.getProperty("data.location");
    }

    @Bean
    public HelloService helloService() {
        return new HelloService();
    }

    @Bean
    public DispatcherServlet dispatcherServlet() {
        return new DispatcherServlet();
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }
}

DispatcherServlet.java Listing

src/main/java/io.advantageous.qbit.example/DispatcherServlet

/*******************************************************************************
 * 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!
 *  http://rick-hightower.blogspot.com/2014/12/rise-of-machines-writing-high-speed.html
 *  http://rick-hightower.blogspot.com/2014/12/quick-guide-to-programming-services-in.html
 *  http://rick-hightower.blogspot.com/2015/01/quick-start-qbit-programming.html
 *  http://rick-hightower.blogspot.com/2015/01/high-speed-soa.html
 *  http://rick-hightower.blogspot.com/2015/02/qbit-event-bus.html
 ******************************************************************************/

package io.advantageous.qbit.example;

import io.advantageous.qbit.http.HttpTransport;
import io.advantageous.qbit.server.ServiceEndpointServer
ServiceEndpointServer
ServiceEndpointServer;
import io.advantageous.qbit.servlet.QBitHttpServlet;

import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.ServletConfig;

import static io.advantageous.qbit.server.EndpointServerBuilder.endpointServerBuilder;

/**
 * @author Rick Hightower
 * Works as of March 16th, 2015.
 */
public class DispatcherServlet extends QBitHttpServlet {

    public static final String SERVICES_API_PROXY_URI_PREAMBLE = "/services/myapp/";


    @Autowired
    private HelloService helloService;
    //Hit this at http://localhost:8080/services/myapp/helloservice/hello


    private ServiceEndpointServer
ServiceEndpointServer
ServiceEndpointServer serviceServer;

    public DispatcherServlet() {

    }

    @Override
    protected void stop() {
        serviceServer.stop();
    }

    @Override
    protected void wireHttpServer(final HttpTransport httpTransport,
                                  final ServletConfig servletConfig) {


        serviceServer = endpointServerBuilder().setHttpTransport(httpTransport)
                .setUri(SERVICES_API_PROXY_URI_PREAMBLE)
                .build().initServices(helloService).startServer();


    }



}

HelloService.java Listing

src/main/java/io.advantageous.qbit.example/HelloService

/*******************************************************************************

 * 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.
 *  ________ __________.______________
 *  \_____  \\______   \   \__    ___/
 *   /  / \  \|    |  _/   | |    |  ______
 *  /   \_/.  \    |   \   | |    | /_____/
 *  \_____\ \_/______  /___| |____|
 *         \__>      \/
 *  ___________.__                  ____.                        _____  .__                                             .__
 *  \__    ___/|  |__   ____       |    |____ ___  _______      /     \ |__| ___________  ____  ______ ______________  _|__| ____  ____
 *    |    |   |  |  \_/ __ \      |    \__  \\  \/ /\__  \    /  \ /  \|  |/ ___\_  __ \/  _ \/  ___// __ \_  __ \  \/ /  |/ ___\/ __ \
 *    |    |   |   Y  \  ___/  /\__|    |/ __ \\   /  / __ \_ /    Y    \  \  \___|  | \(  <_> )___ \\  ___/|  | \/\   /|  \  \__\  ___/
 *    |____|   |___|  /\___  > \________(____  /\_/  (____  / \____|__  /__|\___  >__|   \____/____  >\___  >__|    \_/ |__|\___  >___  >
 *                  \/     \/                \/           \/          \/        \/                 \/     \/                    \/    \/
 *  .____    ._____.
 *  |    |   |__\_ |__
 *  |    |   |  || __ \
 *  |    |___|  || \_\ \
 *  |_______ \__||___  /
 *          \/       \/
 *       ____. _________________    _______         __      __      ___.     _________              __           __      _____________________ ____________________
 *      |    |/   _____/\_____  \   \      \       /  \    /  \ ____\_ |__  /   _____/ ____   ____ |  | __ _____/  |_    \______   \_   _____//   _____/\__    ___/
 *      |    |\_____  \  /   |   \  /   |   \      \   \/\/   // __ \| __ \ \_____  \ /  _ \_/ ___\|  |/ // __ \   __\    |       _/|    __)_ \_____  \   |    |
 *  /\__|    |/        \/    |    \/    |    \      \        /\  ___/| \_\ \/        (  <_> )  \___|    <\  ___/|  |      |    |   \|        \/        \  |    |
 *  \________/_______  /\_______  /\____|__  / /\    \__/\  /  \___  >___  /_______  /\____/ \___  >__|_ \\___  >__| /\   |____|_  /_______  /_______  /  |____|
 *                   \/         \/         \/  )/         \/       \/    \/        \/            \/     \/    \/     )/          \/        \/        \/
 *  __________           __  .__              __      __      ___.
 *  \______   \ ____   _/  |_|  |__   ____   /  \    /  \ ____\_ |__
 *  |    |  _// __ \  \   __\  |  \_/ __ \  \   \/\/   // __ \| __ \
 *   |    |   \  ___/   |  | |   Y  \  ___/   \        /\  ___/| \_\ \
 *   |______  /\___  >  |__| |___|  /\___  >   \__/\  /  \___  >___  /
 *          \/     \/             \/     \/         \/       \/    \/
 *
 * QBit - The Microservice lib for Java : JSON, WebSocket, REST. Be The Web!
 *  http://rick-hightower.blogspot.com/2014/12/rise-of-machines-writing-high-speed.html
 *  http://rick-hightower.blogspot.com/2014/12/quick-guide-to-programming-services-in.html
 *  http://rick-hightower.blogspot.com/2015/01/quick-start-qbit-programming.html
 *  http://rick-hightower.blogspot.com/2015/01/high-speed-soa.html
 *  http://rick-hightower.blogspot.com/2015/02/qbit-event-bus.html

 ******************************************************************************/

package io.advantageous.qbit.example;


import io.advantageous.qbit.annotation.RequestMapping;

@RequestMapping("helloservice")
public class HelloService {


    @RequestMapping("hello")
    public String helloWorld() {
        return "Hello from QBit";
    }
}

build.gradle


apply plugin: 'idea'
apply plugin: 'java'
apply plugin: 'application'


sourceCompatibility = 1.8
version = '1.0'

repositories {
    mavenLocal()
    mavenCentral()
}


dependencies {
    compile group: 'io.advantageous.qbit', name: 'qbit-servlet', version: '0.7.2'
    compile group: 'javax.inject', name: 'javax.inject', version: '1'
    compile('org.springframework.boot:spring-boot-starter-web:1.2.1.RELEASE') {
        exclude module: 'spring-boot-starter-tomcat'
    }
    compile 'org.eclipse.jetty:jetty-webapp:9.+'
    compile 'org.eclipse.jetty:jetty-jsp:9.+'

    testCompile "junit:junit:4.11"
    testCompile "org.slf4j:slf4j-simple:[1.7,1.8)"
}

Test The Service

With your terminal cd qbit-with-spring-boot

then gradle clean build then gradle run and finally visit or curl http://localhost:8080/services/myapp/helloservice/hello you should get the following:

"Hello from QBit"

Summary

Since QBit is working with Spring Boot as you can see, we can do all sorts of things with Spring like discover servers, we could also write an adapter so QBit can run inside of Spring MVC. This would not be hard. This example's DispatcherServlet just uses the QBit support for adapting QBit to run inside of a servlet container. If you are skilled with Spring, you could imagine creating lifecycle listeners and do a lot of the service discovery on the fly. You have built and tested a simple application using QBit and Spring Boot together, 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
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.