istio内部GRPC服务通信

时间:2019-09-28 22:47:19

标签: grpc istio .net-core-3.0

我在使用两个集群内GRPC服务(用netcore3.0编写)时遇到麻烦,我得到了Grpc.Core.RpcException: Status(StatusCode=Unavailable, Detail="Connection reset by peer")(使用uri = <service>.default.svc.cluster.local)或Grpc.Core.RpcException: Status(StatusCode=Unimplemented, Detail="")uri = user.default.svc.cluster.local:80。奇怪的是,如果所有服务都从不同的群集进行通信,则所有服务都能正常工作。我正在使用正确的网址。此处随附的服务之一的配置。

apiVersion: v1
kind: Service
metadata:
  name: user
  labels:
    app: user
    service: user
spec:
  ports:
  - port: 80
    name: grpc-port
    protocol: TCP
  selector:
    app: user
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: user-v1
  labels:
    app: user
    version: v1
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: user
        version: v1
    spec:
      containers:
      - name: user
        image: ***
        imagePullPolicy: IfNotPresent
        env:
        - name: GOOGLE_APPLICATION_CREDENTIALS
          value: "***"
        ports:
        - containerPort: 80
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: user
spec:
  hosts:
  - user.default.svc.cluster.local
  http:
  - route:
    - destination:
        port:
          number: 80
        host: user.default.svc.cluster.local
        subset: v1
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: user
spec:
  host: user.default.svc.cluster.local
  subsets:
  - name: v1
    labels:
      version: v1
---

已修复:我已按照method

的说明使用gRPC的.NETCORE3客户端工厂集成使其成功运行

1 个答案:

答案 0 :(得分:1)

与通常那样手动创建频道和客户端不同。即

var endpoint = "test"; //or http://test
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
Channel channel = new Channel(endpoint, ChannelCredentials.Insecure);
client = new TestService.TestServiceClient(channel);

我在ConfigureServices (startup.cs)中使用了GRPC客户端工厂集成(在添加Grpc.Net.ClientFactory软件包版本0.1.22-pre1之后:

services.AddGrpcClient<TestService.TestServiceClient>(o =>
        {
            AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
            o.BaseAddress = new Uri("http://test");
        });

此后,您可以使用依赖注入来访问客户端。 我不确定为什么第二种方法行得通,但是第一种方法行不通。