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

如何快速增加网站收录温州seo博客

如何快速增加网站收录,温州seo博客,沈阳网站开发技术公司,学网站建设怎么样GRPC介绍 目录 单体架构微服务架构问题原始的grpc 服务端客户端原生rpc的问题 grpc的hello world 服务端客户端 proto文件proto语法 数据类型 基本数据类型其他数据类型 编写风格多服务 单体架构 只能对整体扩容一荣俱荣,一损俱损代码耦合,项目的开…

GRPC介绍

目录

  1. 单体架构
  2. 微服务架构
  3. 问题
  4. 原始的grpc
    • 服务端
    • 客户端
    • 原生rpc的问题
  5. grpc的hello world
    • 服务端
    • 客户端
  6. proto文件
  7. proto语法
    • 数据类型
      • 基本数据类型
      • 其他数据类型
  8. 编写风格
  9. 多服务

单体架构

  1. 只能对整体扩容
  2. 一荣俱荣,一损俱损
  3. 代码耦合,项目的开发者需要知道整个项目的流程

微服务架构

针对单体架构的问题出现了微服务架构

  1. 可以按照服务进行单独扩容
  2. 各个服务之间可以独立开发,独立部署

问题

  1. 代码冗余
  2. 服务之间的调用很麻烦

为什么要使用grpc
grpc使用的意义

原始的grpc

服务端

package mainimport ("fmt""net""net/http""net/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 ("fmt""net/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的问题:

  1. 编写相对复杂,需要自己去关注实现过程
  2. 没有代码提示,容易写错。

grpc的hello world

服务端

  1. 编写一个结构体,名字叫什么不重要
  2. 重要的是得实现protobuf中的所有方法
  3. 监听端口
  4. 注册服务
package mainimport ("context""fmt""google.golang.org/grpc""google.golang.org/grpc/grpclog""grpc_study/grpc_proto/hello_grpc""net"
)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)
}

客户端

  1. 建立连接
  2. 调用方法
package mainimport ("context""fmt""google.golang.org/grpc""google.golang.org/grpc/credentials/insecure""grpc_study/grpc_proto/hello_grpc""log"
)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语法

  1. service 对应的就是go里面的接口,可以作为服务端,客户端
  2. rpc 对应的就是结构体中的方法
  3. 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,name=a1,proto3" json:"a1,omitempty"`A2  float32 `protobuf:"fixed32,2,opt,name=a2,proto3" json:"a2,omitempty"`A3  int32   `protobuf:"varint,3,opt,name=a3,proto3" json:"a3,omitempty"`A4  uint32  `protobuf:"varint,4,opt,name=a4,proto3" json:"a4,omitempty"`A5  uint64  `protobuf:"varint,5,opt,name=a5,proto3" json:"a5,omitempty"`A6  int32   `protobuf:"zigzag32,6,opt,name=a6,proto3" json:"a6,omitempty"`A7  int64   `protobuf:"zigzag64,7,opt,name=a7,proto3" json:"a7,omitempty"`A8  uint32  `protobuf:"fixed32,8,opt,name=a8,proto3" json:"a8,omitempty"`A9  uint64  `protobuf:"fixed64,9,opt,name=a9,proto3" json:"a9,omitempty"`A10 int32   `protobuf:"fixed32,10,opt,name=a10,proto3" json:"a10,omitempty"`A11 int64   `protobuf:"fixed64,11,opt,name=a11,proto3" json:"a11,omitempty"`A12 bool    `protobuf:"varint,12,opt,name=a12,proto3" json:"a12,omitempty"`A13 string  `protobuf:"bytes,13,opt,name=a13,proto3" json:"a13,omitempty"`A14 []byte  `protobuf:"bytes,14,opt,name=a14,proto3" json:"a14,omitempty"`
}
其他数据类型
  1. 数组类型
message ArrayRequest {repeated int64 a1 = 1;repeated string a2 = 2;repeated Request request_list = 3;
}
type ArrayRequest struct {A1          []int64 A2          []string   RequestList []*Request
}
  1. map类型
message MapRequest {map<int64, string> m_i_s = 1;map<string, bool> m_i_b = 2;map<string, ArrayRequest> m_i_arr = 3;
}
type MapRequest struct {MIS   map[int64]stringMIB   map[string]boolMIArr map[string]*ArrayRequest
}
  1. 嵌套类型
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,name=name1,proto3" json:"name1,omitempty"`Q2    *Q1_Q2 `protobuf:"bytes,2,opt,name=q2,proto3" json:"q2,omitempty"`
}

编写风格

  1. 文件名建议下划线,例如:my_student.proto
  2. 包名和目录名对应
  3. 服务名、方法名、消息名均为大驼峰
  4. 字段名为下划线

多服务

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.dinnco.com/news/13375.html

相关文章:

  • 怎么做注册账号的网站营销型网站建设方案
  • 做的视频发到哪个网站好网络推广网站推广方法
  • 有哪些免费b2b网站珠海做网站的公司
  • 网站安全建设杀毒软件最近新闻热点
  • pr软件seo查询爱站网
  • 网站建设提供ftp最新国际消息
  • 厦门网上房地产网官方网站南宁市优化网站公司
  • 中山企业网站制作公司网站建设维护
  • wordpress同步微信新乡seo外包
  • 关于网站设计的书网络推广和seo
  • 不用php做网站国内建站平台有哪些
  • 深圳龙岗做网站公司什么建站程序最利于seo
  • 网站页脚信息网店推广营销方案
  • php网站开发实用技术课后习题李勇seo的博客
  • 免费的java资源网站网络公司网站建设
  • 程序外包平台软文优化
  • 网站开发iis怎么配置爱上链外链购买平台
  • 龙岩任做网站的哪几个比较好长沙网络推广平台
  • rar在线解压网站微软bing搜索引擎
  • 吉林大学建设工程学院网站广西疫情最新消息
  • 公司网站搜索引擎优化网址申请注册
  • 网站秒收录工具如何在百度上做产品推广
  • 深圳外贸seo网站推广深圳英文站seo
  • 医院如何做网站策划轻饮食网络推广方案
  • 网站建设怎么添加视频百度网页翻译
  • 网站模板做网站seo提供服务
  • 做赛事预测网站seo云优化软件
  • 美食分享网站设计国家免费技能培训
  • 辛集建设局官方网站seo培训学院官网
  • 北京网站开发网站建设价格今天高清视频免费播放