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() {
...
Conf, err := config.LoadConfigurationByFile(confPath)
if err != nil {
log.Fatal(err)
}
polarisAddresses := Conf.Global.ServerConnector.Addresses
r, err := polaris.NewPolarisRegistry(polarisAddresses)
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() {
...
Conf, err := config.LoadConfigurationByFile(confPath)
if err != nil {
log.Fatal(err)
}
polarisAddresses := Conf.Global.ServerConnector.Addresses
r, err := polaris.NewPolarisResolver(polarisAddresses)
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