SpringBoot构建微服务实战 之 服务注册的快速打包

SpringBoot构建微服务实战 之 服务注册的快速打包

上一节我们学习了SpringBoot + Zookeeper 来构建微服务并服务注册到Zookeeper中去。下面我将介绍一种企业级应用的打包方式用来提供运营支持。即使用 appassembler-maven-plugin 来快速打包、部署、运行SpringBoot 服务


  • Appassembler

    • 官方介绍:

      • The Application Assembler Plugin is a Maven plugin for generating scripts for starting java applications. All dependencies and the artifact of the project itself are placed in a generated Maven repository in a defined assemble directory. All artifacts (dependencies + the artifact from the project) are added to the classpath in the generated bin scripts.
    • 通常理解:

      • Appassembler 是Maven 的一个构造组件,其目的在于生成可运行的脚本(同时支持Win.和Unix.)来启动java 应用程序。同时将项目的所有依赖(jar的形式)统一打包放在指定的路径下以支持应用程序的运行。

实例

前面我们学习了微服务的注册,但毕竟是在Eclipse IDE中启动的。总的来说运行方式不友好,因而我们想通过直接运行一个脚本就能将我们的服务拉起来,这样运维人员便可以随时接手我们的部分工作进而一定程度上减轻我们的工作量(不是为了偷懒- -!)。下面我们将来实现一下~~

  • 在pom.xml 做声明:

    <build>
        <plugins>
            <plugin>
                
                <groupId>org.codehaus.mojogroupId>
                <artifactId>appassembler-maven-pluginartifactId>
                
                <version>1.10version>
                <configuration>
                    
                    <platforms>
                        <platform>windowsplatform>
                        <platform>unixplatform>
                    platforms>
                    
                    <assembleDirectory>${project.build.directory}/contractassembleDirectory>
                    
                    <repositoryName>librepositoryName>
                    
                    <binFolder>binbinFolder>
                    
                    <configurationDirectory>confconfigurationDirectory>
                    
                    <copyConfigurationDirectory>truecopyConfigurationDirectory>
                    
                    <configurationSourceDirectory>src/main/resourcesconfigurationSourceDirectory>
                    
                    <repositoryLayout>flatrepositoryLayout>
                    <encoding>UTF-8encoding>
                    <logsDirectory>logslogsDirectory>
                    <tempDirectory>tmptempDirectory>
                    <programs>
                        <program>
                            
                            
                            <id>contractid>
                            <mainClass>com.sstps.contract.AppmainClass>
                            <jvmSettings>
                                <extraArguments>
                                    <extraArgument>-serverextraArgument>
                                    <extraArgument>-Xmx2GextraArgument>
                                    <extraArgument>-Xms2GextraArgument>
                                extraArguments>
                            jvmSettings>
                        program>
                    programs>
                configuration>
            plugin>
        plugins>
    build>

    配置的属性请看注解。

  • 执行打包命令:clean package appassembler:assemble
    SpringBoot构建微服务实战 之 服务注册的快速打包_第1张图片

  • 查看生成的文件包
    SpringBoot构建微服务实战 之 服务注册的快速打包_第2张图片

    • bin
      SpringBoot构建微服务实战 之 服务注册的快速打包_第3张图片

      market: Unix shell 脚本
      market.bat Windows Bat 脚本

    • conf
      SpringBoot构建微服务实战 之 服务注册的快速打包_第4张图片
      将原来classpath下的所有文件原封不动copy 过来。

    • lib
      SpringBoot构建微服务实战 之 服务注册的快速打包_第5张图片
      所有的依赖

    • logs 日志输出文件

    • tmp 临时文件存放点

  • 运行java 程序文件我们运行 contract.bat 来将我们的contract服务拉起来。

    SpringBoot构建微服务实战 之 服务注册的快速打包_第6张图片


小结

  • 使用 Application Assembler Maven Plugin 能够快速方便地运行项目,是一个不错的构造组件。

你可能感兴趣的:(SpringBoot)