README
¶
设计的初衷
由于gRPC入门的学习路径很陡峭,不容易入手。而且Go世界中并没有找到什么很趁手的纯grpc开发框架,所以就萌生了简化开发纯gRPC微服务的工具集的想法。
入门
gRPC服务端
- 定义Protobuf
- 构建Protobuf文件
- 实现 grpc service
- 启用 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 ¶
- func NewZipkinClientOption(name, addr string, logger *zap.Logger) (grpc.DialOption, error)
- func NewZipkinServerOption(name, addr string, logger *zap.Logger) (grpc.ServerOption, error)
- func ReadDataFromBody(w http.ResponseWriter, req *http.Request) (map[string]interface{}, error)
- type ClientRegisterFunc
- type Gateway
- type Middleware
- type Option
- func AllowHeaders(headers ...string) Option
- func AllowMethods(methods ...string) Option
- func AllowOrigins(origins ...string) Option
- func Listen(addr string) Option
- func Logger(logger *zap.Logger) Option
- func Name(name string) Option
- func Registry(registry registry.Registry) Option
- func TLS(certFile, keyFile string) Option
- func TTL(ttl int) Option
- func Trans(endpoints ...*config.EndPoint) Option
- func Version(version string) Option
- func WithConfig(conf *config.GatewayConfig) Option
- type Options
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewZipkinClientOption ¶
func NewZipkinServerOption ¶
func ReadDataFromBody ¶
Types ¶
type ClientRegisterFunc ¶
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 网关定义
type Option ¶
type Option func(*Options)
func AllowHeaders ¶
func AllowMethods ¶
func AllowOrigins ¶
func WithConfig ¶
func WithConfig(conf *config.GatewayConfig) Option
Click to show internal directories.
Click to hide internal directories.