Google 在 2018 年下旬開源了一款新的 Java 東西 Jib,可以輕松地將 Java 使用步驟容器化。經(jīng)過 Jib,我們不必要編寫 Dockerfile 或安裝 Docker,經(jīng)過集成到 Maven 或 Gradle 插件,就可以立即將 Java 使用步驟容器化。
開源地點(diǎn):
https://github.com/GoogleContainerTools/jib
一、什么是 Jib
Jib 是一個(gè)快速而簡便的容器鏡像構(gòu)建東西,它作為 Maven 或 Gradle 的一局部運(yùn)轉(zhuǎn),不必要編寫 Dockerfile 或運(yùn)轉(zhuǎn) Docker 保衛(wèi)歷程。它從 Maven 或 Gradle 中構(gòu)建我們的 Docker 鏡像, 并只將產(chǎn)生變動(dòng)的層(而不是整個(gè)使用步驟)推送到注冊(cè)表來節(jié)流名貴的構(gòu)建時(shí)間。如今,我們對(duì) Docker 構(gòu)建流程和 Jib 構(gòu)建流程舉行比力。Docker 構(gòu)建流程,如下所示。
Jib 構(gòu)建流程,則是如此的。
二、實(shí)戰(zhàn)出真知
1. 構(gòu)建一個(gè)簡便的 Java 工程
我們編寫一個(gè)簡便的 Java 類。
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); System.out.println("http://blog.720ui.com"); } }
緊接著,我們?cè)賱?chuàng)建一個(gè) pom.xml 文件。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.lianggzone.sample.lib</groupId> <artifactId>helloworld-samples</artifactId> <version>0.1</version> <packaging>jar</packaging> <name>helloworld-samples</name> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <jib-maven-plugin.version>1.0.2</jib-maven-plugin.version> <maven-compiler-plugin.version>3.8.0</maven-compiler-plugin.version> </properties> <dependencies> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>${maven-compiler-plugin.version}</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <!-- Jib --> <plugin> <groupId>com.google.cloud.tools</groupId> <artifactId>jib-maven-plugin</artifactId> <version>${jib-maven-plugin.version}</version> <configuration> <from> <image>registry.cn-hangzhou.aliyuncs.com/lianggzone/oracle_java8</image> </from> <to> <image>registry.cn-hangzhou.aliyuncs.com/lianggzone/jib-helloworld:v1</image> </to> <container> <jvmFlags> <jvmFlag>-Xms512m</jvmFlag> <jvmFlag>-Xdebug</jvmFlag> </jvmFlags> <mainClass>com.lianggzone.HelloWorld</mainClass> </container> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>build</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
由于默許拜候谷歌的 gcr.io 貨倉,而國內(nèi)拜候 gcr.io 不安定會(huì)常常招致網(wǎng)絡(luò)超時(shí),以是筆者使用了國內(nèi)的阿里云鏡像辦事,那么就不必要拜候谷歌的貨倉了。如今,我們實(shí)行 mvn compile jib:build 下令舉行主動(dòng)化構(gòu)建,它會(huì)從 <from> 拉取鏡像,并把天生的鏡像上傳到 <to> 設(shè)置的地點(diǎn)。這里,筆者還經(jīng)過 ` 設(shè)置了一些 JVM 參數(shù)。
mvn compile jib:build
別的,假如”登錄失敗,未受權(quán)”,必要經(jīng)過 docker login 登錄鑒權(quán)一下。別的,更好的做法是,你可以思索在Maven 中安排憑據(jù)。
<settings> ... <servers> ... <server> <id>registry.cn-hangzhou.aliyuncs.com</id> <username>你的阿里云賬號(hào)</username> <password>你的阿里云暗碼</password> </server> </servers> </settings>
最初,實(shí)行完成后,我們可以在阿里云鏡像貨倉獲取鏡像。
功成名就,如今,我們來驗(yàn)證一把。我們經(jīng)過 docker pull 拉取鏡像,并運(yùn)轉(zhuǎn)。
docker pull registry.cn-hangzhou.aliyuncs.com/lianggzone/jib-helloworld:v1 docker run --name jib-helloworld -it registry.cn-hangzhou.aliyuncs.com/lianggzone/jib-helloworld:v1 /bin/bash
實(shí)行后果,如下所示。
2. 構(gòu)建一個(gè) SpringBoot 的可運(yùn)轉(zhuǎn) Jar
我們來一個(gè)繁復(fù)一些的項(xiàng)目,構(gòu)建一個(gè) SpringBoot 的項(xiàng)目。如今,我們起首必要搭建一個(gè)工程,并創(chuàng)建一個(gè)啟動(dòng)類。
@SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
同時(shí),必要一個(gè) Web 的接口。
@RestController public class WebController { @RequestMapping("/blog") public String index() { return "http://blog.720ui.com"; } }
緊接著,我們?cè)賱?chuàng)建一個(gè) pom.xml 文件。
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.2.RELEASE</version> </parent> <groupId>com.lianggzone.sample.lib</groupId> <artifactId>springboot-samples</artifactId> <version>0.1</version> <packaging>jar</packaging> <name>springboot-samples</name> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <jib-maven-plugin.version>1.0.2</jib-maven-plugin.version> <maven-compiler-plugin.version>3.8.0</maven-compiler-plugin.version> </properties> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>${maven-compiler-plugin.version}</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <!-- Jib --> <plugin> <groupId>com.google.cloud.tools</groupId> <artifactId>jib-maven-plugin</artifactId> <version>${jib-maven-plugin.version}</version> <configuration> <from> <image>registry.cn-hangzhou.aliyuncs.com/lianggzone/oracle_java8</image> </from> <to> <image>registry.cn-hangzhou.aliyuncs.com/lianggzone/jib-springboot:v1</image> </to> <container> <jvmFlags> <jvmFlag>-Xms512m</jvmFlag> <jvmFlag>-Xdebug</jvmFlag> </jvmFlags> </container> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>build</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
如今,我們實(shí)行 mvn compile jib:build 下令舉行主動(dòng)化構(gòu)建。實(shí)行完成后,我們可以在阿里云鏡像貨倉獲取鏡像。
如今,我們?cè)賮眚?yàn)證一把。我們經(jīng)過 docker pull 拉取鏡像,并運(yùn)轉(zhuǎn)。
docker pull registry.cn-hangzhou.aliyuncs.com/lianggzone/jib-springboot:v1 docker run -p 8080:8080 --name jib-springboot -it registry.cn-hangzhou.aliyuncs.com/lianggzone/jib-springboot:v1 /bin/bash
實(shí)行后果,如下所示。
如今,我們拜候
http://localhost:8080/blog ,我們可以正常調(diào)用 API 接口了。
3. 構(gòu)建一個(gè) WAR 工程
Jib 還支持 WAR 項(xiàng)目。假如 Maven 項(xiàng)目使用 war-packaging 典范,Jib 將默許使用 distroless Jetty 作為基本鏡像來擺設(shè)項(xiàng)目。要使用不同的基本鏡像,我們可以自界說 <container><appRoot> , <container> <entrypoint> 和 <container> <args> 。以下是使用 Tomcat 鏡像的案例。
<configuration> <from> <image>tomcat:8.5-jre8-alpine</image> </from> <container> <appRoot>/usr/local/tomcat/webapps/ROOT</appRoot> </container> </configuration>
作者:梁桂釗;原文:
http://news.51cto.com/art/201904/595059.htm
版權(quán)聲明:本文來自互聯(lián)網(wǎng)整理發(fā)布,如有侵權(quán),聯(lián)系刪除
原文鏈接:http://www.freetextsend.comhttp://www.freetextsend.com/shenghuojineng/40676.html