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 小时前
常见 CSS 选择器用法
前端·css·学习·html·前端开发·css选择器
龙鸣丿1 小时前
Linux基础学习笔记
linux·笔记·学习
Nu11PointerException3 小时前
JAVA笔记 | ResponseBodyEmitter等异步流式接口快速学习
笔记·学习
亦枫Leonlew5 小时前
三维测量与建模笔记 - 3.3 张正友标定法
笔记·相机标定·三维重建·张正友标定法
考试宝5 小时前
国家宠物美容师职业技能等级评价(高级)理论考试题
经验分享·笔记·职场和发展·学习方法·业界资讯·宠物
黑叶白树6 小时前
简单的签到程序 python笔记
笔记·python
@小博的博客7 小时前
C++初阶学习第十弹——深入讲解vector的迭代器失效
数据结构·c++·学习
幸运超级加倍~7 小时前
软件设计师-上午题-15 计算机网络(5分)
笔记·计算机网络
南宫生7 小时前
贪心算法习题其四【力扣】【算法学习day.21】
学习·算法·leetcode·链表·贪心算法
懒惰才能让科技进步8 小时前
从零学习大模型(十二)-----基于梯度的重要性剪枝(Gradient-based Pruning)
人工智能·深度学习·学习·算法·chatgpt·transformer·剪枝