In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Original address: Liang Guizhao's blog
Blog address: blog.720ui.com
Welcome to the public number: "Service end thinking." A group of people with the same frequency grow together, improve together, and break the limitations of cognition.
In late 2018, Google open-sourced Jib, a new Java tool that makes it easy to containerize Java applications. With Jib, we don't need to write Dockerfiles or install Docker, we can containerize Java applications immediately by integrating them into Maven or Gradle plugins.
cdn.xitu.io/2019/4/16/16a21cd9c5f0bb2b? w=2412&h=988&f=png&s=125437">
Open Source Address: github.com/GoogleContainerTools/jib
What is Jib?
Jib is a fast and simple container image build tool that runs as part of Maven or Gradle without the need to write Dockerfiles or run Docker daemons. It builds our Docker image from Maven or Gradle and saves valuable build time by pushing only the layers that changed (not the entire application) to the registry. Now, let's compare Docker build flow to Jib build flow. Docker build process, as shown below.
The Jib build process looks like this.
II. Real combat reveals true knowledge 1. Building a Simple Java Project
We write a simple Java class.
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World! "); System.out.println("http://blog.720ui.com"); }}
Next, we create a pom.xml file.
4.0.0 com.lianggzone.sample.lib helloworld-samples 0.1 jar helloworld-samples UTF-8 1.0.2 3.8.0 org.apache.maven.plugins maven-compiler-plugin ${maven-compiler-plugin.version} 1.8 1.8 com.google.cloud.tools jib-maven-plugin ${jib-maven-plugin.version} registry.cn-hangzhou.aliyuncs.com/lianggzone/oracle_java8 registry.cn-hangzhou.aliyuncs.com/lianggzone/jib-helloworld:v1 -Xms512m -Xdebug com.lianggzone.HelloWorld package build
Because the default access to Google's gcr.io repository, and domestic access to gcr.io instability will often lead to network timeout, so the author used the domestic Alibaba cloud mirror service, then do not need to visit Google's repository. Now, we execute mvn compile jib:build to automate the build, which pulls the image from and uploads the resulting image to the set address. Here, the author also sets some JVM parameters through `.
mvn compile jib:build
In addition, if "login failed, unauthorized," you need to log in through docker login authentication. Also, better yet, you might consider placing credentials in Maven.
... ... registry.cn-hangzhou.aliyuncs.com Your Alibaba Cloud Account Your Alibaba Cloud password
Finally, after the execution is completed, we can obtain the image in Alibaba Cloud Mirror Repository.
That's it. Now, let's test it. We pull the image through docker pull and run it.
docker pull registry.cn-hangzhou.aliyuncs.com/lianggzone/jib-helloworld:v1docker run --name jib-helloworld -it registry.cn-hangzhou.aliyuncs.com/lianggzone/jib-helloworld:v1 /bin/bash
The results of the execution are shown below.
2. Building a Runnable Jar for SpringBoot
Let's take a more complex project and build a SpringBoot project. For SpringBoot usage, read my previous article: http://blog.720ui.com/columns/springboot_all/. Now, we first need to build a project and create a startup class.
@SpringBootApplicationpublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}
At the same time, a Web interface is needed.
@RestControllerpublic class WebController { @RequestMapping("/blog") public String index() { return "http://blog.720ui.com"; }}
Next, we create a pom.xml file.
4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.2.RELEASE com.lianggzone.sample.lib springboot-samples 0.1 jar springboot-samples org.springframework.boot spring-boot-starter-web UTF-8 1.0.2 3.8.0 org.apache.maven.plugins maven-compiler-plugin ${maven-compiler-plugin.version} 1.8 1.8 com.google.cloud.tools jib-maven-plugin ${jib-maven-plugin.version} registry.cn-hangzhou.aliyuncs.com/lianggzone/oracle_java8 registry.cn-hangzhou.aliyuncs.com/lianggzone/jib-springboot:v1 -Xms512m -Xdebug package build
Now we execute the mvn compile jib:build command to automate the build. After the execution is completed, we can obtain the image in Alibaba Cloud Image Repository.
Now, let's test it again. We pull the image through docker pull and run it.
docker pull registry.cn-hangzhou.aliyuncs.com/lianggzone/jib-springboot:v1docker run -p 8080:8080 --name jib-springboot -it registry.cn-hangzhou.aliyuncs.com/lianggzone/jib-springboot:v1 /bin/bash
The results of the execution are shown below.
Now, we visit http://localhost:8080/blog, we can call API interface normally.
3. Building a WAR Project
Jib also supports the WAR project. If a Maven project uses war-packaging type, Jib will deploy the project using distrless Jetty as the base image by default. To use different base mirrors, we can customize and 。The following is an example of using Tomcat mirroring.
tomcat:8.5-jre8-alpine The requested URL/usr/webapps/was not found on this server.
Source code address: github.com/lianggzone/jib-samples
Attachment: Reference github.com/GoogleContainerTools/jibhttps://github.com/GoogleContainerTools/jib/tree/master/jib-maven-plugin
(End, please indicate the author and source for reprinting.)
Write it at the end
[Server side thinking]: Let's talk about the core technology of the server side and discuss the project architecture and practical experience of the first-line Internet. At the same time, there are many technical bulls in the "back-end circle" family, looking forward to your participation, a group of people with the same frequency, grow together, improve together, break the limitations of cognition.
More excellent articles, all in the "server side thinking"!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 239
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.