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

相关推荐
云上艺旅1 小时前
K8S学习之基础七十四:部署在线书店bookinfo
学习·云原生·容器·kubernetes
你觉得2051 小时前
哈尔滨工业大学DeepSeek公开课:探索大模型原理、技术与应用从GPT到DeepSeek|附视频与讲义下载方法
大数据·人工智能·python·gpt·学习·机器学习·aigc
A旧城以西3 小时前
数据结构(JAVA)单向,双向链表
java·开发语言·数据结构·学习·链表·intellij-idea·idea
无所谓จุ๊บ3 小时前
VTK知识学习(50)- 交互与Widget(一)
学习·vtk
FAREWELL000753 小时前
C#核心学习(七)面向对象--封装(6)C#中的拓展方法与运算符重载: 让代码更“聪明”的魔法
学习·c#·面向对象·运算符重载·oop·拓展方法
吴梓穆3 小时前
UE5学习笔记 FPS游戏制作38 继承标准UI
笔记·学习·ue5
Three~stone4 小时前
MySQL学习集--DDL
数据库·sql·学习
齐尹秦4 小时前
HTML 音频(Audio)学习笔记
学习
V---scwantop---信4 小时前
英文字体:大胆都市街头Y2Y涂鸦风格品牌海报专辑封面服装字体 Chrome TM – Graffiti Font
笔记·字体
瞌睡不来4 小时前
(学习总结32)Linux 基础 IO
linux·学习·io