I. Overview
Spring Boot Actuator is a child project of spring boot. With it, you can add some production-level services to your app without requiring special configuration. This tutorial shows the use of Eclipse + maven to build a restful app from scratch.
The effect of this application is to access http://localhost:8080/fuck?name=xxx, return a JSON string, and access http://localhost:8080/metrics to see some information about the application heap.
What is Spring Boot actuator?
Actuator is a machine term, there is no unified translation, can be translated as an actuator, actuator through a small change can produce a great move.
Spring Boot Actuator provides some of the features you need in your production environment, such as auditing, health, and data collection. Enables you to manage and monitor your applications over HTTP (requires SPRINGMVC), JMX, and even remote shell (SSH, Telnet) After you deploy the application to the production environment. For example, in an HTTP environment, access the Contexturl/health path to query the current environment's disk space, application and database status, etc., access the Contexturl/metrics path to query the current JVM memory usage, the status of the thread pool, and so on.
Second, the Environment preparation 1. Install JDK8 (Eclipse neon requires jdk1.8)
https://java.com/zh_CN/download/
2. Install eclipse, it is recommended to install Mars or the latest version of Neon
Http://www.eclipse.org/downloads/packages/eclipse-ide-java-ee-developers/marsr
Or
Http://www.eclipse.org/downloads/packages/eclipse-ide-java-ee-developers/neonr
3. Install MAVEN, must be after 3.0 version, recommended installation 3.3.9
https://maven.apache.org/download.cgi
4. Configure the JDK and Maven5 in Eclipse. Install the Spring IDE plugin into eclipse:
Https://marketplace.eclipse.org/content/spring-ide
Or search the Spring IDE directly on Eclipse Marketplace
※ If it is not possible to install, do not use Eclipse Marketplace, use install New software and enter the following address, select all when installing.
Neon:
http://dist.springsource.com/release/TOOLS/update/e4.6/
Mars:
http://dist.springsource.com/release/TOOLS/update/e4.5/
If this plugin does not fit, please look directly below "Iv. Operation Guide 2-Build with Eclipse + Maven plugin"
Third, Operation Guide 1-Build 1 with Eclipse + springide plugin. New spring Starter Project in eclipse
and named Restdemo.
Choose Boot Version 1.3.6, relying on select actuator and web
Then click Finish to finish creating.
2. View project Structure
3. Create a class file 3.1 view the Spring plugin automatically generates the main class file for us Restdemoapplication.java:
package com.example;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublicclass RestdemoApplication { publicstaticvoidmain(String[] args) { SpringApplication.run(RestdemoApplication.class, args); }}
※ @SpringBootApplication annotations provide automatic configuration for the project based on the content in Classpath. In this application, it is equivalent to: @Configuration,
@EnableAutoConfiguration, @ComponentScan the union of the three.
3.2 Create the form file Fuck.java under the Com.example package as follows:
PackageCom.example;/** * @ClassName: Fuck * @Description: TODO (here is a word describing the role of this class) * @author LIUYUEFENG559 * @date July 11, 2016 PM 2:38:22 * * Public class fuck { Private Final LongIdPrivate FinalString content; Public Fuck(LongID, String content) { This. id = ID; This. Content = content; } Public Long getId() {returnId } PublicStringgetcontent() {returnContent }}
3.3 Create the Controller:FuckController.java under the Com.example package, as follows:
PackageCom.example;ImportJava.util.concurrent.atomic.AtomicLong;ImportOrg.springframework.stereotype.Controller;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RequestMethod;ImportOrg.springframework.web.bind.annotation.RequestParam;ImportOrg.springframework.web.bind.annotation.ResponseBody;/** * @ClassName: Fuckcontroller * @Description: TODO (here is a word describing the role of this class) * @author LIUYUEFENG559 * @date July 11, 2016 afternoon 2:40:12 * *@Controller@RequestMapping("/fuck") Public class fuckcontroller { Private Static FinalString template ="Hi,%s! Let's Fuck the GFW, the government and Damn Dog Day Fang binxing! ";Private FinalAtomiclong counter =NewAtomiclong ();@RequestMapping(method = Requestmethod.get) Public@ResponseBody FuckSayfuck( @Requestparam(Value ="Name", required =false, DefaultValue ="Stranger") (String name) {return NewFuck (Counter.incrementandget (), String.Format (template, name)); }}
4. Run the project: right-->run as-a Spring Boot App, the console displays the content as follows to indicate a successful run.
\\ /___ ' _ _ _ _ _ _ _ __ _ \ \ \ (() \_ __ | ' _ | ' _| | ' _ \/ _ ' | \ \ \ \\ /___) | |_) | | | | | | | (_| | ))) ' |____|. __|_| |_|_| |_\_ _, | ////=========|_|==============|___/=/_/_/_/:: Spring Boot:: (v1.3.6.release) ... 2016-07-11 14:46:36.700 INFO 11648---[ main] com.example.RestdemoApplication:Started restdemoapplication in 7.7 seconds (JVM running for 8.844)
※it is equivalent to selecting the Maven build-> clean package, compiling it into a standalone jar, and then using Jar-jar Xxx.jar
To launch the app.
5. Access Http://localhost:8080/fuck on the browser to view the output:
6. External Properties File
Spring boot loads the properties in the following order:
(a) command-line arguments. such as: Java-jar app.jar–name= "Spring"
(b) Spring_application_json. such as java-dspring.application.json= ' {"foo": "Bar"} '-jar Myapp.jar
(iii) Jndi attributes from Java:comp/env
(d) Java System properties (System.getproperties ()).
(v) OS environment variables
(vi) Only attributes contained in the random.* will produce a randomvaluepropertysource
(vii) Application properties file (application-{profile}.properties or Yaml file) specified by profile, external to the jar package
(eight) inside the jar package, the application properties file (application-{profile}.properties or Yaml file) specified by profile
(ix) Application properties (Application.properties and Yaml) outside the jar package.
(10) Application properties (Application.properties and YAML) inside the jar package.
(11) @propertysource annotations within the @Configuration annotation class
(12) Default properties set by Springapplication.setdefaultproperties
What is YAML?
Yaml is a superset of JSON that is easier to read than XML files and is ideal for defining hierarchical configuration data. The Yaml file suffix is yml or ymal,eclipse can be installed Yedit plugin to edit the Yaml file.
The typical Yaml file content format is as follows:
Introducing the Snakeyaml Library will enable the Springapplication class to automatically support Yaml. Using Spring-boot-starter will automatically introduce Yaml.
So in this case, you can configure the template string in the properties file. Modify the Application.properties as follows:
server.port=8080fuck.sentence=Hi, %s! Let‘sand
Modify the Fuckcontroller as follows:
@Controller@RequestMapping("/fuck") Public class fuckcontroller { //private static final String template = "Hi,%s! Let ' s fuck the GFW, the //Government and Damn Dog Day's Fang Binxing! "; Private FinalAtomiclong counter =NewAtomiclong ();@Value("${fuck.sentence}")PrivateString template;@RequestMapping(method = Requestmethod.get) Public@ResponseBody FuckSayfuck( @Requestparam(Value ="Name", required =false, DefaultValue ="Stranger") (String name) {return NewFuck (Counter.incrementandget (), String.Format (template, name)); }}
Run again after the result is the same as the 5th step.
7. Package the project into a deployable war package, rather than a runnable jar Package 7.1 Provides a Springbootservletinitializer implementation class and overrides the Configure method
This will get Servlet3.0 support for the spring framework and can be configured before the app is started by the servlet container. A typical implementation is to have the main class of the application integrate Springbootservletinitializer.
This example modifies the Restdemoapplication class as follows:
PackageCom.example;ImportOrg.springframework.boot.SpringApplication;ImportOrg.springframework.boot.autoconfigure.SpringBootApplication;ImportOrg.springframework.boot.builder.SpringApplicationBuilder;ImportOrg.springframework.boot.context.web.SpringBootServletInitializer;@SpringBootApplication Public class restdemoapplication extends springbootservletinitializer { @Override protectedSpringapplicationbuilderConfigure(Springapplicationbuilder builder) {returnBuilder.sources (Restdemoapplication.class); } Public Static void Main(string[] args) {Springapplication.run (restdemoapplication.class, args); }}
※ The container does not support Servletcontextinitializer if the servlet is used before 3.0. You will need to add Web. XML and configure Dispatcherservlet in Web. XML to load the ApplicationContext.
7.2 Modify the Pom file, change the package format to war, and change the dependency range of the embedded servlet container to provided
This prevents the embedded servlet container from interfering with the servlet container to which the war packet is to be deployed.
In this example, the POM is modified as follows:
<packaging>war</packaging>...<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope></dependency>
7.3 Package The war package and deploy it to Tomcat
MVN clean packages are packaged into a war and placed under Tomcat's WebApps folder.
Iv. Operation Guide 2-Build 1 with Eclipse + Maven plugin. New Maven project in Eclipse
Create a simple project, new maven, maven---project
2. Modifying the Pom file
Modify the Pom file to add the following:
<parent> <groupId>Org.springframework.boot</groupId> <artifactid>Spring-boot-starter-parent</artifactid> <version>1.3.6.RELEASE</version> <relativepath /> <!--lookup parent from repository -- </Parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </Properties> <dependencies> <dependency> <groupId>Org.springframework.boot</groupId> <artifactid>Spring-boot-starter-actuator</artifactid> </Dependency> <dependency> <groupId>Org.springframework.boot</groupId> <artifactid>Spring-boot-starter-web</artifactid> </Dependency> <dependency> <groupId>Org.springframework.boot</groupId> <artifactid>Spring-boot-starter-test</artifactid> <scope>Test</Scope> </Dependency> </dependencies> <build> <plugins> <plugin> <groupId>Org.springframework.boot</groupId> <artifactid>Spring-boot-maven-plugin</artifactid> </plugin> </plugins> </Build>
3. Creating a class file
Copy the three Java files (Fuck.java, Fuckcontroller.java, Restdemoapplication.java) and corresponding package copies shown in step three to Src/main/java
4. Create a Property file
Copy the Application.properties file shown in step three to the Src/main/resources directory.
5. Alt + F5 Update Restdemo2 project dependency 6. MAVEN compiles the project
Project name, run as, Maven build ..., fill in the goal with "Spring-boot:run" and click Run.
You can also run this:
You can use MVN spring-boot:run at the command line to compile the running app.
You can also use the Java Jar Xxx.jar package to run the app after you have packaged the app into a fat jar package using the MVN clean pack.
V. References
Link
https://spring.io/guides/gs/actuator-service/
Https://spring.io/blog/2015/03/18/spring-boot-support-in-spring-tool-suite-3-6-4
http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/
Spring Boot Example-1. Building a RESTful Web service using Spring Boot Actuator