Forráskód Böngészése

添加ocr调用demo

Tao Zhang 5 éve
szülő
commit
3588b5a244

+ 2 - 0
demo/ocr_client/README.md

@@ -0,0 +1,2 @@
+使用proxy服务整合其他grpc服务到此服务治理框架
+客户端支持通过服务治理中心调用grpc服务

+ 71 - 0
demo/ocr_client/main.go

@@ -0,0 +1,71 @@
+/**
+ocr服务调用
+*/
+package main
+
+import (
+	"app.yhyue.com/BP/servicerd/proto"
+	"context"
+	"flag"
+	"google.golang.org/grpc"
+	"io/ioutil"
+	"log"
+	"os"
+)
+
+//服务地址配置
+var (
+	rdserver    = flag.String("rd", "192.168.3.240:10021", "服务治理地址")
+	balancetype = flag.Int("balance", 0, "负载策略 0=随机 1=服务器压力均衡 2=轮训占用")
+	imagefile   = flag.String("img", "/Users/taozhang/Downloads/demo.png", "需要识别的图片")
+)
+
+func init() {
+	flag.Parse()
+}
+
+//
+func checkError(err error) {
+	if err != nil {
+		log.Println(err.Error())
+		panic(err)
+	}
+}
+
+//
+func ocr() {
+	//图像文件读取
+	fi, err := os.Open(*imagefile)
+	checkError(err)
+	bs, err := ioutil.ReadAll(fi)
+	checkError(err)
+	//1.取得可用服务
+	conn, err := grpc.Dial(*rdserver, grpc.WithInsecure())
+	checkError(err)
+	var client proto.ServiceClient
+	client = proto.NewServiceClient(conn)
+	repl, err := client.Apply(context.Background(), &proto.ApplyReqData{Name: "ocr", Balance: int32(*balancetype)})
+	checkError(err)
+	log.Println("结果", repl.Addr, repl.ResourceId)
+	//2.业务调用
+	conn_b, err := grpc.Dial(repl.Addr, grpc.WithInsecure())
+	checkError(err)
+	defer conn_b.Close()
+	demo_client := proto.NewOcrClient(conn_b)
+	demo_repl, err := demo_client.Ocr(context.Background(), &proto.OcrRequest{
+		Image: bs,
+	})
+	checkError(err)
+	log.Println(demo_repl.Message)
+	//3.如果是SEQ负载模式,在这里需要释放资源占用
+	if *balancetype == 2 {
+		_, err := client.Release(context.Background(), &proto.StringReqData{Data: repl.ResourceId})
+		checkError(err)
+	}
+}
+
+func main() {
+	for i := 0; i < 20; i++ {
+		ocr()
+	}
+}

+ 206 - 0
proto/ocr.pb.go

