Home > php教程 > PHP开发 > Building microservices: Getting started with Spring boot

Building microservices: Getting started with Spring boot

高洛峰
Release: 2016-11-22 13:20:48
Original
2254 people have browsed it

What is spring boot

Spring Boot is a new framework provided by the Pivotal team. It is designed to simplify the initial construction and development process of new Spring applications. The framework uses an ad hoc approach to configuration, eliminating the need for developers to define boilerplate configurations. To understand it in my words, spring boot is actually not a new framework. It configures the use of many frameworks by default, just like maven integrates all jar packages, and spring boot integrates all frameworks (I don’t know if this metaphor is appropriate. ).

What are the benefits of using spring boot

In fact, it is simple, fast and convenient! What should we do if we need to build a spring web project?

1) Configure web.xml, load spring and spring mvc

2) Configure database connection, configure spring transactions

3) Configure reading of loading configuration files, enable annotations

4) Configure log files

.. .

After the configuration is completed, deploy tomcat for debugging

...

Microservices are very popular now. If my project just needs to send an email, if my project just needs to produce a point; I need to do this all over again!

But what if you use spring boot?

It’s very simple. I only need a few configurations to quickly and easily set up a web project or build a microservice!

Quick Start

Having said so much, my hands are itchy, let’s give it a try right away!

maven build project

1. Visit http://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 1.7 for the java version, as shown in the figure below:

Building microservices: Getting started with Spring boot

3. Click Generate Project Download the project compressed package

4. After unzipping, use eclipse, Import -> Existing Maven Projects -> Next -> Select the unzipped folder -> Finsh, OK done!

Project structure introduction

Building microservices: Getting started with Spring boot

As shown in the picture above, the basic structure of Spring Boot has 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, the directory results recommended by spingboot are as follows:

root package structure: com.example.myproject

com +- example +- myproject +- Application.java | +- domain | +- Customer.java | +- CustomerRepository.java | +- service | +- CustomerService.java | +- controller | +- CustomerController.java |
Copy after login

1. Application.java is recommended to be placed under the following directory, mainly used for some framework configuration

2. The domain directory is mainly used for the entity (Entity) and data access layer (Repository)

3. The service layer is mainly business code

4. The controller is responsible for page access control

Using the default configuration can save a lot Configuration, of course, you can also change it according to your own preferences

Finally, start the Application main method, and now a java project is set up!

Introducing the web module

1. Add web-supporting modules to pom. , logs and YAML;

spring-boot-starter-test: test modules, including JUnit, Hamcrest, Mockito.

2. Write controller content

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
 </dependency>
Copy after login

@RestController means that all methods in the controller are output in json format, and there is no need to write any jackjson configuration!

3. Start the main program, open the browser and visit http://localhost:8080/hello, and you can see the effect. It’s very simple!

How to do unit testing

打开的src/test/下的测试入口,编写简单的http请求来测试;使用mockmvc进行,利用MockMvcResultHandlers.print()打印出执行结果。

@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 after login

开发环境的调试

热启动在正常开发项目中已经很常见了吧,虽然平时开发web项目过程中,改动项目启重启总是报错;但springBoot对调试支持很好,修改之后可以实时生效,需要添加以下的配置:

    

        org.springframework.boot

        spring-boot-devtools

        true

   

    

        

            org.springframework.boot

            spring-boot-maven-plugin

            

                true

            

        

   

该模块在完整的打包环境下运行的时候会被禁用。如果你使用java -jar启动应用或者用一个特定的classloader启动,它会认为这是一个“生产环境”。

 

总结

使用spring boot可以非常方便、快速搭建项目,使我们不用关心框架之间的兼容性,适用版本等各种问题,我们想使用任何东西,仅仅添加一个配置就可以,所以使用sping boot非常适合构建微服务。


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template