Spring Boot is a very well know framework, 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, how cool is that!
To demonstrate how to integrate QBit with Spring Boot, we will build a simple Hello Word application, that when you request http://localhost:8080/services/myapp/helloservice/hello it will return:
"Hello from QBit"
Let's go over this simple example:
We have a simple HelloService that will be added to a servlet and will return "Hello from QBit" when it is called:
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"; } }
As you can see QBit uses the same style annotations as Spring MVC. We figured these are the ones that most people are familiar with.
@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); }
@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.
@Bean public String dataDir() { return environment.getProperty("data.location"); } @Bean public HelloService helloService() { return new HelloService(); } @Bean public DispatcherServlet dispatcherServlet() { return new DispatcherServlet(); }
Finally for Spring we configure the application:
@Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(Application.class); }
In order to call the method helloWorld() under HelloService when the following address is requested http://localhost:8080/services/myapp/helloservice/hello we need a DispatcherServlet
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 ServiceServer serviceServer; public DispatcherServlet() { } @Override protected void stop() { serviceServer.stop(); } @Override protected void wireHttpServer(final HttpTransport httpTransport, final ServletConfig servletConfig) { serviceServer = serviceServerBuilder().setHttpTransport(httpTransport) .setUri(SERVICES_API_PROXY_URI_PREAMBLE) .build().initServices(helloService).startServer(); } }
This is it, we are done. QBit works with Spring Boot. Some of the things we can do with the two are discover servers with Spring, we could also write an adapter so QBit can run inside of Spring MVC. 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.
Full code Listing
You can also download/clone this example on github, here is the guide.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. * ________ __________.______________ * \_____ \\______ \ \__ ___/ * / / \ \| | _/ | | | ______ * / \_/. \ | \ | | | /_____/ * \_____\ \_/______ /___| |____| * \__> \/ * ___________.__ ____. _____ .__ .__ * \__ ___/| |__ ____ | |____ ___ _______ / \ |__| ___________ ____ ______ ______________ _|__| ____ ____ * | | | | \_/ __ \ | \__ \\ \/ /\__ \ / \ / \| |/ ___\_ __ \/ _ \/ ___// __ \_ __ \ \/ / |/ ___\/ __ \ * | | | 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); } }
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"; } }
References and reading materials:
1. Java Microservices Architecture
2. Microservices by Martin Fowler and James Lewis
3. Microservices Architecture by Chris Richardson
4. Micro services – Java, the Unix Way by James Lewis
5. Microservices, or How I Learned To Stop Making Monoliths and Love Conway’s Law by Cliff Moon
6. Micro service architecure by Fred George
7. Microservices are not a free lunch by Benjamin Wootton
8. Microservice Service Discovery with Consul
9. Antifragility and Microservices by Russ Miles
10. The Unix Philosophy
11. Conway’s Law
12. Reactive Microservices13. Amazon Architecture
14. Migrating to microservices by Adrian Cockroft
15. Microservices with Spring Boot
16. Microservices for the Grumpy Neckbeard
17. Microservices definition on Wikipedia
18. Microservices and DevOps by Adrian Cockcroft
19. Building and Deploying Microservices - Bart Blommaerts
20. Microservices on the JVM - Alexander Heusingfeld
21. Microservices Shaun Abrams
22. High Speed Microservices
No comments:
Post a Comment