gateway

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2022 License: Apache-2.0 Imports: 27 Imported by: 0

README

设计的初衷

由于gRPC入门的学习路径很陡峭,不容易入手。而且Go世界中并没有找到什么很趁手的纯grpc开发框架,所以就萌生了简化开发纯gRPC微服务的工具集的想法。

入门

gRPC服务端
  1. 定义Protobuf
  2. 构建Protobuf文件
  3. 实现 grpc service
  4. 启用 MicroService

在Titan中只要构建 MicroService 就可以完成大量复杂的gRPC起动操作:

注册gRPC服务
package main

import (
	"context"
	"titan/auth/api/v1/authpb"
	"go-titan/boot"
	"go-titan/config"
	"titan/auth/auth"
)

func main() {
	factory := auth.NewAuthServiceFileFactory()
	authSvc := auth.NewAuthService(factory, "./cert.key")

	boot.NewMicroService(). 
		Register(&authpb.Auth_ServiceDesc, authSvc). 
		Start("127.0.0.1:8081") // 起动时设置服务地址
}

需要使用Register方法对每个gRPC生成类中的XXXDesc描述类及接口实现进行注册。

最后调用Start方法进行起动即可。

启用服务注册

Titan 内置支持etcd作为服务注册中心,直接设置etcd的服务地址就可以完成接入。

调用SetRegistry方法可以同时设置多个etcd的服务地址,代码如下所示:

boot.NewMicroService(). 
    Register(&authpb.Auth_ServiceDesc, authSvc).
    SetRegistry("127.0.0.1:5672","10.0.0.2:5672").
    Start("127.0.0.1:8081") 
链接跟踪

Titan 默认支持Zipkin服务进行链路跟踪,首先需要起动zipkin服务,然后通过OpenTracing方法指定Zipkin的服务地址与端口就可以起用链接跟踪。

boot.NewMicroService(). 
    Register(&authpb.Auth_ServiceDesc, authSvc).
    SetRegistry("127.0.0.1:5672","10.0.0.2:5672").
	OpenTracing("127.0.0.1:9411").
    Start("127.0.0.1:8081") 
兼容WebAPI的gRPC服务

为了可以方便地让您的微服务可以支持Http Restful API, MicroService内置了API网关,如果

boot.NewMicroService(). 
    Register(&authpb.Auth_ServiceDesc, authSvc).
    SetRegistry("127.0.0.1:5672","10.0.0.2:5672").
	OpenTracing("127.0.0.1:9411").
    WebAPI("127.0.0.1:8082").
    Transport(authpb.RegisterAuthHandlerFromEndpoint).
	Start("127.0.0.1:8081") 
package main

import (
	"context"
	"titan/auth/api/v1/authpb"
	"titan/catalog/api/v1/catalogpb"
	"go-titan/boot"
)

func main() {
	boot.NewMicroService("service-name").
		Register(pb.RegisterXXXServer, &YourServiceImpl{}).
		// Config(cfg). // 直接从对象中获取设置,则不需要执行以下的方法
		SetRegistry("10.0.0.1:5672"). // 设置服务发现注册中心
		OpenTracing("127.0.0.1:9411"). // 启用OpenTracing		SetOption(serverOption). // 设置gRPC服务器的起动选项
		ServeTLS(certFile, keyFile). // 启用安全访问
		WebAPI("127.0.0.1:8081"). // 启用WebAPI兼容接口
		CORS(corsCfn...). // 开启跨域设置
		Route("/custom_path", customHandler). // 附加路由
		//Config(&cfg).                         // 直接设置配置
		OnAuth(func(ctx context.Context) { // 服务验证Token
		}).
		Start("127.0.0.1:8080")
}
安全认证事件

OnAuth

网关
package main

import (
	"context"
	"titan/auth/api/v1/authpb"
	"titan/catalog/api/v1/catalogpb"
	"go-titan/boot"
)

func main() {
	boot.NewGateway(). // 代理gRPC服务(可选)
		Transport("auth-server", authpb.RegisterAuthHandlerFromEndpoint).
		Transport("internal-svr", catalogpb.RegisterCatalogHandlerFromEndpoint).
		Config(gatewayConfig).
		SetRegistry("10.0.0.1:5672"). // 设置注册中心
		SetOption(dialogOption). // 设置拨号选项
		OpenTracing(true). // 启用链路跟踪
		LoadBalancing(true). // 启用负载均衡
		ServeTLS("cert.rsa", "key.pub"). // 启用安全通信 SSH
		CORS(corsConfig). // 启用跨域访问
		Route("/auth", oAuthHandler). // 增加自定义路由
		OnAuth(func(ctx context.Context) { // 前置验证Token
		}).
		Start()
}
客户端连接器
package main

import "go-titan/boot"

func main() {

	conn := boot.NewServiceConnector("auth-server", []string{"10.0.0.1:5672"}).
		SetOption(dialogOption). // 设置拨号选项 
		OpenTracing(true). // 启用链路跟踪
		LoadBalancing(true). // 启用负载均衡
		ServeTLS("cert.key"). // 启用安全通信 SSH		
		Connect()

	client := pb.CreateAuthClient(conn)

}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewZipkinClientOption

func NewZipkinClientOption(name, addr string, logger *zap.Logger) (grpc.DialOption, error)

func NewZipkinServerOption

func NewZipkinServerOption(name, addr string, logger *zap.Logger) (grpc.ServerOption, error)

func ReadDataFromBody

func ReadDataFromBody(w http.ResponseWriter, req *http.Request) (map[string]interface{}, error)

Types

type ClientRegisterFunc

type ClientRegisterFunc func(context.Context, *runtime.ServeMux, string, []grpc.DialOption) (err error)

type Gateway

type Gateway interface {
	// Options 返回网关配置
	Options() *Options
	// Use 添加中间件实例
	Use(middlewares ...Middleware) Gateway
	// Handle 添加Http处理器
	Handle(method, pattern string, handler runtime.HandlerFunc) Gateway
	// Transport 向网关注册处理器方法
	Transport(serverName string, registerFunc ...ClientRegisterFunc) Gateway
	// Start 起动网关
	Start()
}

Gateway 网关定义

func New

func New(opts ...Option) Gateway

type Middleware

type Middleware func(http.Handler) http.Handler

type Option

type Option func(*Options)

func AllowHeaders

func AllowHeaders(headers ...string) Option

func AllowMethods

func AllowMethods(methods ...string) Option

func AllowOrigins

func AllowOrigins(origins ...string) Option

func Listen

func Listen(addr string) Option

func Logger

func Logger(logger *zap.Logger) Option

func Name

func Name(name string) Option

Name 设置微服务的唯一服务名称

func Registry

func Registry(registry registry.Registry) Option

func TLS

func TLS(certFile, keyFile string) Option

func TTL

func TTL(ttl int) Option

func Trans

func Trans(endpoints ...*config.EndPoint) Option

func Version

func Version(version string) Option

Version 设置微服务的运行版本 默认为 "latest"

func WithConfig

func WithConfig(conf *config.GatewayConfig) Option

type Options

type Options struct {
	ServiceDesc *runtime.ServiceDesc
	Transports  []*config.EndPoint
	Registry    registry.Registry // 注册中心
	Logger      *zap.Logger       // 日志
	CORS        *config.CORSConfig
}

Options 微服务运行期设置选项

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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