Gizmo

一个来自纽约时报的微服务工具包。(A Microservice Toolkit from The New York Times.)

Github stars Tracking Chart

Gizmo 微服务工具包

此工具包提供了将服务器和 pubsub 守护程序组合在一起的软件包,具有以下功能:

  • 标准化配置和日志
  • 带有可配置策略的健康检查端点
  • 用于管理 pprof 端点和日志级别的配置
  • 定义期望值(expectations)和词汇表(vocabulary)的基本接口
  • 包含基本请求信息的结构化日志记录
  • 端点的有用度量
  • 优雅的关闭

安装

注意大写!

go get github.com/NYTimes/gizmo/...

导入路径变更通知

《纽约时报》最近将 github 组织从"NYTimes"改为"nytimes"。这应该不会影响安装,只要您使用正确的包 NYTimes,而不是按照上面的安装说明 nytimes。

然而,其目的是迁移导入路径,使其与在 GitHub 上显示的一致。这将是一个突破性的更改,我们将在更新代码时引入一个主要标记。因此,导入路径将从 github.com/NYTimes/gizmo/server 转到 github.com/nytimes/gizmo/v2/server。这确保人们不会在导入路径更改之间存在类型不匹配。

server

server 包是工具包的主要部分, 服务器依赖于 server.Config 来管理 Server 实现。

它提供1个服务器实现:

SimpleServer,它能够通过5个可用的 Service 实现处理基本的HTTP和JSON请求:SimpleService, JSONService, ContextService, MixedService 和一个 MixedContextService

server/kit

server/kit 包体现了 Gizmo 与 go-kit 结合的目标。

  • 在这个包中你会发现:
    • 一个更有主见、选择更少的服务器。
    • go-kit 用于服务 HTTP/JSON;gRPC 用于服务 HTTP2/RPC
    • 如果在App Engine、Kubernetes Engine、Compute Engine 或 AWS EC2 实例中运行,监视、跟踪和度量将自动注册。
      • 要更改错误报告和跟踪的名称和版本,请使用 SERVICE_NAMESERVICE_VERSION 环境变量。
    • 在 GCP 中,日志转到本地 stdout 或直接转到 Stackdriver。
    • 使用 Go 的 1.8 优雅 HTTP 关闭。
    • 使用此软件包的服务预计将部署到 GCP。

observe

observe 包为度量提供可观察性帮助,并通过 OpenCensus 进行跟踪

  • server/kit (很快就是 SimpleServer)利用这个包创建一个具有合理默认值的 StackDriver 导出器
  • GoogleProjectIDIsGAE 可以帮助您决定底层平台

auth

auth 包提供了验证入站身份验证令牌的原语

  • PublicKeySource 接口用于从 JSON Web Key Sets 提供 *rsa.PublicKeys
  • Verifier 结构使用自定义解码器和验证器函数组合关键源实现,以简化服务器端令牌验证。

auth/gcp

auth/gcp 包提供了2个基于谷歌云平台 auth.PublicKeySourceoauth2.TokenSource 实现:

  • "Identity" 标识密钥源和令牌源依赖于 GCP 的 identity JWT mechanism for asserting instance identities。这是在 GCP 上断言实例标识的首选方法。
  • "IAM" 密钥源和令牌源依赖 GCP 的 IAM 服务来 signingverifying JWTs。如果需要,这个方法可以在 GCP 之外使用,并且可以为用户从第一代 App Engine(其中身份令牌不可用)运行时过渡到第2代 App Engine 提供桥梁。

config

config 包包含一些有用的函数,可以从 JSON 文件或环境变量加载到配置结构。

还有许多用于不同云服务和数据库的公共配置选项和凭据的结构。

pubsub

pubsub 包中包含两个 (publishersubscriber) 通用接口,用于将数据发布到队列以及订阅和使用这些队列中的数据。

pubsub 接口有4种实现方式:

  • 对于通过亚马逊的 SNS/SQS 的 pubsub,您可以使用 pubsub/aws 包;
  • 对于通过 Google 的 Pubsub,您可以使用 pubsub/gcp 包;
  • 对于通过 Kafka topics的 Pubsub,您可以使用 pubsub/kafka 包;
  • 要通过 HTTP 发布,您可以使用 pubsub/http 包;

pubsub/pubsubtest

pubsub/pubsubtest 包包含 pubsub.Publisherpubsub.MultiPublisher 接口的测试实现,并且 pubsub.Subscriber 接口允许开发人员轻松地模拟和测试他们的 pubsub 实现。

示例

  • examples 子目录中有几个使用 serverpubsub 的参考实现。
  • GoDoc中也有一些例子:在这里

如果您遇到任何问题,请创建一个问题,并在 Gophers Slack Workspace 工作空间的 #gizmo 频道上找到您的发现。

Gizmo 徽标基于 RenéeFrench 设计的 Go 吉祥物,并根据 Creative Commons Attribution 3.0 许可证拥有版权。

(First edition: vz edited at 2019.09.075)

Overview

