grpc

Provide gRPC services externally by exposing gRPC services

Detailed examples referenceherearrow-up-right

Configuration

Exceptpublic configurationadd the following configuration

asjard:
  servers:
    grpc:
      options:
        maxConnectionIdle: 5m
        maxConnectionAge: 0s
        maxConnectionAgeGrace: 0s
        time: 10s
        timeout: 1s

example

writeprotobuffile

implement

package apiv1

import (
	"context"

	pb "protos-repo/example/api/v1/sample"

	"github.com/asjard/asjard/pkg/server/grpc"
	"github.com/asjard/asjard/pkg/server/rest"
)

type SampleAPI struct {
	pb.UnimplementedSampleServer
}

func NewSampleAPI() *SampleAPI {
	return &SampleAPI{}
}

func (api *SampleAPI) Start() error { return nil }
func (api *SampleAPI) Stop()        {}

// GRPC service
func (api *SampleAPI) GrpcServiceDesc() *grpc.ServiceDesc { return &pb.Sample_ServiceDesc }

// HTTP service
func (api *SampleAPI) RestServiceDesc() *rest.ServiceDesc { return &pb.SampleRestServiceDesc }

func (api *SampleAPI) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
	return &pb.HelloReply{
		Message: "hello " + in.Name,
	}, nil
}

Last updated