mservice

package
v0.0.0-...-0f09646 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 30, 2021 License: Apache-2.0 Imports: 21 Imported by: 1

README

mservice

server 由 grpc + webapi 组成

Grpc 注册

Grpc 会将Grpc方法进行注册

Webapi 注册

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	GRPC *grpc.ClientConn
	// contains filtered or unexported fields
}

GRPC Client

func NewClientEx

func NewClientEx(name string, cfg *ClientConfig) (*Client, error)

需要关注这个 name

func (*Client) Close

func (c *Client) Close()

type ClientConfig

type ClientConfig struct {
	Balancer balancer.Balancer
	Name     string
	Desc     *common.ServiceOpDesc // 这里拿到的是每个服务的 proto 文件中的配置信息
}

func DefaultClientConfig

func DefaultClientConfig() *ClientConfig

获取客户端默认配置

type GrpcRegister

type GrpcRegister interface {
	GetServer() *grpc.Server                       // 获取grpc's server
	RegisterService(name string, methods []string) // 注册服务及其提供的接口方法
}

GRPC 提供的注册

type GrpcServer

type GrpcServer struct {
	// contains filtered or unexported fields
}

GRPC 服务主体

func (*GrpcServer) Close

func (s *GrpcServer) Close() error

func (*GrpcServer) GetServer

func (s *GrpcServer) GetServer() *grpc.Server

func (*GrpcServer) GrpcAddInterceptor

func (s *GrpcServer) GrpcAddInterceptor(i grpc.UnaryServerInterceptor)

提供服务端拦截器 添加

func (*GrpcServer) RegisterService

func (s *GrpcServer) RegisterService(name string, methods []string)

往GRPC服务中添加待注册的service

func (*GrpcServer) Serve

func (s *GrpcServer) Serve(ln net.Listener)

启动GRPC服务

type HandlerFunc

type HandlerFunc func(ctx *context.T, w http.ResponseWriter, req *http.Request)

type Middleware

type Middleware func(HandlerFunc) HandlerFunc

中间件 - 本质就是一个func,将函数保存起来等到需要的时候对入参进行处理

type Options

type Options interface {
	GetServiceDesc(serviceName string) *common.ServiceOpDesc
	GetMethodDesc(service, method string) (*common.ServiceOpDesc, *common.MethodOpDesc)
	GetAllServiceDesc() []*common.ServiceOpDesc
}

type Server

type Server struct {
	Port int // 端口号

	GrpcServer
	WebServer
	// contains filtered or unexported fields
}

func (*Server) Close

func (s *Server) Close() error

关闭 GRPC + WebApi 服务

func (*Server) GetAllServiceDesc

func (s *Server) GetAllServiceDesc() []*common.ServiceOpDesc

func (*Server) GetMethodDesc

func (s *Server) GetMethodDesc(serviceName, methodName string) (*common.ServiceOpDesc, *common.MethodOpDesc)

func (*Server) GetServiceDesc

func (s *Server) GetServiceDesc(service string) *common.ServiceOpDesc

func (*Server) RegisterServiceDesc

func (s *Server) RegisterServiceDesc(op *common.ServiceOpDesc)

通过调用Register将服务的Desc注入进来

func (*Server) Run

func (s *Server) Run(ctx context.T) error

run GRPC and WebApi

type WebApiRegister

type WebApiRegister interface {
	WebApiRegister(method string, handler func(ctx *context.T, w http.ResponseWriter, req *http.Request))
	WebApiRegisterMethod(service, method string, handler func(ctx *context.T, w http.ResponseWriter, req *http.Request)) bool

	// 解码
	WebApiDecode(ctx *context.T, req *http.Request, arg interface{}) error
	WebApiHandleResp(*context.T, http.ResponseWriter, interface{}, error)
}

WebApi 注册

type WebServer

type WebServer struct {
	// contains filtered or unexported fields
}

WEB 服务 + 利用http的路由保存method,以供提供WebApi服务

func (*WebServer) AddMiddleware

func (s *WebServer) AddMiddleware(m ...Middleware)

func (*WebServer) Close

func (s *WebServer) Close() error

关闭服务

func (*WebServer) Ping

func (s *WebServer) Ping(w http.ResponseWriter, req *http.Request)

func (*WebServer) Serve

func (s *WebServer) Serve(ctx context.T, ln net.Listener) error

启动服务

func (*WebServer) WebApiDecode

func (s *WebServer) WebApiDecode(ctx *context.T, req *http.Request, arg interface{}) error

下面👇两个方法是涉及到 grpc 方法 《=》 webapi 相互转化问题

func (*WebServer) WebApiHandleResp

func (s *WebServer) WebApiHandleResp(ctx *context.T, w http.ResponseWriter, resp interface{}, err error)

将GRPC结果再转换为HTTP的返回结果

func (*WebServer) WebApiRegister

func (s *WebServer) WebApiRegister(method string, handler func(ctx *context.T, w http.ResponseWriter, req *http.Request))

注册方法

func (*WebServer) WebApiRegisterMethod

func (s *WebServer) WebApiRegisterMethod(serviceName, methodName string, handler func(ctx *context.T, w http.ResponseWriter, req *http.Request)) bool

func (*WebServer) WebapiHandlerFunc

func (s *WebServer) WebapiHandlerFunc(path string, handler HandlerFunc)

注册接口到路由中 - 具体实现

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL