You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
70 lines
1.6 KiB
70 lines
1.6 KiB
4 years ago
|
package main
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"fmt"
|
||
|
"log"
|
||
|
|
||
|
pb "github.com/go-kratos/kratos/examples/helloworld/helloworld"
|
||
|
"github.com/go-kratos/kratos/v2"
|
||
|
"github.com/go-kratos/kratos/v2/transport/grpc"
|
||
|
"github.com/go-kratos/nacos/registry"
|
||
|
"github.com/nacos-group/nacos-sdk-go/clients"
|
||
|
"github.com/nacos-group/nacos-sdk-go/common/constant"
|
||
|
"github.com/nacos-group/nacos-sdk-go/vo"
|
||
|
)
|
||
|
|
||
|
// server is used to implement helloworld.GreeterServer.
|
||
|
type server struct {
|
||
|
pb.UnimplementedGreeterServer
|
||
|
}
|
||
|
|
||
|
// SayHello implements helloworld.GreeterServer
|
||
|
func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
|
||
|
return &pb.HelloReply{Message: fmt.Sprintf("Welcome %+v!", in.Name)}, nil
|
||
|
}
|
||
|
|
||
|
func main() {
|
||
|
grpcSrv := grpc.NewServer(
|
||
|
grpc.Address(":9109"),
|
||
|
)
|
||
|
|
||
|
s := &server{}
|
||
|
pb.RegisterGreeterServer(grpcSrv, s)
|
||
|
|
||
|
sc := []constant.ServerConfig{
|
||
|
*constant.NewServerConfig("127.0.0.1", 8848),
|
||
|
}
|
||
|
|
||
|
cc := constant.ClientConfig{
|
||
|
NamespaceId: "public", //namespace id
|
||
|
TimeoutMs: 5000,
|
||
|
NotLoadCacheAtStart: true,
|
||
|
LogDir: "/tmp/nacos/log",
|
||
|
CacheDir: "/tmp/nacos/cache",
|
||
|
RotateTime: "1h",
|
||
|
MaxAge: 3,
|
||
|
LogLevel: "debug",
|
||
|
}
|
||
|
// a more graceful way to create naming client
|
||
|
client, err := clients.NewNamingClient(
|
||
|
vo.NacosClientParam{
|
||
|
ClientConfig: &cc,
|
||
|
ServerConfigs: sc,
|
||
|
},
|
||
|
)
|
||
|
if err != nil {
|
||
|
log.Panic(err)
|
||
|
}
|
||
|
r := registry.New(client)
|
||
|
app := kratos.New(
|
||
|
kratos.Name("helloworld"),
|
||
|
kratos.Server(grpcSrv),
|
||
|
kratos.Registrar(r),
|
||
|
)
|
||
|
|
||
|
if err := app.Run(); err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
}
|