1
0
mirror of https://github.com/go-kratos/kratos.git synced 2026-05-22 10:15:24 +02:00
Files
kratos/examples/registry/consul/server/main.go
T

64 lines
1.4 KiB
Go
Raw Normal View History

2021-03-09 22:02:18 +08:00
package main
import (
"context"
"fmt"
2021-06-12 20:20:18 +08:00
"log"
2021-03-09 22:02:18 +08:00
2021-06-12 20:20:18 +08:00
"github.com/go-kratos/kratos/examples/helloworld/helloworld"
"github.com/go-kratos/kratos/registry/consul/v2"
2021-03-09 22:02:18 +08:00
"github.com/go-kratos/kratos/v2"
"github.com/go-kratos/kratos/middleware/recovery/v2"
2021-03-09 22:02:18 +08:00
"github.com/go-kratos/kratos/v2/transport/grpc"
2021-05-26 16:43:59 +08:00
"github.com/go-kratos/kratos/v2/transport/http"
2021-03-09 22:02:18 +08:00
"github.com/hashicorp/consul/api"
)
// server is used to implement helloworld.GreeterServer.
type server struct {
2021-06-12 20:20:18 +08:00
helloworld.UnimplementedGreeterServer
2021-03-09 22:02:18 +08:00
}
// SayHello implements helloworld.GreeterServer
2021-06-12 20:20:18 +08:00
func (s *server) SayHello(ctx context.Context, in *helloworld.HelloRequest) (*helloworld.HelloReply, error) {
return &helloworld.HelloReply{Message: fmt.Sprintf("Welcome %+v!", in.Name)}, nil
2021-03-09 22:02:18 +08:00
}
func main() {
2021-05-26 16:43:59 +08:00
consulClient, err := api.NewClient(api.DefaultConfig())
if err != nil {
2021-06-12 20:20:18 +08:00
log.Fatal(err)
2021-05-26 16:43:59 +08:00
}
2021-06-12 18:30:17 +08:00
httpSrv := http.NewServer(
http.Address(":8000"),
2021-05-26 16:43:59 +08:00
http.Middleware(
recovery.Recovery(),
2021-06-12 18:30:17 +08:00
),
2021-05-26 16:43:59 +08:00
)
grpcSrv := grpc.NewServer(
grpc.Address(":9000"),
grpc.Middleware(
recovery.Recovery(),
),
)
2021-06-12 20:20:18 +08:00
s := &server{}
helloworld.RegisterGreeterServer(grpcSrv, s)
helloworld.RegisterGreeterHTTPServer(httpSrv, s)
2021-05-26 16:43:59 +08:00
r := registry.New(consulClient)
2021-03-09 22:02:18 +08:00
app := kratos.New(
kratos.Name("helloworld"),
kratos.Server(
grpcSrv,
2021-05-26 16:43:59 +08:00
httpSrv,
2021-03-09 22:02:18 +08:00
),
2021-03-12 00:59:46 +08:00
kratos.Registrar(r),
2021-03-09 22:02:18 +08:00
)
if err := app.Run(); err != nil {
2021-06-12 20:20:18 +08:00
log.Fatal(err)
2021-03-09 22:02:18 +08:00
}
}