polaris

package module
v0.0.0-...-11c3cac Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2024 License: Apache-2.0 Imports: 14 Imported by: 2

README

registry-polaris (This is a community driven project)

Some application runtime use polaris for service discovery. Polaris is a cloud-native service discovery and governance center. It can be used to solve the problem of service connection, fault tolerance, traffic control and secure in distributed and microservice architecture.

How to install registry-polaris?

go get -u github.com/hertz-contrib/registry/polaris

How to use with Hertz 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()
}

How to use with Hertz 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)
	}
}

How to install polaris?

Polaris support stand-alone and cluster. More information can be found in install polaris

Todolist

Welcome to contribute your ideas

Use polaris with Hertz

See example

example

Prepare
make prepare
Run server
go run example/server/main.go
Run client
go run example/client/main.go

Compatibility

Compatible with polaris (v1.4.0 - v1.10.0), latest stable version is recommended. If you want to use other server version, please modify the version in Makefile to test.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ChangePolarisInstanceToHertz

func ChangePolarisInstanceToHertz(PolarisInstance model.Instance) discovery.Instance

ChangePolarisInstanceToHertz transforms polaris instance to Hertz instance.

func GetInfoHostAndPort

func GetInfoHostAndPort(Addr string) (string, int, error)

GetInfoHostAndPort gets Host and port from info.Addr.

func GetInstanceKey

func GetInstanceKey(namespace, serviceName, host, port string) string

GetInstanceKey generates instanceKey for one instance.

func GetPolarisConfig

func GetPolarisConfig(configFile ...string) (api.SDKContext, error)

GetPolarisConfig get polaris config from endpoints.

func SplitDescription

func SplitDescription(description string) (string, string)

SplitDescription splits description to namespace and serviceName.

Types

type Registry

type Registry interface {
	registry.Registry
	// contains filtered or unexported methods
}

Registry is extension interface of Hertz registry.Registry.

func NewPolarisRegistry

func NewPolarisRegistry(configFile ...string) (Registry, error)

NewPolarisRegistry creates a polaris based registry.

type Resolver

type Resolver interface {
	discovery.Resolver
}

Resolver is extension interface of Hertz discovery.Resolver.

func NewPolarisResolver

func NewPolarisResolver(configFile ...string) (Resolver, error)

NewPolarisResolver creates a polaris based resolver.

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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