GRPC能否集成到颤动网中? [英] Can gRPC be integrated into flutter-web?

查看:15
本文介绍了GRPC能否集成到颤动网中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将gPRC集成到flutter-web,但总是失败。我不知道是我的代码有问题,还是GRPC无法集成到颤动网中。

dependencies:
  flutter:
    sdk: flutter
  grpc: ^2.1.3
  protobuf: ^1.0.1

以下是我的服务器端代码:

我有两个问题。 第一个问题是GRPC是否可以集成到fluter-web中。 第二个问题是我需要什么库,有没有什么例子? 谢谢。

推荐答案

简短回答:是,您可以


目前,GRPC-Web需要在GRPC服务器前面有一个Web代理,以便将请求和响应转换为浏览器可以使用的内容。有关详细信息,请参阅https://grpc.io/blog/state-of-grpc-web/

您可以使用Envoy作为Web代理。

使用特使的步骤如下:

  1. 将您的Web客户端频道设置为颤动

    GrpcWebClientChannel.xhr(Uri.parse('http://localhost:8080'));
    
  2. 将服务器设置为侦听以下示例:

    path := "127.0.0.1:3001"
    
  3. https://www.envoyproxy.io/安装特使

  4. 为特使创建配置,如下例所示。另存为envoy.yaml

数据-lang="js"数据-隐藏="假"数据-控制台="真"数据-巴贝尔="假">
admin:
  access_log_path: /tmp/admin_access.log
  address:
    socket_address: { address: 0.0.0.0, port_value: 9901 }

static_resources:
  listeners:
  - name: listener_0
    address:
      socket_address: { address: 0.0.0.0, port_value: 8080 }
    filter_chains:
    - filters:
      - name: envoy.filters.network.http_connection_manager
        typed_config:
          "@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
          codec_type: auto
          stat_prefix: ingress_http
          route_config:
            name: local_route
            virtual_hosts:
            - name: local_service
              domains: ["*"]
              routes:
              - match: { prefix: "/" }
                route:
                  cluster: greeter_service
                  max_stream_duration:
                    grpc_timeout_header_max: 0s
              cors:
                allow_origin_string_match:
                - prefix: "*"
                allow_methods: GET, PUT, DELETE, POST, OPTIONS
                allow_headers: keep-alive,user-agent,cache-control,content-type,content-transfer-encoding,x-accept-content-transfer-encoding,x-accept-response-streaming,x-user-agent,x-grpc-web,grpc-timeout
                max_age: "1728000"
                expose_headers: id,token,grpc-status,grpc-message
          http_filters:
          - name: envoy.filters.http.grpc_web
          - name: envoy.filters.http.cors
          - name: envoy.filters.http.router
  clusters:
  - name: greeter_service
    connect_timeout: 0.25s
    type: logical_dns
    http2_protocol_options: {}
    lb_policy: round_robin
    # win/mac hosts: Use address: host.docker.internal instead of address: localhost in the line below
    load_assignment:
      cluster_name: cluster_0
      endpoints:
        - lb_endpoints:
            - endpoint:
                address:
                  socket_address:
                    address: 0.0.0.0
                    port_value: 3001

  1. 使用配置运行特使(Linux机器中的示例):

    $ envoy -c envoy.yaml
    

现在,尝试运行Ffltter Web客户端和服务器。

参见https://github.com/sigurdm/grpc_web_flutter_examplehttps://github.com/grpc/grpc-web/tree/master/net/grpc/gateway/examples/helloworld

这篇关于GRPC能否集成到颤动网中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