-
Notifications
You must be signed in to change notification settings - Fork 140
[Detailed Tutorial] Using QBit Microservice lib with Spring Boot
##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.
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.
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"
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/qbit-with-spring-boot/archive/master.zip) and unzip the source repository for this guide, or clone it using Git:
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
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);
}
}
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();
}
}
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";
}
}
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)"
}
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"
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!
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