jergoo / go-grpc-tutorial Goto Github PK
View Code? Open in Web Editor NEWA simple tutorial about Golang gRPC.
Home Page: https://jergoo.github.io/go-grpc-tutorial/
License: MIT License
A simple tutorial about Golang gRPC.
Home Page: https://jergoo.github.io/go-grpc-tutorial/
License: MIT License
jergoo你好,上次的外网访问问题我已经解决。
但是,有新的问题 :(
目前我使用仓库里的hello_trace项目代码运行,能够访问到页面,
但是仅显示了h1标题(/debug/events、/debug/requests),具体的trace内容都不显示,直接空白的table标签。
请问是什么原因呢,谢谢!
网页样子如下(就是标题以下都是空白):
运行hello_http_2不成功,另外hello_http怎么用?
一直报错
2017/08/09 14:27:13 transport: http2Client.notifyError got notified that the client transport was broken unexpected EOF.
2017/08/09 14:27:13 transport: http2Client.notifyError got notified that the client transport was broken unexpected EOF.
2017/08/09 14:27:13 transport: http2Client.notifyError got notified that the client transport was broken unexpected EOF.
2017/08/09 14:27:14 transport: http2Client.notifyError got notified that the client transport was broken unexpected EOF.
2017/08/09 14:27:14 transport: http2Client.notifyError got notified that the client transport was broken unexpected EOF.
2017/08/09 14:27:14 transport: http2Client.notifyError got notified that the client transport was broken unexpected EOF.
2017/08/09 14:27:14 transport: http2Client.notifyError got notified that the client transport was broken unexpected EOF.
2017/08/09 14:27:14 transport: http2Client.notifyError got notified that the client transport was broken unexpected EOF.
2017/08/09 14:27:14 transport: http2Client.notifyError got notified that the client transport was broken unexpected EOF.
2017/08/09 14:27:14 transport: http2Client.notifyError got notified that the client transport was broken unexpected EOF.
2017/08/09 14:27:14 transport: http2Client.notifyError got notified that the client transport was broken unexpected EOF.
2017/08/09 14:27:14 transport: http2Client.notifyError got notified that the client transport was broken unexpected EOF.
2017/08/09 14:27:14 transport: http2Client.notifyError got notified that the client transport was broken unexpected EOF.
2017/08/09 14:27:14 transport: http2Client.notifyError got notified that the client transport was broken unexpected EOF.
2017/08/09 14:27:14 transport: http2Client.notifyError got notified that the client transport was broken unexpected EOF.
1. hello_http_2: I use http instead of https ,
codes are like this, and in shell:
curl -X POST -k http://127.0.0.1:50051/example/echo -d '{"name": "Jesse Yan"}'
but have problems :
{"error":"transport is closing","code":14}
// httpdemo/server/main.go
package main
import (
"crypto/tls"
"io/ioutil"
"net"
"net/http"
"strings"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
pb "proto/bi/service/httpsdemo"
"golang.org/x/net/context"
"golang.org/x/net/http2"
"google.golang.org/grpc"
"google.golang.org/grpc/grpclog"
)
// 定义helloHTTPService并实现约定的接口
type helloHTTPService struct{}
// HelloHTTPService Hello HTTP服务
var HelloHTTPService = helloHTTPService{}
// SayHello 实现Hello服务接口
func (h helloHTTPService) SayHello(ctx context.Context, in *pb.HelloHTTPRequest) (*pb.HelloHTTPResponse, error) {
resp := new(pb.HelloHTTPResponse)
resp.Message = "Hello " + in.Name + "."
return resp, nil
}
func main() {
endpoint := "127.0.0.1:50051"
conn, err := net.Listen("tcp", endpoint)
if err != nil {
grpclog.Fatalf("TCP Listen err:%v\n", err)
}
// grpc server
grpcServer := grpc.NewServer()
pb.RegisterHelloHTTPServer(grpcServer, HelloHTTPService)
// gw server
ctx := context.Background()
dopts := []grpc.DialOption{grpc.WithInsecure()}
gwmux := runtime.NewServeMux()
if err = pb.RegisterHelloHTTPHandlerFromEndpoint(ctx, gwmux, endpoint, dopts); err != nil {
grpclog.Fatalf("Failed to register gw server: %v\n", err)
}
// http服务
mux := http.NewServeMux()
mux.Handle("/", gwmux)
httpServer := &http.Server{
Addr: endpoint,
Handler: grpcHandlerFunc(grpcServer, mux),
//TLSConfig: getTLSConfig(),
}
grpclog.Infof("gRPC and https listen on: %s\n", endpoint)
//if err = srv.Serve(tls.NewListener(conn, srv.TLSConfig)); err != nil {
if err = httpServer.Serve(conn); err != nil {
grpclog.Fatal("ListenAndServe: ", err)
}
return
}
// grpcHandlerFunc returns an http.Handler that delegates to grpcServer on incoming gRPC
// connections or otherHandler otherwise. Copied from cockroachdb.
func grpcHandlerFunc(grpcServer *grpc.Server, otherHandler http.Handler) http.Handler {
if otherHandler == nil {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
grpcServer.ServeHTTP(w, r)
})
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.ProtoMajor == 2 && strings.Contains(r.Header.Get("Content-Type"), "application/grpc") {
grpcServer.ServeHTTP(w, r)
} else {
otherHandler.ServeHTTP(w, r)
}
})
}
go run main.go
FATAL: 2018/03/20 16:26:09 rpc error: code = Unavailable desc = transport is closing
when I request RPC in rpc_client like this:
// httpdemo/client/main.go
package main
import (
pb "github.com/jergoo/go-grpc-example/proto/hello_http" // 引入proto包
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/grpclog"
)
const (
// Address gRPC服务地址
Address = "127.0.0.1:50052"
)
func main() {
// TLS连接
//creds, err := credentials.NewClientTLSFromFile("../keys/server.pem", "server name")
//if err != nil {
// grpclog.Fatalf("Failed to create TLS credentials %v", err)
//}
//conn, err := grpc.Dial(Address, grpc.WithTransportCredentials(creds))
conn, err := grpc.Dial(Address, grpc.WithInsecure())
if err != nil {
grpclog.Fatalln(err)
}
defer conn.Close()
// 初始化客户端
c := pb.NewHelloHTTPClient(conn)
// 调用方法
reqBody := new(pb.HelloHTTPRequest)
reqBody.Name = "Jesse Yan"
r, err := c.SayHello(context.Background(), reqBody)
if err != nil {
grpclog.Fatalln(err)
}
grpclog.Println(r.Message)
}
// OR: curl -X POST -k https://localhost:50052/example/echo -d '{"name": "gRPC-HTTP is working!"}'
FATAL: 2018/02/28 10:06:14 Failed to create server TLS credentials open ../../keys/server.pem: no such file or directory
请教,那个文件明明在的,运行就是找不到,是为啥??感谢
2020/11/10 11:56:03 http: TLS handshake error from 127.0.0.1:54433: remote error: tls: bad certificate
2020/11/10 11:56:04 http: TLS handshake error from 127.0.0.1:54434: remote error: tls: bad certificate
2020/11/10 11:56:06 http: TLS handshake error from 127.0.0.1:54435: remote error: tls: bad certificate
1.server 中 SayHello 绑定的struct 为啥是 T,而不是 *T ?
2.grpclog.Pringln() 不会在控制台输出内容,你的怎么输出的 ?我是自己加了fmt 的方法才打了出来,我以为是我自己哪儿复制错了呢。
您好,
我在SF上按照 https://segmentfault.com/a/1190000008087436 做或使用 hello_trace 中的代码,
虽然 server、client都能够运行,但只能通过虚拟机本地curl访问127.0.0.1:50051/debug/events
,
目前我是物理机是windows,虚拟是linux的方式,想要通过物理机访问x.x.x.x:50051/debug/events
,
并且也修改了http.ListenAndServe
为go http.ListenAndServe("0.0.0.0:50051", nil)
。
但是依旧无法访问,
请问应该如何编写或修改什么配置,才能达到物理机能访问看到虚拟机的debug页面呢?
谢谢。
// 注册HelloService
pb.RegisterHelloServer(s, HelloService)
Cannot use HelloService (type helloService) as type HelloServer less...
##############################################################
D:\GoWorkSpace\src\go-grpc-example\hello>go run server\main.go
....\google.golang.org\grpc\transport\http_util.go:465: f.fr.SetReuseFrames und
efined (type *http2.Framer has no field or method SetReuseFrames)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.