Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

有考虑加入rpc server 和 rpc client的模板和分层吗 #50

Open
cpj555 opened this issue Mar 16, 2024 · 5 comments
Open

有考虑加入rpc server 和 rpc client的模板和分层吗 #50

cpj555 opened this issue Mar 16, 2024 · 5 comments

Comments

@cpj555
Copy link

cpj555 commented Mar 16, 2024

大佬模板整的挺好的,用这套搞了两应用,正好碰到用户体系的问题可能要拆点服务出去,不知道有没有现成的东西套下

@codingcn
Copy link
Member

codingcn commented Mar 16, 2024

我先给个简单的例子参考着弄吧,不是很复杂。

提示:nunu-layout-micro这个以前写到一半搁置了,还没开源。

定义服务端

internal/server/grpc.go

package server

import (
	"fmt"
	"github.com/spf13/viper"
	"go.uber.org/zap"
	pb "nunu-layout-micro/api/user/v1"
	"nunu-layout-micro/internal/rpc"
	"nunu-layout-micro/pkg/log"
	"nunu-layout-micro/pkg/server/grpc"
)

func NewGRPCServer(
	logger *log.Logger,
	conf *viper.Viper,
	userServer rpc.UserServer,
) *grpc.Server {
	// 创建gRPC服务器
	s := grpc.NewServer(
		logger,
		grpc.WithServerHost(conf.GetString("grpc.host")),
		grpc.WithServerPort(conf.GetInt("grpc.port")),
	)
	logger.Info("grpc server start", zap.String("addr", fmt.Sprintf("%s:%d", conf.GetString("grpc.host"), conf.GetInt("grpc.port"))))

	// 注册GRPC服务
	pb.RegisterUserServiceServer(s, userServer)
	return s
}

internal/rpc/rpc.go

package rpc

import "nunu-layout-micro/pkg/log"

type RpcServer struct {
	logger *log.Logger
}

func NewRpcServer(logger *log.Logger) *RpcServer {
	return &RpcServer{
		logger: logger,
	}
}

internal/rpc/user.go

package rpc

import (
	"context"
	pb "nunu-layout-micro/api/user/v1"
	"nunu-layout-micro/internal/service"
)

type UserServer interface {
	Register(context.Context, *pb.RegisterRequest) (*pb.RegisterResponse, error)
	Login(context.Context, *pb.LoginRequest) (*pb.LoginResponse, error)
	UpdateProfile(context.Context, *pb.UpdateProfileRequest) (*pb.UpdateProfileResponse, error)
	GetProfile(context.Context, *pb.GetProfileRequest) (*pb.GetProfileResponse, error)
}

func NewUserServer(rpc *RpcServer, userService service.UserService) UserServer {
	return &userServer{
		userService: userService,
		RpcServer:   rpc,
	}
}

type userServer struct {
	userService service.UserService
	*RpcServer
}

func (s userServer) Register(ctx context.Context, request *pb.RegisterRequest) (*pb.RegisterResponse, error) {
	return s.userService.Register(ctx, request)
}

func (s userServer) Login(ctx context.Context, request *pb.LoginRequest) (*pb.LoginResponse, error) {
	return s.userService.Login(ctx, request)
}

func (s userServer) UpdateProfile(ctx context.Context, request *pb.UpdateProfileRequest) (*pb.UpdateProfileResponse, error) {
	//TODO implement me
	panic("implement me")
}

func (s userServer) GetProfile(ctx context.Context, request *pb.GetProfileRequest) (*pb.GetProfileResponse, error) {
	return s.userService.GetProfile(ctx, request)
}

定义客户端

internal/repository/repository.go

package repository

import (
	"context"
	polaris "github.com/polarismesh/grpc-go-polaris"
	"github.com/spf13/viper"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	pb "nunu-layout-micro/api/user/v1"
	"nunu-layout-micro/pkg/log"
)

type Repository struct {
	logger  *log.Logger
	userCli pb.UserServiceClient
}

func NewRepository(logger *log.Logger, userCli pb.UserServiceClient) *Repository {
	return &Repository{
		logger:  logger,
		userCli: userCli,
	}
}
func NewUserServiceClient(conf *viper.Viper, logger *log.Logger) (pb.UserServiceClient, func(), error) {
	// grpc客户端连接获取
	// 使用 grpc-go-polaris 提供的 DialContext 即可
        // polaris 是腾讯开源的一个服务治理框架,不使用它也可以,直接正常写原生grpc即可
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
	conn, err := polaris.DialContext(ctx, "polaris://QuickStartEchoServerGRPC",
		polaris.WithGRPCDialOptions(grpc.WithTransportCredentials(insecure.NewCredentials())),
		polaris.WithDisableRouter(),
	)
	if err != nil {
		logger.Sugar().Fatalf("Failed to dial gRPC service: %v", err)
	}
	return pb.NewUserServiceClient(conn), func() {
		cancel()
	}, nil
}

internal/repository/user.go

package repository

import (
	"context"
	pb "nunu-layout-micro/api/user/v1"
)

type UserRepository interface {
	Register(ctx context.Context, in *pb.RegisterRequest) (*pb.RegisterResponse, error)
	Login(ctx context.Context, in *pb.LoginRequest) (*pb.LoginResponse, error)
	UpdateProfile(ctx context.Context, in *pb.UpdateProfileRequest) (*pb.UpdateProfileResponse, error)
	GetProfile(ctx context.Context, in *pb.GetProfileRequest) (*pb.GetProfileResponse, error)
}

func NewUserRepository(r *Repository) UserRepository {
	return &userRepository{
		Repository: r,
	}
}

type userRepository struct {
	*Repository
}

func (u userRepository) Register(ctx context.Context, in *pb.RegisterRequest) (*pb.RegisterResponse, error) {
	return u.userCli.Register(ctx, in)
}

func (u userRepository) Login(ctx context.Context, in *pb.LoginRequest) (*pb.LoginResponse, error) {
	return u.userCli.Login(ctx, in)
}

func (u userRepository) UpdateProfile(ctx context.Context, in *pb.UpdateProfileRequest) (*pb.UpdateProfileResponse, error) {
	return u.userCli.UpdateProfile(ctx, in)
}

func (u userRepository) GetProfile(ctx context.Context, in *pb.GetProfileRequest) (*pb.GetProfileResponse, error) {
	return u.userCli.GetProfile(ctx, in)
}

@alfuckk
Copy link

alfuckk commented Oct 22, 2024

总是报mustEmbedUnimplementedrvServer

@alfuckk
Copy link

alfuckk commented Oct 22, 2024

非常期待大佬能把grpc的部分也单开一个仓库

@codingcn
Copy link
Member

非常期待大佬能把grpc的部分也单开一个仓库

等我空闲下来吧,其实grpc自己加进去很简单的

@alfuckk
Copy link

alfuckk commented Oct 23, 2024

好的,这个模板是我所见过的最标准的了。感谢开源开发者

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants