Building microservices: An Introduction to Spring Boot
What is Spring Boot
Spring Boot is a new framework provided by the Pivotal team that is designed to simplify the initial setup and development of new Spring applications. The framework is configured in a way that eliminates the need for developers to define boilerplate configurations. In my words, Spring Boot is not a new framework. It has a default configuration for many frameworks. Just as Maven integrates all jar packages, Spring Boot integrates all frameworks (I don’t know if this metaphor is appropriate).
What are the benefits of using Spring Boot
Simple, fast and convenient! What do you do when you need to build a Spring Web project?
1) Configure web. XML and load Spring and Spring MVC
2) Configure database connection and Spring transactions
3) The configuration load can read the configuration file and enable annotation
4) Configure log files
…
Deploy Tomcat debugging once the configuration is complete
…
Microservices are very popular now. If my project only needs to send an email, if my project only needs to produce a credit; I need to do this!
But what about using Spring Boot?
Simple, I can quickly and easily set up a Web project or build a microservice with very few configurations!
Quick start
Said so much, the hand itch is very, immediately to try a round!
- Maven build project
1. Access start.spring. IO /
2, select the build tool Maven Project, Spring Boot version 1.3.6 and some basic Project information, click “Switch to the full version.” Select Java version 1.7, as shown in the following figure:
3. Click Generate Project to download the Project package
Existing Maven Projects -> Next -> Finsh, OK done!
- Introduction to project Structure
As shown in the figure above, the Spring Boot infrastructure consists of three files:
- L SRC /main/ Java program development and main program entry
- L SRC /main/resources configuration file
- L SRC /test/ Java test program
In addition, spingBoot suggests the following directory result:
Root Package structure: com.example.myProject
com +- example +- myproject +- Application.java | +- domain | +- Customer.java | +- CustomerRepository.java | +- service | +- CustomerService.java | +- controller | +- CustomerController.java |Copy the code
Application. Java is recommended to be placed under the following directory. It is mainly used to do some framework configuration
Domain directory is used for Entity and data access layer.
3. The Service layer is mainly business class code
4. Controller is responsible for page access control
Using the default configuration saves a lot of configuration, but you can also change it as you like
Finally, launch the Application Main method, and a Java project is set up!
- Importing a Web Module
1. Add a web support module to POm. XML:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Copy the code
There are two modules in the pom.xml file by default:
Spring-boot-starter: Core module, including automatic configuration support, logging, and YAML;
Spring-boot-starter -test: tests modules, including JUnit, Hamcrest, and Mockito.
2. Write controller content
@RestController public class HelloWorldController { @RequestMapping("/hello") public String index() { return "Hello World"; }}Copy the code
@restController means that all the methods in the controller are output in JSON format, so there is no need to write jackJSON configuration!
3, start the main program, open a browser to http://localhost:8080/hello, you can see the effect, is there a simple!
How to do unit tests
Open the test portal under SRC /test/ and write a simple HTTP request to test; Using mockmvc, using MockMvcResultHandlers. Print () to print out the results.
@RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = MockServletContext.class) @WebAppConfiguration public class HelloWorldControlerTests { private MockMvc mvc; @Before public void setUp() throws Exception { mvc = MockMvcBuilders.standaloneSetup(new HelloWorldController()).build(); } @Test public void getHello() throws Exception { mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON)) .andExpect(MockMvcResultMatchers.status().isOk()) .andDo(MockMvcResultHandlers.print()) .andReturn(); }}Copy the code
Debug the development environment
Hot start is very common in normal development projects, although during the development of web projects, changes to the project start and restart always report errors; However, springBoot has good debugging support and changes can take effect in real time. You need to add the following configuration:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
Copy the code
This module is disabled when running in a fully packaged environment. If you start your application with a Java-jar or with a specific Classloader, it will consider it a “production environment.”
conclusion
Using Spring Boot makes it very easy and quick to build a project. We don’t have to worry about compatibility between frameworks, suitable versions, etc. We want to use anything, just add a configuration, so using Sping Boot is perfect for building microservices.