-
Notifications
You must be signed in to change notification settings - Fork 140
[Rough Cut] Using QBit microservice lib with Spring Boot
You can use Spring Boot and QBit together. You could configure QBit to work with/in Spring MVC, or you can just use QBit as the servlet that handles requests.
Remember this about QBit. It runs standalone with Jetty or Vertx. It can run in any Servlet container.
Here is a basic example combining QBit in the Spring Boot world.
$ tree
.
├── build.gradle
├── gradle
│ └── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── qbit-spring-boot-together.iml
├── settings.gradle
└── src
└── main
├── java
│ └── io
│ └── advantageous
│ └── qbit
│ └── examples
│ └── spring
│ ├── Application.java
│ ├── DispatcherServlet.java
│ └── HelloService.java
└── resources
└── default.properties
11 directories, 11 files
We present the above which is a simple project structure. We used gradle but you could easily use maven.
First we define the Application and Configuration bean as follows:
/**
* @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);
}
}
There are many ways to do this. This example picked a simple approach.
Next we subclass a QBit Servlet as follows:
package io.advantageous.qbit.examples.spring;
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
*/
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();
}
}
Now one could imagine that we could do all sorts of thing with Spring like discover servers and what not. We could also write an adapter so QBit can run inside of Spring MVC. This would not be hard. The above just uses the QBit support for adapting QBit to run inside of a servlet container.
Lastly is our Hello World example which we injected into our Servlet.
import io.advantageous.qbit.annotation.RequestMapping;
@RequestMapping("helloservice")
public class HelloService {
@RequestMapping("hello")
public String helloWorld() {
return "Hello from QBit";
}
}
If you are skilled with Spring, you could imagine creating lifecycle listeners and do a lot of the service discovery on the fly. (I did this before with Crank and Geoff did it before as well.)
Here is the build file.
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.6.1-SNAPSHOT'
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)"
}
You can find the complete example in git.
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