0

我有两个不同的微服务在相同的命名空间中运行,它们都具有相同的上下文路径(例如 - my/context/path),进一步的控制器在它们中是不同的,例如服务一支持 - my/context/path/service1并且 service2 现在支持 my/context/path/service2 当我这样定义时,它总是重定向到 service1,有没有可能的方法来实现这一点?下面是我的VS:


        apiVersion: networking.istio.io/v1alpha3
        kind: VirtualService
        metadata:
        name: test-service
        namespace: ns-ns
        spec:
         gateways:
         - gateway.ns-ns
        hosts:
         - '*'
       http:
       - match:
        - uri:
          prefix: /my/context/path
        route:
        - destination:
            host: service1.ns-ns.svc.cluster.local
            port:
              number: 9000
      - route:
        - destination:
            host: service2.ns-ns.svc.cluster.local
            port:
              number: 9000

我也在VS下面尝试过,但这似乎也重定向到第一个服务。

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: test-service
  namespace: ns-ns
spec:
  gateways:
  - gateway.ns-ns
  hosts:
  - '*'
  http:
    - match:
      - uri:
          prefix: /my/context/path
      route:
        - destination:
            host: service1.ns-ns.svc.cluster.local
            port:
              number: 9000
    - match:        
      - uri:
          prefix: /my/context/path/service2
      route:
        - destination:
            host: service2.ns-ns.svc.cluster.local
            port:
              number: 9000

我不确定这是否可以实现,或者我是否需要将两个服务的上下文部分分开?

4

1 回答 1

1

路由按顺序匹配。因此,您需要从最具体到最通用的开始。例如

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: test-service
  namespace: ns-ns
spec:
  gateways:
  - gateway.ns-ns
  hosts:
  - '*'
  http:
    - match:        
      - uri:
          prefix: /my/context/path/service2
      route:
        - destination:
            host: service2.ns-ns.svc.cluster.local
            port:
              number: 9000
    - match:
      - uri:
          prefix: /my/context/path
      route:
        - destination:
            host: service1.ns-ns.svc.cluster.local
            port:
              number: 9000
于 2021-11-18T08:52:23.703 回答