Spring Boot Deployment and service configuration

Source: Internet
Author: User
Tags tmp folder

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:

    1. # project ContextPath, generally in the official release version, we do not configure
    2. Server.context-path=/myspringboot
    3. # error page, specifying the URL of the jump when an error occurs. Please check the Basicerrorcontroller source code to know
    4. Server.error.path=/error
    5. # Service Port
    6. server.port=9090
    7. # Session maximum timeout (minutes), default is
    8. server.session-timeout=
    9. # 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
    10. # server.address=192.168. 16.11


Tomcat
Tomcat is the default container for spring boot, and here are a few common configurations:

  1. # Tomcat maximum number of threads, default is
  2. server.tomcat.max-threads=
  3. # tomcat URI encoding
  4. server.tomcat.uri-encoding=utf-8
  5. # 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)
  6. Server.tomcat.basedir=h:/springboot-tomcat-tmp
  7. # Open the Tomcat access log, and you can set the method for the log format:
  8. #server. tomcat.access-log-enabled=True
  9. #server. tomcat.access-log-pattern=
  10. # Accesslog directory, default in Basedir/logs
  11. #server. tomcat.accesslog.directory=
  12. # log file directory
  13. Logging.path=h:/springboot-tomcat-tmp
  14. # log file name, default is Spring.log
  15. 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:

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-web</artifactId>
  5. <exclusions>
  6. <exclusion>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-tomcat</artifactId>
  9. </exclusion>
  10. </exclusions>
  11. </dependency>
  12. <dependency>
  13. <groupId>org.springframework.boot</groupId>
  14. <artifactId>spring-boot-starter-jetty</artifactId>
  15. </dependency>
  16. <dependencies>

Packaged
Packaging method:
CMD into the project directory, using the MVN Clean Packages command to package, take my project projects as an example:

    1. 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
    1. Modify the startup class, Inherit Springbootservletinitializer, and override the Configure method
  1. Public class Springbootsampleapplication extends springbootservletinitializer{
  2. private static final Logger Logger = Loggerfactory.getlogger (springbootsampleapplication.   Class);
  3. @Override
  4. protected Springapplicationbuilder Configure (Springapplicationbuilder builder) {
  5. return Builder.sources (this.getclass ());
  6. }
  7. }
    1. Modify the jar in the Pom file as war
    1. <!--<packaging>jar</packaging>--
    2. <packaging>war</packaging>
    1. Modify Pom to exclude Tomcat plugins
  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. <exclusions>
  5. <exclusion>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-tomcat</artifactId>
  8. </exclusion>
  9. </exclusions>
  10. </dependency>
    1. 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

    1. App.name=myapp
    2. server.port=8080
    3. Spring.profiles.active=dev

Application-dev.properties

    1. server.port=8081

Application-stg.properties

    1. 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.

    1. 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
    2. 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
    3. 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

  1. Interface definition
  2. Public interface SendMessage {
  3. //Send SMS Method definition
  4. public Void Send ();
  5. }
  6. Dev Environment Implementation Class
  7. @Component
  8. @Profile ("Dev")
  9. Public class Devsendmessage implements SendMessage {
  10. @Override
  11. public Void Send () {
  12. System.out.println (">>>>>>>>dev Send () <<<<<<<<");
  13. }
  14. }
  15. STG Environment Implementation Class
  16. @Component
  17. @Profile ("STG")
  18. Public class Stgsendmessage implements SendMessage {
  19. @Override
  20. public Void Send () {
  21. System.out.println (">>>>>>>>stg Send () <<<<<<<<");
  22. }
  23. }
  24. Start class
  25. @SpringBootApplication
  26. Public class Profiledemoapplication {
  27. @Value ("${app.name}")
  28. private String name;
  29. @Autowired
  30. private SendMessage SendMessage;
  31. @PostConstruct
  32. public void init () {
  33. Sendmessage.send (); //The corresponding class will be instantiated according to the environment specified by profile
  34. }
  35. }

3, Logback-spring.xml also support the node to support the distinction

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <configuration>
  3. <include resource="Org/springframework/boot/logging/logback/base.xml"/>
  4. <logger name="Org.springframework.web" level="INFO"/>
  5. <springprofile name="Default" >
  6. <logger name="org.springboot.sample" level="TRACE"/>
  7. </springProfile>
  8. <springprofile name="Dev" >
  9. <logger name="org.springboot.sample" level="DEBUG"/>
  10. </springProfile>
  11. <springprofile name="Staging" >
  12. <logger name="org.springboot.sample" level="INFO"/>
  13. </springProfile>
  14. </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:

    1. 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

    1. #!/bin/sh
    2. Rm-f Tpid
    3. Nohup Java-jar/data/app/myapp.jar--SPRING.PROFILES.ACTIVE=STG >/dev/null 2>&1 &
    4. Echo $! > Tpid

stop.sh

    1. Tpid= ' Cat Tpid | awk ' {print '} '
    2. Tpid= ' Ps-aef | grep $tpid | awk ' {print $} ' |grep $tpid '
    3. If [${tpid}]; Then
    4. Kill-9 $tpid
    5. Fi

check.sh

    1. #!/bin/sh
    2. Tpid= ' Cat Tpid | awk ' {print '} '
    3. Tpid= ' Ps-aef | grep $tpid | awk ' {print $} ' |grep $tpid '
    4. If [${tpid}]; Then
    5. Echo App is running.
    6. Else
    7. Echo App is not running.
    8. Fi

kill.sh

      1. #!/bin/sh
      2. # Kill-9 ' ps-ef|grep project name |awk ' {print $} '
      3. Kill-9 ' ps-ef|grep Demo|awk ' {print $} '

Spring Boot Deployment and service configuration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.