go-kratos 学习笔记(5) 删除hellword从新生成服务

go-kratos 删除hellword模版里的服务,从新生成服务users

直接删除api/helloword 目录,在api目录下新建 users目录;使用命令生成新的proto

#项目根目录执行 
kratos proto add api/users/user.proto

buf generate

生成的user.proto需要根据自己buf管理的proto生成代码的目录稍微修改一下路径

syntax = "proto3";

package gen.users;
import "google/api/annotations.proto";

option go_package = "xgs_kratos/gen/users;users";
option java_multiple_files = true;
option java_package = "api.users";

service User {
	rpc CreateUser (CreateUserRequest) returns (CreateUserReply){
		option (google.api.http) = {
			post: "/CreateUser",
			body: "*",
		};
	};
	rpc UpdateUser (UpdateUserRequest) returns (UpdateUserReply);
	rpc DeleteUser (DeleteUserRequest) returns (DeleteUserReply);
	rpc GetUser (GetUserRequest) returns (GetUserReply);
	rpc ListUser (ListUserRequest) returns (ListUserReply);
}

message CreateUserRequest {}
message CreateUserReply {}

message UpdateUserRequest {}
message UpdateUserReply {}

message DeleteUserRequest {}
message DeleteUserReply {}

message GetUserRequest {}
message GetUserReply {}

message ListUserRequest {}
message ListUserReply {}

执行 buf generate

buf generate

#在app目录下执行生成新的users

kratos new users --nomod

生成新的目录之后 ,生成服务端的代码,需要先删除innterna目录下的几个greeter.go

这个章节的是把配置问价的proto也给分离出来了,app/cofig/users/config.proto

#执行
buf generate

cd app/users
#生成服务端代码  
kratos proto server ../../api/users/user.proto -t ./internal/service

剩下的就是需要我们去写 biz data service server里的代码了

service/user.go代码如下

package service

import (
	"context"
	"xgs_kratos/app/users/internal/biz"

	pb "xgs_kratos/gen/users"
)

type UserService struct {
	pb.UnimplementedUserServer
	uc *biz.UserUsecase
}

func NewUserService(uc *biz.UserUsecase) *UserService {
	return &UserService{
		uc: uc,
	}
}

func (s *UserService) CreateUser(ctx context.Context, req *pb.CreateUserRequest) (*pb.CreateUserReply, error) {
	return &pb.CreateUserReply{}, nil
}

//func (s *UserService) UpdateUser(ctx context.Context, req *pb.UpdateUserRequest) (*pb.UpdateUserReply, error) {
//	return &pb.UpdateUserReply{}, nil
//}
//func (s *UserService) DeleteUser(ctx context.Context, req *pb.DeleteUserRequest) (*pb.DeleteUserReply, error) {
//	return &pb.DeleteUserReply{}, nil
//}
//func (s *UserService) GetUser(ctx context.Context, req *pb.GetUserRequest) (*pb.GetUserReply, error) {
//	return &pb.GetUserReply{}, nil
//}
//func (s *UserService) ListUser(ctx context.Context, req *pb.ListUserRequest) (*pb.ListUserReply, error) {
//	return &pb.ListUserReply{}, nil
//}

biz/user.go

package biz

import (
	"context"
	"github.com/go-kratos/kratos/v2/log"
	"xgs_kratos/gen/users"
)

type User struct {
}

// UserRepo 定义数据仓库接口
type UserRepo interface {
	CreateUser(context.Context, *users.CreateUserRequest) (*users.CreateUserReply, error)
}

// UserUsecase 定义业务逻辑
type UserUsecase struct {
	repo UserRepo
	log  *log.Helper
}

// NewUsecase 创建Usecase
func NewUserUsecase(repo UserRepo, logger log.Logger) *UserUsecase {
	return &UserUsecase{repo: repo, log: log.NewHelper(logger)}
}

// CreateUser 创建用户
func (uc *UserUsecase) CreateUser(ctx context.Context, req *users.CreateUserRequest) (*users.CreateUserReply, error) {
	return uc.repo.CreateUser(ctx, req)
}

data/user.go

package data

import (
	"context"
	"github.com/go-kratos/kratos/v2/log"
	"xgs_kratos/app/users/internal/biz"
	"xgs_kratos/gen/users"
)

//data 层处理数据的存储和读取

type userRepo struct {
	data *Data
	log  *log.Helper
}

// NewUserRepo .
func NewUserRepo(data *Data, logger log.Logger) biz.UserRepo {
	return &userRepo{
		data: data,
		log:  log.NewHelper(logger),
	}
}

func (r *userRepo) CreateUser(ctx context.Context, req *users.CreateUserRequest) (*users.CreateUserReply, error) {
	return &users.CreateUserReply{}, nil
}

server下的http和grpc里面的问价需要修改成对应的service

http.go

package server

import (
	"xgs_kratos/app/users/internal/service"
	conf "xgs_kratos/gen/config/users"
	v1 "xgs_kratos/gen/users"

	"github.com/go-kratos/kratos/v2/log"
	"github.com/go-kratos/kratos/v2/middleware/recovery"
	"github.com/go-kratos/kratos/v2/transport/http"
)

// NewHTTPServer new an HTTP server.
func NewHTTPServer(c *conf.Server, user *service.UserService, logger log.Logger) *http.Server {
	var opts = []http.ServerOption{
		http.Middleware(
			recovery.Recovery(),
		),
	}
	if c.Http.Network != "" {
		opts = append(opts, http.Network(c.Http.Network))
	}
	if c.Http.Addr != "" {
		opts = append(opts, http.Address(c.Http.Addr))
	}
	if c.Http.Timeout != nil {
		opts = append(opts, http.Timeout(c.Http.Timeout.AsDuration()))
	}
	srv := http.NewServer(opts...)
	v1.RegisterUserHTTPServer(srv, user)
	return srv
}

grpc.go

package server

import (
	"xgs_kratos/app/users/internal/service"
	conf "xgs_kratos/gen/config/users"
	v1 "xgs_kratos/gen/users"

	"github.com/go-kratos/kratos/v2/log"
	"github.com/go-kratos/kratos/v2/middleware/recovery"
	"github.com/go-kratos/kratos/v2/transport/grpc"
)

// NewGRPCServer new a gRPC server.
func NewGRPCServer(c *conf.Server, user *service.UserService, logger log.Logger) *grpc.Server {
	var opts = []grpc.ServerOption{
		grpc.Middleware(
			recovery.Recovery(),
		),
	}
	if c.Grpc.Network != "" {
		opts = append(opts, grpc.Network(c.Grpc.Network))
	}
	if c.Grpc.Addr != "" {
		opts = append(opts, grpc.Address(c.Grpc.Addr))
	}
	if c.Grpc.Timeout != nil {
		opts = append(opts, grpc.Timeout(c.Grpc.Timeout.AsDuration()))
	}
	srv := grpc.NewServer(opts...)
	v1.RegisterUserServer(srv, user)
	return srv
}

分别修改依赖注入 server.ProviderSet, data.ProviderSet, biz.ProviderSet, service.ProviderSet

data.go

var ProviderSet = wire.NewSet(NewData, NewUserRepo, CreateRegister)

biz.go

var ProviderSet = wire.NewSet(NewUserUsecase)

service.go

var ProviderSet = wire.NewSet(NewUserService)

cd 到 app/users/cmd/users 下执行 wire

在切换到 app/users 下执行 kratos run

或者是根目录执行 kratos run -w users

项目的代码 码云 https://gitee.com/gebilaoxie/xgs_kratos.git

相关推荐
我曾经是个程序员3 分钟前
鸿蒙学习记录之http网络请求
服务器·学习·http
m0_7482323913 分钟前
WebRTC学习二:WebRTC音视频数据采集
学习·音视频·webrtc
虾球xz2 小时前
游戏引擎学习第55天
学习·游戏引擎
oneouto2 小时前
selenium学习笔记(二)
笔记·学习·selenium
sealaugh322 小时前
aws(学习笔记第十九课) 使用ECS和Fargate进行容器开发
笔记·学习·aws
炭烤玛卡巴卡2 小时前
学习postman工具使用
学习·测试工具·postman
thesky1234563 小时前
活着就好20241224
学习·算法
蜗牛hb3 小时前
VMware Workstation虚拟机网络模式
开发语言·学习·php
汤姆和杰瑞在瑞士吃糯米粑粑3 小时前
【C++学习篇】AVL树
开发语言·c++·学习
虾球xz3 小时前
游戏引擎学习第58天
学习·游戏引擎