当前位置: > > > > 如何使用 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 的新手。
请帮我设置以下内容,
- 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的相关知识,请关注米云公众号!
