All Products
Search
Document Center

Alibaba Cloud Service Mesh:Use the SDK for Java to manage Istio resources

Last Updated:Sep 12, 2023

After you integrate the SDK for a programming language, you can manage Istio resources in your code. This topic describes how to use Service Mesh (ASM) SDK for Java whose version is fabric8io/istio-client 6.0.0-RC1 to manage Istio resources in Java 11.

Prerequisites

Preparations

Install dependencies

Add the following dependencies to the Maven configuration file pom.xml:

<dependencies>
  <dependency>
    <groupId>io.fabric8</groupId>
    <artifactId>istio-client</artifactId>
    <version>6.0.0-RC1</version>
  </dependency>
</dependencies>

Configure a connection

  1. Log on to the ASM console.

  2. In the left-side navigation pane, choose Service Mesh > Mesh Management.

  3. On the Mesh Management page, find the ASM instance that you want to configure. Click the name of the ASM instance or click Manage in the Actions column.

  4. On the details page of the ASM instance, click Base Information in the left-side navigation pane. On the Basic Information page, click Connection.

  5. In the Connection panel, copy the content of the system kubeconfig file to the $HOME/.kube/config directory of your machine.

    Note

    By default, the system kubeconfig file is used to configure a connection. For more information about how to use a specific kubeconfig file, see the Create an Istio gateway section of this topic.

Generate configuration files

Save the virtualService.yaml and gateway.yaml files created in Use ASM SDK to manage Istio resources in code to the static resource folder of your project.

Note

By default, the static resource folder resides in the src/main/resources directory of a Maven project.

Create a virtual service

If you use istio-client to create Istio resources in Java, you can use YAML files or method chaining. To create a virtual service, you can use one of the following methods:

Method 1: Use a YAML file to create a virtual service

import java.io.FileReader;
import java.io.IOException;

import io.fabric8.istio.api.networking.v1beta1.VirtualService;
import io.fabric8.istio.client.DefaultIstioClient;
import io.fabric8.istio.client.IstioClient;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.utils.IOHelpers;

public class VirtualServiceExample {
    public static void main( String[] args ) {
        IstioClient istioClient = new DefaultIstioClient();
        final String NAMESPACE = "default"; // The namespace in which you want to create the resource. 
        VirtualService virtualService = istioClient.v1beta1().virtualServices().load(
            VirtualServiceExample.class.getResourceAsStream("/virtualService.yaml")
            ).get();
        istioClient.v1beta1().virtualServices().inNamespace(NAMESPACE).resource(virtualService).create();
        printAllVirtualServices(istioClient);
        istioClient.close();
    }


    // Display all virtual services in the ASM instance. 
    static void printAllVirtualServices(IstioClient istioClient) {
        istioClient.v1beta1().virtualServices().list().getItems().forEach(System.out::println);
    }
}

Method 2: Use method chaining to create a virtual service

You can use the method chaining capability of istio-client to create a virtual service by running code.

import io.fabric8.istio.api.networking.v1beta1.VirtualServiceBuilder;
import io.fabric8.istio.client.DefaultIstioClient;
import io.fabric8.istio.client.IstioClient;

public class VirtualServiceChainingExample {
    public static void main(String[] args) {
        IstioClient istioClient = new DefaultIstioClient();
        final String NAMESPACE = "default";
        istioClient.v1beta1().virtualServices().inNamespace(NAMESPACE).resource(
            new VirtualServiceBuilder()
            .withNewMetadata()
            .withName("bookinfo")
            .endMetadata()
            .withNewSpec()
                .addToHosts("*")
                .addToGateways("bookinfo-gateway")
                .addNewHttp()
                    .addNewMatch().withNewUri().withNewStringMatchExactType("/productpage").endUri().endMatch()
                    .addNewMatch().withNewUri().withNewStringMatchPrefixType("/static").endUri().endMatch()
                    .addNewMatch().withNewUri().withNewStringMatchExactType("/login").endUri().endMatch()
                    .addNewMatch().withNewUri().withNewStringMatchExactType("/logout").endUri().endMatch()
                    .addNewMatch().withNewUri().withNewStringMatchPrefixType("/api/v1/products").endUri().endMatch()
                    .addNewRoute()
                        .withNewDestination()
                            .withHost("productpage")
                            .withNewPort()
                                .withNumber(9080)
                            .endPort()
                        .endDestination()
                    .endRoute()
                .endHttp()
            .endSpec()
            .build()
            ).create();
        printAllVirtualServices(istioClient);
        istioClient.close();
    }

    // Display all virtual services in the ASM instance. 
    static void printAllVirtualServices(IstioClient istioClient) {
        istioClient.v1beta1().virtualServices().list().getItems().forEach(System.out::println);
    }
}

Create an Istio gateway

After you use a specific kubeconfig file to configure a connection, you can create an Istio gateway in the default namespace by using the gateway.yaml file. In this example, a YAML file is used to create an Istio gateway. For more information about how to use method chaining, see the Method 2: Use method chaining to create a virtual service section of this topic.

import java.io.FileReader;
import java.io.IOException;

import io.fabric8.istio.api.networking.v1beta1.Gateway;
import io.fabric8.istio.client.DefaultIstioClient;
import io.fabric8.istio.client.IstioClient;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.utils.IOHelpers;

public class GateWayExample {
    public static void main(String[] args) throws IOException {
        // Read the specified kubeconfig file to configure a connection. 
        String kubeconfigContents = null;
        FileReader reader = new FileReader("{path to kube config file}"); // Replace the content of {} with the path of the kubeconfig file that you want to use. 
        kubeconfigContents = IOHelpers.readFully(reader);
        Config config = Config.fromKubeconfig(null, kubeconfigContents, null);

        IstioClient istioClient = new DefaultIstioClient(config);
        final String NAMESPACE = "default"; // The namespace in which you want to create the resource. 
        Gateway gateway = istioClient.v1beta1().gateways().load(
            GateWayExample.class.getResourceAsStream("/gateway.yaml")
        ).get();
        istioClient.v1beta1().gateways().inNamespace(NAMESPACE).resource(gateway).create();
        printAllGateways(istioClient);
        istioClient.close();
    }

    // Display all gateways in the ASM instance. 
    static void printAllGateways(IstioClient istioClient) {
        istioClient.v1beta1().gateways().list().getItems().forEach(System.out::println);
    }
}

What to do next

After the virtual service and Istio gateway are created, you can visit http://{IP address of the ingress gateway service}/productpage in a browser to access the Bookinfo application.