用户工具

站点工具


spring:boot
  • 安装springboot
SDKMAN!,springboot-cli,
curl -s "https://get.sdkman.io" | bash
source /root/.sdkman/bin/sdkman-init.sh && sdk help
sdk install springboot && spring --version
mvn spring-boot:run,spring run Application.java,
env server.port=8080 mvn spring-boot:run,mvn spring-boot:run -Dspring-boot.run.arguments=--server.port=8080
  • 打包package
mvn package
java -jar yourapp.jar --server.port=8888 > /dev/null 2>&1 &,
<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
  • 使用环境变量隐藏账户密码
vi application-dev.yml
spring.datasource.password=${mysql_password:test_mysql_pwd}
//密码在命令历史中可见,测试环境不提供密码变量则默认使用test_mysql_pwd
java -Dmysql_password=cmd_mysql_pwd -jar app.jar
//密码在环境变量中,可以是~/.profile,也可以是单独的.env文件(局部变量更好一些)
vi .env
export mysql_password=env_mysql_pwd
source .env && java -jar app.jar
  • 瘦身,不打包lib目录,使用LOADER_PATH加载很少变化的jar
mvn dependency:tree|grep +-|sed 's/.*+- / /g'|sed 's/:.*/,/g'|sort|uniq,输出依赖groupId
jar tvf target/app.jar|grep -n '\.jar$'|sed 's/.*\/lib\///g'|sed 's/-[^-]*$/,/g',输出残余artifactId
jar xvf target/app.jar BOOT-INF/lib/,提取依赖lib目录
java -Dloader.path="BOOT-INF/lib/" -jar app.jar,实测能运行
      <plugin>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-maven-plugin</artifactId>
          <configuration>
              <layout>ZIP</layout>
              <!-- 打包system scope的jar包,比如短信等第三方jar -->
              <includeSystemScope>true</includeSystemScope>
              <!--去除在生产环境中不变的依赖-->
              <excludeGroupIds>
	        aopalliance,
	        ch.qos.logback
              </excludeGroupIds>
              <excludeArtifactIds>
	        okio,
	        xpp3_min
                //个别输出有误:json-lib-2.4-jdk15.jar
              </excludeArtifactIds>
          </configuration>
      </plugin>
  • 容器docker
mvn package -Dmaven.test.skip=true docker:build
docker run --name=springboot-docker-demo -p 8080:8080 -t demo,docker stop name,
<plugin>
    <groupId>com.spotify</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>0.4.11</version>
    <configuration>
        <imageName>${project.artifactId}</imageName>
        <dockerDirectory>src/main/docker</dockerDirectory>
        <resources>
            <resource>
                <targetPath>/</targetPath>
                <directory>${project.build.directory}</directory>
                <include>${project.build.finalName}.jar</include>
            </resource>
        </resources>
    </configuration>
</plugin>
vi src/main/docker/Dockerfile
FROM frolvlad/alpine-oraclejdk8:slim
VOLUME /tmp
ADD demo-0.0.1-SNAPSHOT.jar app.jar
RUN sh -c 'touch /app.jar'
ENV JAVA_OPTS=""
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]
  • 部署tomcat
不建议:springboot就是为了方便部署,应优先使用
修改pom.xml,修改package=war
剔除tomcat依赖,停用springboot打包插件
发布时先解压war文件,重命名为ROOT,放到tomcat/webapps目录,或者配置server.xml
实现接口SpringBootServletInitializer,覆盖方法configure,调用SpringApplicationBuilder.sources(Application.class)
<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
      <scope>provided</scope>
</dependency>
spring/boot.txt · 最后更改: 2019/09/27 16:11 由 admin