123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- package grpc
- import (
- "kpt-grpc-demo/config"
- "kpt-grpc-demo/pkg/ops"
- opsPb "kpt-grpc-demo/proto/go/backend/ops"
- "kpt-grpc-demo/util/grpc/server"
- "go.uber.org/dig"
- "google.golang.org/grpc"
- "google.golang.org/grpc/reflection"
- )
- type Dependency struct {
- dig.In
- // 依赖
- ops.Options
- }
- type Servlet struct {
- server *server.Servlet
- Dep *Dependency
- opsPb.UnimplementedFieldServiceServer
- }
- // Run start run service
- func (s *Servlet) Run() {
- s.server.Run()
- }
- func (s *Servlet) Close() error {
- return s.server.Close()
- }
- // New Servlet impl gRPC
- func New(cfg *config.AppConfig, dep Dependency) *Servlet {
- // 允许 bangla 作为 grpc server 时,可以向客户端传输20m的数据
- maxSize := 20 * 1024 * 1024
- srv := server.New(&server.Config{
- ServerEnv: cfg.AppEnv,
- ServerName: cfg.AppName,
- ServerHost: cfg.GRPCServerAddr,
- MetricsHost: cfg.GRPCMetricsAddr,
- }, grpc.MaxRecvMsgSize(maxSize), grpc.MaxSendMsgSize(maxSize))
- servlet := &Servlet{
- server: srv,
- Dep: &dep,
- }
- opsPb.RegisterFieldServiceServer(srv.Server(), servlet)
- reflection.Register(srv.Server())
- return servlet
- }
|