On the first day of the 7-day learning Spring cloud, familiarize yourself with spring boot and create an app using Spring boot.
Spring boot is a new framework for the spring team, and the core technology used is the spring framework, mainly spring 4.x, so if you are familiar with spring 4, you can quickly accept and learn this framework. Spring boot can be seen as a layer on the basis of the spring framework, which includes modules that facilitate configuration management and rapid development of developers, as well as a number of out-of-the-box tools, such as monitoring.
Spring Boot official documents are available in Chinese translation:
Https://github.com/qibaoguang/Spring-Boot-Reference-Guide
There is no difference between implementing a spring boot development environment and traditional applications:
Ide:myeclipse 10
jdk:jdk1.7
WINDOWS:MVN 3
In a desktop Windows environment, a separate installation is required for us to package and operate using the command line. The eclipse environment also needs to install the MVN plug-in, of course, if the use of MyEclipse, then the MVN environment is sufficient. Here are the steps to build the spring boot HelloWorld app. Note that this is a Web application that uses embedded tomcat.
1) preferred to create one of the simplest MAVEN projects, such as:
This application has only one class, and the code is as follows:
Package Cloud.simple.hello;import Org.springframework.boot.*;import org.springframework.boot.autoconfigure.*; Import Org.springframework.stereotype.*;import org.springframework.web.bind.annotation.*;@ Controller@springbootapplicationpublic class Samplecontroller { @ResponseBody @RequestMapping (value = "/") String Home () { return "Hello world!"; } public static void Main (string[] args) throws Exception { springapplication.run (samplecontroller.class, args);} }
@SpringBootApplication equivalent to @configuration, @EnableAutoConfiguration, and @ComponentScan, you can also use these 3 annotations at the same time. Where @configuration, @ComponentScan is the syntax of the spring framework, is available in spring 3.x, and is used to create configuration information and scan packages in code mode. @EnableAutoConfiguration is the spring boot syntax, which means that automatic configuration will be used. If you download the spring boot source, you will see that spring boot implements a number of starter applications, which are some of the configuration information (a bit like Docker, a set of environments, an application concept), and spring Boot sees the introduction of the starter package, which can be calculated if your app is automatically configured.
2) configuration Pom.xml
This application does not need the configuration file, after writing the class can directly configure the Pom.xml file, of course, the first configuration pom.xml is the same. The pom file is configured as follows:
<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >< modelversion>4.0.0</modelversion><!--Spring Boot Basic Environment--><parent><groupid> org.springframework.boot</groupid><artifactid>spring-boot-starter-parent</artifactid>< version>1.3.1.release</version></parent><groupid>spring.boot</groupid>< Artifactid>cloud-simple-helloword</artifactid><version>0.0.1</version><packaging> jar</packaging><name>cloud-simple-helloword</name><dependencies><!--Web application Basic Environment configuration-- ><dependency><groupId>org.springframework.boot</groupId><artifactId> Spring-boot-starter-web</artifactid></dependency></dependencies><build><plugins> <plugin><groupid>org.springframework.boot</groupid><artifactid>spring-boot-maven-plugin</artifactid></plugin></plugins></ Build></project>
After the Pom file is configured, you can run the app, click F11, or in the Samplecontroller class right-click Run Java application to see the app start and run.
At this point in the browser input http://localhost:8080/, you will see the HelloWorld word, this is a Web application, using the embedded tomcat.
In the POM configuration we only use Spring-boot-starter-web dependencies, spring boot will download the relevant jar package based on this dependency and initialize the basic operating environment, such as binding port 8080.
Spring boot encapsulates all configuration information as a key value type, and you want to change the default configuration by passing the key value pair to the app, such as if we want to change the bound port to 8081, then you can pass in the main method "-server.port=8081", or simply use:
Springapplication.run (Samplecontroller.class, "--server.port=8081");
3) Deploy the Spring boot app
To deploy the spring boot app, you prefer to package the spring boot app, and the Spring-boot-maven-plugin plugin you see in the Pom file is packaged with the spring boot app.
Enter the project catalog to run the MVN package, such as:
D:\CLOUD-SIMPLE-HELLOWORD>MVN Package
After packing, you can enter the target directory to execute the application using the Java Native command.
D:\cloud-simple-helloword\target>java-jar Cloud-simple-helloword-0.0.1.jar--server.port=8081
So, you'll see a jar-based web App launch.
Some of the out-of-the-box applications provided by Spring Boot are very easy to use, such as monitoring, and you only need to introduce them in the Pom file:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
After introduction, spring boot is enabled by default, and you can enter it in the browser by running the app:
Http://localhost:8080/health
You can see the default monitoring information:
{' Status ': ' Up ', ' diskspace ': {' status ': ' Up ', ' total ': 161067397120, ' free ': 91618398208, ' threshold ': 10485760}}
Information includes the program execution status and basic disk information.
Spring Cloud Tutorial using spring Boot to create an app