利用NDP部署Spring Boot应用

阿凡达2018-07-04 10:50

1. 引言

本文主要分享利用公司的NDP自动化部署平台来部署Spring Boot应用,实践操作篇。

最近SpringBoot很火啊,公司越来越多的项目开始采用SpringBoot来作为主框架来搭建,充分说明大家对新技术的热情。我接触Spring Boot在2015年,我记得当时社区的开发人员对Spring框架越来越复杂的配置吐槽越来越多,所以Pivotal公司又搞了一个Spring Boot来简化Spring的配置。

Spring Boot在简化配置、打包和集成第三方工具方面确实做的很好,有很多人性化的配置,大大减低了Spring开发人员的入门门槛。如果你打算做一个小项目(实际上我觉得10W代码以下的项目都是小项目),那么不妨试试用Spring Boot来作为你的应用框架,确实有种令人耳目一新的感觉。

2. 多Module的Spring Boot项目

由于我的项目使用DDD的架构模式,所以自然就会进行多Module的分层。我的项目结构如图所示:

.
|-- .gitignore
|-- .gitreview
|-- README.md
|-- cloud_formatter.xml
|-- permission-application
|-- permission-client
|-- permission-domain
|-- permission-infrastructure
|-- permission-web
`-- pom.xml

多Module的项目结构非常常见,如果你的项目没有多个Module,请直接跳过本节。多Module的项目注意一点,在父pom里面,不要直接依赖Spring Boot的parent,而是相关依赖设置为import(如下代码所示),然而再导入相关的依赖。这样你的项目依赖就比较清晰直观。

<dependency>
   <!-- Import dependency management from Spring Boot -->
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>1.4.3.RELEASE</version>
    <type>pom</type>
    <scope>import</scope>
</dependency>

为了让我们的能顺利的打出可执行的jar包,还需要配置一个额外的插件,在父pom中配置

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>1.4.3.RELEASE</version>
</plugin>

在web的pom中配置:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
        <executions>
          <execution>
              <goals>
                 <goal>repackage</goal>
               </goals>
           </execution>
        </executions>
</plugin>

3. 配置NDP

NDP这个新的自动化部署平台主要由李晶在维护,虽然目前功能比较少,但是基本点都覆盖到了。首先申明,我不熟李晶的托,NDP上有些功能缺少还是比较好用的。NDP的地址为:http://ndp.netease.com

首先,要使用这个产品,需要在CMDB上添加应用和集群。例如,我有一个permission的应用,下面根据四个环境划分为四个集群。

添加好应用和集群后应该就可以在NDP上可以看到你的应用了:

  • 3.1 集群配置 我以permission-dev这个集群为例说一下NDP的配置,我的permission-dev集群配置如下:

  • 3.2 构建配置

    构建配置和老的OMAD比较像,我的配置如下:

注意选择自定义配置文件,我提一下我的ant的配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE project [<!ENTITY buildfile SYSTEM "file:./build-user.xml">]>
<project basedir="." default="deploy" name="demo">
<property name="groovy" value="groovy"/>
<property name="python" value="python"/>
<!-- jdk6 <property name="mvn" value="mvn1.6.0_64"/> -->
<!-- jdk7 <property name="mvn" value="mvn1.7.0_64"/> -->
<property name="mvn" value="mvnJDK8" />
<property name="src.dir" value="src/main/java"/>
<property name="resources.dir" value="src/main/resources"/>
<property name="dist.dir" value="target"/>
<property name="compress.dir" value="compressed"/>
<property name="baseline.dir" value="${basedir}"/>
<property name="artifact.dir" value="${baseline.dir}/compressed"/>
<property name="conf.dir" value="${baseline.dir}/conf"/>

<available property="folderexist" file="${conf.dir}" type="dir"/>

<target name="clean">
    <delete dir="${dist.dir}"/>
</target>

<target name="parentInstall">
    <exec dir=".." executable="${mvn}" failonerror="true">
        <arg line=" clean install -Dmaven.test.skip=true"/>
    </exec>
</target>

<target name="package" depends="clean,parentInstall">
    <exec dir="${baseline.dir}" executable="${mvn}" failonerror="true">
        <arg line=" clean package -Dmaven.test.skip=true"/>
    </exec>
</target>

<target name="compress-app">
    <mkdir dir="${compress.dir}"/>
            <mkdir dir="${compress.dir}/lib"/>
            <copy todir="${compress.dir}/lib" preservelastmodified="true">
                <fileset dir="${dist.dir}/" includes="*.jar"/>
            </copy>
</target>

<target name="deploy">
    <echo message="begin auto deploy......"/>
    <antcall target="package"/>
    <antcall target="compress-app" />
    <delete dir="${dist.dir}"/>
</target>
</project>
  • 这个ant配置文件是我自己编写的,基本上能满足大部分项目的需求。如果编译有问题,请自行更改。

  • 3.3 发布配置 在发布配置中新增有一个实例,我新增了一个实例default。

    具体配置参数如下:

    注意两个配置参数:

    main_class_name,这里填org.springframework.boot.loader.JarLauncher,因为Spring Boot的主引导类就是这个类,而不是@SpringBootApplication的类。大家可以解压jar包,查看其中的META-INF/MANIFEST.MF文件,其中的Main-Class就是org.springframework.boot.loader.JarLauncher

    还一个值得注意的参数就是jvm_extra,我这里填的-Dspring.profiles.active=liantiao -Ddubbo.protocol.host=10.185.3.138。因为我使用Spring Boot自带的profile来做配置文件管理,在我的resource目录下有一个config目录,其中的配置如下所示。

    config
    |-- application-dev.properties
    |-- application-liantiao.properties
    |-- application-online.properties
    |-- application-perf.properties
    |-- application-test.properties
    `-- application-yanlian.properties
    

    只需要在java的启动参数输入对应的环境名称就可以指定使用的配置文件,是不是好用很多?! -Ddubbo.protocol.host=10.185.3.138是用来解决dubbo多网卡的问题,没有使用dubbo的同学直接忽略。

4. 小结

Spring Boot是非常指定一试的应用框架,但是能在生成环境中使用起来也很关键。我利用NDP来部署Spring Boot应用已经由快两个月了,没有发生任何问题。NDP本身也还在不断完善中,喜欢尝鲜的同学不妨一试。

本文来自网易实践者社区,经作者蒋文康授权发布。