环境

  • kubernetes:1.18
  • Docker:19.03
  • Istio:1.4.4
  • IntelliJ Java:2020
  • kubernetes-client:4.10.1
  • istio-client:1.1.1
  • Java:OpenJDK13
  • Spring Boot:2.1.14.RELEASE 

 

工程说明

Java访问Istio和访问Kubernetes一样,通过istio-java-api 访问。下面的例子为,链接https的kubernetes。其他方式类似。

 

注意点

Java版本JDK9及其以上,此处用的13

IP和端口号

证书内容需要Base64编码

 

工程依赖

<?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>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.14.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.you.micro</groupId>
    <artifactId>istio-demo</artifactId>
    <version>0.0.1</version>
    <name>istio-demo</name>
    <description>Istio Demo project for Spring Boot</description>

    <properties>
        <java.version>1.13</java.version>
    </properties>

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

        <dependency>
            <groupId>io.fabric8</groupId>
            <artifactId>kubernetes-client</artifactId>
            <version>4.10.1</version>
        </dependency>

        <dependency>
            <groupId>me.snowdrop</groupId>
            <artifactId>istio-client</artifactId>
            <version>1.1.1</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

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

</project>

 

工程源码

package com.you.micro.istiodemo.test;

import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.ConfigBuilder;

import me.snowdrop.istio.client.DefaultIstioClient;
import me.snowdrop.istio.client.IstioClient;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.core.io.ClassPathResource;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Base64;

public class ConnectIstio {

    public static void main(String[] args) {
        ConnectIstio connectIstio = new ConnectIstio();
        IstioClient demoIstioClient = connectIstio.getIstioClient();
        if (demoIstioClient != null) {
            String hostString = demoIstioClient.getMasterUrl().getHost();
            System.out.println("k8s host: " + hostString);
            connectIstio.closeIstioClient(demoIstioClient);
        } else {
            System.out.println("istio client is null");
        }

    }

    public IstioClient getIstioClient() {
        //文件为 /etc/kubernetes/pki 证书文件, 此处放在 工程的 resources 目录下
        String kubeCaPath = "ca.crt";
        String clientCertPath = "apiserver-kubelet-client.crt";
        String clientKeyPath = "apiserver-kubelet-client.key";
        String masterUrl = "https://ip:port";

        ClassPathResource pathResource = new ClassPathResource(kubeCaPath);
        String caData = readClassPathResourceFile(pathResource);
        pathResource = new ClassPathResource(clientCertPath);
        String certData = readClassPathResourceFile(pathResource);
        pathResource = new ClassPathResource(clientKeyPath);
        String keyData = readClassPathResourceFile(pathResource);

        IstioClient istioClient = null;

        if (StringUtils.isEmpty(caData) || StringUtils.isEmpty(certData) || StringUtils.isEmpty(keyData)) {
            return istioClient;
        }

        Config kubeConfig = new ConfigBuilder()
                .withMasterUrl(masterUrl)
                .withCaCertData(Base64.getEncoder().encodeToString(caData.getBytes()))
                .withClientCertData(Base64.getEncoder().encodeToString(certData.getBytes()))
                .withClientKeyData(Base64.getEncoder().encodeToString(keyData.getBytes()))
                .build();

        istioClient = new DefaultIstioClient(kubeConfig);
        return istioClient;

    }

    public String readClassPathResourceFile(ClassPathResource pathResource) {
        String fileContent = "";
        if (pathResource == null) {
            return fileContent;
        }
        boolean fileIsExists = pathResource.exists();
        if (!fileIsExists) {
            return fileContent;
        } else {
            try {
                fileContent = IOUtils.toString(new FileInputStream(pathResource.getFile()));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return fileContent;
    }

    public void closeIstioClient(IstioClient istioClient) {
        if (istioClient != null) {
            istioClient.close();
        }
    }

}

 

Logo

开源、云原生的融合云平台

更多推荐