当前位置:首页 > 文章列表 > Golang > Go问答 > 如何使用 Envoy 和 Grpc_web 连接 go grpc 服务器与 dart grpc 客户端

如何使用 Envoy 和 Grpc_web 连接 go grpc 服务器与 dart grpc 客户端

来源:stackoverflow 2024-04-28 09:48:34 0浏览 收藏

珍惜时间,勤奋学习!今天给大家带来《如何使用 Envoy 和 Grpc_web 连接 go grpc 服务器与 dart grpc 客户端》,正文内容主要涉及到等等,如果你正在学习Golang,或者是对Golang有疑问,欢迎大家关注我!后面我会持续更新相关内容的,希望都能帮到正在学习的大家!

问题内容

我是 grpc_web 和 envoy 的新手。

请帮我设置以下内容,

  1. grpc_go 服务器作为 docker 容器在 ec2 实例上运行
  2. dart web 客户端正在本地 pc 上运行
  3. 需要从 dart web 应用向 grpc_go 服务器发出 grpc 调用请求
  4. 使用 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删除
exec中closeAfterStart的目的是什么exec中closeAfterStart的目的是什么
上一篇
exec中closeAfterStart的目的是什么
深入理解PHP数组反转的算法
下一篇
深入理解PHP数组反转的算法
查看更多
最新文章
查看更多
课程推荐
  • 前端进阶之JavaScript设计模式
    前端进阶之JavaScript设计模式
    设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
    542次学习
  • GO语言核心编程课程
    GO语言核心编程课程
    本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
    508次学习
  • 简单聊聊mysql8与网络通信
    简单聊聊mysql8与网络通信
    如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
    497次学习
  • JavaScript正则表达式基础与实战
    JavaScript正则表达式基础与实战
    在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
    487次学习
  • 从零制作响应式网站—Grid布局
    从零制作响应式网站—Grid布局
    本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
    484次学习
查看更多
AI推荐
  • 笔灵AI生成答辩PPT:高效制作学术与职场PPT的利器
    笔灵AI生成答辩PPT
    探索笔灵AI生成答辩PPT的强大功能,快速制作高质量答辩PPT。精准内容提取、多样模板匹配、数据可视化、配套自述稿生成,让您的学术和职场展示更加专业与高效。
    23次使用
  • 知网AIGC检测服务系统:精准识别学术文本中的AI生成内容
    知网AIGC检测服务系统
    知网AIGC检测服务系统,专注于检测学术文本中的疑似AI生成内容。依托知网海量高质量文献资源,结合先进的“知识增强AIGC检测技术”,系统能够从语言模式和语义逻辑两方面精准识别AI生成内容,适用于学术研究、教育和企业领域,确保文本的真实性和原创性。
    35次使用
  • AIGC检测服务:AIbiye助力确保论文原创性
    AIGC检测-Aibiye
    AIbiye官网推出的AIGC检测服务,专注于检测ChatGPT、Gemini、Claude等AIGC工具生成的文本,帮助用户确保论文的原创性和学术规范。支持txt和doc(x)格式,检测范围为论文正文,提供高准确性和便捷的用户体验。
    37次使用
  • 易笔AI论文平台:快速生成高质量学术论文的利器
    易笔AI论文
    易笔AI论文平台提供自动写作、格式校对、查重检测等功能,支持多种学术领域的论文生成。价格优惠,界面友好,操作简便,适用于学术研究者、学生及论文辅导机构。
    46次使用
  • 笔启AI论文写作平台:多类型论文生成与多语言支持
    笔启AI论文写作平台
    笔启AI论文写作平台提供多类型论文生成服务,支持多语言写作,满足学术研究者、学生和职场人士的需求。平台采用AI 4.0版本,确保论文质量和原创性,并提供查重保障和隐私保护。
    40次使用
微信登录更方便
  • 密码登录
  • 注册账号
登录即同意 用户协议隐私政策
返回登录
  • 重置密码