治理平台 Polaris | 青训营笔记

127 阅读3分钟

这是我参与「第五届青训营」伴学笔记创作活动的第 4 天

治理平台

提供一站式的微服务治理,包括服务注册与发现、配置管理、服务网关、APM、日志分析、智能运维等功能,致力于帮助企业快速构建高扩展、低成本、高性能的分布式系统的同时,通过立体式的监控运维体系,确保企业微服务安全、稳定运行。

Polaris

简介

北极星是腾讯开源的服务治理平台,致力于解决分布式和微服务架构中的服务管理、流量管理、配置管理、故障容错和可观测性问题,针对不同的技术栈和环境提供服务治理的标准方案和最佳实践。下面介绍北极星的应用场景、功能特性、系统组件和常见问题。

微服务架构

对于中型以上规模的业务系统来说,更适合采用分布式服务架构,例如:微信支付这种超大型系统涉及上百个功能模块和上千名研发人员,采用单体架构简直是个灾难。因此,需要将不同的功能拆成分布式服务,每个服务由少量研发人员独立维护。

分布式服务可以解决单体架构的问题,但是用起来并不容易,需要配套的基础设施:

  • 本地函数调用变成远程服务调用,需要高效的网络请求框架。
  • 网络请求需要解决服务寻址、故障容错和各种应用场景中的流量调度问题。
  • 应用数量大幅增加,需要 CICD 流水线和发布平台简化应用的测试和运维工作。

微服务和分布式服务没有本质的区别,微服务更强调服务的粒度要细。在落地过程中,服务的粒度其实没有绝对的标准,要从实际问题出发选择合理方案,不要盲目追求微服务。

北极星致力于打造一个支持多语言、多框架的服务治理平台,帮助用户解决分布式服务或者微服务架构中的服务管理、流量管理、配置管理、故障容错和可观测性问题。

使用

我们可以通过 CloudWeGo 开源框架的中间件对其进行使用。

Server

import (
	"context"
	"log"
	"time"

	"github.com/cloudwego/hertz/pkg/app"
	"github.com/cloudwego/hertz/pkg/app/server"
	"github.com/cloudwego/hertz/pkg/app/server/registry"
	"github.com/cloudwego/hertz/pkg/common/utils"
	"github.com/cloudwego/hertz/pkg/protocol/consts"
	"github.com/hertz-contrib/registry/polaris"
)

const (
	confPath  = "polaris.yaml"
	Namespace = "Polaris"
	// At present,polaris server tag is v1.4.0,can't support auto create namespace,
	// If you want to use a namespace other than default,Polaris ,before you register an instance,
	// you should create the namespace at polaris console first.
)

func main() {
	r, err := polaris.NewPolarisRegistry(confPath)

	if err != nil {
		log.Fatal(err)
	}

	Info := &registry.Info{
		ServiceName: "hertz.test.demo",
		Addr:        utils.NewNetAddr("tcp", "127.0.0.1:8888"),
		Tags: map[string]string{
			"namespace": Namespace,
		},
	}
	h := server.Default(server.WithRegistry(r, Info), server.WithExitWaitTime(10*time.Second))

	h.GET("/hello", func(ctx context.Context, c *app.RequestContext) {
		c.String(consts.StatusOK, "Hello,Hertz!")
	})

	h.Spin()
}

Client

import (
	"context"
	"log"

	hclient "github.com/cloudwego/hertz/pkg/app/client"
	"github.com/cloudwego/hertz/pkg/app/middlewares/client/sd"
	"github.com/cloudwego/hertz/pkg/common/config"
	"github.com/cloudwego/hertz/pkg/common/hlog"
	"github.com/hertz-contrib/registry/polaris"
)

const (
	confPath  = "polaris.yaml"
	Namespace = "Polaris"
	// At present,polaris server tag is v1.4.0,can't support auto create namespace,
	// if you want to use a namespace other than default,Polaris ,before you register an instance,
	// you should create the namespace at polaris console first.
)

func main() {
	r, err := polaris.NewPolarisResolver(confPath)
	if err != nil {
		log.Fatal(err)
	}

	client, err := hclient.NewClient()
	client.Use(sd.Discovery(r))

	for i := 0; i < 10; i++ {
		// config.WithTag sets the namespace tag for service discovery
		status, body, err := client.Get(context.TODO(), nil, "http://hertz.test.demo/hello", config.WithSD(true), config.WithTag("namespace", Namespace))
		if err != nil {
			hlog.Fatal(err)
		}
		hlog.Infof("code=%d,body=%s\n", status, body)
	}
}

参考

polarismesh.cn/docs

github.com/hertz-contr…