【Go】五、Grpc 的入门使用
grpc 与 protobuf
grpc 使用的是 protobuf 协议,其是一个通用的 rpc 框架,基本支持主流的所有语言、其底层使用 http/2 进行网络通信,具有较高的效率
protobuf 是一种序列化格式,这种格式具有 序列化以及解码速度快(对比json、xml 速度快 2 - 100 倍),压缩率高等优点,是一个性能炸弹
基础环境配置
我们使用之前,要先安装 protobuf 的相关环境:
在 github 上下载 protoc 并配置好环境变量(环境变量配置到 bin 这一级)(用来生成源码)
记得在编译器中安装 protobuf support 插件
protobuf尝试
对 protobuf 进行尝试:创建一个.proto 文件:
syntax = "proto3";message HelloRequest {string name = 1; // 注意这里不是赋值,而是指定编号
}
之后使用 protoc 生成源码
protoc -I . --go_out=. --go-grpc_out=require_unimplemented_servers=false:. ./helloworld.proto
测试proto的使用:
func main() {req := HelloWorld.HelloRequest{Name: "Chen",Age: 16,Courses: []string{"C", "Go"},}rsp, _ := proto.Marshal(&req) // 编码newReq := HelloWorld.HelloRequest{} // 创建一个空的proto_ = proto.Unmarshal(rsp, &newReq) // 解码,解码存储在 newReq 中fmt.Println(newReq.Name, newReq.Age, newReq.Courses)
}
实际开发尝试
创建目录结构:
grpc_test
server
server.go
client
proto
helloworld.proto
helloworld.pb.go(自动生成)
helloworld_grpc.pb.go(自动生成)
首先自己编写 helloworld.proto:
syntax = "proto3";
// 生成的包名
option go_package = ".;proto";// 标注生成的方法接口
service Greeter {rpc SayHello (HelloRequest) returns (HelloReply);
}// 生成的结构体
message HelloRequest {string name = 1;
}message HelloReply {string message = 1;
}
之后使用命令:
protoc -I . --go_out=. --go-grpc_out=require_unimplemented_servers=false:. ./helloworld.proto
生成helloworld.pb.go 文件:
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.32.0
// protoc v4.25.3
// source: helloworld.protopackage protoimport (protoreflect "google.golang.org/protobuf/reflect/protoreflect"protoimpl "google.golang.org/protobuf/runtime/protoimpl"reflect "reflect"sync "sync"
)const (// Verify that this generated code is sufficiently up-to-date._ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)// Verify that runtime/protoimpl is sufficiently up-to-date._ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
)type HelloRequest struct {state protoimpl.MessageStatesizeCache protoimpl.SizeCacheunknownFields protoimpl.UnknownFieldsName string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`
}func (x *HelloRequest) Reset() {*x = HelloRequest{}if protoimpl.UnsafeEnabled {mi := &file_helloworld_proto_msgTypes[0]ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))ms.StoreMessageInfo(mi)}
}func (x *HelloRequest) String() string {return protoimpl.X.MessageStringOf(x)
}func (*HelloRequest) ProtoMessage() {}func (x *HelloRequest) ProtoReflect() protoreflect.Message {mi := &file_helloworld_proto_msgTypes[0]if protoimpl.UnsafeEnabled && x != nil {ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))if ms.LoadMessageInfo() == nil {ms.StoreMessageInfo(mi)}return ms}return mi.MessageOf(x)
}// Deprecated: Use HelloRequest.ProtoReflect.Descriptor instead.
func (*HelloRequest) Descriptor() ([]byte, []int) {return file_helloworld_proto_rawDescGZIP(), []int{0}
}func (x *HelloRequest) GetName() string {if x != nil {return x.Name}return ""
}type HelloReply struct {state protoimpl.MessageStatesizeCache protoimpl.SizeCacheunknownFields protoimpl.UnknownFieldsMessage string `protobuf:"bytes,1,opt,name=message,proto3" json:"message,omitempty"`
}func (x *HelloReply) Reset() {*x = HelloReply{}if protoimpl.UnsafeEnabled {mi := &file_helloworld_proto_msgTypes[1]ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))ms.StoreMessageInfo(mi)}
}func (x *HelloReply) String() string {return protoimpl.X.MessageStringOf(x)
}func (*HelloReply) ProtoMessage() {}func (x *HelloReply) ProtoReflect() protoreflect.Message {mi := &file_helloworld_proto_msgTypes[1]if protoimpl.UnsafeEnabled && x != nil {ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))if ms.LoadMessageInfo() == nil {ms.StoreMessageInfo(mi)}return ms}return mi.MessageOf(x)
}// Deprecated: Use HelloReply.ProtoReflect.Descriptor instead.
func (*HelloReply) Descriptor() ([]byte, []int) {return file_helloworld_proto_rawDescGZIP(), []int{1}
}func (x *HelloReply) GetMessage() string {if x != nil {return x.Message}return ""
}var File_helloworld_proto protoreflect.FileDescriptorvar file_helloworld_proto_rawDesc = []byte{0x0a, 0x10, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x2e, 0x70, 0x72, 0x6f,0x74, 0x6f, 0x22, 0x22, 0x0a, 0x0c, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x52, 0x65, 0x71, 0x75, 0x65,0x73, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09,0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x22, 0x26, 0x0a, 0x0a, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x52,0x65, 0x70, 0x6c, 0x79, 0x12, 0x18, 0x0a, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x18,0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x32, 0x31,0x0a, 0x07, 0x47, 0x72, 0x65, 0x65, 0x74, 0x65, 0x72, 0x12, 0x26, 0x0a, 0x08, 0x53, 0x61, 0x79,0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x12, 0x0d, 0x2e, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x52, 0x65, 0x71,0x75, 0x65, 0x73, 0x74, 0x1a, 0x0b, 0x2e, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x52, 0x65, 0x70, 0x6c,0x79, 0x42, 0x09, 0x5a, 0x07, 0x2e, 0x3b, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x06, 0x70, 0x72,0x6f, 0x74, 0x6f, 0x33,
}var (file_helloworld_proto_rawDescOnce sync.Oncefile_helloworld_proto_rawDescData = file_helloworld_proto_rawDesc
)func file_helloworld_proto_rawDescGZIP() []byte {file_helloworld_proto_rawDescOnce.Do(func() {file_helloworld_proto_rawDescData = protoimpl.X.CompressGZIP(file_helloworld_proto_rawDescData)})return file_helloworld_proto_rawDescData
}var file_helloworld_proto_msgTypes = make([]protoimpl.MessageInfo, 2)
var file_helloworld_proto_goTypes = []interface{}{(*HelloRequest)(nil), // 0: HelloRequest(*HelloReply)(nil), // 1: HelloReply
}
var file_helloworld_proto_depIdxs = []int32{0, // 0: Greeter.SayHello:input_type -> HelloRequest1, // 1: Greeter.SayHello:output_type -> HelloReply1, // [1:2] is the sub-list for method output_type0, // [0:1] is the sub-list for method input_type0, // [0:0] is the sub-list for extension type_name0, // [0:0] is the sub-list for extension extendee0, // [0:0] is the sub-list for field type_name
}func init() { file_helloworld_proto_init() }
func file_helloworld_proto_init() {if File_helloworld_proto != nil {return}if !protoimpl.UnsafeEnabled {file_helloworld_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {switch v := v.(*HelloRequest); i {case 0:return &v.statecase 1:return &v.sizeCachecase 2:return &v.unknownFieldsdefault:return nil}}file_helloworld_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {switch v := v.(*HelloReply); i {case 0:return &v.statecase 1:return &v.sizeCachecase 2:return &v.unknownFieldsdefault:return nil}}}type x struct{}out := protoimpl.TypeBuilder{File: protoimpl.DescBuilder{GoPackagePath: reflect.TypeOf(x{}).PkgPath(),RawDescriptor: file_helloworld_proto_rawDesc,NumEnums: 0,NumMessages: 2,NumExtensions: 0,NumServices: 1,},GoTypes: file_helloworld_proto_goTypes,DependencyIndexes: file_helloworld_proto_depIdxs,MessageInfos: file_helloworld_proto_msgTypes,}.Build()File_helloworld_proto = out.Filefile_helloworld_proto_rawDesc = nilfile_helloworld_proto_goTypes = nilfile_helloworld_proto_depIdxs = nil
}
以及:
helloworld_grpc.pb.go
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
// versions:
// - protoc-gen-go-grpc v1.3.0
// - protoc v4.25.3
// source: helloworld.protopackage protoimport (context "context"grpc "google.golang.org/grpc"codes "google.golang.org/grpc/codes"status "google.golang.org/grpc/status"
)// This is a compile-time assertion to ensure that this generated file
// is compatible with the grpc package it is being compiled against.
// Requires gRPC-Go v1.32.0 or later.
const _ = grpc.SupportPackageIsVersion7const (Greeter_SayHello_FullMethodName = "/Greeter/SayHello"
)// GreeterClient is the client API for Greeter service.
//
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
type GreeterClient interface {SayHello(ctx context.Context, in *HelloRequest, opts ...grpc.CallOption) (*HelloReply, error)
}type greeterClient struct {cc grpc.ClientConnInterface
}func NewGreeterClient(cc grpc.ClientConnInterface) GreeterClient {return &greeterClient{cc}
}func (c *greeterClient) SayHello(ctx context.Context, in *HelloRequest, opts ...grpc.CallOption) (*HelloReply, error) {out := new(HelloReply)err := c.cc.Invoke(ctx, Greeter_SayHello_FullMethodName, in, out, opts...)if err != nil {return nil, err}return out, nil
}// GreeterServer is the server API for Greeter service.
// All implementations should embed UnimplementedGreeterServer
// for forward compatibility
type GreeterServer interface {SayHello(context.Context, *HelloRequest) (*HelloReply, error)
}// UnimplementedGreeterServer should be embedded to have forward compatible implementations.
type UnimplementedGreeterServer struct {
}func (UnimplementedGreeterServer) SayHello(context.Context, *HelloRequest) (*HelloReply, error) {return nil, status.Errorf(codes.Unimplemented, "method SayHello not implemented")
}// UnsafeGreeterServer may be embedded to opt out of forward compatibility for this service.
// Use of this interface is not recommended, as added methods to GreeterServer will
// result in compilation errors.
type UnsafeGreeterServer interface {mustEmbedUnimplementedGreeterServer()
}func RegisterGreeterServer(s grpc.ServiceRegistrar, srv GreeterServer) {s.RegisterService(&Greeter_ServiceDesc, srv)
}func _Greeter_SayHello_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {in := new(HelloRequest)if err := dec(in); err != nil {return nil, err}if interceptor == nil {return srv.(GreeterServer).SayHello(ctx, in)}info := &grpc.UnaryServerInfo{Server: srv,FullMethod: Greeter_SayHello_FullMethodName,}handler := func(ctx context.Context, req interface{}) (interface{}, error) {return srv.(GreeterServer).SayHello(ctx, req.(*HelloRequest))}return interceptor(ctx, in, info, handler)
}// Greeter_ServiceDesc is the grpc.ServiceDesc for Greeter service.
// It's only intended for direct use with grpc.RegisterService,
// and not to be introspected or modified (even as a copy)
var Greeter_ServiceDesc = grpc.ServiceDesc{ServiceName: "Greeter",HandlerType: (*GreeterServer)(nil),Methods: []grpc.MethodDesc{{MethodName: "SayHello",Handler: _Greeter_SayHello_Handler,},},Streams: []grpc.StreamDesc{},Metadata: "helloworld.proto",
}
尝试编写业务逻辑(这里应该写在handler中,由于业务过于简单,先写在server中):
server.go:
注意这里 grpc 帮我们处理了服务器不能连续被访问的问题,不需要我们手动通过一个死循环进行处理
package mainimport ("context""net""google.golang.org/grpc""FirstGo/goon/grpc_test/proto"
)type Server struct{}// 业务逻辑
// 第一个参数必须是context,error必须加
func (s *Server) SayHello(ctx context.Context, request *proto.HelloRequest) (*proto.HelloReply, error) {return &proto.HelloReply{Message: "hello " + request.Name,}, nil
}func main() {g := grpc.NewServer()proto.RegisterGreeterServer(g, &Server{})lis, err := net.Listen("tcp", "0.0.0.0:8080")if err != nil {panic("failed to listen: " + err.Error())}err = g.Serve(lis)if err != nil {panic("failed to start grpc: " + err.Error())}
}
编写客户端
client.go:
package mainimport ("FirstGo/goon/grpc_test/proto""context""fmt""google.golang.org/grpc"
)func main() {// 尝试拨号conn, err := grpc.Dial("127.0.0.1:8080", grpc.WithInsecure())if err != nil {panic(err)}defer conn.Close()// 创建客户端c := proto.NewGreeterClient(conn)// 调用对应的方法r, err := c.SayHello(context.Background(), &proto.HelloRequest{Name: "Chen"})if err != nil {panic(err)}fmt.Println(r.Message)
}
GRPC的四种数据传输模式
RPC 还具有四种数据模式,其分别是:
-
简单模式(上述模式)
客户端发起一次请求,服务器返回一次响应
-
服务端数据流模式
客户发起一次请求,服务器返回一段连续的数据流,最典型的例子是:客户发送一段股票代码,服务端实时将股票的数据源源不断的返回给客户端
-
客户端数据流模式
与服务端数据流模式相反,这种是由客户端源源不断的向服务端发送数据流,在发送结束后,由服务端发送一个响应,这种的典型例子是:物联网终端向服务器报送数据
-
双向数据流模式
这种是客户端和服务端都可以向双方发送数据流,这个时候双方的数据都可以相互发送,也就是可以实时交互,这种最典型的例子就是聊天机器人
实际测试:
建立文件结构:
stream_grpc_test
server
server.go
client
client.go
proto
stream.pb.gp
stream.proto
stream_grpc.pb.go
stream.proto:
syntax = "proto3";option go_package = ".;proto";service Greeter {rpc GetStream(StreamReqData) returns (stream StreamResData); // 服务端流模式,返回的响应数据是流rpc PutStream(stream StreamReqData) returns (StreamResData); // 客户端流模式,传给服务器的数据是流rpc AllStream(stream StreamReqData) returns (stream StreamResData); // 双向流模式
}message StreamReqData {string data = 1;
}message StreamResData {string data = 1;
}
服务端流模式简单使用
只写了 服务端流模式的 server,go:
const PORT = ":50052"type server struct {
}// 对于服务端数据传输模式,参考以下内容:
// 没有 context 参数,而是将返回作为入参传入
func (s *server) GetStream(req *proto.StreamReqData, res proto.Greeter_GetStreamServer) error {i := 0for {i++_ = res.Send(&proto.StreamResData{Data: fmt.Sprintf("%v", time.Now().Unix()),})time.Sleep(time.Second)if i > 10 {break}}return nil
}// 客户端数据流模式
// 只有一个用来不断接收的入参
func (s *server) PutStream(cliStr proto.Greeter_PutStreamServer) error {return nil
}// 双向数据流模式,和客户端模式相同,只有一个入参
func (s *server) AllStream(allStr proto.Greeter_AllStreamServer) error {return nil
}func main() {lis, err := net.Listen("tcp", PORT)if err != nil {panic(err)}s := grpc.NewServer()proto.RegisterGreeterServer(s, &server{})s.Serve(lis)}
只针对于服务端流的client.go
func main() {// 拨号conn, err := grpc.Dial("localhost:50052", grpc.WithInsecure())if err != nil {panic(err)}defer conn.Close()c := proto.NewGreeterClient(conn)// 这里要注意:我们在进行调用的时候使用的仍让是 simple 的模式,而不是服务端的函数模式res, _ := c.GetStream(context.Background(), &proto.StreamReqData{Data: "Chen"})// 使用一个死循环来接收客户端传进来的数据for {a, err := res.Recv()if err != nil {fmt.Println(err)break}fmt.Println(a)}
}
客户端流模式的简单使用
server.go:
package mainimport ("FirstGo/goon/stream_grpc_test/proto""fmt""google.golang.org/grpc""net""time"
)const PORT = ":50052"type server struct {
}// 对于服务端数据传输模式,参考以下内容:
// 没有 context 参数,而是将返回作为入参传入
func (s *server) GetStream(req *proto.StreamReqData, res proto.Greeter_GetStreamServer) error {i := 0for {i++_ = res.Send(&proto.StreamResData{Data: fmt.Sprintf("%v", time.Now().Unix()),})time.Sleep(time.Second)if i > 10 {break}}return nil
}// 客户端数据流模式
// 只有一个用来不断接收的入参
func (s *server) PutStream(cliStr proto.Greeter_PutStreamServer) error {// 客户端流模式,客户端不断的发送数据给服务器for {if a, err := cliStr.Recv(); err != nil {fmt.Println(err)} else {fmt.Println(a.Data)}}return nil
}// 双向数据流模式,和客户端模式相同,只有一个入参
func (s *server) AllStream(allStr proto.Greeter_AllStreamServer) error {return nil
}func main() {lis, err := net.Listen("tcp", PORT)if err != nil {panic(err)}s := grpc.NewServer()proto.RegisterGreeterServer(s, &server{})s.Serve(lis)}
client.go:
package mainimport ("FirstGo/goon/stream_grpc_test/proto""context""fmt""google.golang.org/grpc""time"
)func main() {// 拨号conn, err := grpc.Dial("localhost:50052", grpc.WithInsecure())if err != nil {panic(err)}defer conn.Close()c := proto.NewGreeterClient(conn)putS, _ := c.PutStream(context.Background())i := 0for {i++putS.Send(&proto.StreamReqData{Data: fmt.Sprintf("Chen %d", i)})time.Sleep(time.Second)if i > 10 {break}}
}
双向流模式的简单使用
server.go:
package mainimport ("FirstGo/goon/stream_grpc_test/proto""fmt""google.golang.org/grpc""net""sync""time"
)const PORT = ":50052"type server struct {
}// 对于服务端数据传输模式,参考以下内容:
// 没有 context 参数,而是将返回作为入参传入
func (s *server) GetStream(req *proto.StreamReqData, res proto.Greeter_GetStreamServer) error {i := 0for {i++_ = res.Send(&proto.StreamResData{Data: fmt.Sprintf("%v", time.Now().Unix()),})time.Sleep(time.Second)if i > 10 {break}}return nil
}// 客户端数据流模式
// 只有一个用来不断接收的入参
func (s *server) PutStream(cliStr proto.Greeter_PutStreamServer) error {// 客户端流模式,客户端不断的发送数据给服务器for {if a, err := cliStr.Recv(); err != nil {fmt.Println(err)} else {fmt.Println(a.Data)}}return nil
}// 双向数据流模式,和客户端模式相同,只有一个入参
func (s *server) AllStream(allStr proto.Greeter_AllStreamServer) error {// 不可以像下面这样简单使用,因为 Recv() 会阻塞主线程//allStr.Recv()//allStr.Send()wg := sync.WaitGroup{}wg.Add(2) // 添加两个等待线程go func() {defer wg.Done()for {data, _ := allStr.Recv()fmt.Println("收到客户端消息:" + data.Data)}}()go func() {defer wg.Done()for {_ = allStr.Send(&proto.StreamResData{Data: "我是服务器"})time.Sleep(time.Second)}}()wg.Wait()return nil
}func main() {lis, err := net.Listen("tcp", PORT)if err != nil {panic(err)}s := grpc.NewServer()proto.RegisterGreeterServer(s, &server{})s.Serve(lis)}
client.go:
package mainimport ("FirstGo/goon/stream_grpc_test/proto""context""fmt""google.golang.org/grpc""sync""time"
)func main() {// 拨号conn, err := grpc.Dial("localhost:50052", grpc.WithInsecure())if err != nil {panic(err)}defer conn.Close()c := proto.NewGreeterClient(conn)// // 这里要注意:我们在进行调用的时候使用的仍让是 simple 的模式,而不是服务端的函数模式// 服务端模式的客户端//res, _ := c.GetStream(context.Background(), &proto.StreamReqData{Data: "Chen"})// // 使用一个死循环来接收客户端传进来的数据//for {// a, err := res.Recv()// if err != nil {// fmt.Println(err)// break// }// fmt.Println(a)//}// 客户端数据流模式//putS, _ := c.PutStream(context.Background())//i := 0//for {// i++// putS.Send(&proto.StreamReqData{Data: fmt.Sprintf("Chen %d", i)})// time.Sleep(time.Second)// if i > 10 {// break// }//}// 双向流模式allStr, _ := c.AllStream(context.Background())wg := sync.WaitGroup{}wg.Add(2) // 添加两个等待线程go func() {defer wg.Done()for {data, _ := allStr.Recv()fmt.Println("收到客户端消息:" + data.Data)}}()go func() {defer wg.Done()for {_ = allStr.Send(&proto.StreamReqData{Data: "我是Chen (客户端)"})time.Sleep(time.Second)}}()wg.Wait()
}
相关文章:

【Go】五、Grpc 的入门使用
grpc 与 protobuf grpc 使用的是 protobuf 协议,其是一个通用的 rpc 框架,基本支持主流的所有语言、其底层使用 http/2 进行网络通信,具有较高的效率 protobuf 是一种序列化格式,这种格式具有 序列化以及解码速度快(…...

PDF加粗内容重复读取解决方案
文章目录 前言发现问题解决方案问题分析大致逻辑 show my code 前言 在使用pdfplumber读取PDF的过程中,由于加黑的内容会被莫名其妙的读取两次,带来了很大的困扰。这篇文章将给出解决方案。 发现问题 在在使用pdfplumber读取PDF的过程中,读…...

Golang 并发 Channel的用法
目录 Golang 并发 Channel的用法1. channel 的创建2. nil channel读写阻塞示例close示例 3. channel 的读写4. channel 只读只写5. 关闭channelchannel关闭后,剩余的数据能否取到读取关闭的channel,将获取零值使用ok判断,是否关闭使用for-ran…...

cfa复习资料介绍之二:notes(SchweserNotes)
什么是CFA notes? CFA资料Study Notes都是外国一些出版机构针对CFA考试提供的复习资料,而其中Schweser在国内的名气最大,用的人也最多。内容详尽并且突出重点,并且CFA Notes的内容相比于官方curriculum教材更加符合中国CFA考生的心态&#x…...

FITC Palmitate Conjugate,FITC-棕榈酸酯缀合物,可以用标准 FITC 滤光片组进行成像
FITC Palmitate Conjugate,FITC-棕榈酸酯缀合物,可以用标准 FITC 滤光片组进行成像 您好,欢迎来到新研之家 文章关键词:FITC Palmitate Conjugate,FITC-棕榈酸酯缀合物,FITC 棕榈酸酯缀合物,F…...

本机防攻击简介
定义 在网络中,存在着大量针对CPU(Central Processing Unit)的恶意攻击报文以及需要正常上送CPU的各类报文。针对CPU的恶意攻击报文会导致CPU长时间繁忙的处理攻击报文,从而引发其他业务的中断甚至系统的中断;大量正常…...

Python 进阶语法:JSON
1 什么是 JSON? 1.1 JSON 的定义 JSON 是 JavaScript Object Notation 的简写,字面上的意思是 JavaScript 对象标记。本质上,JSON 是轻量级的文本数据交换格式。轻量级,是拿它与另一种数据交换格式XML进行比较,相当轻…...

mescroll 在uni-app 运行的下拉刷新和上拉加载的组件
官网传送门: https://www.mescroll.com/uni.html 最近使用到了mescroll 但是一直都是整个页面的滚动, 最近需求有需要局部滚动, 收藏了一个博主的文章觉得写的还挺好, 传送门: https://blog.csdn.net/Minions_Fatman/article/details/134754926?spm1001.2014.3001.5506 使用…...

netty的TCP服务端和客户端实现
第一步:引入依赖 <dependencies><dependency><groupId>io.netty</groupId><artifactId>netty-all</artifactId><version>4.1.90.Final</version></dependency></dependencies> 第二步:实…...

合纵连横 – 以 Flink 和 Amazon MSK 构建 Amazon DocumentDB 之间的实时数据同步
在大数据时代,实时数据同步已经有很多地方应用,包括从在线数据库构建实时数据仓库,跨区域数据复制。行业落地场景众多,例如,电商 GMV 数据实时统计,用户行为分析,广告投放效果实时追踪ÿ…...

HBase 进阶
参考来源: B站尚硅谷HBase2.x 目录 Master 架构RegionServer 架构写流程MemStore Flush读流程HFile 结构读流程合并读取数据优化 StoreFile CompactionRegion Split预分区(自定义分区)系统拆分 Master 架构 Master详细架构 1)Meta 表格介…...

一周学会Django5 Python Web开发-Django5路由命名与反向解析reverse与resolve
锋哥原创的Python Web开发 Django5视频教程: 2024版 Django5 Python web开发 视频教程(无废话版) 玩命更新中~_哔哩哔哩_bilibili2024版 Django5 Python web开发 视频教程(无废话版) 玩命更新中~共计25条视频,包括:2024版 Django5 Python we…...

好奇!为什么gateway和springMVC之间依赖冲突?
Gateway和SpringMVC之间存在冲突,可能是因为它们分别基于不同的技术栈。具体来说: 技术栈差异:Spring Cloud Gateway 是建立在 Spring Boot 2.x 和 Spring WebFlux 基础之上的,它使用的是非阻塞式的 Netty 服务器。而 Spring MVC…...

一些内网渗透总结
windows命令收集 信息收集: 查看系统版本和补丁信息: systeminfo 查看系统开放端口: netstat -ano 查看系统进程: tasklist /svc 列出详细进程: tasklist /V /FO CSV 查看ip地址和dns信息: ipconfig /all 查看当前用户: whoami /user 查看计算机用户列表: net user 查看计算机…...

C#版字节跳动SDK - SKIT.FlurlHttpClient.ByteDance
前言 在我们日常开发工作中对接第三方开放平台,找一款封装完善且全面的SDK能够大大的简化我们的开发难度和提高工作效率。今天给大家推荐一款C#开源、功能完善的字节跳动SDK:SKIT.FlurlHttpClient.ByteDance。 项目官方介绍 可能是全网唯一的 C# 版字…...

深度学习系列60: 大模型文本理解和生成概述
参考网络课程:https://www.bilibili.com/video/BV1UG411p7zv/?p98&spm_id_frompageDriver&vd_source3eeaf9c562508b013fa950114d4b0990 1. 概述 包含理解和分类两大类问题,对应的就是BERT和GPT两大类模型;而交叉领域则对应T5 2.…...

SpringBoot 使用 JWT 保护 Rest Api 接口
用 spring-boot 开发 RESTful API 非常的方便,在生产环境中,对发布的 API 增加授权保护是非常必要的。现在我们来看如何利用 JWT 技术为 API 增加授权保护,保证只有获得授权的用户才能够访问 API。 一、Jwt 介绍 JSON Web Token (JWT)是一个开…...

大蟒蛇(Python)笔记(总结,摘要,概括)——第10章 文件和异常
目录 10.1 读取文件 10.1.1 读取文件的全部内容 10.1.2 相对文件路径和绝对文件路径 10.1.3 访问文件中的各行 10.1.4 使用文件的内容 10.1.5 包含100万位的大型文件 10.1.6 圆周率中包含你的生日吗 10.2 写入文件 10.2.1 写入一行 10.2.2 写入多行 10.3 异常 10.3.1 处理Ze…...

使用JDBC操作数据库(IDEA编译器)
目录 JDBC的本质 JDBC好处 JDBC操作MySQL数据库 1.创建工程导入驱动jar包 2.编写测试代码 相关问题 JDBC的本质 官方(sun公司) 定义的一套操作所有关系型数据库的规则,即接口各个数据库厂商去实现这套接口,提供数据库驱动jar包我们可以使用这…...

Vue图片浏览组件v-viewer,支持旋转、缩放、翻转等操作
Vue图片浏览组件v-viewer,支持旋转、缩放、翻转等操作 之前用过viewer.js,算是市场上用过最全面的图片预览。v-viewer,是基于viewer.js的一个图片浏览的Vue组件,支持旋转、缩放、翻转等操作。 基本使用 安装:npm安装…...

大蟒蛇(Python)笔记(总结,摘要,概括)——第2章 变量和简单的数据类型
目录 2.1 运行hello_world.py时发生的情况 2.2 变量 2.2.1 变量的命名和使用 2.2.2 如何在使用变量时避免命名错误 2.2.3 变量是标签 2.3 字符串 2.3.1 使用方法修改字符串的大小写 2.3.2 在字符串中使用变量 2.3.3 使用制表符或换行符来添加空白 2.3.4 删除空白 2.3.5 删除…...

SpringCloud-Gateway网关的使用
本文介绍如何再 SpringCloud 项目中引入 Gateway 网关并完成网关服务的调用。Gateway 网关是一个在微服务架构中起到入口和路由控制的关键组件。它负责处理客户端请求,进行路由决策,并将请求转发到相应的微服务。Gateway 网关还可以实现负载均衡、安全认…...

想要学习编程,有什么推荐的书籍吗
如果你要变得更好,C语言是一个极佳的选择,其原因有二。首先,C语言缺乏任何现代的安全功能,这意味着你必须更为警惕,时刻了解真正发生的事情。如果你能写出安全、健壮的C代码,那你就能用任何编程语言写出安全…...

LWM(LargeWorldModel)大世界模型-可文字可图片可视频-多模态LargeWorld-视频问答成功运行-实现循环问答多次问答
Large World Model(LWM)现在大火,其最主要特点是不仅能够针对文本进行检索交互,还能对图片、视频进行问答交互,自从上文《LWM(LargeWorldModel)大世界模型-可文字可图片可视频-多模态LargeWorld-详细安装记录》发出后&…...

线阵相机之帧超时
1 帧超时的效果 在帧超时时间内相机若未采集完一张图像所需的行数,则相机会直接完成这张图像的采集,并自动将缺失行数补黑出图,机制有以下几种选择: 1. 丢弃整张补黑的图像 2. 保留补黑部分出图 3.丢弃补黑部分出图...

模型转换案例学习:等效替换不支持算子
文章介绍 Qualcomm Neural Processing SDK (以下简称SNPE)支持Caffe、ONNX、PyTorch和TensorFlow等不同ML框架的算子。对于某些特定的不支持的算子,我们介绍一种算子等效替换的方法来完成模型转换。本案例来源于https://github.com/quic/qidk…...

js 数组排序的方式
var numberList [5, 100, 94, 71, 49, 36, 2, 4]; 冒泡排序: 相邻的数据进行两两比较,小数放在前面,大数放在后面,这样一趟下来,最小的数就被排在了第一位,第二趟也是如此,如此类推࿰…...

手机连接电脑后资源管理器无法识别(识别设备但无法访问文件)
问题描述 小米8刷了pixel experience系统,今天用电脑连接后无法访问手机文件,但是手机选择了usb传输模式为文件传输 解决办法 在设备和打印机页面中右键选择属性 点击改变设置 卸载驱动,注意勾选删除设备的驱动程序软件 卸载后重新连接手机,电脑弹出希望对设备进行什么操作时…...

安装unget包 sqlsugar时报错,完整的报错解决
前置 .net6的开发环境 问题 ? 打开unget官网,搜索报错的依赖Oracle.ManagedDataAccess.Core unget官网 通过unget搜索Oracle.ManagedDataAccess.Core查看该依赖的依赖 发现应该是需要的依赖Oracle.ManagedDataAccess.Core(>3.21.100)不支持.net6的环境 解…...

oracle数据库事务的四大特性与隔离级别与游标
数据库事务的四大特性: 这里提到了 ACID 四个特性,分别是: A(Atomicity): 原子性,确保事务中的所有操作要么全部执行成功,要么全部不执行,不存在部分执行的情况。 C(…...