| :-- | :-- | :-- |
| 项目主页 | https://github.com/zq2599/blog_demos | 该项目在GitHub上的主页 |
| git仓库地址(https) | https://github.com/zq2599/blog_demos.git | 该项目源码的仓库地址,https协议 |
| git仓库地址(ssh) | git@github.com:zq2599/blog_demos.git | 该项目源码的仓库地址,ssh协议 |
- 这个git项目中有多个文件夹,本章的应用在kubernetesclient文件夹下,如下图红框所示:
- 打开《Kubernetes官方java客户端:准备》中创建的的kubernetesclient工程,在里面创建子工程,名为helloworld,这是个SpringBoot工程,pom.xml内容如下:
xsi:schemaLocation=“http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd”> 4.0.0 com.bolingcavalry kubernetesclient 1.0-SNAPSHOT …/pom.xml com.bolingcavalry helloworld 0.0.1-SNAPSHOT helloworld Demo project for Spring Boot jar org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-actuator org.projectlombok lombok true io.kubernetes client-java org.springframework.boot spring-boot-maven-plugin 2.3.0.RELEASE true package com.bolingcavalry.demo; import com.google.gson.Gson; import io.kubernetes.client.openapi.ApiClient; import io.kubernetes.client.openapi.Configuration; import io.kubernetes.client.openapi.apis.CoreV1Api; import io.kubernetes.client.openapi.models.V1PodList; import io.kubernetes.client.util.Config; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; import java.util.stream.Collectors; @SpringBootApplication @RestController @Slf4j public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @RequestMapping(value = “/hello”) public List hello() throws Exception { ApiClient client = Config.defaultClient(); Configuration.setDefaultApiClient(client); CoreV1Api api = new CoreV1Api(); // 调用客户端API取得所有pod信息 V1PodList v1PodList = api.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null); // 使用Gson将集合对象序列化成JSON,在日志中打印出来 log.info(“pod info n{}”, new Gson().toJson(v1PodList)); return v1PodList .getItems() .stream() .map(value -> value.getmetadata().getNamespace() “:” value.getmetadata().getName()) .collect(Collectors.toList()); } } 还记得《Kubernetes官方java客户端之二:序列化和反序列化问题》提到的序列化问题吗?上述代码中,log.info那段代码里对V1PodList执行序列化的是Gson,并且hello方法返回的也不是V1PodList实例,而是新做的一个List实例,这样做是因为jackson对V1PodList做序列化会导致异常,这里要避免jackson参与序列化操作; 应用的代码已经写完,接下来是镜像制作用到的Dockerfile文件,该文件和刚才创建的pom.xml文件在同一个目录下(即子工程helloworld的文件夹下),Dockerfile文件内容如下: FROM openjdk:8u212-jdk-stretch as builder WORKDIR application ARG JAR_FILE=target/*.jar COPY ${JAR_FILE} application.jar RUN java -Djarmode=layertools -jar application.jar extract FROM openjdk:8u212-jdk-stretch WORKDIR application COPY --from=builder application/dependencies/ ./ COPY --from=builder application/spring-boot-loader/ ./ COPY --from=builder application/snapshot-dependencies/ ./ COPY --from=builder application/application/ ./ ENTRYPOINT [“java”, “org.springframework.boot.loader.JarLauncher”] mvn clean package -U -DskipTests 接下来要制作镜像文件了,请确保当前电脑已经安装并运行了docker,另外构建docker镜像的操作我仅在macOS和Linux操作系统下执行成功,在Windows环境能否成功请自行尝试; 在Dockerfile所在目录执行以下命令即可创建docker镜像文件: docker build -t 192.168.50.43:5888/common/helloworld:1.0-SNAPSHOT . apiVersion: v1 kind: Service metadata: name: helloworld namespace : kubernetesclient spec: type: NodePort ports: nodePort: 30100【一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义】
浏览器打开:qq.cn.hn/FTf 免费领取
指定基础镜像,这是分阶段构建的前期阶段



