1C企业的公共接口定义

339 阅读3分钟

(#proto)原形

该资源库包含Protobuf的文件(合同和服务描述),用于为支持Protobuf的编程语言自动生成类/结构。

](#глоссарий)词汇表

  1. 合同 - 文件protomessage ,这是对象/类/结构的协议。
  2. 服务 - 在文件proto ,这是service ,这是关于grpc 服务及其输入/输出方法的协议。
  3. 服务方法 - 在proto ,它是rpc ,这是关于服务方法及其投入/产出合同的协议。

](#структура-репозитория)存储库结构

  • draft - 包含.proto文件的原型/草稿
  • gen - 包含自动生成的目录和文件*.pb.go ,作为模块插入到其他项目中,在golang
  • rasapies - 包含服务的软件包描述文件ras
    • protocol/v1 - 包含与协议一起工作所需的合同。ras
    • messages/v1 - 包含用于协议工作的消息合同,endpoint ras
    • encoding - 包含合同/字段的辅助选项和文件.proto ,用于在1C二进制协议中自动生成encode/decode 。为了实现自动生成,你需要создать 插件,用于protoc
    • api/v1 - 包含合同和服务ras

这规定了为与ras (无论何种语言)合作而创建的服务将根据其合同工作。

THIS 允许你为任何语言自动创建一个客户端和服务器模板

  • serializeapis - 包含描述1C平台通用的合同的文件
    • v8platform/serialize/v1 - 包含通用的第一版合同
  • third_party - 包含可插入的外部合同的文件。

](#как-использовать)如何使用

首先应了解以下情况。

  1. protoc 是一个适用于.proto 文件的编译器,但要使其发挥作用,必须为每种语言安装不同的插件。 例如,对于golang - 它被称为protoc-gen-go

  2. buf 是一个编译器插件应用程序,用于.proto,protoc ,它使用反向配置编译文件。

  3. 在你的机器上安装buf

go install github.com/bufbuild/buf/cmd/buf # required
go install github.com/bufbuild/buf/cmd/protoc-gen-buf-breaking
go install github.com/bufbuild/buf/cmd/protoc-gen-buf-lint
  1. 安装protobuf和插件
go install google.golang.org/protobuf/cmd/protoc-gen-go
go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc
go install github.com/srikrsna/protoc-gen-gotag
  1. 添加到你的项目文件buf.gen.yaml ,以生成*.pb.go
---
version: v1
managed:
  enabled: true
  go_package_prefix:
    default: github.com/your-name/repo-name/gen/go
    except:
      - buf.build/googleapis/googleapis
      - googleapies/google/api

plugins:
  - name: go
    out: ./gen/go
    opt: paths=source_relative
  - name: go-grpc
    out: ./gen/go
    opt: paths=source_relative,require_unimplemented_servers=false
  #  - name: grpc-gateway
  #    out: ./gen/go
  #    opt:
  #      - paths=source_relative
  #      - generate_unbound_methods=true
  #      - grpc_api_configuration=grpc-rest-bindings.yml
  - name: gotag
    out: .
    opt:
      - paths=source_relative
      - outdir=./gen/go

  1. 创建文件grpc-rest-bindings.yaml ,生成grpc-gateway
---
---
type: google.api.Service
config_version: 3

name: ras.api.v1
title: RAS API v1

apis:
  - name: ClustersService
  - mane: ClusterAdminService
  - mane: InfobasesService
http:
  rules:
    # Clusters
    - selector: ras.api.v1.ClustersService.Clusters
      get: '/clusters'
    - selector: ras.api.v1.ClustersService.GetCluster
      get: '/clusters/{cluster.id}'
    - selector: ras.api.v1.ClustersService.AddCluster
      post: '/clusters'
      body: '*'
    - selector: ras.api.v1.ClustersService.DeleteCluster
      delete: '/clusters/{cluster.id}'

    # Cluster admin
    - selector: ras.api.v1.ClusterAdminService.Admins
      get: '/clusters/{cluster.id}/admins'
      additional_bindings:
        - get: "/admins"
    - selector: ras.api.v1.ClusterAdminService.AddAdmin
      post: '/clusters/{cluster.id}/admins'
      body: 'admin_info'
      additional_bindings:
        - post: "/admins"
          body: 'admin_info'
    - selector: ras.api.v1.ClusterAdminService.DeleteAdmin
      delete: '/clusters/{cluster.id}/admins/{admin_name}'
      additional_bindings:
        - delete: "/admins/{admin_name}"

    # Infobases
    - selector: ras.api.v1.InfobasesService.Infobases
      get: '/infobases'
      additional_bindings:
        - get: '/clusters/{cluster.id}/infobases'
    - selector: ras.api.v1.InfobasesService.LookupInfobase
      get: "/infobases/lookup"
      additional_bindings:
        - get: '/clusters/{cluster.id}/infobases/lookup'

  1. 运行生成命令
buf generate https://github.com/v8platform/protos.git
  1. 对你的客户端或服务器文件进行编码
  • 文件为服务器Server/main.go
package main

import (
	"context"
	"fmt"
	"log"
	"net"

	// This import path is based on the name declaration in the go.mod,
	// and the gen/proto/go output location in the buf.gen.yaml.
	rasv1 "github.com/your-name/repo-name/gen/go/ras/api/v1"
	serializev1 "github.com/your-name/repo-name/gen/go/v8platform/protocol/v1"

	"google.golang.org/grpc"
	codes "google.golang.org/grpc/codes"
	status "google.golang.org/grpc/status"
	emptypb "google.golang.org/protobuf/types/known/emptypb"
)

func main() {
	if err := run(); err != nil {
		log.Fatal(err)
	}
}

func run() error {
	listenOn := "127.0.0.1:8080"
	listener, err := net.Listen("tcp", listenOn)
	if err != nil {
		return fmt.Errorf("failed to listen on %s: %w", listenOn, err)
	}

	server := grpc.NewServer()
	rasv1.RegisterClustersServiceServer(server, &rasClusterServiceServer{})
	log.Println("Listening on", listenOn)
	if err := server.Serve(listener); err != nil {
		return fmt.Errorf("failed to serve gRPC server: %w", err)
	}

	return nil
}

// petStoreServiceServer implements the PetStoreService API.
type rasClusterServiceServer struct {
	rasv1.UnimplementedClustersServiceServer
}

func (rasClusterServiceServer) Clusters(ctx context.Context, req *rasv1.GetClustersRequest) (*rasv1.GetClustersResponse, error) {
	return nil, status.Errorf(codes.Unimplemented, "method Clusters not implemented")
}
func (rasClusterServiceServer) GetCluster(ctx context.Context, req *rasv1.GetClusterRequest) (*serializev1.ClusterInfo, error) {
	return nil, status.Errorf(codes.Unimplemented, "method GetCluster not implemented")
}
func (rasClusterServiceServer) AddCluster(ctx context.Context, req *rasv1.AddClusterRequest) (*rasv1.AddClusterResponse, error) {
	return nil, status.Errorf(codes.Unimplemented, "method AddCluster not implemented")
}
func (rasClusterServiceServer) DeleteCluster(ctx context.Context, req *rasv1.DeleteClusterRequest) (*emptypb.Empty, error) {
	return nil, status.Errorf(codes.Unimplemented, "method DeleteCluster not implemented")
}
  • 为客户提供的文件client/main.go
package main

import (
	"context"
	"fmt"
	"log"

	// This import path is based on the name declaration in the go.mod,
	// and the gen/proto/go output location in the buf.gen.yaml.
	rasv1 "github.com/your-name/repo-name/gen/go/ras/api/v1"
	"google.golang.org/grpc"
)

func main() {
	if err := run(); err != nil {
		log.Fatal(err)
	}
}
func run() error {
	connectTo := "127.0.0.1:8080"
	conn, err := grpc.Dial(connectTo, grpc.WithBlock(), grpc.WithInsecure())
	if err != nil {
		return fmt.Errorf("failed to connect to ClustersService on %s: %w", connectTo, err)
	}
	log.Println("Connected to", connectTo)

	clusterService := rasv1.NewClustersServiceClient(conn)
	resp, err := clusterService.Clusters(context.Background(), &rasv1.GetClustersRequest{})
	if err != nil {
		return fmt.Errorf("failed to Clusters: %w", err)
	}

	log.Printf("Successfully Clusters %s", resp.String())
	return nil
}
  1. 可以运行了

GitHub

github.com/v8platform/…