架构实战篇(十七):Spring Boot Assembly 整合 thymeleaf

时光斑驳了记忆 2018-10-29 15:36:05 ⋅ 688 阅读

架构实战篇(四):Spring Boot 整合 Thymeleaf
架构实战篇(十六):Spring Boot Assembly服务化打包

前言

当你的服务器上面的tomcat部署了好几个web项目,当有个别项目需要更新升级的时候,往往我们会把tomcat 关闭然后替换需要升级的war包,然后再启动tomcat

这样做的弊端

  • war包传输比较慢,每次都要把依赖包都上传上去

  • 重启tomcat 会把所有的服务都停掉,体验不好

  • 动态修改静态资源麻烦

下面就通过使用 Assembly 和集成 Thymeleaf 规避上面的弊端

一、先看下目录结构

目录结构

我们在 Thymeleaf 的基础上增加了 Assembly 的支持

二、配置 spring boot loader

pom.xml 增加 依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-loader</artifactId>
        </dependency>

在resources 目录下增加 loader.properties

loader.main=sample.web.ui.SampleWebUiApplication

在 start.sh 命令中使用

#!/bin/bash
cd `dirname $0`
BIN_DIR=`pwd`
cd ..
DEPLOY_DIR=`pwd`
CONF_DIR=$DEPLOY_DIR/conf


PIDS=`ps -ef | grep java | grep "$CONF_DIR" |awk '{print $2}'`
if [ -n "$PIDS" ]; then
    echo "ERROR: The server already started!"
    echo "PID: $PIDS"
    exit 1
fi

LOGS_DIR=""
if [ -n "$LOGS_FILE" ]; then
    LOGS_DIR=`dirname $LOGS_FILE`
else
    LOGS_DIR=$DEPLOY_DIR/logs
fi
if [ ! -d $LOGS_DIR ]; then
    mkdir $LOGS_DIR
fi
STDOUT_FILE=$LOGS_DIR/stdout.log

LIB_DIR=$DEPLOY_DIR/lib
LIB_JARS=`ls $LIB_DIR|grep .jar|awk '{print "'$LIB_DIR'/"$0}'|tr "\n" ":"`

JAVA_OPTS="$JAVA_OPTS -Duser.timezone=Asia/Shanghai -Djava.awt.headless=true -Djava.net.preferIPv4Stack=true "
JAVA_DEBUG_OPTS=""
if [ "$1" = "debug" ]; then
    JAVA_DEBUG_OPTS=" -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n "
fi
JAVA_JMX_OPTS=""
if [ "$1" = "jmx" ]; then
    JAVA_JMX_OPTS=" -Dcom.sun.management.jmxremote.port=1099 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false "
fi
JAVA_MEM_OPTS="${JAVA_MEM_OPTS}"

SPRING_PROFILES_ACTIVE=${SPRING_PROFILES_ACTIVE:=prod}

echo -e "Starting the server ...\c"

CMD="java $JAVA_OPTS $JAVA_MEM_OPTS $JAVA_DEBUG_OPTS $JAVA_JMX_OPTS -Dspring.profiles.active=$SPRING_PROFILES_ACTIVE -classpath $CONF_DIR:$LIB_JARS \
-Dloader.config.location=${CONF_DIR}/loader.properties \
org.springframework.boot.loader.PropertiesLauncher"

if [ "$1" = "run" ]; then
    $CMD
else
    nohup $CMD > $STDOUT_FILE 2>&1 &

    echo "OK!"
    PIDS=`ps -ef | grep java | grep "$DEPLOY_DIR" | awk '{print $2}'`
    echo "PID: $PIDS"
    echo "STDOUT: $STDOUT_FILE"
fi

优点:不用频繁修改启动脚本

三、Thymeleaf 读取不到模板页面问题解决

