feat(加入docker-java): 加入docker-java
This commit is contained in:
parent
38a72d5ea2
commit
9afa1cb1c4
31
pom.xml
31
pom.xml
|
@ -336,6 +336,37 @@
|
||||||
<version>2.4.0</version>
|
<version>2.4.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- docker java -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.github.docker-java</groupId>
|
||||||
|
<artifactId>docker-java</artifactId>
|
||||||
|
<version>3.2.13</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- docker java httpclient -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.github.docker-java</groupId>
|
||||||
|
<artifactId>docker-java-transport-httpclient5</artifactId>
|
||||||
|
<version>3.2.13</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.httpcomponents.client5</groupId>
|
||||||
|
<artifactId>httpclient5</artifactId>
|
||||||
|
<version>5.2</version> <!-- 或者适合你项目的版本 -->
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.httpcomponents.core5</groupId>
|
||||||
|
<artifactId>httpcore5</artifactId>
|
||||||
|
<version>5.2</version> <!-- 或者适合你项目的版本 -->
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|
|
@ -0,0 +1,37 @@
|
||||||
|
package com.guwan.backend.config;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import com.github.dockerjava.api.DockerClient;
|
||||||
|
import com.github.dockerjava.core.DockerClientBuilder;
|
||||||
|
import com.github.dockerjava.core.DefaultDockerClientConfig;
|
||||||
|
import com.github.dockerjava.transport.DockerHttpClient;
|
||||||
|
import com.github.dockerjava.httpclient5.ApacheDockerHttpClient;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class DockerConfig {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public DockerClient dockerClient() {
|
||||||
|
// 配置 Docker 主机
|
||||||
|
DefaultDockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
|
||||||
|
.withDockerHost("tcp://localhost:2375") // 设置 Docker 主机地址
|
||||||
|
.build();
|
||||||
|
|
||||||
|
// 创建 ApacheHttpClient5 实现
|
||||||
|
DockerHttpClient httpClient = new ApacheDockerHttpClient
|
||||||
|
.Builder()
|
||||||
|
.dockerHost(config.getDockerHost())
|
||||||
|
.maxConnections(100)
|
||||||
|
.connectionTimeout(Duration.ofSeconds(30))
|
||||||
|
.responseTimeout(Duration.ofSeconds(45))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
// 创建 Docker 客户端并返回
|
||||||
|
return DockerClientBuilder.getInstance(config)
|
||||||
|
.withDockerHttpClient(httpClient)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
}
|
|
@ -286,8 +286,8 @@ public class CommonController {
|
||||||
|
|
||||||
// 使用 JSONObject 构造JSON
|
// 使用 JSONObject 构造JSON
|
||||||
JSONObject jsonObject = new JSONObject();
|
JSONObject jsonObject = new JSONObject();
|
||||||
jsonObject.put("input", input);
|
jsonObject.putOpt("input", input);
|
||||||
jsonObject.put("voice", voiceModel);
|
jsonObject.putOpt("voice", voiceModel);
|
||||||
|
|
||||||
String jsonInputString = jsonObject.toString();
|
String jsonInputString = jsonObject.toString();
|
||||||
System.out.println(jsonInputString);
|
System.out.println(jsonInputString);
|
||||||
|
|
|
@ -0,0 +1,60 @@
|
||||||
|
package com.guwan.backend.controller;
|
||||||
|
|
||||||
|
import com.github.dockerjava.api.model.Container;
|
||||||
|
import com.github.dockerjava.api.model.Image;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import com.github.dockerjava.api.DockerClient;
|
||||||
|
import com.github.dockerjava.api.model.Info;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/common")
|
||||||
|
public class DockerController {
|
||||||
|
|
||||||
|
private final DockerClient dockerClient;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public DockerController(DockerClient dockerClient) {
|
||||||
|
this.dockerClient = dockerClient;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/docker-info")
|
||||||
|
public String getDockerInfo() {
|
||||||
|
Info info = dockerClient.infoCmd().exec(); // 获取 Docker 守护进程的信息
|
||||||
|
|
||||||
|
|
||||||
|
System.out.println("info = " + info);
|
||||||
|
|
||||||
|
return info.toString(); // 将 Docker 信息返回到 HTTP 响应
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取镜像列表
|
||||||
|
*
|
||||||
|
* @param
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@GetMapping("/imageList")
|
||||||
|
public List<Image> imageList() {
|
||||||
|
List<Image> imageList = dockerClient.listImagesCmd().withShowAll(true).exec();
|
||||||
|
|
||||||
|
System.out.println("imageList = " + imageList);
|
||||||
|
return imageList;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@GetMapping("/listContainers")
|
||||||
|
public List<Container> listContainers() {
|
||||||
|
List<Container> exec = dockerClient.listContainersCmd().exec();
|
||||||
|
|
||||||
|
exec.forEach(container ->
|
||||||
|
System.out.println("name = " + Arrays.toString(container.getNames())));
|
||||||
|
return exec;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,23 @@
|
||||||
|
package com.guwan.backend.service;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import com.github.dockerjava.api.DockerClient;
|
||||||
|
import com.github.dockerjava.api.model.Info;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class DockerService {
|
||||||
|
|
||||||
|
private final DockerClient dockerClient;
|
||||||
|
|
||||||
|
// 通过构造函数注入 DockerClient
|
||||||
|
@Autowired
|
||||||
|
public DockerService(DockerClient dockerClient) {
|
||||||
|
this.dockerClient = dockerClient;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void printDockerInfo() {
|
||||||
|
Info info = dockerClient.infoCmd().exec(); // 获取 Docker 守护进程的信息
|
||||||
|
System.out.println(info.toString());
|
||||||
|
}
|
||||||
|
}
|
|
@ -222,3 +222,6 @@ xxl:
|
||||||
logpath: logs/xxljob-logs
|
logpath: logs/xxljob-logs
|
||||||
logretentiondays: 7
|
logretentiondays: 7
|
||||||
|
|
||||||
|
docker:
|
||||||
|
host: tcp://localhost:2375
|
||||||
|
api-version: 1.45
|
||||||
|
|
|
@ -0,0 +1,75 @@
|
||||||
|
package com.guwan.backend;
|
||||||
|
|
||||||
|
import cn.hutool.json.JSONObject;
|
||||||
|
import okhttp3.*;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class TestOj {
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
OkHttpClient client = new OkHttpClient();
|
||||||
|
|
||||||
|
MediaType mediaType = MediaType.parse("application/json");
|
||||||
|
|
||||||
|
//
|
||||||
|
JSONObject jsonObject = new JSONObject();
|
||||||
|
jsonObject.putOpt("language_id", 91);
|
||||||
|
jsonObject.putOpt("source_code", "class Main {\n" +
|
||||||
|
"\n" +
|
||||||
|
"\n" +
|
||||||
|
" public static void main(String[] args) {\n" +
|
||||||
|
" System.out.println(twoSum());\n" +
|
||||||
|
" }\n" +
|
||||||
|
" public static int twoSum() {\n" +
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
" return 1;\n" +
|
||||||
|
" }\n" +
|
||||||
|
"}");
|
||||||
|
jsonObject.putOpt("stdin", "SnVkZ2Uw");
|
||||||
|
|
||||||
|
String jsonInputString = jsonObject.toString();
|
||||||
|
//
|
||||||
|
// String jsonInputString = jsonObject.toString();
|
||||||
|
// System.out.println(jsonInputString);
|
||||||
|
|
||||||
|
RequestBody body = RequestBody.create(mediaType,
|
||||||
|
jsonInputString);
|
||||||
|
Request request = new Request.Builder()
|
||||||
|
.url("https://judge0-ce.p.rapidapi.com/submissions?base64_encoded=false&wait=true&fields=*")
|
||||||
|
.post(body)
|
||||||
|
.addHeader("x-rapidapi-key", "42c1d2d793mshf3e3050425b2b1bp142fe6jsn9d9553e51070")
|
||||||
|
.addHeader("x-rapidapi-host", "judge0-ce.p.rapidapi.com")
|
||||||
|
.addHeader("Content-Type", "application/json")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Response response = client.newCall(request).execute();
|
||||||
|
|
||||||
|
System.out.println("response = " + response);
|
||||||
|
|
||||||
|
// OkHttpClient client = new OkHttpClient();
|
||||||
|
//
|
||||||
|
// Request request = new Request.Builder()
|
||||||
|
// .url("https://judge0-ce.p.rapidapi.com/languages")
|
||||||
|
// .get()
|
||||||
|
// .addHeader("x-rapidapi-key", "42c1d2d793mshf3e3050425b2b1bp142fe6jsn9d9553e51070")
|
||||||
|
// .addHeader("x-rapidapi-host", "judge0-ce.p.rapidapi.com")
|
||||||
|
// .build();
|
||||||
|
//
|
||||||
|
// Response response = client.newCall(request).execute();
|
||||||
|
|
||||||
|
// 检查请求是否成功
|
||||||
|
if (response.isSuccessful()) {
|
||||||
|
// 获取并打印响应体内容
|
||||||
|
String responseBody = response.body().string(); // 读取响应体内容为字符串
|
||||||
|
System.out.println(responseBody);
|
||||||
|
} else {
|
||||||
|
System.out.println("Request failed. Code: " + response.code());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue