go - gRPC 可以集成到 Flutter-web 中吗?
问题描述
我试图融入gPRC
,flutter-web
但总是失败。不知道是我的代码有问题还是GRPC无法集成到flutter-web中。
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 代理。
这里使用 envoy 的步骤:
将您的 Web 客户端频道设置为颤动
GrpcWebClientChannel.xhr(Uri.parse('http://localhost:8080'));
将服务器设置为侦听以下示例:
path := "127.0.0.1:3001"
为特使创建配置,如下例所示。另存为
envoy.yaml
:
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
使用配置运行 envoy(Linux 框中的示例):
$ envoy -c envoy.yaml
现在,尝试运行 Flutter Web 客户端和服务器。
请参阅https://github.com/sigurdm/grpc_web_flutter_example或https://github.com/grpc/grpc-web/tree/master/net/grpc/gateway/examples/helloworld
推荐阅读
- reactjs - 使用 ReactJS 在数组中推送一个项目
- c# - Roslyn 与 fastcoloredtextbox 集成以获取 c# 的语法错误
- java - 检查 Event(eventName(String), eventStartTime(LocalDate), eventEndTime(LocalDate)) 槽是否为空
- php - 更新 chrome 后,我在 selenium 中收到此错误
- azure - 将映像从 docker hub 部署到 AZure
- javafx - 如何将 ControlsFX 与 javafx 和 FXML 一起使用?
- html - 显示一定长度的字符串而不截断
- python - 在嵌套列表中的特定位置附加元素
- django - Django 重定向,参数不在 URL 中
- android - Agora 实时消息