@@ -0,0 +1,206 @@
+// Code generated by protoc-gen-go. DO NOT EDIT.
+// source: ocr.proto
+
+//OCR图像转文字服务
+
+package proto
+
+import (
+	context "context"
+	fmt "fmt"
+	proto "github.com/golang/protobuf/proto"
+	grpc "google.golang.org/grpc"
+	codes "google.golang.org/grpc/codes"
+	status "google.golang.org/grpc/status"
+	math "math"
+)
+
+// Reference imports to suppress errors if they are not otherwise used.
+var _ = proto.Marshal
+var _ = fmt.Errorf
+var _ = math.Inf
+
+// This is a compile-time assertion to ensure that this generated file
+// is compatible with the proto package it is being compiled against.
+// A compilation error at this line likely means your copy of the
+// proto package needs to be updated.
+const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
+
+type OcrRequest struct {
+	Image                []byte   `protobuf:"bytes,1,opt,name=image,proto3" json:"image,omitempty"`
+	XXX_NoUnkeyedLiteral struct{} `json:"-"`
+	XXX_unrecognized     []byte   `json:"-"`
+	XXX_sizecache        int32    `json:"-"`
+}
+
+func (m *OcrRequest) Reset()         { *m = OcrRequest{} }
+func (m *OcrRequest) String() string { return proto.CompactTextString(m) }
+func (*OcrRequest) ProtoMessage()    {}
+func (*OcrRequest) Descriptor() ([]byte, []int) {
+	return fileDescriptor_52282bc2e1d222c6, []int{0}
+}
+
+func (m *OcrRequest) XXX_Unmarshal(b []byte) error {
+	return xxx_messageInfo_OcrRequest.Unmarshal(m, b)
+}
+func (m *OcrRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+	return xxx_messageInfo_OcrRequest.Marshal(b, m, deterministic)
+}
+func (m *OcrRequest) XXX_Merge(src proto.Message) {
+	xxx_messageInfo_OcrRequest.Merge(m, src)
+}
+func (m *OcrRequest) XXX_Size() int {
+	return xxx_messageInfo_OcrRequest.Size(m)
+}
+func (m *OcrRequest) XXX_DiscardUnknown() {
+	xxx_messageInfo_OcrRequest.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_OcrRequest proto.InternalMessageInfo
+
+func (m *OcrRequest) GetImage() []byte {
+	if m != nil {
+		return m.Image
+	}
+	return nil
+}
+
+type OcrResponse struct {
+	Message              string   `protobuf:"bytes,1,opt,name=message,proto3" json:"message,omitempty"`
+	XXX_NoUnkeyedLiteral struct{} `json:"-"`
+	XXX_unrecognized     []byte   `json:"-"`
+	XXX_sizecache        int32    `json:"-"`
+}
+
+func (m *OcrResponse) Reset()         { *m = OcrResponse{} }
+func (m *OcrResponse) String() string { return proto.CompactTextString(m) }
+func (*OcrResponse) ProtoMessage()    {}
+func (*OcrResponse) Descriptor() ([]byte, []int) {
+	return fileDescriptor_52282bc2e1d222c6, []int{1}
+}
+
+func (m *OcrResponse) XXX_Unmarshal(b []byte) error {
+	return xxx_messageInfo_OcrResponse.Unmarshal(m, b)
+}
+func (m *OcrResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+	return xxx_messageInfo_OcrResponse.Marshal(b, m, deterministic)
+}
+func (m *OcrResponse) XXX_Merge(src proto.Message) {
+	xxx_messageInfo_OcrResponse.Merge(m, src)
+}
+func (m *OcrResponse) XXX_Size() int {
+	return xxx_messageInfo_OcrResponse.Size(m)
+}
+func (m *OcrResponse) XXX_DiscardUnknown() {
+	xxx_messageInfo_OcrResponse.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_OcrResponse proto.InternalMessageInfo
+
+func (m *OcrResponse) GetMessage() string {
+	if m != nil {
+		return m.Message
+	}
+	return ""
+}
+
+func init() {
+	proto.RegisterType((*OcrRequest)(nil), "proto.OcrRequest")
+	proto.RegisterType((*OcrResponse)(nil), "proto.OcrResponse")
+}
+
+func init() {
+	proto.RegisterFile("ocr.proto", fileDescriptor_52282bc2e1d222c6)
+}
+
+var fileDescriptor_52282bc2e1d222c6 = []byte{
+	// 124 bytes of a gzipped FileDescriptorProto
+	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xe2, 0xcc, 0x4f, 0x2e, 0xd2,
+	0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0x05, 0x53, 0x4a, 0x4a, 0x5c, 0x5c, 0xfe, 0xc9, 0x45,
+	0x41, 0xa9, 0x85, 0xa5, 0xa9, 0xc5, 0x25, 0x42, 0x22, 0x5c, 0xac, 0x99, 0xb9, 0x89, 0xe9, 0xa9,
+	0x12, 0x8c, 0x0a, 0x8c, 0x1a, 0x3c, 0x41, 0x10, 0x8e, 0x92, 0x3a, 0x17, 0x37, 0x58, 0x4d, 0x71,
+	0x41, 0x7e, 0x5e, 0x71, 0xaa, 0x90, 0x04, 0x17, 0x7b, 0x6e, 0x6a, 0x71, 0x31, 0x4c, 0x19, 0x67,
+	0x10, 0x8c, 0x6b, 0x64, 0xcc, 0xc5, 0xec, 0x9f, 0x5c, 0x24, 0xa4, 0x03, 0xa1, 0x04, 0x21, 0x36,
+	0xe9, 0x21, 0xcc, 0x97, 0x12, 0x42, 0x16, 0x82, 0x18, 0x97, 0xc4, 0x06, 0x16, 0x32, 0x06, 0x04,
+	0x00, 0x00, 0xff, 0xff, 0xd9, 0x2d, 0xf8, 0xb2, 0x9c, 0x00, 0x00, 0x00,
+}
+
+// Reference imports to suppress errors if they are not otherwise used.
+var _ context.Context
+var _ grpc.ClientConnInterface
+
+// This is a compile-time assertion to ensure that this generated file
+// is compatible with the grpc package it is being compiled against.
+const _ = grpc.SupportPackageIsVersion6
+
+// OcrClient is the client API for Ocr service.
+//
+// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream.
+type OcrClient interface {
+	Ocr(ctx context.Context, in *OcrRequest, opts ...grpc.CallOption) (*OcrResponse, error)
+}
+
+type ocrClient struct {
+	cc grpc.ClientConnInterface
+}
+
+func NewOcrClient(cc grpc.ClientConnInterface) OcrClient {
+	return &ocrClient{cc}
+}
+
+func (c *ocrClient) Ocr(ctx context.Context, in *OcrRequest, opts ...grpc.CallOption) (*OcrResponse, error) {
+	out := new(OcrResponse)
+	err := c.cc.Invoke(ctx, "/proto.Ocr/Ocr", in, out, opts...)
+	if err != nil {
+		return nil, err
+	}
+	return out, nil
+}
+
+// OcrServer is the server API for Ocr service.
+type OcrServer interface {
+	Ocr(context.Context, *OcrRequest) (*OcrResponse, error)
+}
+
+// UnimplementedOcrServer can be embedded to have forward compatible implementations.
+type UnimplementedOcrServer struct {
+}
+
+func (*UnimplementedOcrServer) Ocr(ctx context.Context, req *OcrRequest) (*OcrResponse, error) {
+	return nil, status.Errorf(codes.Unimplemented, "method Ocr not implemented")
+}
+
+func RegisterOcrServer(s *grpc.Server, srv OcrServer) {
+	s.RegisterService(&_Ocr_serviceDesc, srv)
+}
+
+func _Ocr_Ocr_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
+	in := new(OcrRequest)
+	if err := dec(in); err != nil {
+		return nil, err
+	}
+	if interceptor == nil {
+		return srv.(OcrServer).Ocr(ctx, in)
+	}
+	info := &grpc.UnaryServerInfo{
+		Server:     srv,
+		FullMethod: "/proto.Ocr/Ocr",
+	}
+	handler := func(ctx context.Context, req interface{}) (interface{}, error) {
+		return srv.(OcrServer).Ocr(ctx, req.(*OcrRequest))
+	}
+	return interceptor(ctx, in, info, handler)
+}
+
+var _Ocr_serviceDesc = grpc.ServiceDesc{
+	ServiceName: "proto.Ocr",
+	HandlerType: (*OcrServer)(nil),
+	Methods: []grpc.MethodDesc{
+		{
+			MethodName: "Ocr",
+			Handler:    _Ocr_Ocr_Handler,
+		},
+	},
+	Streams:  []grpc.StreamDesc{},
+	Metadata: "ocr.proto",
+}

+ 15 - 0
proto_src/ocr.proto

@@ -0,0 +1,15 @@
+syntax = "proto3";
+//OCR图像转文字服务
+package proto;
+
+message OcrRequest {
+  bytes image = 1;
+}
+
+message OcrResponse {
+    string message = 1;
+}
+
+service Ocr {
+  rpc Ocr(OcrRequest) returns (OcrResponse);
+}

+ 1 - 0
tools/README.md

@@ -0,0 +1 @@
+工具包

+ 2 - 0
tools/servicerdproxy/README.md

@@ -0,0 +1,2 @@
+服务代理<br/>
+为其他GRPC服务,代为注册到服务治理中心

+ 101 - 0
tools/servicerdproxy/main.go

@@ -0,0 +1,101 @@
+/**
+服务治理 服务提供者端代理工具
+代为注册服务,维护服务生命周期
+*/
+package main
+
+import (
+	"app.yhyue.com/BP/servicerd/proto"
+	"context"
+	"flag"
+	"fmt"
+	"google.golang.org/grpc"
+	"log"
+	"os"
+	"os/signal"
+	"time"
+)
+
+var (
+	rd_server       = flag.String("rd", "192.168.3.240:10021", "服务治理中心地址")
+	service_name    = flag.String("sn", "ocr", "服务名称")
+	service_ip      = flag.String("ip", "192.168.20.100", "服务提供者IP")
+	service_port    = flag.Int("port", 25400, "服务提供者端口")
+	service_workers = flag.Int("wks", 5, "服务提供者支持并发数")
+)
+
+var meta *proto.ServiceMeta
+
+func init() {
+	flag.Parse()
+	meta = &proto.ServiceMeta{
+		Name:    *service_name,
+		Ip:      *service_ip,
+		Port:    int32(*service_port),
+		Balance: 0,
+		Workers: int32(*service_workers),
+	}
+}
+
+//
+func heartbeat() {
+	conn, err := grpc.Dial(*rd_server, grpc.WithInsecure())
+	if err != nil {
+		return
+	}
+	defer conn.Close()
+	client := proto.NewHeartBeatClient(conn)
+	//调用服务端推送流
+	resp, _ := client.PutStream(context.Background())
+	tm := time.NewTicker(20 * time.Second)
+	for {
+		select {
+		case <-tm.C:
+			reqstreamData := &proto.StreamReqData{ServiceName: *service_name, ServiceAddr: fmt.Sprintf("%s:%d", *service_ip, *service_port)}
+			_ = resp.Send(reqstreamData)
+		}
+	}
+}
+
+//服务注册
+func registe() {
+	conn, err := grpc.Dial(*rd_server, grpc.WithInsecure())
+	if err != nil {
+		return
+	}
+	defer conn.Close()
+	client := proto.NewServiceClient(conn)
+	ret, err := client.Registe(context.TODO(), meta)
+	if err != nil {
+		log.Println(err.Error())
+	} else {
+		log.Println(ret)
+	}
+
+}
+
+//服务注销
+func destory() {
+	conn, err := grpc.Dial(*rd_server, grpc.WithInsecure())
+	if err != nil {
+		return
+	}
+	defer conn.Close()
+	client := proto.NewServiceClient(conn)
+	ret, err := client.Destory(context.TODO(), meta)
+	if err != nil {
+		log.Println(err.Error())
+	} else {
+		log.Println(ret)
+	}
+}
+
+func main() {
+	go heartbeat() //心跳
+	registe()
+	c := make(chan os.Signal, 1)
+	signal.Notify(c, os.Interrupt, os.Kill)
+	<-c
+	log.Println("服务注销")
+	destory()
+}

+ 0 - 0
sma/README.md → tools/sma/README.md


+ 0 - 0
sma/agent.go → tools/sma/agent.go