資料內(nèi)容:
7.持續(xù)集成
項(xiàng)目開發(fā)完成后,通過 jenkins 持續(xù)集成,打包成 docker 鏡像后上傳到服務(wù)器上的 docker
repository 后運(yùn)行,所以需要在 pom.xml 文件里配置 maven docker 插件。
Pom.xml 關(guān)鍵配置:
<resources><!-- 使用@@站位符,輸出 Dockerfile 至 docker 文件夾 -->
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.11</version>
<configuration><imageName>172.168.4.43:5000/${project.artifactId}</imageName>
<dockerDirectory>src/main/docker</dockerDirectory>
<pushImage>true</pushImage>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
在 src/main/下建立 docker 目錄,并新建 Dockerfile,內(nèi)容如下:
FROM java:8
VOLUME /tmp
ADD spring-boot-demo-0.0.1-SNAPSHOT.jar app.jar
EXPOSE 8080
CMD java -jar app.jar
當(dāng)程序經(jīng)過測試并提交到 gitlab 后,jenkins 將自動構(gòu)建并打包成 docker 鏡像發(fā)布到遠(yuǎn)程
docker 倉庫。同時(shí) jenkins 會通知遠(yuǎn)程服務(wù)下載該鏡像并重新運(yùn)行。
8.集成 mybatis
所有數(shù)據(jù)庫操作采用 Mybatis 來管理。集成 Mybatis 關(guān)鍵配置如下:
添加 pom.xm 依賴
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter-test</artifactId>
<version>1.3.0</version>
</dependency>
<!--以 mysql 數(shù)據(jù)庫為例-->
<dependency><groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>