to springboot (from Baidu Encyclopedia) Microservice is an emerging software architecture, which is to split a large single application and service into dozens of supporting microservices.
A microservices strategy can make life easier by extending individual components rather than the entire application stack to meet service level agreements.
For large applications, adding more users means providing a larger Elastic Compute Cloud (EC2) instance size, even if only some of these functions are scaled up. The net result is that enterprise users only pay for EC2 instances that support that portion of their needs beyond the microservices.
Advantages of Microservices One of the greatest advantages of
microservice applications is that they tend to use computing resources more efficiently than traditional applications. This is because they deal with functional bottlenecks by extending components. This way, developers only need to deploy computing resources for additional components, rather than deploying a whole new iteration of the application. The end result is that more resources are available for other tasks.
Another benefit of microservice applications is that they are faster and easier to update. When developers make changes to a traditional monolithic application, they must do detailed QA testing to ensure that the changes do not affect other features or functionality. But with microservices, developers can update individual components of the application without affecting other parts. Testing microservice applications is still required, but it makes it easier to identify and isolate problems, speeding up development and supporting DevOps and continuous application development.
The third benefit is that the microservices architecture facilitates emerging cloud services such as event-driven computing. Features like AWS Lambda (AWS Lambda is a fine-grained approach to deploying code, managing services, and monitoring the running status of lightweight services) allow developers to write code that sleeps until an application event is triggered. Computing resources are only required for event processing, and enterprises only need to pay for each event instead of a fixed number of computing instances.
2 , the most basic springboot practice
Create a standard MavenWeb project
pom.xml dependency configuration
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.0.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.7</java.version> </properties> <dependencies> <!-- web project auto-configuration module--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Automatic configuration template (including spring-boot-starter-web dependency) --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!-- log configuration --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </dependency> </dependencies> <build> <finalName>SpringBoot</finalName> </build>
Class
@SpringBootApplication
@RestController
public class SimpleExample {
@RequestMapping("/")
public String hello(){
return "SpringBoot SimpleExample !";
}
public static void main(String[] args) {
SpringApplication.run(Application.class,args) ;
}
}
Directly run the main method.
Browser access http://localhost:8080/ to display the following information
SpringBoot SimpleExample!
3, Default configuration and convention directory structure
Default web container and port
Default embedded Tomcat, port number is 8080
spring- Boot project source directory structure convention
Maven resource file directory: /src/java/resources
Spring-boot project static file directory: /src/java/resources/static
spring-boot project template file directory: /src/java/resources/templates
The support of spring-boot static home page, that is, index.html placed in the following directory structure will be directly mapped to the root directory of the application
classpath:/META-INF/resources/index.html
classpath:/resources/index.html
classpath:/static /index.html
calsspath:/public/index.html
/src/Java/resources/templates This directory is not the default directory of the homepage file, so we need to manually map the application root path to /src/java/resources/templates/index .html. You can use the following methods
@RequestMapping("/") public String index(){ return "index"; }
springboot default configuration file (used to configure various basic properties)
\src\main\resources\application.properties
reference document: application.properties configuration list
The following are some commonly used configuration items
# tomcat maximum number of threads, the default is 200 server.tomcat.max-threads=800 # URI encoding of tomcat server.tomcat.uri-encoding=UTF-8 # Temporary folder for storing Tomcat's log, Dump and other files, the default is the system's tmp folder (eg: C:\Users\Shanhy\AppData\Local\Temp) server.tomcat.basedir=H:/springboot-tomcat-tmp # Open Tomcat's Access log, and you can set the log format method: #server.tomcat.access-log-enabled=true #server.tomcat.access-log-pattern= # accesslog directory, the default is basedir/logs #server.tomcat.accesslog.directory= # log file directory logging.path=H:/springboot-tomcat-tmp # log file name, default is spring.log logging.file=myapp.log
4. Modify some common default configuration methods and the basic process of page display data
Modify the default port number, there are mainly two ways
to modify
the Add the following configuration information
server.port=8080 to the application.properties file to
implement the EmbeddedServletContainerCustomizer interface
@SpringBootApplication @RestController public class Application implements EmbeddedServletContainerCustomizer{ public static void main(String[] args) { SpringApplication.run(Application.class,args); } @Override public void customize(ConfigurableEmbeddedServletContainer configurableEmbeddedServletContainer) { configurableEmbeddedServletContainer.setPort(8081); } }
Turn off the thymeleaf cache Add the following configuration information
to the application.properties file
spring.thymeleaf.cache: false
server.tomcat.access_log_enabled: true
server.tomcat.basedir: target/tomcat
page display data
thymeleaf template engine official website
online reference information
For example:
@RequestMapping("/hello/{name}") public String hello(@PathVariable("name") String name, Model model) { model.addAttribute("name", name); //By default, it will go to the \src\main\resources\templates directory to find the hello.html file return "hello"; }
display on the page
<!DOCTYPE HTML> <!-- thymeleaf namespace needs to be introduced --> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Getting Started: Serving Web Content</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> </head> <body> <!-- Get the value of name in the model--> <p th:text="'Hello , ' + ${name} + '!'" /> </body> </html>
5 , Deploy and run
Inherit SpringBootServletInitializer to override configure method
@SpringBootApplication @RestController public class SimpleExample extends SpringBootServletInitializer{ @RequestMapping("/") public String hello(){ return "SpringBoot SimpleExample !"; } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return super.configure(builder); } public static void main(String[] args) { SpringApplication.run(Application.class,args); } }
Modify the jar in the pom file to war
<!-- <packaging>jar</packaging> -->
<packaging>war</packaging>
Modify the pom to exclude the tomcat plugin
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency>