关于云计算:Kubernetes官方java客户端之八fluent-style

1次阅读

共计 7405 个字符,预计需要花费 19 分钟才能阅读完成。

欢送拜访我的 GitHub

https://github.com/zq2599/blog_demos

内容:所有原创文章分类汇总及配套源码,波及 Java、Docker、Kubernetes、DevOPS 等;

概览

  1. 本文是《Kubernetes 官网 java 客户端》系列的第八篇,以下提到的 <font color=”blue”>java 客户端 </font> 都是指 client-jar.jar;
  2. 前文《Kubernetes 官网 java 客户端之七:patch 操作》波及的知识点、代码、操作都太多了,<font color=”red”> 对作者和读者都是莫大的折磨 </font>,到了本篇咱们轻松一下,写几段简略晦涩的代码,理解 java 客户端对 fluent style 编程的反对,并且编码实现后的验证操作也很简略;

对于 fluent styel

  1. 也称为 fluid coding, fluent programming,是一种加强代码可读性的格调,使得浏览代码时更加天然晦涩,特点是函数返回无关类型,使得多个函数的调用前后链接起来。
  2. 对于 fluent style 能够参考 Martin Flowler 于 2005 年发表的文章,地址是:https://martinfowler.com/blik…,应用 fluent style 前后的代码对比方下图所示:


### 源码下载

  1. 如果您不想编码,能够在 GitHub 下载所有源码,地址和链接信息如下表所示 (https://github.com/zq2599/blo…:
名称 链接 备注
我的项目主页 https://github.com/zq2599/blo… 该我的项目在 GitHub 上的主页
git 仓库地址 (https) https://github.com/zq2599/blo… 该我的项目源码的仓库地址,https 协定
git 仓库地址 (ssh) git@github.com:zq2599/blog_demos.git 该我的项目源码的仓库地址,ssh 协定
  1. 这个 git 我的项目中有多个文件夹,本章的利用在 <font color=”blue”>kubernetesclient</font> 文件夹下,如下图红框所示:

实战步骤概述

  1. 在父工程 <font color=”blue”>kubernetesclient</font> 上面新建名为 <font color=”red”>fluent</font> 的子工程;
  2. fluent 工程中只有一个类 FluentStyleApplication,启动的 main 办法以及 fluent style 的代码都在此类中;
  3. <font color=”blue”>FluentStyleApplication.java</font> 提供四个 web 接口,性能别离是:新建 namespace、新建 deployment、新建 service、删除后面三个接口新建的所有资源;
  4. <font color=”blue”>fluent</font> 工程编码实现后,不须要做成镜像部署在 kubernetes 环境外部,而是作为一个一般 SpringBoot 利用找个 java 环境启动即可,与《Kubernetes 官网 java 客户端之三:内部利用》一文的部署和启动统一;
  5. 顺次调用每个接口,验证性能是否合乎预期;

编码

  1. 在父工程 <font color=”blue”>kubernetesclient</font> 上面新建名为 <font color=”red”>fluent</font> 的 maven 子工程,pom.xml 内容如下,须要留神的是排除掉 <font color=”blue”>spring-boot-starter-json</font>,起因请参考《Kubernetes 官网 java 客户端之二:序列化和反序列化问题》:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.bolingcavalry</groupId>
        <artifactId>kubernetesclient</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <groupId>com.bolingcavalry</groupId>
    <artifactId>fluent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>fluent</name>
    <description>Demo project for fluent style</description>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-json</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>io.kubernetes</groupId>
            <artifactId>client-java</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.0.RELEASE</version>
            </plugin>
        </plugins>
    </build>

</project>
  1. 新建 <font color=”blue”>FluentStyleApplication.java</font>,首先,该类作为启动类,要有 main 办法:
public static void main(String[] args) {SpringApplication.run(FluentStyleApplication.class, args);
}
  1. 定义常量 <font color=”blue”>NAMESPACE</font> 作为本次实战的 namespace:
private final static String NAMESPACE = "fluent";
  1. 用 @PostConstruct 注解润饰 setDefaultApiClient 办法,令其在实例化时执行一次,外面做了一些全局性的初始化设置,留神 <font color=”blue”>kubeConfigPath</font> 变量对应的 config 文件门路要正确:
/**
     * 默认的全局设置
     * @return
     * @throws Exception
     */
    @PostConstruct
    private void setDefaultApiClient() throws Exception {
        // 寄存 K8S 的 config 文件的全门路
        String kubeConfigPath = "/Users/zhaoqin/temp/202007/05/config";
        // 以 config 作为入参创立的 client 对象,能够拜访到 K8S 的 API Server
        ApiClient client = ClientBuilder
                .kubeconfig(KubeConfig.loadKubeConfig(new FileReader(kubeConfigPath)))
                .build();

        // 会打印和 API Server 之间申请响应的具体内容,生产环境慎用
        client.setDebugging(true);

        // 创立操作类
        Configuration.setDefaultApiClient(client);
    }
  1. 接下来是创立 namespace 的 web 服务,如下所示,因为 namespace 在 kubernetes 的 apiVersion 是 v1,因而创立的是 <font color=”blue”>V1Namespace</font> 实例:
 @RequestMapping(value = "/fluent/createnamespace")
    public V1Namespace createnamespace() throws Exception {V1Namespace v1Namespace = new V1NamespaceBuilder()
                .withNewMetadata()
                .withName(NAMESPACE)
                .addToLabels("label1", "aaa")
                .addToLabels("label2", "bbb")
                .endMetadata()
                .build();

        return new CoreV1Api().createNamespace(v1Namespace, null, null, null);
    }
  1. 为了更清晰的展示 <font color=”blue”>fluent style</font> 成果,将上述代码与创立 namespace 的 yaml 文件内容放在一起比照,如下图所示,可见对照着 yaml 文件就能将代码写进去:

  1. 接下来是创立 service 的代码,为了便于和 yaml 对应起来,代码中特意加了缩进:
 @RequestMapping(value = "/fluent/createservice")
    public V1Service createservice() throws Exception {V1Service v1Service = new V1ServiceBuilder()
                // meta 设置
                .withNewMetadata()
                    .withName("nginx")
                .endMetadata()

                // spec 设置
                .withNewSpec()
                    .withType("NodePort")
                    .addToPorts(new V1ServicePort().port(80).nodePort(30103))
                    .addToSelector("name", "nginx")
                .endSpec()
                .build();

        return new CoreV1Api().createNamespacedService(NAMESPACE, v1Service, null, null, null);
    }
  1. 创立 deployment 的代码如下,因为内容较多所以绝对简单一些,请留神,因为 deployment 在 kubernetes 的 apiVersion 是 <font color=”blue”>extensions/v1beta1</font>,因而创立的是 <font color=”red”>ExtensionsV1beta1Deployment</font> 实例:
 @RequestMapping(value = "/fluent/createdeployment")
    public ExtensionsV1beta1Deployment createdeployment() throws Exception {ExtensionsV1beta1Deployment v1Deployment = new ExtensionsV1beta1DeploymentBuilder()
                // meta 设置
                .withNewMetadata()
                    .withName("nginx")
                .endMetadata()

                // spec 设置
                .withNewSpec()
                    .withReplicas(1)
                    // spec 的 templat
                    .withNewTemplate()
                        // template 的 meta
                        .withNewMetadata()
                            .addToLabels("name", "nginx")
                        .endMetadata()

                        // template 的 spec
                        .withNewSpec()
                            .addNewContainer()
                                .withName("nginx")
                                .withImage("nginx:1.18.0")
                                .addToPorts(new V1ContainerPort().containerPort(80))
                            .endContainer()
                        .endSpec()

                    .endTemplate()
                .endSpec()
                .build();

        return new ExtensionsV1beta1Api().createNamespacedDeployment(NAMESPACE, v1Deployment, null, null, null);
    } 
  1. 从上述代码可见,<font color=”blue”>withXXX</font> 和 <font color=”blue”>endXXX</font> 是成对呈现的,请在编程的时候留神不要脱漏了 endXXX,倡议在写 withXXX 的同时就把 endXXX 写上;
  2. 最初一个办法是清理所有资源的,后面创立的 deployment、service、namespace 都在此一次性清理掉,实际操作中发现了一个难堪的状况:删除 deployment 和 namespace 时,发送到 API Server 的删除申请都收到的操作胜利的响应,但 kubernetes 客户端在反序列化响应内容时抛出异样(日志中显示了详细情况),<font color=”red”> 鄙人能力无限暂未找到解决之道 </font>,因而只能用 try catch 来防止整个办法抛出异样,好在 kubernetes 实际上曾经删除胜利了,影响不大:
    @RequestMapping(value = "/fluent/clear")
    public String clear() throws Exception {

        // 删除 deployment
        try {new ExtensionsV1beta1Api().deleteNamespacedDeployment("nginx", NAMESPACE, null, null, null, null, null, null);
        } catch (Exception e)
        {log.error("delete deployment error", e);
        }

        CoreV1Api coreV1Api = new CoreV1Api();

        // 删除 service
        coreV1Api.deleteNamespacedService("nginx", NAMESPACE, null, null, null, null, null, null);

        // 删除 namespace
        try {coreV1Api.deleteNamespace(NAMESPACE, null, null, null, null, null, null);
        } catch (Exception e)
        {log.error("delete namespace error", e);
        }

        return "clear finish," + new Date();}
  1. 编码已实现,启动 fluent 工程,接下来开始验证性能是否失常;

验证

  1. 将 fluent 工程间接在 IEDA 环境启动;
  2. 浏览器拜访:<font color=”blue”>http://localhost:8080/fluent/createnamespace</font>,页面会展现 API Server 返回的残缺 namespace 信息:

  1. 浏览器拜访:<font color=”blue”>http://localhost:8080/fluent/createservice</font>,页面会展现 API Server 返回的残缺 service 信息:

  1. 浏览器拜访:<font color=”blue”>http://localhost:8080/fluent/createdeployment</font>,页面会展现 API Server 返回的残缺 deployment 信息:

  1. 验证后面几个接口创立的服务是否可用,我这里 kubernetes 的 IP 地址是 <font color=”blue”>192.168.50.135</font>,因而拜访:<font color=”blue”>http://192.168.50.135:30103</font>,能够失常显示 nginx 首页:

  1. SSH 登录 kubernetes 服务器查看,通过 kubernetes 的 java 客户端创立的资源都失常:

  1. 验证实现后,浏览器拜访:<font color=”blue”>http://localhost:8080/fluent/clear</font>,即可清理掉后面三个接口创立的资源;
  • 至此,基于 fluent style 调用 java 客户端的实战就实现了,心愿您能纯熟应用此格调的 API 调用,使得编码变得更加轻松晦涩,顺便预报一下,下一篇持续做一些简略轻松的操作,指标是相熟 java 客户端的罕用操作;

你不孤独,欣宸原创一路相伴

  1. Java 系列
  2. Spring 系列
  3. Docker 系列
  4. kubernetes 系列
  5. 数据库 + 中间件系列
  6. DevOps 系列

欢送关注公众号:程序员欣宸

微信搜寻「程序员欣宸」,我是欣宸,期待与您一起畅游 Java 世界 …
https://github.com/zq2599/blog_demos

正文完
 0