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.
go get -u github.com/kitex-contrib/registry-polaris
import (
...
"context"
"log"
"net"
"github.com/cloudwego/kitex/pkg/registry"
"github.com/polarismesh/polaris-go/pkg/config"
"github.com/cloudwego/kitex-examples/hello/kitex_gen/api"
"github.com/cloudwego/kitex-examples/hello/kitex_gen/api/hello"
"github.com/cloudwego/kitex/server"
polaris "github.com/kitex-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 := ®istry.Info{
ServiceName: "echo",
Tags: map[string]string{
"namespace": Namespace,
},
}
// https://www.cloudwego.io/docs/kitex/tutorials/framework-exten/service_discovery/#usage-example
newServer := hello.NewServer(new(HelloImpl), server.WithRegistry(r), server.WithRegistryInfo(Info),
server.WithServiceAddr(&net.TCPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 8888}))
err = newServer.Run()
if err != nil {
log.Fatal(err)
}
...
}
import (
...
"context"
"log"
"time"
"github.com/cloudwego/kitex-examples/hello/kitex_gen/api"
"github.com/cloudwego/kitex-examples/hello/kitex_gen/api/hello"
"github.com/cloudwego/kitex/client"
polaris "github.com/kitex-contrib/registry-polaris"
"github.com/polarismesh/polaris-go/pkg/config"
...
)
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)
}
// https://www.cloudwego.io/docs/kitex/tutorials/framework-exten/service_discovery/#usage-example
// client.WithTag sets the namespace tag for service discovery
newClient := hello.MustNewClient("echo", client.WithTag("namespace", Namespace),
client.WithResolver(r), client.WithRPCTimeout(time.Second*60))
ctx, cancel := context.WithTimeout(context.Background(), time.Second*60)
resp, err := newClient.Echo(ctx, &api.Request{Message: "Hi,polaris!"})
cancel()
if err != nil {
log.Fatal(err)
}
log.Println(resp)
...
}
}
Polaris support stand-alone and cluster. More information can be found in install polaris
Welcome to contribute your ideas
See example and test
Compatible with polaris.
maintained by: liu-song