视网站亏损了为什么还做,googlechrome,seo点击软件,江苏省建设工程信息网官网【1】新建一个目录whgserviceproto#xff0c;目录下新建一个proto包#xff1a;whgserviceproto.proto
#xff08;注意目录和包名称保持一致#xff09;
//协议为proto3
syntax proto3;
// 指定生成的Go代码在你项目中的导入路径
option go_package…【1】新建一个目录whgserviceproto目录下新建一个proto包whgserviceproto.proto
注意目录和包名称保持一致
//协议为proto3
syntax proto3;
// 指定生成的Go代码在你项目中的导入路径
option go_package./;whgserviceproto; //注意whgserviceproto会变成转换后的包名称
package whgserviceproto;
// 定义服务接口
// 可定义多个服务每个服务可定义多个接口
service WHG{rpc SayStatus (WHGRequest) returns (WHGResponse){}
}
// 请求参数结构
message WHGRequest{ string name 1;
}
// 响应参数结构
message WHGResponse{string status 1;
}
【2】生成xx.pb.go和xx_grpc.pb.go文件
1进入proto目录
2protoc --go_out. whgserviceproto.proto
3protoc --go-grpc_out. whgserviceproto.proto 【3】新建一个WHGSERVER目录增加一个main.go文件
package mainimport (context//flagfmtlognetgoogle.golang.org/grpcgrpc-go-master/examples/whgservice/whgserviceproto
)type WHGServerObject struct {whgserviceproto.UnimplementedWHGServer
}func (s *WHGServerObject) SayStatus(ctx context.Context, req *whgserviceproto.WHGRequest) (resp *whgserviceproto.WHGResponse, err error) {log.Printf(recv:%v, req.GetName())return whgserviceproto.WHGResponse{Status: req.GetName() run}, nil
}func main() {//创建listen监听端口fmt.Println(server start)//listener, err : net.Listen(tcp, 127.0.0.1:8888)listener, err : net.Listen(tcp, 0.0.0.0:8888)if err ! nil {panic(err)}//创建 gRPC 服务对象s : grpc.NewServer()//调用函数的结构体注册到grpc服务中whgserviceproto.RegisterWHGServer(s, WHGServerObject{})log.Printf(server listening at %v, listener.Addr())//运行gRPC服务if err s.Serve(listener); err ! nil {panic(err)}}