跳转到主要内容

Polaris 服务治理

Polaris 模块在 Lynx 里是一个 control-plane 插件,把服务注册、服务发现、配置访问、watch 能力和治理能力集中到同一个 runtime 里。

Runtime 事实

项目
Go modulegithub.com/go-lynx/lynx-polaris
配置前缀lynx.polaris
Runtime 插件名polaris.control.plane
公开 APIGetPolarisPlugin()GetPolaris()GetServiceInstances()GetConfig(...)WatchService(...)WatchConfig(...)CheckRateLimit(...)GetMetrics()

实现提供了什么

从代码看,Polaris 支持:

  • 服务注册与发现
  • 配置加载与 config source 集成
  • 服务 watcher 和配置 watcher
  • 限流检查
  • 负载均衡和路由相关辅助能力
  • retry、circuit breaker、metrics、health check

它远不只是一个简单的注册中心适配器。

配置

lynx:
polaris:
namespace: svc-namespace
token: token
weight: 100
ttl: 5
timeout: 5s
enable_service_watch: true
enable_config_watch: true
enable_rate_limit: true

除此之外,插件通常还需要官方 Polaris SDK 侧配置文件,一般通过 config_path 指定连接器级别行为。

官方模板实际怎么配

官方模板不会在 bootstrap.local.yaml 里启用 Polaris。它会把本地启动先压小,再把 Polaris 放进 configs/bootstrap.yaml

lynx:
application:
name: user-service
version: v1.0.0

polaris:
config_path: "configs/polaris.yaml"
namespace: demo
token: "..."
weight: 100
ttl: 10
timeout: 5s

这也是为什么 Polaris 在模板里看起来会和 HTTP、Redis 不太一样。它是控制面插件,所以官方脚手架把它当成治理层能力,而不是最小本地运行时的一部分。

如何使用

plugin, err := polaris.GetPolarisPlugin()
instances, err := polaris.GetServiceInstances("user-service")
content, err := polaris.GetConfig("application.yaml", "DEFAULT_GROUP")
allowed, err := polaris.CheckRateLimit("user-service", labels)

相关页面