<assembly>
    <id>assembly</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>true</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>src/main/assembly/bin</directory>
            <outputDirectory>bin</outputDirectory>
            <fileMode>0755</fileMode>
        </fileSet>
        <fileSet>
            <directory>src/main/resources/</directory>
            <outputDirectory>conf</outputDirectory>
            <includes>
                <include>*.properties</include>
                <include>static/**</include>
                <include>templates/**</include>
            </includes>
        </fileSet>
    </fileSets>
    <dependencySets>
        <dependencySet>
            <outputDirectory>lib</outputDirectory>
        </dependencySet>
    </dependencySets>
</assembly>

在 assembly.xml 文件中增加

<include>static/**</include>
<include>templates/**</include>

四、打包后的springboot应用结构图

这样配置完成后,执行mvn 打包命令就会把 static 和 templates 文件都复制到 conf 目录下

网上也有一些说修改 assembly 的默认路径的,但是那种效果并不是很理想

总结:这个方案本身并没有带来什么新东西,主要是通过组合现有的技术来解决和方便项目的升级和维护。

关注我们



全部评论: 0

    我有话说:

    架构实战(四):Spring Boot整合 Thymeleaf

    Thymeleaf 是一种模板语言。那模板语言或模板引擎是什么?

    架构实战六):Spring Boot Assembly服务化打包

    使用assembly来打包springboot微服务项目,让发布更简单

    架构实战):Spring Boot Data JPA 快速入门

    Spring Data JPA 是Spring Data 的一个子项目,它通过提供基于JPA的Repository极大了减少了操作JPA的代码。

    架构实战五):Spring Boot 解耦之事件驱动

    通过使用spring 事件来解决业务代码的耦合

    架构实战二):Spring Boot 分布式Session共享Redis

    分布式Web网站一般都会碰到集群session共享问题,小编整理了一套解决方案,内附GitHub 源码地址哦~~~

    架构实战一):Spring Boot 集成企业级搜索引擎 SolrCloud

    Solr是以Lucene为基础实现的文本检索应用服务。Solr部署方式有单机方式、多机Master-Slaver方式、Cloud方式。

    架构实战(三)-Spring Boot架构搭建RESTful API案例

    之前分享了Spring Boot 整合Swagger 让API可视化和前后端分离架构 受到了大家一致好评 ,本节就接着上节的代码做了详细的查询代码的补充和完善并搭建RESTful API架构案例。

    架构实战(六):Spring Boot RestTemplate的使用

    RestTemplate是Spring提供的用于访问Rest服务的客户端,RestTemplate提供了多种便捷访问远程Http服务的方法,能够大大提高客户端的编写效率。

    架构实战四):Spring Boot 多缓存实战

    多场景下的不同缓存策略解决方案

    架构实战(一)-Spring Boot+MyBatis基础架构搭建

    Spring的追求一定是简单点简单点,让java的开发变得更加简单、容易。瞧瞧的告诉你们直接copy就能用哦~~~

    架构实战):Spring Boot 集成 Dubbo

    Dubbo是阿里巴巴SOA服务化治理方案的核心框架,一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案。

    架构实战三):Spring Boot Logback 邮件通知

    日志对于应用程序来说是非常重要的,当你的程序报错了,而你又不知道是多么可怕的一件事情,本文使用logback把程序报错信息邮件到开发者

    微服务架构实战(六):Spring boot2.x 集成阿里大鱼短信接口详解与Demo

    Spring boot2.x 集成阿里大鱼短信接口,发送短信验证码及短信接口详解。

    架构实战(二)-Spring Boot整合Swagger,让你的API可视化

    你还在跟前端对接上花费很多的时间而没有效果吗?你还在为写接口文档而烦恼吗?今天就教大家一个接口对接神器...

    码云推荐:一个优秀的分布式spring boot/Spring Cloud API限流框架,特别适合微服务架构

    一个优秀的分布式spring boot/Spring Cloud API限流框架,特别适合微服务架构.

    架构实战(九):Spring Boot 集成 RocketMQ

    快速集成阿里开源消息队列 RocketMQ

    微服务架构学习笔记:gRPC Spring Boot Starter 2.2.0 发布,及使用步骤

    gRPC Spring Boot Starter 项目是一个 gRPC 的 Spring Boot 模块。内嵌一个 gRPC Server 对外提供服务,并支持 Spring Cloud 的服务发现