如何使用 Envoy 和 Grpc_web 连接 go grpc 服务器与 dart grpc 客户端
来源:stackoverflow
时间:2024-04-28 09:48:34 148浏览 收藏
珍惜时间,勤奋学习!今天给大家带来《如何使用 Envoy 和 Grpc_web 连接 go grpc 服务器与 dart grpc 客户端》,正文内容主要涉及到等等,如果你正在学习Golang,或者是对Golang有疑问,欢迎大家关注我!后面我会持续更新相关内容的,希望都能帮到正在学习的大家!
问题内容
我是 grpc_web 和 envoy 的新手。
请帮我设置以下内容,
- grpc_go 服务器作为 docker 容器在 ec2 实例上运行
- dart web 客户端正在本地 pc 上运行
- 需要从 dart web 应用向 grpc_go 服务器发出 grpc 调用请求
- 使用 envoy 代理来转发请求。 envoy 代理作为容器在同一个 ec2 实例中运行
我收到以下错误“响应:null,预告片:{access-control-allow-credentials:true,access-control-allow-origin:http://127.0.0.1:9000,变化:origin} )”。
grpc_go:
package main import ( "context" "flag" "fmt" "log" "net" "google.golang.org/grpc" pb "google.golang.org/grpc/examples/helloworld/helloworld" ) var ( port = flag.int("port", 50051, "the server port") ) // server is used to implement helloworld.greeterserver. type server struct { pb.unimplementedgreeterserver } // sayhello implements helloworld.greeterserver func (s *server) sayhello(ctx context.context, in *pb.hellorequest) (*pb.helloreply, error) { log.printf("received: %v", in.getname()) return &pb.helloreply{message: "hello " + in.getname()}, nil } func (s *server) sayhelloagain(ctx context.context, in *pb.hellorequest) (*pb.helloreply, error) { return &pb.helloreply{message: "hello again " + in.getname()}, nil } func main() { flag.parse() lis, err := net.listen("tcp", fmt.sprintf(":%d", *port)) if err != nil { log.fatalf("failed to listen: %v", err) } s := grpc.newserver() pb.registergreeterserver(s, &server{}) log.printf("server listening at %v", lis.addr()) if err := s.serve(lis); err != nil { log.fatalf("failed to serve: %v", err) } }
grpc_dart_client:
import 'package:grpc/grpc_web.dart'; import 'package:grpc_web/app.dart'; import 'package:grpc_web/src/generated/echo.pbgrpc.dart'; void main() { final channel = grpcwebclientchannel.xhr(uri.parse('http://ec2-ip:8080')); final service = echoserviceclient(channel); final app = echoapp(service); final button = queryselector('#send') as buttonelement; button.onclick.listen((e) async { final msg = queryselector('#msg') as textinputelement; final value = msg.value!.trim(); msg.value = ''; if (value.isempty) return; if (value.indexof(' ') > 0) { final countstr = value.substring(0, value.indexof(' ')); final count = int.tryparse(countstr); if (count != null) { app.repeatecho(value.substring(value.indexof(' ') + 1), count); } else { app.echo(value); } } else { app.echo(value); } }); }
envoy.yaml:
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: echo_service timeout: 0s 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,custom-header-1,x-accept-content-transfer-encoding,x-accept-response-streaming,x-user-agent,x-grpc-web,grpc-timeout max_age: "1728000" expose_headers: custom-header-1,grpc-status,grpc-message http_filters: - name: envoy.filters.http.grpc_web - name: envoy.filters.http.cors - name: envoy.filters.http.router clusters: - name: echo_service connect_timeout: 0.25s type: logical_dns http2_protocol_options: {} lb_policy: round_robin load_assignment: cluster_name: cluster_0 endpoints: - lb_endpoints: - endpoint: address: socket_address: address: app port_value: 50051
grpc_go_docker_file:
# install git. # git is required for fetching the dependencies. run apk update && apk add --no-cache git workdir /app copy go.mod go.sum ./ run go mod download copy . . run cgo_enabled=0 goos=linux go build -a -installsuffix cgo -o main . # start a new stage from scratch from alpine:latest run apk --no-cache add ca-certificates workdir /root/ # copy the pre-built binary file from the previous stage. observe we also copied the .env file copy --from=builder /app/main . # expose port 50051 to the outside world expose 50051 cmd ["./main"]
envoy_docker:
COPY envoy.yaml /etc/envoy/envoy.yaml CMD /usr/local/bin/envoy -c /etc/envoy/envoy.yaml -l trace --log-path /tmp/envoy_info.log
我已经卡了两天多了,请帮帮我。提前致谢
正确答案
谢谢大家的回复。
我使用 ec2 实例的 ip 修复了此问题。
clusters: - name: echo_service connect_timeout: 0.25s type: logical_dns http2_protocol_options: {} lb_policy: round_robin load_assignment: cluster_name: cluster_0 endpoints: - lb_endpoints: - endpoint: address: socket_address: address: app port_value: 50051
我使用了 ec2 实例的 ip 和容器端口,而不是 envoy.yaml 中的容器“地址:app”(app 是容器名称),现在 envoy 正在将请求转发到服务器。
本篇关于《如何使用 Envoy 和 Grpc_web 连接 go grpc 服务器与 dart grpc 客户端》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!
声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
最新阅读
更多>
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
课程推荐
更多>
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 507次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习