当前位置: 首页 > news >正文

合肥网站建设认准 晨飞网络山东做网站建设公司

合肥网站建设认准 晨飞网络,山东做网站建设公司,网店服务平台,旅游网站建设参考文献GRPC介绍 目录 单体架构微服务架构问题原始的grpc 服务端客户端原生rpc的问题 grpc的hello world 服务端客户端 proto文件proto语法 数据类型 基本数据类型其他数据类型 编写风格多服务 单体架构 只能对整体扩容一荣俱荣#xff0c;一损俱损代码耦合#xff0c;项目的开…GRPC介绍 目录 单体架构微服务架构问题原始的grpc 服务端客户端原生rpc的问题 grpc的hello world 服务端客户端 proto文件proto语法 数据类型 基本数据类型其他数据类型 编写风格多服务 单体架构 只能对整体扩容一荣俱荣一损俱损代码耦合项目的开发者需要知道整个项目的流程 微服务架构 针对单体架构的问题出现了微服务架构 可以按照服务进行单独扩容各个服务之间可以独立开发独立部署 问题 代码冗余服务之间的调用很麻烦 为什么要使用grpc grpc使用的意义 原始的grpc 服务端 package mainimport (fmtnetnet/httpnet/rpc )type Server struct { } type Req struct {Num1 intNum2 int } type Res struct {Num int }func (s Server) Add(req Req, res *Res) error {res.Num req.Num1 req.Num2return nil }func main() {// 注册rpc服务rpc.Register(new(Server))rpc.HandleHTTP()listen, err : net.Listen(tcp, :8080)if err ! nil {fmt.Println(err)return}http.Serve(listen, nil) }客户端 package mainimport (fmtnet/rpc )type Req struct {Num1 intNum2 int } type Res struct {Num int }func main() {req : Req{1, 2}client, err : rpc.DialHTTP(tcp, :8080)if (err ! nil) {fmt.Println(err)return}var res Resclient.Call(Server.Add, req, res)fmt.Println(res) }原生rpc的问题 编写相对复杂需要自己去关注实现过程没有代码提示容易写错。 grpc的hello world 服务端 编写一个结构体名字叫什么不重要重要的是得实现protobuf中的所有方法监听端口注册服务 package mainimport (contextfmtgoogle.golang.org/grpcgoogle.golang.org/grpc/grpcloggrpc_study/grpc_proto/hello_grpcnet )type HelloServiceServer struct { }func (s HelloServiceServer) SayHello(ctx context.Context, request *hello_grpc.HelloRequest) (res *hello_grpc.HelloResponse, err error) {fmt.Println(请求来了, request)return hello_grpc.HelloResponse{Message: Hello Xiaoyu_Wang,Name: Server,}, nil }func main() {// 监听端口listen, err : net.Listen(tcp, :8080)if err ! nil {grpclog.Fatalf(Failed to listen: %v, err)}// 创建一个gRPC服务器实例。s : grpc.NewServer()server : HelloServiceServer{}// 将server结构体注册为gRPC服务。hello_grpc.RegisterHelloServiceServer(s, server)fmt.Println(grpc server running :8080)// 开始处理客户端请求。err s.Serve(listen) }客户端 建立连接调用方法 package mainimport (contextfmtgoogle.golang.org/grpcgoogle.golang.org/grpc/credentials/insecuregrpc_study/grpc_proto/hello_grpclog )func main() {addr : :8080// 使用 grpc.Dial 创建一个到指定地址的 gRPC 连接。// 此处使用不安全的证书来实现 SSL/TLS 连接conn, err : grpc.Dial(addr, grpc.WithTransportCredentials(insecure.NewCredentials()))if err ! nil {log.Fatalf(fmt.Sprintf(grpc connect addr [%s] 连接失败 %s, addr, err))}defer conn.Close()// 初始化客户端client : hello_grpc.NewHelloServiceClient(conn)result, err : client.SayHello(context.Background(), hello_grpc.HelloRequest{Name: Xiaoyu_Wang,Message: ok,})fmt.Println(result, err) }proto文件 syntax proto3; // 指定proto版本 package hello_grpc; // 指定默认包名// 指定golang包名 option go_package /hello_grpc;//定义rpc服务 service HelloService {// 定义函数rpc SayHello (HelloRequest) returns (HelloResponse) {} }// HelloRequest 请求内容 message HelloRequest {string name 1; // 消息号string message 2; }// HelloResponse 响应内容 message HelloResponse{string name 1;string message 2; }proto语法 service 对应的就是go里面的接口可以作为服务端客户端rpc 对应的就是结构体中的方法message对应的也是结构体 数据类型 基本数据类型 message Request {double a1 1;float a2 2;int32 a3 3;uint32 a4 4;uint64 a5 5;sint32 a6 6;sint64 a7 7;fixed32 a8 8;fixed64 a9 9;sfixed32 a10 10;sfixed64 a11 11;bool a12 12;string a13 13;bytes a14 14; }对应的go类型 type Request struct {state protoimpl.MessageStatesizeCache protoimpl.SizeCacheunknownFields protoimpl.UnknownFieldsA1 float64 protobuf:fixed64,1,opt,namea1,proto3 json:a1,omitemptyA2 float32 protobuf:fixed32,2,opt,namea2,proto3 json:a2,omitemptyA3 int32 protobuf:varint,3,opt,namea3,proto3 json:a3,omitemptyA4 uint32 protobuf:varint,4,opt,namea4,proto3 json:a4,omitemptyA5 uint64 protobuf:varint,5,opt,namea5,proto3 json:a5,omitemptyA6 int32 protobuf:zigzag32,6,opt,namea6,proto3 json:a6,omitemptyA7 int64 protobuf:zigzag64,7,opt,namea7,proto3 json:a7,omitemptyA8 uint32 protobuf:fixed32,8,opt,namea8,proto3 json:a8,omitemptyA9 uint64 protobuf:fixed64,9,opt,namea9,proto3 json:a9,omitemptyA10 int32 protobuf:fixed32,10,opt,namea10,proto3 json:a10,omitemptyA11 int64 protobuf:fixed64,11,opt,namea11,proto3 json:a11,omitemptyA12 bool protobuf:varint,12,opt,namea12,proto3 json:a12,omitemptyA13 string protobuf:bytes,13,opt,namea13,proto3 json:a13,omitemptyA14 []byte protobuf:bytes,14,opt,namea14,proto3 json:a14,omitempty }其他数据类型 数组类型 message ArrayRequest {repeated int64 a1 1;repeated string a2 2;repeated Request request_list 3; }type ArrayRequest struct {A1 []int64 A2 []string RequestList []*Request }map类型 message MapRequest {mapint64, string m_i_s 1;mapstring, bool m_i_b 2;mapstring, ArrayRequest m_i_arr 3; }type MapRequest struct {MIS map[int64]stringMIB map[string]boolMIArr map[string]*ArrayRequest }嵌套类型 message Q1 {message Q2{string name2 2;}string name1 1;Q2 q2 2; }type Q1 struct {state protoimpl.MessageStatesizeCache protoimpl.SizeCacheunknownFields protoimpl.UnknownFieldsName1 string protobuf:bytes,1,opt,namename1,proto3 json:name1,omitemptyQ2 *Q1_Q2 protobuf:bytes,2,opt,nameq2,proto3 json:q2,omitempty }编写风格 文件名建议下划线例如my_student.proto包名和目录名对应服务名、方法名、消息名均为大驼峰字段名为下划线 多服务 syntax proto3;option go_package /duo_grpc;service VideoService {rpc Look (Request) returns (Response) {} }message Request{string name 1; }message Response{string name 1; }service OderService {rpc Buy (Request) returns (Response) {} }
http://www.hkea.cn/news/14330270/

