服务之间相互调用
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
run.toml
|
||||
tmp
|
||||
etc/express.yaml
|
||||
@@ -0,0 +1,158 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"lone-services/pkg/discovery"
|
||||
"lone-services/pkg/modelbase"
|
||||
"lone-services/pkg/mysql"
|
||||
"lone-services/pkg/redis"
|
||||
"lone-services/pkg/utils"
|
||||
"lone-services/pkg/validate"
|
||||
express "lone-services/rpc/express/pb"
|
||||
"lone-services/services/express/internal/config"
|
||||
"lone-services/services/express/internal/server"
|
||||
"lone-services/services/express/internal/svc"
|
||||
"net"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/conf"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"github.com/zeromicro/go-zero/core/service"
|
||||
"github.com/zeromicro/go-zero/zrpc"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/reflection"
|
||||
)
|
||||
|
||||
var configFile = flag.String("f", "etc/express.yaml", "the config file")
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
var c config.Config
|
||||
conf.MustLoad(*configFile, &c)
|
||||
|
||||
// Nacos配置拉取初始化
|
||||
nacosParam := utils.NacosConfig{
|
||||
Hosts: c.Nacos.Hosts,
|
||||
NamespaceId: c.Nacos.NamespaceId,
|
||||
Group: c.Nacos.Group,
|
||||
ConfigID: c.Nacos.ConfigID,
|
||||
}
|
||||
utils.InitConfig(nacosParam)
|
||||
|
||||
// 日志配置
|
||||
logConf := logx.LogConf{
|
||||
ServiceName: utils.GetConfigString("log.serviceName"),
|
||||
Mode: utils.GetConfigString("log.mode"),
|
||||
Encoding: utils.GetConfigString("log.encoding"),
|
||||
Level: utils.GetConfigString("log.level"),
|
||||
Path: utils.GetConfigString("log.path"),
|
||||
KeepDays: utils.GetConfigInt("log.keepDays"),
|
||||
MaxSize: utils.GetConfigInt("log.maxSize"),
|
||||
MaxBackups: utils.GetConfigInt("log.maxBackups"),
|
||||
Compress: utils.GetConfigBool("log.compress"),
|
||||
}
|
||||
logx.SetUp(logConf)
|
||||
logx.AddWriter(logx.NewWriter(os.Stdout))
|
||||
|
||||
listenOn := utils.GetConfigString("base.listenOn")
|
||||
mode := utils.GetConfigString("base.mode")
|
||||
serviceName := utils.GetConfigString("base.name")
|
||||
|
||||
// 初始化服务发现
|
||||
if err := discovery.Init(discovery.Config{
|
||||
Hosts: c.Nacos.Hosts,
|
||||
NamespaceId: c.Nacos.NamespaceId,
|
||||
Group: c.Nacos.Group,
|
||||
}); err != nil {
|
||||
logx.Errorf("nacos init: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
port, err := listenPort(listenOn)
|
||||
if err != nil {
|
||||
logx.Errorf("parse ListenOn: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// 注册服务
|
||||
if err := discovery.Register(discovery.Instance{
|
||||
ServiceName: serviceName,
|
||||
IP: c.Nacos.RegisterIP,
|
||||
Port: port,
|
||||
Group: c.Nacos.Group,
|
||||
}); err != nil {
|
||||
logx.Errorf("nacos register: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
logx.Infof("服务注册成功: %s:%d", c.Nacos.RegisterIP, port)
|
||||
|
||||
defer func() {
|
||||
if err := discovery.Deregister(); err != nil {
|
||||
logx.Errorf("nacos deregister: %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
// 初始化MySQL
|
||||
db, err := mysql.New(mysql.Config{
|
||||
Host: utils.GetConfigString("mysql.host"),
|
||||
Port: utils.GetConfigInt("mysql.port"),
|
||||
User: utils.GetConfigString("mysql.user"),
|
||||
Password: utils.GetConfigString("mysql.password"),
|
||||
Database: utils.GetConfigString("mysql.database"),
|
||||
Charset: utils.GetConfigString("mysql.charset"),
|
||||
Prefix: utils.GetConfigString("mysql.prefix"),
|
||||
ReadHost: utils.GetConfigString("mysql_read.host"),
|
||||
ReadPort: utils.GetConfigInt("mysql_read.port"),
|
||||
ReadUser: utils.GetConfigString("mysql_read.user"),
|
||||
ReadPassword: utils.GetConfigString("mysql_read.password"),
|
||||
ReadDatabase: utils.GetConfigString("mysql_read.database"),
|
||||
})
|
||||
if err != nil {
|
||||
logx.Errorf("mysql init: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// 初始化Redis
|
||||
if err := redis.Init(redis.Config{
|
||||
Host: utils.GetConfigString("redis.host"),
|
||||
Port: utils.GetConfigInt("redis.port"),
|
||||
Password: utils.GetConfigString("redis.password"),
|
||||
DB: utils.GetConfigInt("redis.db"),
|
||||
}); err != nil {
|
||||
logx.Errorf("redis init: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
debug := utils.GetConfigBool("mysql.debug")
|
||||
modelbase.Init(db, modelbase.Config{Prefix: utils.GetConfigString("mysql.prefix"), Debug: debug})
|
||||
|
||||
// 初始化服务信息
|
||||
rpcConf := zrpc.RpcServerConf{
|
||||
ListenOn: listenOn,
|
||||
}
|
||||
rpcConf.Mode = mode
|
||||
ctx := svc.NewServiceContext(c, db)
|
||||
|
||||
s := zrpc.MustNewServer(rpcConf, func(grpcServer *grpc.Server) {
|
||||
express.RegisterExpressServer(grpcServer, server.NewExpressServer(ctx))
|
||||
if mode == service.DevMode || mode == service.TestMode {
|
||||
reflection.Register(grpcServer)
|
||||
}
|
||||
})
|
||||
defer s.Stop()
|
||||
|
||||
s.AddUnaryInterceptors(validate.UnaryServerInterceptor(validate.MustNew()))
|
||||
|
||||
logx.Infof("Starting rpc server at %s...", listenOn)
|
||||
s.Start()
|
||||
}
|
||||
|
||||
// listenPort 拆分端口
|
||||
func listenPort(listenOn string) (uint64, error) {
|
||||
_, portStr, err := net.SplitHostPort(listenOn)
|
||||
if err != nil {
|
||||
return utils.NumberZero, err
|
||||
}
|
||||
return strconv.ParseUint(portStr, utils.NumberTen, utils.NumberSixtyFourth)
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
// goctl 1.10.2
|
||||
// Source: express.proto
|
||||
|
||||
package expressClient
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"lone-services/rpc/express/pb"
|
||||
|
||||
"github.com/zeromicro/go-zero/zrpc"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
type (
|
||||
EmtpyRequest = express.EmtpyRequest
|
||||
Request = express.Request
|
||||
Response = express.Response
|
||||
|
||||
Express interface {
|
||||
Ping(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Response, error)
|
||||
Items(ctx context.Context, in *EmtpyRequest, opts ...grpc.CallOption) (*Response, error)
|
||||
}
|
||||
|
||||
defaultExpress struct {
|
||||
cli zrpc.Client
|
||||
}
|
||||
)
|
||||
|
||||
func NewExpress(cli zrpc.Client) Express {
|
||||
return &defaultExpress{
|
||||
cli: cli,
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultExpress) Ping(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Response, error) {
|
||||
client := express.NewExpressClient(m.cli.Conn())
|
||||
return client.Ping(ctx, in, opts...)
|
||||
}
|
||||
|
||||
func (m *defaultExpress) Items(ctx context.Context, in *EmtpyRequest, opts ...grpc.CallOption) (*Response, error) {
|
||||
client := express.NewExpressClient(m.cli.Conn())
|
||||
return client.Items(ctx, in, opts...)
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package config
|
||||
|
||||
type Config struct {
|
||||
Nacos NacosConf
|
||||
}
|
||||
|
||||
type NacosConf struct {
|
||||
Hosts []string
|
||||
NamespaceId string `json:",optional"`
|
||||
Group string `json:",optional"`
|
||||
RegisterIP string `json:",optional"`
|
||||
ConfigID string `json:",optional"`
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"lone-services/pkg/utils"
|
||||
"lone-services/pkg/validate"
|
||||
express "lone-services/rpc/express/pb"
|
||||
"reflect"
|
||||
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
type BaseLogic struct {
|
||||
}
|
||||
|
||||
func (l *BaseLogic) checkParams(in interface{}, v validate.IValidator) *express.Response {
|
||||
rv := reflect.ValueOf(in)
|
||||
if rv.Kind() != reflect.Ptr || rv.IsNil() {
|
||||
return &express.Response{
|
||||
Code: utils.ErrorParams.Code,
|
||||
Msg: "request must be non‑nil proto pointer",
|
||||
}
|
||||
}
|
||||
|
||||
resp := validate.ValidateFromProto(in, v)
|
||||
if resp != utils.StringEmpty {
|
||||
return &express.Response{
|
||||
Code: utils.ErrorParams.Code,
|
||||
Msg: resp,
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *BaseLogic) fail(status utils.Status) (*express.Response, error) {
|
||||
return l.out(status, status.Msg)
|
||||
}
|
||||
func (l *BaseLogic) out(status utils.Status, msg string) (*express.Response, error) {
|
||||
return &express.Response{Code: status.Code, Msg: msg}, nil
|
||||
}
|
||||
|
||||
func (l *BaseLogic) ok(data any) (*express.Response, error) {
|
||||
buf, _ := jsoniter.Marshal(data)
|
||||
return &express.Response{Code: utils.Ok.Code, Msg: utils.Ok.Msg, Data: string(buf)}, nil
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"lone-services/rpc/express/pb"
|
||||
"lone-services/services/express/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type ItemsLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewItemsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ItemsLogic {
|
||||
return &ItemsLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *ItemsLogic) Items(in *express.EmtpyRequest) (*express.Response, error) {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return &express.Response{}, nil
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
express "lone-services/rpc/express/pb"
|
||||
|
||||
"lone-services/services/express/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type PingLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
BaseLogic
|
||||
}
|
||||
|
||||
func NewPingLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PingLogic {
|
||||
return &PingLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *PingLogic) Ping(in *express.Request) (*express.Response, error) {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return l.ok("this is a test")
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
// goctl 1.10.2
|
||||
// Source: express.proto
|
||||
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"lone-services/rpc/express/pb"
|
||||
"lone-services/services/express/internal/logic"
|
||||
"lone-services/services/express/internal/svc"
|
||||
)
|
||||
|
||||
type ExpressServer struct {
|
||||
svcCtx *svc.ServiceContext
|
||||
express.UnimplementedExpressServer
|
||||
}
|
||||
|
||||
func NewExpressServer(svcCtx *svc.ServiceContext) *ExpressServer {
|
||||
return &ExpressServer{
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *ExpressServer) Ping(ctx context.Context, in *express.Request) (*express.Response, error) {
|
||||
l := logic.NewPingLogic(ctx, s.svcCtx)
|
||||
return l.Ping(in)
|
||||
}
|
||||
|
||||
func (s *ExpressServer) Items(ctx context.Context, in *express.EmtpyRequest) (*express.Response, error) {
|
||||
l := logic.NewItemsLogic(ctx, s.svcCtx)
|
||||
return l.Items(in)
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package svc
|
||||
|
||||
import (
|
||||
"lone-services/pkg/utils"
|
||||
"lone-services/services/express/internal/config"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type ServiceContext struct {
|
||||
Config config.Config
|
||||
DB *gorm.DB
|
||||
Prefix string
|
||||
}
|
||||
|
||||
func NewServiceContext(c config.Config, db *gorm.DB) *ServiceContext {
|
||||
|
||||
return &ServiceContext{
|
||||
Config: c,
|
||||
DB: db,
|
||||
Prefix: utils.GetConfigString("mysql.prefix"),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user