Name With Ownernytimes/gizmo
Primary LanguageGo
Program languageGo (Language Count: 3)
PlatformAmazon Elastic Compute Cloud (EC2), Google App Engine, Google Cloud Platform, Kubernetes, Linux, Mac, Google Compute Engine
License:Apache License 2.0
Release Count54
Last Release Namev1.3.6 (Posted on )
First Release Namev0.1.0 (Posted on )
Created At2015-12-15 18:09:36
Pushed At2023-07-13 14:31:44
Last Commit At2021-04-30 11:27:05
Stargazers Count3.8k
Watchers Count115
Fork Count226
Commits Count439
Has Issues Enabled
Issues Count72
Issue Open Count16
Pull Requests Count216
Pull Requests Open Count17
Pull Requests Close Count435
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private

Gizmo Microservice Toolkit GoDoc Build Status Coverage Status

This toolkit provides packages to put together server and pubsub daemons with the following features:

  • Standardized configuration and logging
  • Health check endpoints with configurable strategies
  • Configuration for managing pprof endpoints and log levels
  • Basic interfaces to define expectations and vocabulary
  • Structured logging containing basic request information
  • Useful metrics for endpoints
  • Graceful shutdowns

Install

Notice the capitalization!

go get github.com/NYTimes/gizmo/...

Import Path Change Notice

The New York Times recently changed the github organization from NYTimes to nytimes. This should not affect the installation as long as you use the proper casing NYTimes and not nytimes per installation instructions above.

However, the intention is to migrate the import paths to be consistent with how it's shown on GitHub. This will be a breaking change and we will introduce a major tag when we update the code. Therefore, the import path will go from github.com/NYTimes/gizmo/server to github.com/nytimes/gizmo/v2/server. This ensures that people will not have type-mismatches between import path changes.

Packages

server

The server package is the bulk of the toolkit and relies on server.Config to manage Server implementations.

It offers 1 server implementation:

SimpleServer, which is capable of handling basic HTTP and JSON requests via 5 of the available Service implementations: SimpleService, JSONService, ContextService, MixedService and a MixedContextService.

server/kit

The server/kit package embodies Gizmo's goals to combine with go-kit.

  • In this package you'll find:
    • A more opinionated server with fewer choices.
    • go-kit used for serving HTTP/JSON & gRPC used for serving HTTP2/RPC
    • Monitoring, traces and metrics are automatically registered if running within App Engine, Cloud Run, Kubernetes Engine, Compute Engine or AWS EC2 Instances.
      • to change the name and version for Error reporting and Traces use SERVICE_NAME and SERVICE_VERSION environment variables.
    • Logs go to stdout locally or directly to Stackdriver when in GCP.
    • Using Go's 1.8 graceful HTTP shutdown.
    • Services using this package are expected to deploy to GCP.

observe

The observe package provides observability helpers for metrics and tracing through OpenCensus

  • server/kit (and soon SimpleServer) utilizes this package to create a StackDriver exporter with sane defaults
  • GoogleProjectID, IsGAE, and IsCloudRun can help you make decisions about the underlying platform

auth

The auth package provides primitives for verifying inbound authentication tokens:

  • The PublicKeySource interface is meant to provide *rsa.PublicKeys from JSON Web Key Sets.
  • The Verifier struct composes key source implementations with custom decoders and verifier functions to streamline server side token verification.

auth/gcp

The auth/gcp package provides 2 Google Cloud Platform based auth.PublicKeySource and oauth2.TokenSource implementations:

  • The "Identity" key source and token source rely on GCP's identity JWT mechanism for asserting instance identities. This is the preferred method for asserting instance identity on GCP.
  • The "IAM" key source and token source rely on GCP's IAM services for signing and verifying JWTs. This method can be used outside of GCP, if needed and can provide a bridge for users transitioning from the 1st generation App Engine (where Identity tokens are not available) runtime to the 2nd.

The auth/gcp package also includes an Authenticator, which encapsulates a Google Identity verifier and oauth2 credentials to manage a basic web auth flow.

config

The config package contains a handful of useful functions to load to configuration structs from JSON files or environment variables.

There are also many structs for common configuration options and credentials of different Cloud Services and Databases.

pubsub

The pubsub package contains two (publisher and subscriber) generic interfaces for publishing data to queues as well as subscribing and consuming data from those queues.

There are 4 implementations of pubsub interfaces:

  • For pubsub via Amazon's SNS/SQS, you can use the pubsub/aws package

  • For pubsub via Google's Pubsub, you can use the pubsub/gcp package

  • For pubsub via Kafka topics, you can use the pubsub/kafka package

  • For publishing via HTTP, you can use the pubsub/http package

pubsub/pubsubtest

The pubsub/pubsubtest package contains test implementations of the pubsub.Publisher, pubsub.MultiPublisher, and pubsub.Subscriber interfaces that will allow developers to easily mock out and test their pubsub implementations.

Examples

  • Several reference implementations utilizing server and pubsub are available in the examples subdirectory.
  • There are also examples within the GoDoc: here

If you experience any issues please create an issue and/or reach out on the #gizmo channel in the Gophers Slack Workspace with what you've found.

The Gizmo logo was based on the Go mascot designed by Renée French and copyrighted under the Creative Commons Attribution 3.0 license.

To the top