相关文章:

  • 精通网站建设工资多少沪江博客wordpress模板
  • 公司网站建设入什么费用中信建设有限责任公司内部网站
  • seo sem是指什么意思seo技巧分享
  • 如何制作动漫网站模板wordpress时间轴插件
  • 网站建设比较合理的流程织梦网站如何生成伪静态
  • 一起做网商网站怎么样遵义网络科技有限公司
  • 做seo网站优化多少钱网站建设与管理教学视频教程
  • 公司网站域名是什么意思盘锦做网站电话
  • 做淘宝需要知道什么网站全球设计行
  • 怎样建设免费网站软件技术职业生涯规划书
  • 湛江市住房和城乡建设网站宿州网站建设贰聚思诚信
  • 南宁码科网站建设徐州关键字优化资讯
  • 电话推销网站建设上海数据开放网站建设
  • 余姚网站建设yyshj毕业设计博客网站开发
  • 搭建网站的步骤和顺序酒泉建设局网站
  • 给网站做排名优化学什么好处wordpress怎么更改后台路径
  • 青岛网站设计报价免费可商用的cms
  • 合肥市城乡和建设网站单位网站建设制作
  • 网站设计 中高端用什么软件做网站最简单 最方便
  • 最新免费网站源码如何免费建站
  • 黄江镇网站建设公司国内专业的企业展厅设计
  • 龙岗做网站哪里找网站管理与维护的优势
  • 站长统计app软件下载官网wordpress xss漏洞
  • asp网站建设 win7河南省新闻出版学校
  • 网络营销与网站推广的北京注册公司核名网站
  • 建设银行的网站是什么情况宁波seo外包公司
  • 天津通用网站建设收费河南网站建设的公司
  • 保定网络公司网站wordpress微博同步
  • 手机可以做3d动漫视频网站有哪些黑科技广告推广神器
  • 建德市住房和城乡建设局网站怎么更改网站栏目id