---优化docker镜像打包
This commit is contained in:
@ -19,7 +19,7 @@
|
||||
<maven.compiler.target>${java.version}</maven.compiler.target>
|
||||
<libServerUrl>http://10.10.2.102:9500</libServerUrl>
|
||||
<libServerUser>pcitc</libServerUser>
|
||||
<libServerPwd>K9$pQ3!zX7@rT2&w</libServerPwd>
|
||||
<libServerPwd>K9pQ3!zX7@rT2w</libServerPwd>
|
||||
<ignoreUpload>false</ignoreUpload>
|
||||
</properties>
|
||||
|
||||
|
||||
@ -20,7 +20,7 @@
|
||||
<docker-image-name>pcitc/itc-pcitc-demo</docker-image-name>
|
||||
<libServerUrl>http://10.10.2.102:9509/</libServerUrl>
|
||||
<libServerUser>pcitc</libServerUser>
|
||||
<libServerPwd>K9$pQ3!zX7@rT2&w</libServerPwd>
|
||||
<libServerPwd>K9pQ3!zX7@rT2w</libServerPwd>
|
||||
<ignoreUpload>false</ignoreUpload>
|
||||
<docker-jar-name>${project.build.finalName}-server.jar</docker-jar-name>
|
||||
</properties>
|
||||
|
||||
@ -5,8 +5,8 @@ FROM jdk8u112:v1
|
||||
RUN mkdir -p /root/logs/java/ \
|
||||
&& mkdir -p /scm/logs/ \
|
||||
&& mkdir -p /opt/app/ \
|
||||
&& mkdir -p /opt/app/lib/ \
|
||||
&& mkdir -p /uploadFile/ \
|
||||
&& mkdir -p /opt/libs/ \
|
||||
&& mkdir -p /var/log/app \
|
||||
&& mkdir -p /opt/app/logs \
|
||||
&& mkdir -p /opt/agent/config
|
||||
@ -29,14 +29,14 @@ ADD ./${SOURCE_NAME} /opt/app/app.jar
|
||||
#RUN test -f /opt/app/app.jar || { echo "Error: JAR file not found at ${SOURCE_NAME}"; exit 1; }
|
||||
|
||||
#应用名称
|
||||
ENV APP_NAME=itc-pcitc-mdm
|
||||
ENV APP_NAME=demo-service
|
||||
#skywalking-oap-server地址
|
||||
ENV SKYWALKING_SERVER=10.10.2.102:11800
|
||||
ENV ENV_APP_FILE_PATH=/opt/app/app.jar
|
||||
ENV ENV_APP_PRO_FILE=local
|
||||
ENV LIB_SERVER_ADDR=http://10.10.2.102:9500/
|
||||
ENV LIB_SERVER_ADDR=http://10.10.2.102:9509/
|
||||
ENV LIB_SERVER_USER=pcitc
|
||||
ENV LIB_SERVER_PWD=K9$pQ3!zX7@rT2&w
|
||||
ENV LIB_SERVER_PWD=K9pQ3!zX7@rT2w
|
||||
|
||||
EXPOSE 8096 5005
|
||||
|
||||
|
||||
@ -1,6 +1,10 @@
|
||||
package com.xjrsoft;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.stream.Stream;
|
||||
@ -13,6 +17,9 @@ import org.springframework.test.context.junit4.SpringRunner;
|
||||
import com.xjrsoft.module.common.db.service.CommonCallService;
|
||||
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.http.HttpResponse;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = ITCDemoApplication.class)
|
||||
@ -29,20 +36,21 @@ public class CallTest {
|
||||
System.out.println(out);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
String filePath = "D:\\java\\conf\\cn-ips.txt";
|
||||
boolean first = false;
|
||||
// 2. 流式读取(适合大文件,逐行处理,不占内存)
|
||||
try (Stream<String> lineStream = Files.lines(Paths.get(filePath))) {
|
||||
lineStream.forEach(line -> {
|
||||
// 逐行处理逻辑(如解析、过滤)
|
||||
if (!line.isEmpty()) { // 跳过空行
|
||||
System.out.print(";" + line);
|
||||
}
|
||||
});
|
||||
} catch (IOException e) {
|
||||
System.err.println("流式读取失败:" + e.getMessage());
|
||||
private static String encode(String val) throws UnsupportedEncodingException {
|
||||
return URLEncoder.encode(val, StandardCharsets.UTF_8.name());
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
// HttpRequest post = HttpUtil.createPost("http://10.10.2.102:9500/sys/jar/download?name="+encode("netty-common-4.1.84.Final.jar")+"&env=local");
|
||||
// post.header("Content-Type", "application/json;charset=UTF-8");
|
||||
// post.header("Authorization", "Basic cGNpdGM6SzkkcFEzIXpYN0ByVDImdw==");
|
||||
// HttpResponse response = post.execute();
|
||||
// //byte[] bytes = response.bodyBytes();
|
||||
// System.out.println(response.header("Content-Type"));
|
||||
// System.out.println(response.headers());
|
||||
// System.out.println(response.header("Content-Length"));
|
||||
//System.out.println(response.body());
|
||||
JarUtils.download("http://10.10.2.102:9500/","Basic cGNpdGM6SzkkcFEzIXpYN0ByVDImdw==","local","netty-common-4.1.84.Final.jar",new File("D:/netty-common-4.1.84.Final.jar"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -0,0 +1,200 @@
|
||||
package com.xjrsoft;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class JarUtils {
|
||||
|
||||
/**
|
||||
* @Description: TODO(这里用一句话描述这个方法的作用)
|
||||
* @param baseURI jar管理服务地址
|
||||
* @param env 环境
|
||||
* @param name jar名称
|
||||
* @param outDir 保存jar包文件夹
|
||||
* @throws Exception
|
||||
* @return void 返回类型
|
||||
*/
|
||||
public static void download(String baseURI,String authorization,String env,String name,File jar) throws Exception {
|
||||
try {
|
||||
HttpURLConnection httpConn = null;
|
||||
// 获取输入输出流
|
||||
InputStream is = null;
|
||||
FileOutputStream os = null;
|
||||
try {
|
||||
URL url = new URL(parseDowloadUrl(baseURI,env,name));
|
||||
httpConn = (HttpURLConnection) url.openConnection();
|
||||
httpConn.setRequestMethod("POST");
|
||||
httpConn.setDoOutput(true);
|
||||
httpConn.setDoInput(true);
|
||||
httpConn.setUseCaches(false);
|
||||
httpConn.setConnectTimeout(30 * 1000); // 连接超时
|
||||
httpConn.setReadTimeout(5 * 60 * 1000); // 读取超时
|
||||
httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
|
||||
httpConn.setRequestProperty("Authorization", authorization);
|
||||
int responseCode = httpConn.getResponseCode();
|
||||
if (responseCode < 200 || responseCode >= 300) {
|
||||
String errorMsg = String.format("下载Jar失败:URL=%s,响应码=%d,响应信息=%s",
|
||||
url.toString(), responseCode, httpConn.getResponseMessage());
|
||||
System.out.println(errorMsg);
|
||||
throw new RuntimeException(errorMsg);
|
||||
}
|
||||
|
||||
is = httpConn.getInputStream();
|
||||
os = new FileOutputStream(jar);
|
||||
// 写入文件流,缓存为10MB
|
||||
byte[] buf = new byte[2048];
|
||||
int len;
|
||||
long totalBytes = 0; // 记录下载字节数,便于日志排查
|
||||
while((len=is.read(buf)) != -1) {
|
||||
os.write(buf, 0, len);
|
||||
totalBytes += len;
|
||||
}
|
||||
os.flush();
|
||||
String msg =String.format("Jar下载成功!名称=%s,保存路径=%s,总大小=%d KB",name, jar.getAbsolutePath(), totalBytes / 1024);
|
||||
System.out.println(msg);
|
||||
} finally {
|
||||
if(is!=null) is.close();
|
||||
if(os!=null) os.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("下载jar["+name+"]失败!",e);
|
||||
}
|
||||
}
|
||||
|
||||
private static String parseDowloadUrl(String url,String env,String name) throws UnsupportedEncodingException {
|
||||
if(url.endsWith("/")) {
|
||||
url = url.substring(0, url.length()-1);
|
||||
}
|
||||
return url+"/sys/jar/download?name="+encode(name)+"&&env="+encode(env);
|
||||
}
|
||||
|
||||
private static String encode(String val) throws UnsupportedEncodingException {
|
||||
return URLEncoder.encode(val, StandardCharsets.UTF_8.name());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Jar包信息列表
|
||||
*
|
||||
* @param baseURI jar管理服务基础地址
|
||||
* @param authorization 认证令牌(如Bearer token)
|
||||
* @param appName 应用名称
|
||||
* @param env 环境标识(如dev/test/prod)
|
||||
* @return Jar信息列表
|
||||
* @throws Exception 当请求失败或处理异常时抛出
|
||||
*/
|
||||
public static List<TJarInfo> getJarInfo(String baseURI, String authorization, String appName, String env) throws Exception {
|
||||
// 构建表单参数(application/x-www-form-urlencoded格式需用&分隔)
|
||||
String encodedAppName = URLEncoder.encode(appName, StandardCharsets.UTF_8.name());
|
||||
String encodedEnv = URLEncoder.encode(env, StandardCharsets.UTF_8.name());
|
||||
// 构建请求URL
|
||||
URL url = new URL(parseUrl(baseURI, "/sys/jar/getAppInfo?appName=" + encodedAppName + "&env=" + encodedEnv));
|
||||
HttpURLConnection httpConn = null;
|
||||
|
||||
try {
|
||||
// 初始化HTTP连接
|
||||
httpConn = (HttpURLConnection) url.openConnection();
|
||||
httpConn.setRequestMethod("GET");
|
||||
httpConn.setDoOutput(true);
|
||||
httpConn.setDoInput(true);
|
||||
httpConn.setUseCaches(false);
|
||||
httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
|
||||
httpConn.setRequestProperty("Authorization", authorization);
|
||||
|
||||
// 获取响应码
|
||||
int responseCode = httpConn.getResponseCode();
|
||||
// 根据响应码选择输入流(200用正常流,其他用错误流)
|
||||
try (InputStream in = responseCode == 200 ? httpConn.getInputStream() : httpConn.getErrorStream();
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8))) {
|
||||
|
||||
if (responseCode != 200) {
|
||||
// 读取错误信息并抛出异常
|
||||
String errorMsg = readAllLines(br);
|
||||
throw new IOException("获取jar明细失败,响应码:" + responseCode + ",错误信息:" + errorMsg);
|
||||
}
|
||||
|
||||
// 解析正常响应数据
|
||||
List<TJarInfo> infos = new ArrayList<>();
|
||||
String line = null;
|
||||
while ((line = br.readLine()) != null){
|
||||
String[] splits = line.split("=", 2); // 限制分割为2部分,避免值中包含=的情况
|
||||
if (splits.length == 2) { // 校验分割结果,避免数组越界
|
||||
infos.add(new TJarInfo(splits[0], "Y".equals(splits[1])));
|
||||
} else {
|
||||
// 日志记录非法格式的行(建议替换为日志框架)
|
||||
System.err.println("无效的响应行格式:" + line);
|
||||
}
|
||||
}
|
||||
return infos;
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
// 包装异常,保留原始堆栈
|
||||
throw new Exception("获取jar明细失败:" + e.getMessage(), e);
|
||||
} finally {
|
||||
// 确保连接关闭
|
||||
if (httpConn != null) {
|
||||
httpConn.disconnect();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取BufferedReader的所有行
|
||||
*/
|
||||
private static String readAllLines(BufferedReader br) throws IOException {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String line;
|
||||
while ((line = br.readLine()) != null) {
|
||||
sb.append(line).append(System.lineSeparator());
|
||||
}
|
||||
return sb.toString().trim();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the text read from the given reader as a String. Closes the given
|
||||
* reader upon return.
|
||||
*/
|
||||
public static String getText(BufferedReader reader) throws IOException {
|
||||
try {
|
||||
StringBuilder source = new StringBuilder();
|
||||
String line = reader.readLine();
|
||||
|
||||
while (line != null) {
|
||||
source.append(line);
|
||||
source.append('\n');
|
||||
line = reader.readLine();
|
||||
}
|
||||
return source.toString();
|
||||
} finally {
|
||||
try {
|
||||
reader.close();
|
||||
} catch (IOException ex) {
|
||||
// Ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static String parseUrl(String url,String path) {
|
||||
if(url.endsWith("/")) {
|
||||
url = url.substring(0, url.length()-1);
|
||||
}
|
||||
return url+path;
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
URLEncoder.encode(null, StandardCharsets.UTF_8.name());
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,19 @@
|
||||
package com.xjrsoft;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class TJarInfo {
|
||||
|
||||
private String name; //jar名称
|
||||
|
||||
private boolean custom;//开发jar包,需要强制更新
|
||||
|
||||
public TJarInfo(String name, boolean custom) {
|
||||
super();
|
||||
this.name = name;
|
||||
this.custom = custom;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -79,12 +79,8 @@
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>${java.version}</source>
|
||||
<target>${java.version}</target>
|
||||
</configuration>
|
||||
<groupId>com.geg</groupId>
|
||||
<artifactId>tanuki-compiler-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
<resources>
|
||||
|
||||
@ -181,12 +181,8 @@
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>${java.version}</source>
|
||||
<target>${java.version}</target>
|
||||
</configuration>
|
||||
<groupId>com.geg</groupId>
|
||||
<artifactId>tanuki-compiler-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
<resources>
|
||||
|
||||
@ -38,12 +38,8 @@
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>${java.version}</source>
|
||||
<target>${java.version}</target>
|
||||
</configuration>
|
||||
<groupId>com.geg</groupId>
|
||||
<artifactId>tanuki-compiler-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
<resources>
|
||||
|
||||
@ -74,12 +74,8 @@
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>${java.version}</source>
|
||||
<target>${java.version}</target>
|
||||
</configuration>
|
||||
<groupId>com.geg</groupId>
|
||||
<artifactId>tanuki-compiler-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
<resources>
|
||||
|
||||
@ -18,6 +18,8 @@
|
||||
<maven.compiler.source>${java.version}</maven.compiler.source>
|
||||
<maven.compiler.target>${java.version}</maven.compiler.target>
|
||||
<docker-image-name>docker.ges.bjgastx.com/itc-pcitc-mdm</docker-image-name>
|
||||
<docker-jar-name>${project.build.finalName}-server.jar</docker-jar-name>
|
||||
<appName>pcitc-mdm-service</appName>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
@ -32,22 +34,6 @@
|
||||
<finalName>app</finalName>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<version>2.7.6</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>repackage</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<!--<classifier>classes</classifier>-->
|
||||
<attach>false</attach>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<configuration>
|
||||
@ -61,30 +47,14 @@
|
||||
<source>${java.version}</source>
|
||||
<target>${java.version}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>com.geg</groupId>
|
||||
<artifactId>tanuki-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>com.spotify</groupId>
|
||||
<artifactId>docker-maven-plugin</artifactId>
|
||||
<version>1.2.2</version>
|
||||
<configuration>
|
||||
<dockerHost>${docker-registry}</dockerHost>
|
||||
<timeout>600000</timeout>
|
||||
<imageName>${docker-image-name}:${maven.build.timestamp}</imageName>
|
||||
<baseImage>java</baseImage>
|
||||
<dockerDirectory>${dockerDirectory}</dockerDirectory>
|
||||
<pushImage>${docker-image-name}:${maven.build.timestamp}</pushImage>
|
||||
<skipDockerBuild>false</skipDockerBuild>
|
||||
<buildArgs>
|
||||
<SOURCE_NAME>${docker-jar-name}</SOURCE_NAME>
|
||||
</buildArgs>
|
||||
<resources>
|
||||
<resource>
|
||||
<targetPath>/</targetPath>
|
||||
<directory>${project.build.directory}</directory>
|
||||
<include>${docker-jar-name}</include>
|
||||
</resource>
|
||||
</resources>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
<resources>
|
||||
|
||||
Reference in New Issue
Block a user