zoukankan      html  css  js  c++  java
  • Spring Boot 部署与服务配置

    Spring Boot 其默认是集成web容器的,启动方式由像普通Java程序一样,main函数入口启动。其内置Tomcat容器或Jetty容器,具体由配置来决定(默认Tomcat)。当然你也可以将项目打包成war包,放到独立的web容器中(Tomcat、weblogic等等),当然在此之前你要对程序入口做简单调整。

    项目构建我们使用Maven或Gradle,这将使项目依赖、jar包管理、以及打包部署变的非常方便。

    一、内嵌 Server 配置

    Spring Boot将容器内置后,它通过配置文件的方式类修改相关server配置。 
    先看一下下面的图,为关于server的配置列项: 
    配置 
    配置

    其中常用的配置只有少数几个,已经用紫色标记起来。红框圈起来的部分,看名称分类就可以明白其作用。 
    对server的几个常用的配置做个简单说明:

    1. # 项目contextPath,一般在正式发布版本中,我们不配置  
    2. server.context-path=/myspringboot  
    3. # 错误页,指定发生错误时,跳转的URL。请查看BasicErrorController源码便知  
    4. server.error.path=/error  
    5. # 服务端口  
    6. server.port=9090  
    7. # session最大超时时间(分钟),默认为30  
    8. server.session-timeout=60  
    9. # 该服务绑定IP地址,启动服务器时如本机不是该IP地址则抛出异常启动失败,只有特殊需求的情况下才配置  
    10. # server.address=192.168.16.11  


    Tomcat 
    Tomcat为Spring Boot的默认容器,下面是几个常用配置:

    1. # tomcat最大线程数,默认为200  
    2. server.tomcat.max-threads=800  
    3. # tomcat的URI编码  
    4. server.tomcat.uri-encoding=UTF-8  
    5. # 存放Tomcat的日志、Dump等文件的临时文件夹,默认为系统的tmp文件夹(如:C:UsersShanhyAppDataLocalTemp)  
    6. server.tomcat.basedir=H:/springboot-tomcat-tmp  
    7. # 打开Tomcat的Access日志,并可以设置日志格式的方法:  
    8. #server.tomcat.access-log-enabled=true  
    9. #server.tomcat.access-log-pattern=  
    10. # accesslog目录,默认在basedir/logs  
    11. #server.tomcat.accesslog.directory=  
    12. # 日志文件目录  
    13. logging.path=H:/springboot-tomcat-tmp  
    14. # 日志文件名称,默认为spring.log  
    15. logging.file=myapp.log  

    Jetty 
    如果你要选择Jetty,也非常简单,就是把pom中的tomcat依赖排除,并加入Jetty容器的依赖,如下:

    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>   

    打包 
    打包方法: 
    CMD进入项目目录,使用 mvn clean package 命令打包,以我的项目工程为例:

    1. E:spring-boot-sample>mvn clean package  

    可以追加参数 -Dmaven.test.skip=true 跳过测试。 
    打包后的文件存放于项目下的target目录中,如:spring-boot-sample-0.0.1-SNAPSHOT.jar 
    如果pom配置的是war包,则为spring-boot-sample-0.0.1-SNAPSHOT.war

    二、部署到JavaEE容器

    1. 修改启动类,继承 SpringBootServletInitializer 并重写 configure 方法
    1. public class SpringBootSampleApplication extends SpringBootServletInitializer{  
    2.   
    3.     private static final Logger logger = LoggerFactory.getLogger(SpringBootSampleApplication.class);  
    4.   
    5.     @Override  
    6.     protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {  
    7.         return builder.sources(this.getClass());  
    8.     }  
    9.   
    10. }  
    1. 修改pom文件中jar 为 war
    1. <!-- <packaging>jar</packaging> -->  
    2. <packaging>war</packaging>  
    1. 修改pom,排除tomcat插件
    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. 打包部署到容器 
      使用命令 mvn clean package 打包后,同一般J2EE项目一样部署到web容器。

    三、使用Profile区分环境

    spring boot 可以在 “配置文件”、“Java代码类”、“日志配置” 中来配置profile区分不同环境执行不同的结果

    1、配置文件 
    使用配置文件application.yml 和 application.properties 有所区别 
    以application.properties 为例,通过文件名来区分环境 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  


    在启动程序的时候通过添加 –spring.profiles.active={profile} 来指定具体使用的配置 
    例如我们执行 java -jar demo.jar –spring.profiles.active=dev 那么上面3个文件中的内容将被如何应用? 
    Spring Boot 会先加载默认的配置文件,然后使用具体指定的profile中的配置去覆盖默认配置。

    1. app.name 只存在于默认配置文件 application.properties 中,因为指定环境中不存在同样的配置,所以该值不会被覆盖   
    2. server.port 默认为8080,但是我们指定了环境后,将会被覆盖。如果指定stg环境,server.port 则为 8082   
    3. spring.profiles.active 默认指定dev环境,如果我们在运行时指定 –spring.profiles.active=stg 那么将应用stg环境,最终 server.port 的值为8082  

    2、Java类中@Profile注解 
    下面2个不同的类实现了同一个接口,@Profile注解指定了具体环境

    1. // 接口定义  
    2. public interface SendMessage {  
    3.   
    4.     // 发送短信方法定义  
    5.     public void send();  
    6.   
    7. }  
    8.   
    9. // Dev 环境实现类  
    10. @Component  
    11. @Profile("dev")  
    12. public class DevSendMessage implements SendMessage {  
    13.   
    14.     @Override  
    15.     public void send() {  
    16.         System.out.println(">>>>>>>>Dev Send()<<<<<<<<");  
    17.     }  
    18.   
    19. }  
    20.   
    21. // Stg环境实现类  
    22. @Component  
    23. @Profile("stg")  
    24. public class StgSendMessage implements SendMessage {  
    25.   
    26.     @Override  
    27.     public void send() {  
    28.         System.out.println(">>>>>>>>Stg Send()<<<<<<<<");  
    29.     }  
    30.   
    31. }  
    32.   
    33. // 启动类  
    34. @SpringBootApplication  
    35. public class ProfiledemoApplication {  
    36.   
    37.     @Value("${app.name}")  
    38.     private String name;  
    39.   
    40.     @Autowired  
    41.     private SendMessage sendMessage;  
    42.   
    43.     @PostConstruct  
    44.     public void init(){  
    45.         sendMessage.send();// 会根据profile指定的环境实例化对应的类  
    46.     }  
    47.   
    48. }  

    3、logback-spring.xml也支持有节点来支持区分

    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.   
    6.     <springProfile name="default">  
    7.         <logger name="org.springboot.sample" level="TRACE" />  
    8.     </springProfile>  
    9.   
    10.     <springProfile name="dev">  
    11.         <logger name="org.springboot.sample" level="DEBUG" />  
    12.     </springProfile>  
    13.   
    14.     <springProfile name="staging">  
    15.         <logger name="org.springboot.sample" level="INFO" />  
    16.     </springProfile>  
    17.   
    18. </configuration>  

    再说一遍文件名不要用logback.xml 请使用logback-spring.xml

    四、指定外部的配置文件

    有些系统,关于一些数据库或其他第三方账户等信息,由于安全问题,其配置并不会提前配置在项目中暴露给开发人员。 
    对于这种情况,我们在运行程序的时候,可以通过参数指定一个外部配置文件。 
    以 demo.jar 为例,方法如下:

    1. java -jar demo.jar --spring.config.location=/opt/config/application.properties  

    其中文件名随便定义,无固定要求。

    五、创建一个Linux 应用的sh脚本

    下面几个脚本仅供参考,请根据自己需要做调整 
    start.sh

    1. #!/bin/sh  
    2.   
    3. rm -f tpid  
    4.   
    5. nohup java -jar /data/app/myapp.jar --spring.profiles.active=stg > /dev/null 2>&1 &  
    6.   
    7. echo $! > tpid  

    stop.sh

    1. tpid=`cat tpid | awk '{print $1}'`  
    2. tpid=`ps -aef | grep $tpid | awk '{print $2}' |grep $tpid`  
    3. if [ ${tpid} ]; then  
    4.         kill -9 $tpid  
    5. fi  

    check.sh

    1. #!/bin/sh  
    2.   
    3. tpid=`cat tpid | awk '{print $1}'`  
    4. tpid=`ps -aef | grep $tpid | awk '{print $2}' |grep $tpid`  
    5. if [ ${tpid} ]; then  
    6.         echo App is running.  
    7. else  
    8.         echo App is NOT running.  
    9. fi  

    kill.sh

      1. #!/bin/sh  
      2. # kill -9 `ps -ef|grep 项目名称|awk '{print $2}'`  
      3. kill -9 `ps -ef|grep demo|awk '{print $2}'`  
  • 相关阅读:
    Linux中的用户和用户组
    GCC编译过程
    C++设计模式——单例模式(转)
    快速排序之python
    归并排序之python
    计数排序之python
    希尔排序之python
    插入排序之python
    选择排序之python
    冒泡排序之python
  • 原文地址:https://www.cnblogs.com/muliu/p/6362847.html
Copyright © 2011-2022 走看看