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.
80 lines
1.9 KiB
80 lines
1.9 KiB
3 years ago
|
package main
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"fmt"
|
||
|
"os"
|
||
|
|
||
|
"github.com/go-kratos/kratos/contrib/registry/consul/v2"
|
||
|
"github.com/go-kratos/kratos/examples/helloworld/helloworld"
|
||
|
"github.com/go-kratos/kratos/v2"
|
||
|
"github.com/go-kratos/kratos/v2/log"
|
||
|
"github.com/go-kratos/kratos/v2/middleware/logging"
|
||
|
"github.com/go-kratos/kratos/v2/middleware/recovery"
|
||
|
"github.com/go-kratos/kratos/v2/transport/grpc"
|
||
|
"github.com/go-kratos/kratos/v2/transport/http"
|
||
|
"github.com/hashicorp/consul/api"
|
||
|
)
|
||
|
|
||
|
// server is used to implement helloworld.GreeterServer.
|
||
|
type server struct {
|
||
|
helloworld.UnimplementedGreeterServer
|
||
|
}
|
||
|
|
||
|
// SayHello implements helloworld.GreeterServer
|
||
|
func (s *server) SayHello(ctx context.Context, in *helloworld.HelloRequest) (*helloworld.HelloReply, error) {
|
||
|
return &helloworld.HelloReply{Message: fmt.Sprintf("Welcome %+v!", in.Name)}, nil
|
||
|
}
|
||
|
|
||
|
func main() {
|
||
|
logger := log.NewStdLogger(os.Stdout)
|
||
|
|
||
|
consulClient, err := api.NewClient(api.DefaultConfig())
|
||
|
if err != nil {
|
||
|
log.NewHelper(logger).Fatal(err)
|
||
|
}
|
||
|
go runServer("1.0.0", logger, consulClient, 8000)
|
||
|
go runServer("1.0.0", logger, consulClient, 8010)
|
||
|
|
||
|
runServer("2.0.0", logger, consulClient, 8020)
|
||
|
}
|
||
|
|
||
|
func runServer(version string, logger log.Logger, client *api.Client, port int) {
|
||
|
logger = log.With(logger, "version", version, "port:", port)
|
||
|
log := log.NewHelper(logger)
|
||
|
|
||
|
httpSrv := http.NewServer(
|
||
|
http.Address(fmt.Sprintf(":%d", port)),
|
||
|
http.Middleware(
|
||
|
recovery.Recovery(),
|
||
|
logging.Server(logger),
|
||
|
),
|
||
|
)
|
||
|
grpcSrv := grpc.NewServer(
|
||
|
grpc.Address(fmt.Sprintf(":%d", port+1000)),
|
||
|
grpc.Middleware(
|
||
|
recovery.Recovery(),
|
||
|
logging.Server(logger),
|
||
|
),
|
||
|
)
|
||
|
|
||
|
s := &server{}
|
||
|
helloworld.RegisterGreeterServer(grpcSrv, s)
|
||
|
helloworld.RegisterGreeterHTTPServer(httpSrv, s)
|
||
|
|
||
|
r := consul.New(client)
|
||
|
app := kratos.New(
|
||
|
kratos.Name("helloworld"),
|
||
|
kratos.Server(
|
||
|
grpcSrv,
|
||
|
httpSrv,
|
||
|
),
|
||
|
kratos.Version(version),
|
||
|
kratos.Registrar(r),
|
||
|
)
|
||
|
|
||
|
if err := app.Run(); err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
}
|