Spring Boot is an integrated Web container by default and starts in the same way as a normal Java program, starting with the main function entry. Its built-in tomcat container or jetty container, which is determined by the configuration (default tomcat). Of course you can also package the project into a war package, put it into a separate web container (Tomcat, WebLogic, and so on), of course, before you make a simple adjustment to the program portal.
Project Building we use MAVEN or Gradle, which makes project dependencies, jar package management, and package deployment very convenient.
One, embedded Server configuration
Spring boot uses the configuration file to modify the relevant server configuration after it is built into the container.
Let's take a look at the following diagram for a configuration column entry for the server:
Only a few of the commonly used configurations have been marked with purple. Red-framed parts, look at the name classification can understand its role.
Make a brief description of several commonly used configurations of the server:
- # project ContextPath, generally in the official release version, we do not configure
- Server.context-path=/myspringboot
- # error page, specifying the URL of the jump when an error occurs. Please check the Basicerrorcontroller source code to know
- Server.error.path=/error
- # Service Port
- server.port=9090
- # Session maximum timeout (minutes), default is
- server.session-timeout=
- # The service binds the IP address, and when the server is started, if the IP address is not the same as this, then the exception is thrown, and only the special requirements are configured
- # server.address=192.168. 16.11
Tomcat
Tomcat is the default container for spring boot, and here are a few common configurations:
- # Tomcat maximum number of threads, default is
- server.tomcat.max-threads=
- # tomcat URI encoding
- server.tomcat.uri-encoding=utf-8
- # A temporary folder for files such as Tomcat logs, dumps, and so on, default to the system's TMP folder (for example: C:\Users\Shanhy\AppData\Local\Temp)
- Server.tomcat.basedir=h:/springboot-tomcat-tmp
- # Open the Tomcat access log, and you can set the method for the log format:
- #server. tomcat.access-log-enabled=True
- #server. tomcat.access-log-pattern=
- # Accesslog directory, default in 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
Jetty
If you are going to choose jetty, it is also very simple to exclude the Tomcat dependency in Pom and add the dependency of the jetty container as follows:
- <dependencies>
- <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>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-jetty</artifactId>
- </dependency>
- <dependencies>
Packaged
Packaging method:
CMD into the project directory, using the MVN Clean Packages command to package, take my project projects as an example:
- E:\SPRING-BOOT-SAMPLE>MVN Clean Package
You can append the parameter-dmaven.test.skip=true to skip the test.
The packaged files are stored in the target directory under the project, such as: Spring-boot-sample-0.0.1-snapshot.jar
Spring-boot-sample-0.0.1-snapshot.war If the POM is configured with a war package
Ii. deploy to a Java EE container
- Modify the startup class, Inherit Springbootservletinitializer, and override the Configure method
- Public class Springbootsampleapplication extends springbootservletinitializer{
- private static final Logger Logger = Loggerfactory.getlogger (springbootsampleapplication. Class);
- @Override
- protected Springapplicationbuilder Configure (Springapplicationbuilder builder) {
- return Builder.sources (this.getclass ());
- }
- }
- Modify the jar in the Pom file as war
- <!--<packaging>jar</packaging>--
- <packaging>war</packaging>
- Modify Pom to exclude Tomcat plugins
- <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>
- Package deployment to a container
Use the command MVN clean package to deploy to a web container just like a generic Java EE project.
Iii. using profile to differentiate the environment
Spring boot can configure profiles to differentiate between different environments and perform different results in "Configuration Files", "Java Code Classes", "Log Configuration"
1. Configuration files
Using configuration files Application.yml and application.properties differ
Take Application.properties as an example to differentiate the environment by file name Application-{profile}.properties
Application.properties
- App.name=myapp
- server.port=8080
- Spring.profiles.active=dev
Application-dev.properties
- server.port=8081
Application-stg.properties
- server.port=8082
Specify the specific configuration to use when starting the program by adding –spring.profiles.active={profile}
For example, if we execute Java-jar demo.jar–spring.profiles.active=dev, then how will the contents of the above 3 files be applied?
Spring Boot Loads the default configuration file first, and then overwrites the default configuration with the configuration in the specified profile.
- App.name exists only in the default profile application.properties because the same configuration does not exist in the specified environment, so the value is not overwritten
- Server.port defaults to 8080, but when we specify an environment, it will be overwritten. If you specify a STG environment, Server.port is 8082
- spring.profiles.active Specifies the dev environment by default, and if we specify –SPRING.PROFILES.ACTIVE=STG at runtime then the STG environment will be applied and the final Server.port value is 8082
2. @profile annotations in Java class
The following 2 different classes implement the same interface, @Profile annotations specify the specific environment
- Interface definition
- Public interface SendMessage {
- //Send SMS Method definition
- public Void Send ();
- }
- Dev Environment Implementation Class
- @Component
- @Profile ("Dev")
- Public class Devsendmessage implements SendMessage {
- @Override
- public Void Send () {
- System.out.println (">>>>>>>>dev Send () <<<<<<<<");
- }
- }
- STG Environment Implementation Class
- @Component
- @Profile ("STG")
- Public class Stgsendmessage implements SendMessage {
- @Override
- public Void Send () {
- System.out.println (">>>>>>>>stg Send () <<<<<<<<");
- }
- }
- Start class
- @SpringBootApplication
- Public class Profiledemoapplication {
- @Value ("${app.name}")
- private String name;
- @Autowired
- private SendMessage SendMessage;
- @PostConstruct
- public void init () {
- Sendmessage.send (); //The corresponding class will be instantiated according to the environment specified by profile
- }
- }
3, Logback-spring.xml also support the node to support the distinction
- <?xml version="1.0" encoding="UTF-8"?>
- <configuration>
- <include resource="Org/springframework/boot/logging/logback/base.xml"/>
- <logger name="Org.springframework.web" level="INFO"/>
- <springprofile name="Default" >
- <logger name="org.springboot.sample" level="TRACE"/>
- </springProfile>
- <springprofile name="Dev" >
- <logger name="org.springboot.sample" level="DEBUG"/>
- </springProfile>
- <springprofile name="Staging" >
- <logger name="org.springboot.sample" level="INFO"/>
- </springProfile>
- </configuration>
Again, do not use the Logback.xml file name. Use Logback-spring.xml
Iv. specifying the external configuration file
Some systems, such as information about databases or other third-party accounts, are not configured to be exposed to developers in the project because of security issues.
In this case, when we run the program, we can specify an external configuration file through the parameters.
Taking Demo.jar as an example, the method is as follows:
- Java-jar Demo.jar--spring.config.location=/opt/config/application.properties
Where the file name is arbitrarily defined, no fixed requirements.
V. Create an SH script for a Linux application
The following scripts are for reference only, please make adjustments according to your needs
start.sh
- #!/bin/sh
- Rm-f Tpid
- Nohup Java-jar/data/app/myapp.jar--SPRING.PROFILES.ACTIVE=STG >/dev/null 2>&1 &
- Echo $! > Tpid
stop.sh
- Tpid= ' Cat Tpid | awk ' {print '} '
- Tpid= ' Ps-aef | grep $tpid | awk ' {print $} ' |grep $tpid '
- If [${tpid}]; Then
- Kill-9 $tpid
- Fi
check.sh
- #!/bin/sh
- Tpid= ' Cat Tpid | awk ' {print '} '
- Tpid= ' Ps-aef | grep $tpid | awk ' {print $} ' |grep $tpid '
- If [${tpid}]; Then
- Echo App is running.
- Else
- Echo App is not running.
- Fi
kill.sh
- #!/bin/sh
- # Kill-9 ' ps-ef|grep project name |awk ' {print $} '
- Kill-9 ' ps-ef|grep Demo|awk ' {print $} '
Spring Boot Deployment and service configuration