Istio Java SDK API - 路由-流量拆分

 

案例

80% v1 版本 20% v2 Istio源码配置

 

环境

参考 Istio Java SDK API - 连接访问 博文

 

注意点

add:说明该资源是  [ ]  资源

with:  说明该资源是  { }  资源

 

源码

package com.micro.istiodemo.test;

import me.snowdrop.istio.api.networking.v1alpha3.DestinationRule;
import me.snowdrop.istio.api.networking.v1alpha3.DestinationRuleBuilder;
import me.snowdrop.istio.api.networking.v1alpha3.VirtualService;
import me.snowdrop.istio.api.networking.v1alpha3.VirtualServiceBuilder;
import me.snowdrop.istio.client.IstioClient;

import java.util.HashMap;

/*
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: flow
  namespace: istio-test
spec:
  host: flow
  subsets:
  - name: flow-v1
    labels:
      version: v1
  - name: flow-v2
    labels:
      version: v2
 */

// 80% v1 20% v2 in istio-test namespace
public class ServiceRouteIstio {

    public static void main(String[] args) {
        ConnectIstio connectIstio = new ConnectIstio();
        IstioClient istioClient = connectIstio.getIstioClient();
        if (istioClient ==null){
            return;
        }
        ServiceRouteIstio routeIstio = new ServiceRouteIstio();
        DestinationRule flowDestinationRule = routeIstio.getFlowDestinationRule();
        if (flowDestinationRule!=null){
            istioClient.destinationRule().inNamespace("istio-test").create(flowDestinationRule);
        }
        VirtualService flowVirtualService = routeIstio.getFlowVirtualService();
        if (flowVirtualService!=null){
            istioClient.virtualService().inNamespace("istio-test").create(flowVirtualService);
        }

        //删除
//        boolean isDelete = istioClient.destinationRule().inNamespace("istio-test").delete(flowDestinationRule);
//        boolean isDelete = istioClient.virtualService().inNamespace("istio-test").delete(flowVirtualService);

        connectIstio.closeIstioClient(istioClient);
    }

    public DestinationRule getFlowDestinationRule() {
        DestinationRule destinationRule = new DestinationRuleBuilder()
                .withApiVersion("networking.istio.io/v1alpha3")
                .withNewMetadata()
                .withName("flow")
                .withNamespace("istio-test") //no! go to create!
                .endMetadata()
                .withNewSpec()
                .withHost("flow")
                .addNewSubset().withName("flow-v1").withLabels(new HashMap() {{
                    put("version", "v1");
                }}).endSubset()
                .addNewSubset().withName("flow-v2").withLabels(new HashMap() {{
                    put("version", "v2");
                }}).endSubset()
                .endSpec()
                .build();
        return destinationRule;
    }

    /*
    apiVersion: networking.istio.io/v1alpha3
    kind: VirtualService
    metadata:
      name: flow
      namespace: istio-test
    spec:
      hosts:
        - flow
      http:
      - route:
        - destination:
            host: flow
            subset: flow-v1
          weight: 80
        - destination:
            host: flow
            subset: flow-v2
          weight: 20

    */
    public VirtualService getFlowVirtualService() {
        VirtualService virtualService = new VirtualServiceBuilder()
                .withApiVersion("networking.istio.io/v1alpha3")
                .withNewMetadata().withName("flow").withNamespace("istio-test").endMetadata()
                .withNewSpec()
                .addToHosts("flow")
                .addNewHttp().addNewRoute()
                .withNewDestination().withHost("flow").withSubset("flow-v1").endDestination()
                .withWeight(80)
                .endRoute()
                .addNewRoute()
                .withNewDestination().withHost("flow").withSubset("flow-v2").endDestination()
                .withWeight(20)
                .endRoute()
                .endHttp()
                .endSpec()
                .build();
        return virtualService;

    }

}

 

你可能感兴趣的:(java,micro,istio)