All Products
Search
Document Center

Alibaba Cloud Service Mesh:Manage Istio resources with the Java SDK

Last Updated:Aug 27, 2026

By integrating a language-specific SDK client into your application, you can manage Istio resources directly in your code. This topic uses Java 11 and the fabric8io/istio-client 6.0.0-RC1 SDK to demonstrate how to create and manage Istio resources programmatically.

Prerequisites

Preparations

Install dependencies

Add the following dependency 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. In the left-side navigation pane of the instance details page, click Base Information, and then click Connection on the right side of the page.

  5. In the Connection panel, copy the content of the file to the $HOME/.kube/config path.

Note

By default, the connection uses the system kubeconfig file, which corresponds to new DefaultIstioClient() in the code examples. To use a specific kubeconfig file, initialize the client with Config.fromKubeconfig, as shown in Create a gateway rule.

Generate configuration files

Save the virtualService.yaml and gateway.yaml files that you created on the Manage Istio resources programmatically page to the static resource folder of your project.

Note

For Maven projects, the default folder is src/main/resources.

Create a virtual service

When you use the Java istio-client to create an Istio resource, you can choose between two approaches. Both approaches apply to any Istio resource type.

  • YAML file — Load the resource definition from a .yaml file. Use this approach to keep resource definitions separate from your code or to reuse existing manifests.

  • Method chaining — Build the resource entirely in code. Use this approach when you want to construct the resource programmatically.

    The following examples create a virtual service with each approach.

Method 1: Create a virtual service by using a YAML file

The following example loads the resource definition from virtualService.yaml and creates the 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 the resource is created.
        VirtualService virtualService = istioClient.v1beta1().virtualServices().load(
            VirtualServiceExample.class.getResourceAsStream("/virtualService.yaml")
            ).get();
        istioClient.v1beta1().virtualServices().inNamespace(NAMESPACE).resource(virtualService).create();
        printAllVirtualServices(istioClient);
        istioClient.close();
    }

    // Print all VirtualServices in the mesh.
    static void printAllVirtualServices(IstioClient istioClient) {
        istioClient.v1beta1().virtualServices().list().getItems().forEach(System.out::println);
    }
}

The printAllVirtualServices method prints all virtual services in the mesh. Confirm that the virtual service you created appears in the output.

Method 2: Create a virtual service by using method chaining

The istio-client supports method chaining, which lets you create a virtual service entirely in 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();
    }

    // Print all VirtualServices in the mesh.
    static void printAllVirtualServices(IstioClient istioClient) {
        istioClient.v1beta1().virtualServices().list().getItems().forEach(System.out::println);
    }
}

The printAllVirtualServices method prints all virtual services in the mesh. Confirm that the bookinfo virtual service appears in the output.

Create a gateway rule

This example connects with a specific kubeconfig file, as described in Configure a connection, and creates the gateway rule that is defined in gateway.yaml in the default namespace. It creates the gateway rule from a YAML file, whereas Method 2: Create a virtual service by using method chaining shows the equivalent method-chaining approach.

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 kubeconfig connection configuration file.
        String kubeconfigContents = null;
        FileReader reader = new FileReader("{path to kube config file}"); // Replace the content in {} with the actual path to the kubeconfig file.
        kubeconfigContents = IOHelpers.readFully(reader);
        Config config = Config.fromKubeconfig(null, kubeconfigContents, null);

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

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

The printAllGateways method prints all gateways in the mesh. Confirm that the gateway rule you created appears in the output.

Access the Bookinfo application

After you create the virtual service and the gateway rule, open http://{IP address of the ingress gateway service}/productpage in your browser to view the Bookinfo application.