mirror of
https://github.com/zhufuyi/sponge.git
synced 2025-10-30 12:06:41 +08:00
add option params
This commit is contained in:
@@ -10,7 +10,8 @@ handler:
|
|||||||
@go build
|
@go build
|
||||||
protoc --proto_path=. --proto_path=./third_party \
|
protoc --proto_path=. --proto_path=./third_party \
|
||||||
--go_out=. --go_opt=paths=source_relative \
|
--go_out=. --go_opt=paths=source_relative \
|
||||||
--go-gin_out=. --go-gin_opt=paths=source_relative --go-gin_opt=plugins=handler \
|
--go-gin_out=. --go-gin_opt=paths=source_relative --go-gin_opt=plugin=handler \
|
||||||
|
--go-gin_opt=moduleName=yourModuleName --go-gin_opt=serverName=yourServerName --go-gin_opt=out=internal/handler \
|
||||||
--plugin=./protoc-gen-go-gin* \
|
--plugin=./protoc-gen-go-gin* \
|
||||||
api/v1/*.proto
|
api/v1/*.proto
|
||||||
|
|
||||||
@@ -19,11 +20,14 @@ service:
|
|||||||
protoc --proto_path=. --proto_path=./third_party \
|
protoc --proto_path=. --proto_path=./third_party \
|
||||||
--go_out=. --go_opt=paths=source_relative \
|
--go_out=. --go_opt=paths=source_relative \
|
||||||
--go-grpc_out=. --go-grpc_opt=paths=source_relative \
|
--go-grpc_out=. --go-grpc_opt=paths=source_relative \
|
||||||
--go-gin_out=. --go-gin_opt=paths=source_relative --go-gin_opt=plugins=service \
|
--go-gin_out=. --go-gin_opt=paths=source_relative --go-gin_opt=plugin=service \
|
||||||
|
--go-gin_opt=moduleName=yourModuleName --go-gin_opt=serverName=yourServerName --go-gin_opt=out=internal/service \
|
||||||
--plugin=./protoc-gen-go-gin* \
|
--plugin=./protoc-gen-go-gin* \
|
||||||
api/v1/*.proto
|
api/v1/*.proto
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
@rm -vrf api/v1/*.go
|
@rm -vrf api/v1/*.go
|
||||||
@rm -vrf protoc-gen-go-gin*
|
@rm -vrf protoc-gen-go-gin*
|
||||||
|
@rm -vrf internal/service
|
||||||
|
@rm -vrf internal/handler
|
||||||
@echo Done.
|
@echo Done.
|
||||||
|
|||||||
@@ -70,22 +70,22 @@ protoc --proto_path=. --proto_path=./third_party \
|
|||||||
api/v1/*.proto
|
api/v1/*.proto
|
||||||
```
|
```
|
||||||
|
|
||||||
生成*_router.pb.go和handler模板文件*_handler.go,用在由proto生成http的handler使用
|
生成*_router.pb.go和handler模板文件*_logic.go,用在由proto生成http的handler使用
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
protoc --proto_path=. --proto_path=./third_party \
|
protoc --proto_path=. --proto_path=./third_party \
|
||||||
--go_out=. --go_opt=paths=source_relative \
|
--go_out=. --go_opt=paths=source_relative \
|
||||||
--go-gin_out=. --go-gin_opt=paths=source_relative --go-gin_opt=plugins=handler \
|
--go-gin_out=. --go-gin_opt=paths=source_relative --go-gin_opt=plugin=handler \
|
||||||
--plugin=./protoc-gen-go-gin* \
|
--go-gin_opt=moduleName=yourModuleName --go-gin_opt=serverName=yourServerName --go-gin_opt=out=internal/handler \
|
||||||
api/v1/*.proto
|
api/v1/*.proto
|
||||||
```
|
```
|
||||||
|
|
||||||
生成*_router.pb.go和调用rpc模板文件*_service.go,用在rpc的gateway上
|
生成*_router.pb.go和调用rpc模板文件*_logic.go,用在rpc的gateway上
|
||||||
```bash
|
```bash
|
||||||
protoc --proto_path=. --proto_path=./third_party \
|
protoc --proto_path=. --proto_path=./third_party \
|
||||||
--go_out=. --go_opt=paths=source_relative \
|
--go_out=. --go_opt=paths=source_relative \
|
||||||
--go-grpc_out=. --go-grpc_opt=paths=source_relative \
|
--go-grpc_out=. --go-grpc_opt=paths=source_relative \
|
||||||
--go-gin_out=. --go-gin_opt=paths=source_relative --go-gin_opt=plugins=service \
|
--go-gin_out=. --go-gin_opt=paths=source_relative --go-gin_opt=plugin=service \
|
||||||
--plugin=./protoc-gen-go-gin* \
|
--go-gin_opt=moduleName=yourModuleName --go-gin_opt=serverName=yourServerName --go-gin_opt=out=internal/service \
|
||||||
api/v1/*.proto
|
api/v1/*.proto
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -8,19 +8,19 @@ import (
|
|||||||
"google.golang.org/protobuf/compiler/protogen"
|
"google.golang.org/protobuf/compiler/protogen"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GenerateFile generates a _handler.pb.go file.
|
// GenerateFile generates a handler.go file.
|
||||||
func GenerateFile(gen *protogen.Plugin, file *protogen.File) *protogen.GeneratedFile {
|
func GenerateFile(gen *protogen.Plugin, file *protogen.File) (string, *protogen.GeneratedFile) {
|
||||||
if len(file.Services) == 0 {
|
if len(file.Services) == 0 {
|
||||||
return nil
|
return "", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
filename := file.GeneratedFilenamePrefix + "_handler.go"
|
filename := file.GeneratedFilenamePrefix + "_logic.go"
|
||||||
g := gen.NewGeneratedFile(filename, file.GoImportPath)
|
g := gen.NewGeneratedFile(filename, file.GoImportPath)
|
||||||
|
|
||||||
for _, s := range file.Services {
|
for _, s := range file.Services {
|
||||||
genService(g, s)
|
genService(g, s)
|
||||||
}
|
}
|
||||||
return g
|
return filename, g
|
||||||
}
|
}
|
||||||
|
|
||||||
func genService(g *protogen.GeneratedFile, s *protogen.Service) {
|
func genService(g *protogen.GeneratedFile, s *protogen.Service) {
|
||||||
|
|||||||
@@ -19,7 +19,8 @@ var (
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
serverNameExampleV1 "github.com/zhufuyi/sponge/api/serverNameExample/v1"
|
serverNameExampleV1 "module_name_example/api/server_name_example/v1"
|
||||||
|
|
||||||
//"github.com/zhufuyi/sponge/pkg/gin/middleware"
|
//"github.com/zhufuyi/sponge/pkg/gin/middleware"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -8,19 +8,19 @@ import (
|
|||||||
"google.golang.org/protobuf/compiler/protogen"
|
"google.golang.org/protobuf/compiler/protogen"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GenerateFile generates a _service.pb.go file.
|
// GenerateFile generates a service.go file.
|
||||||
func GenerateFile(gen *protogen.Plugin, file *protogen.File) *protogen.GeneratedFile {
|
func GenerateFile(gen *protogen.Plugin, file *protogen.File) (string, *protogen.GeneratedFile) {
|
||||||
if len(file.Services) == 0 {
|
if len(file.Services) == 0 {
|
||||||
return nil
|
return "", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
filename := file.GeneratedFilenamePrefix + "_service.go"
|
filename := file.GeneratedFilenamePrefix + "_logic.go"
|
||||||
g := gen.NewGeneratedFile(filename, file.GoImportPath)
|
g := gen.NewGeneratedFile(filename, file.GoImportPath)
|
||||||
|
|
||||||
for _, s := range file.Services {
|
for _, s := range file.Services {
|
||||||
genService(g, s)
|
genService(g, s)
|
||||||
}
|
}
|
||||||
return g
|
return filename, g
|
||||||
}
|
}
|
||||||
|
|
||||||
func genService(g *protogen.GeneratedFile, s *protogen.Service) {
|
func genService(g *protogen.GeneratedFile, s *protogen.Service) {
|
||||||
@@ -33,7 +33,7 @@ func genService(g *protogen.GeneratedFile, s *protogen.Service) {
|
|||||||
for _, m := range s.Methods {
|
for _, m := range s.Methods {
|
||||||
funCode := fmt.Sprintf(`func (c *%sClient) %s(ctx context.Context, req *serverNameExampleV1.%s) (*serverNameExampleV1.%s, error) {
|
funCode := fmt.Sprintf(`func (c *%sClient) %s(ctx context.Context, req *serverNameExampleV1.%s) (*serverNameExampleV1.%s, error) {
|
||||||
// implement me
|
// implement me
|
||||||
// If required, fill in the code to fetch data from other microservices here.
|
// If required, fill in the code to fetch data from other rpc servers here.
|
||||||
return c.%sCli.%s(ctx, req)
|
return c.%sCli.%s(ctx, req)
|
||||||
}
|
}
|
||||||
`, field.LowerName, m.GoName, m.Input.GoIdent.GoName, m.Output.GoIdent.GoName, field.LowerName, m.GoName)
|
`, field.LowerName, m.GoName, m.Input.GoIdent.GoName, m.Output.GoIdent.GoName, field.LowerName, m.GoName)
|
||||||
|
|||||||
@@ -19,8 +19,8 @@ var (
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
serverNameExampleV1 "github.com/zhufuyi/sponge/api/serverNameExample/v1"
|
serverNameExampleV1 "module_name_example/api/server_name_example/v1"
|
||||||
"github.com/zhufuyi/sponge/internal/rpcclient"
|
"module_name_example/internal/rpcclient"
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ serverNameExampleV1.{{$.Name}}Logicer = (*{{$.LowerName}}Client)(nil)
|
var _ serverNameExampleV1.{{$.Name}}Logicer = (*{{$.LowerName}}Client)(nil)
|
||||||
|
|||||||
@@ -1,9 +1,13 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/zhufuyi/sponge/cmd/protoc-gen-go-gin/internal/generate/handler"
|
"github.com/zhufuyi/sponge/cmd/protoc-gen-go-gin/internal/generate/handler"
|
||||||
"github.com/zhufuyi/sponge/cmd/protoc-gen-go-gin/internal/generate/router"
|
"github.com/zhufuyi/sponge/cmd/protoc-gen-go-gin/internal/generate/router"
|
||||||
@@ -17,11 +21,13 @@ const exampleTip = `
|
|||||||
# generate *_router.pb.go file
|
# generate *_router.pb.go file
|
||||||
protoc --proto_path=. --proto_path=./third_party --go-gin_out=. --go-gin_opt=paths=source_relative *.proto
|
protoc --proto_path=. --proto_path=./third_party --go-gin_out=. --go-gin_opt=paths=source_relative *.proto
|
||||||
|
|
||||||
# generate *_router.pb.go and *_handler.go files, Note: You need to move *_handler.go to the internal/handler directory
|
# generate *_router.pb.go and handler *_logic.go file
|
||||||
protoc --proto_path=. --proto_path=./third_party --go-gin_out=. --go-gin_opt=paths=source_relative --go-gin_opt=plugins=handler *.proto
|
protoc --proto_path=. --proto_path=./third_party --go-gin_out=. --go-gin_opt=paths=source_relative --go-gin_opt=plugin=handler \
|
||||||
|
--go-gin_opt=moduleName=yourModuleName --go-gin_opt=serverName=yourServerName --go-gin_opt=out=internal/handler *.proto
|
||||||
|
|
||||||
# generate *_router.pb.go and *_service.go files, Note: You need to move *_service.go to the internal/service directory
|
# generate *_router.pb.go and service *_logic.go
|
||||||
protoc --proto_path=. --proto_path=./third_party --go-gin_out=. --go-gin_opt=paths=source_relative --go-gin_opt=plugins=service *.proto
|
protoc --proto_path=. --proto_path=./third_party --go-gin_out=. --go-gin_opt=paths=source_relative --go-gin_opt=plugin=service \
|
||||||
|
--go-gin_opt=moduleName=yourModuleName --go-gin_opt=serverName=yourServerName --go-gin_opt=out=internal/service *.proto
|
||||||
`
|
`
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -34,7 +40,12 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var flags flag.FlagSet
|
var flags flag.FlagSet
|
||||||
var plugins = flags.String("plugins", "", "list of plugins to enable (supported values: handler,service)")
|
|
||||||
|
var plugin, moduleName, serverName, out string
|
||||||
|
flags.StringVar(&plugin, "plugin", "", "list of plugin to enable (supported values: handler or service)")
|
||||||
|
flags.StringVar(&moduleName, "moduleName", "", "import module name")
|
||||||
|
flags.StringVar(&serverName, "serverName", "", "import server name")
|
||||||
|
flags.StringVar(&out, "out", "", "plugin generation code output folder")
|
||||||
|
|
||||||
options := protogen.Options{
|
options := protogen.Options{
|
||||||
ParamFunc: flags.Set,
|
ParamFunc: flags.Set,
|
||||||
@@ -42,9 +53,8 @@ func main() {
|
|||||||
|
|
||||||
options.Run(func(gen *protogen.Plugin) error {
|
options.Run(func(gen *protogen.Plugin) error {
|
||||||
handlerFlag, serviceFlag := false, false
|
handlerFlag, serviceFlag := false, false
|
||||||
pluginNames := strings.Split(*plugins, ",")
|
pluginName := strings.ReplaceAll(plugin, " ", "")
|
||||||
for _, plugin := range pluginNames {
|
switch pluginName {
|
||||||
switch strings.ReplaceAll(plugin, " ", "") {
|
|
||||||
case "handler":
|
case "handler":
|
||||||
handlerFlag = true
|
handlerFlag = true
|
||||||
case "service":
|
case "service":
|
||||||
@@ -53,7 +63,6 @@ func main() {
|
|||||||
default:
|
default:
|
||||||
return fmt.Errorf("protoc-gen-go-gin: unknown plugin %q", plugin)
|
return fmt.Errorf("protoc-gen-go-gin: unknown plugin %q", plugin)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
gen.SupportedFeatures = uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL)
|
gen.SupportedFeatures = uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL)
|
||||||
for _, f := range gen.Files {
|
for _, f := range gen.Files {
|
||||||
@@ -63,12 +72,66 @@ func main() {
|
|||||||
router.GenerateFile(gen, f)
|
router.GenerateFile(gen, f)
|
||||||
|
|
||||||
if handlerFlag {
|
if handlerFlag {
|
||||||
handler.GenerateFile(gen, f)
|
filename, gf := handler.GenerateFile(gen, f)
|
||||||
|
content, err := gf.Content()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
if serviceFlag {
|
err = saveFile(moduleName, serverName, out, filename, content)
|
||||||
service.GenerateFile(gen, f)
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
gf.Skip()
|
||||||
|
} else if serviceFlag {
|
||||||
|
filename, gf := service.GenerateFile(gen, f)
|
||||||
|
content, err := gf.Content()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = saveFile(moduleName, serverName, out, filename, content)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
gf.Skip()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func saveFile(moduleName string, serverName string, out string, filename string, content []byte) error {
|
||||||
|
if moduleName == "" {
|
||||||
|
panic("--go-gin_opt option error, 'moduleName' cannot be empty\n" +
|
||||||
|
" usage example: --go-gin_opt=moduleName=yourModuleName --go-gin_opt=serverName=yourServerName --go-gin_opt=out=internal/service")
|
||||||
|
}
|
||||||
|
if serverName == "" {
|
||||||
|
panic("--go-gin_opt option error, 'serverName' cannot be empty\n" +
|
||||||
|
" usage example: --go-gin_opt=moduleName=yourModuleName --go-gin_opt=serverName=yourServerName --go-gin_opt=out=internal/service")
|
||||||
|
}
|
||||||
|
if out == "" {
|
||||||
|
panic("--go-gin_opt option error, 'out' cannot be empty\n" +
|
||||||
|
" usage example: --go-gin_opt=moduleName=yourModuleName --go-gin_opt=serverName=yourServerName --go-gin_opt=out=internal/service")
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = os.MkdirAll(out, 0666)
|
||||||
|
_, name := filepath.Split(filename)
|
||||||
|
file := out + "/" + name
|
||||||
|
if isExists(file) {
|
||||||
|
err := os.Rename(file, file+".bak."+time.Now().Format("150405"))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
content = bytes.ReplaceAll(content, []byte("module_name_example"), []byte(moduleName))
|
||||||
|
content = bytes.ReplaceAll(content, []byte("server_name_example"), []byte(serverName))
|
||||||
|
return os.WriteFile(file, content, 0666)
|
||||||
|
}
|
||||||
|
|
||||||
|
func isExists(path string) bool {
|
||||||
|
_, err := os.Stat(path)
|
||||||
|
if err != nil {
|
||||||
|
return !os.IsNotExist(err)
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,103 +0,0 @@
|
|||||||
package initial
|
|
||||||
|
|
||||||
import (
|
|
||||||
"flag"
|
|
||||||
"fmt"
|
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"github.com/zhufuyi/sponge/configs"
|
|
||||||
"github.com/zhufuyi/sponge/internal/config"
|
|
||||||
"github.com/zhufuyi/sponge/internal/model"
|
|
||||||
|
|
||||||
"github.com/zhufuyi/sponge/pkg/logger"
|
|
||||||
"github.com/zhufuyi/sponge/pkg/nacoscli"
|
|
||||||
"github.com/zhufuyi/sponge/pkg/stat"
|
|
||||||
"github.com/zhufuyi/sponge/pkg/tracer"
|
|
||||||
|
|
||||||
"github.com/jinzhu/copier"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
version string
|
|
||||||
configFile string
|
|
||||||
enableConfigCenter bool
|
|
||||||
)
|
|
||||||
|
|
||||||
// Config 初始化配置
|
|
||||||
func Config() {
|
|
||||||
initConfig()
|
|
||||||
cfg := config.Get()
|
|
||||||
|
|
||||||
// 初始化日志
|
|
||||||
_, _ = logger.Init(
|
|
||||||
logger.WithLevel(cfg.Logger.Level),
|
|
||||||
logger.WithFormat(cfg.Logger.Format),
|
|
||||||
logger.WithSave(cfg.Logger.IsSave),
|
|
||||||
)
|
|
||||||
|
|
||||||
// 初始化数据库
|
|
||||||
model.InitMysql()
|
|
||||||
model.InitCache(cfg.App.CacheType)
|
|
||||||
|
|
||||||
// 初始化链路跟踪
|
|
||||||
if cfg.App.EnableTracing {
|
|
||||||
tracer.InitWithConfig(
|
|
||||||
cfg.App.Name,
|
|
||||||
cfg.App.Env,
|
|
||||||
cfg.App.Version,
|
|
||||||
cfg.Jaeger.AgentHost,
|
|
||||||
strconv.Itoa(cfg.Jaeger.AgentPort),
|
|
||||||
cfg.App.TracingSamplingRate,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 初始化打印系统和进程资源
|
|
||||||
if cfg.App.EnableStat {
|
|
||||||
stat.Init(stat.WithLog(logger.Get()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 初始化配置
|
|
||||||
func initConfig() {
|
|
||||||
flag.StringVar(&version, "version", "", "service Version Number")
|
|
||||||
flag.BoolVar(&enableConfigCenter, "enable-cc", false, "whether to get from the configuration center, "+
|
|
||||||
"if true, the '-c' parameter indicates the configuration center")
|
|
||||||
flag.StringVar(&configFile, "c", "", "configuration file")
|
|
||||||
flag.Parse()
|
|
||||||
|
|
||||||
if enableConfigCenter {
|
|
||||||
// 从配置中心获取配置(先获取nacos配置,再根据nacos配置中心读取服务配置)
|
|
||||||
if configFile == "" {
|
|
||||||
configFile = configs.Path("serverNameExample_cc.yml")
|
|
||||||
}
|
|
||||||
nacosConfig, err := config.NewCenter(configFile)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
appConfig := &config.Config{}
|
|
||||||
params := &nacoscli.Params{}
|
|
||||||
_ = copier.Copy(params, &nacosConfig.Nacos)
|
|
||||||
err = nacoscli.Init(appConfig, params)
|
|
||||||
if err != nil {
|
|
||||||
panic(fmt.Sprintf("connect to configuration center err, %v", err))
|
|
||||||
}
|
|
||||||
if appConfig.App.Name == "" {
|
|
||||||
panic("read the config from center error, config data is empty")
|
|
||||||
}
|
|
||||||
config.Set(appConfig)
|
|
||||||
} else {
|
|
||||||
// 从本地配置文件获取配置
|
|
||||||
if configFile == "" {
|
|
||||||
configFile = configs.Path("serverNameExample.yml")
|
|
||||||
}
|
|
||||||
err := config.Init(configFile)
|
|
||||||
if err != nil {
|
|
||||||
panic("init config error: " + err.Error())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if version != "" {
|
|
||||||
config.Get().App.Version = version
|
|
||||||
}
|
|
||||||
//fmt.Println(config.Show())
|
|
||||||
}
|
|
||||||
@@ -1,44 +0,0 @@
|
|||||||
package initial
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/zhufuyi/sponge/internal/config"
|
|
||||||
"github.com/zhufuyi/sponge/internal/model"
|
|
||||||
|
|
||||||
"github.com/zhufuyi/sponge/pkg/app"
|
|
||||||
"github.com/zhufuyi/sponge/pkg/tracer"
|
|
||||||
)
|
|
||||||
|
|
||||||
// RegisterClose 注册app需要释放的资源
|
|
||||||
func RegisterClose(servers []app.IServer) []app.Close {
|
|
||||||
var closes []app.Close
|
|
||||||
|
|
||||||
// 关闭服务
|
|
||||||
for _, s := range servers {
|
|
||||||
closes = append(closes, s.Stop)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 关闭mysql
|
|
||||||
closes = append(closes, func() error {
|
|
||||||
return model.CloseMysql()
|
|
||||||
})
|
|
||||||
|
|
||||||
// 关闭redis
|
|
||||||
if config.Get().App.CacheType == "redis" {
|
|
||||||
closes = append(closes, func() error {
|
|
||||||
return model.CloseRedis()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// 关闭trace
|
|
||||||
if config.Get().App.EnableTracing {
|
|
||||||
closes = append(closes, func() error {
|
|
||||||
ctx, _ := context.WithTimeout(context.Background(), 2*time.Second) //nolint
|
|
||||||
return tracer.Close(ctx)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
return closes
|
|
||||||
}
|
|
||||||
@@ -1,82 +0,0 @@
|
|||||||
package initial
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"strconv"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/zhufuyi/sponge/internal/config"
|
|
||||||
"github.com/zhufuyi/sponge/internal/server"
|
|
||||||
|
|
||||||
"github.com/zhufuyi/sponge/pkg/app"
|
|
||||||
"github.com/zhufuyi/sponge/pkg/servicerd/registry"
|
|
||||||
"github.com/zhufuyi/sponge/pkg/servicerd/registry/consul"
|
|
||||||
"github.com/zhufuyi/sponge/pkg/servicerd/registry/etcd"
|
|
||||||
"github.com/zhufuyi/sponge/pkg/servicerd/registry/nacos"
|
|
||||||
)
|
|
||||||
|
|
||||||
// RegisterServers 注册app服务
|
|
||||||
func RegisterServers() []app.IServer {
|
|
||||||
var cfg = config.Get()
|
|
||||||
var servers []app.IServer
|
|
||||||
|
|
||||||
// 创建grpc服务
|
|
||||||
grpcAddr := ":" + strconv.Itoa(cfg.Grpc.Port)
|
|
||||||
grpcRegistry, grpcInstance := registryService("grpc", cfg.App.Host, cfg.Grpc.Port)
|
|
||||||
grpcServer := server.NewGRPCServer(grpcAddr,
|
|
||||||
server.WithGrpcReadTimeout(time.Duration(cfg.Grpc.ReadTimeout)*time.Second),
|
|
||||||
server.WithGrpcWriteTimeout(time.Duration(cfg.Grpc.WriteTimeout)*time.Second),
|
|
||||||
server.WithGrpcRegistry(grpcRegistry, grpcInstance),
|
|
||||||
)
|
|
||||||
servers = append(servers, grpcServer)
|
|
||||||
|
|
||||||
return servers
|
|
||||||
}
|
|
||||||
|
|
||||||
func registryService(scheme string, host string, port int) (registry.Registry, *registry.ServiceInstance) {
|
|
||||||
instanceEndpoint := fmt.Sprintf("%s://%s:%d", scheme, host, port)
|
|
||||||
cfg := config.Get()
|
|
||||||
|
|
||||||
switch cfg.App.RegistryDiscoveryType {
|
|
||||||
// 使用consul注册服务
|
|
||||||
case "consul":
|
|
||||||
iRegistry, instance, err := consul.NewRegistry(
|
|
||||||
cfg.Consul.Addr,
|
|
||||||
cfg.App.Name+"_"+scheme+"_"+host,
|
|
||||||
cfg.App.Name,
|
|
||||||
[]string{instanceEndpoint},
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
return iRegistry, instance
|
|
||||||
// 使用etcd注册服务
|
|
||||||
case "etcd":
|
|
||||||
iRegistry, instance, err := etcd.NewRegistry(
|
|
||||||
cfg.Etcd.Addrs,
|
|
||||||
cfg.App.Name+"_"+scheme+"_"+host,
|
|
||||||
cfg.App.Name,
|
|
||||||
[]string{instanceEndpoint},
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
return iRegistry, instance
|
|
||||||
// 使用nacos注册服务
|
|
||||||
case "nacos":
|
|
||||||
iRegistry, instance, err := nacos.NewRegistry(
|
|
||||||
cfg.NacosRd.IPAddr,
|
|
||||||
cfg.NacosRd.Port,
|
|
||||||
cfg.NacosRd.NamespaceID,
|
|
||||||
cfg.App.Name+"_"+scheme+"_"+host,
|
|
||||||
cfg.App.Name,
|
|
||||||
[]string{instanceEndpoint},
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
return iRegistry, instance
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/zhufuyi/sponge/cmd/serverNameExample_grpcExample/initial"
|
|
||||||
|
|
||||||
"github.com/zhufuyi/sponge/pkg/app"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
initial.Config()
|
|
||||||
servers := initial.RegisterServers()
|
|
||||||
closes := initial.RegisterClose(servers)
|
|
||||||
|
|
||||||
a := app.New(servers, closes)
|
|
||||||
a.Run()
|
|
||||||
}
|
|
||||||
@@ -1,102 +0,0 @@
|
|||||||
package initial
|
|
||||||
|
|
||||||
import (
|
|
||||||
"flag"
|
|
||||||
"fmt"
|
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"github.com/zhufuyi/sponge/configs"
|
|
||||||
"github.com/zhufuyi/sponge/internal/config"
|
|
||||||
"github.com/zhufuyi/sponge/internal/rpcclient"
|
|
||||||
|
|
||||||
"github.com/zhufuyi/sponge/pkg/logger"
|
|
||||||
"github.com/zhufuyi/sponge/pkg/nacoscli"
|
|
||||||
"github.com/zhufuyi/sponge/pkg/stat"
|
|
||||||
"github.com/zhufuyi/sponge/pkg/tracer"
|
|
||||||
|
|
||||||
"github.com/jinzhu/copier"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
version string
|
|
||||||
configFile string
|
|
||||||
enableConfigCenter bool
|
|
||||||
)
|
|
||||||
|
|
||||||
// Config 初始化配置
|
|
||||||
func Config() {
|
|
||||||
initConfig()
|
|
||||||
cfg := config.Get()
|
|
||||||
|
|
||||||
// 初始化日志
|
|
||||||
_, _ = logger.Init(
|
|
||||||
logger.WithLevel(cfg.Logger.Level),
|
|
||||||
logger.WithFormat(cfg.Logger.Format),
|
|
||||||
logger.WithSave(cfg.Logger.IsSave),
|
|
||||||
)
|
|
||||||
|
|
||||||
// 初始化链路跟踪
|
|
||||||
if cfg.App.EnableTracing {
|
|
||||||
tracer.InitWithConfig(
|
|
||||||
cfg.App.Name,
|
|
||||||
cfg.App.Env,
|
|
||||||
cfg.App.Version,
|
|
||||||
cfg.Jaeger.AgentHost,
|
|
||||||
strconv.Itoa(cfg.Jaeger.AgentPort),
|
|
||||||
cfg.App.TracingSamplingRate,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 初始化rpc服务连接
|
|
||||||
rpcclient.NewServerNameExampleRPCConn()
|
|
||||||
|
|
||||||
// 初始化打印系统和进程资源
|
|
||||||
if cfg.App.EnableStat {
|
|
||||||
stat.Init(stat.WithLog(logger.Get()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 初始化配置
|
|
||||||
func initConfig() {
|
|
||||||
flag.StringVar(&version, "version", "", "service Version Number")
|
|
||||||
flag.BoolVar(&enableConfigCenter, "enable-cc", false, "whether to get from the configuration center, "+
|
|
||||||
"if true, the '-c' parameter indicates the configuration center")
|
|
||||||
flag.StringVar(&configFile, "c", "", "configuration file")
|
|
||||||
flag.Parse()
|
|
||||||
|
|
||||||
if enableConfigCenter {
|
|
||||||
// 从配置中心获取配置(先获取nacos配置,再根据nacos配置中心读取服务配置)
|
|
||||||
if configFile == "" {
|
|
||||||
configFile = configs.Path("serverNameExample_cc.yml")
|
|
||||||
}
|
|
||||||
nacosConfig, err := config.NewCenter(configFile)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
appConfig := &config.Config{}
|
|
||||||
params := &nacoscli.Params{}
|
|
||||||
_ = copier.Copy(params, &nacosConfig.Nacos)
|
|
||||||
err = nacoscli.Init(appConfig, params)
|
|
||||||
if err != nil {
|
|
||||||
panic(fmt.Sprintf("connect to configuration center err, %v", err))
|
|
||||||
}
|
|
||||||
if appConfig.App.Name == "" {
|
|
||||||
panic("read the config from center error, config data is empty")
|
|
||||||
}
|
|
||||||
config.Set(appConfig)
|
|
||||||
} else {
|
|
||||||
// 从本地配置文件获取配置
|
|
||||||
if configFile == "" {
|
|
||||||
configFile = configs.Path("serverNameExample.yml")
|
|
||||||
}
|
|
||||||
err := config.Init(configFile)
|
|
||||||
if err != nil {
|
|
||||||
panic("init config error: " + err.Error())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if version != "" {
|
|
||||||
config.Get().App.Version = version
|
|
||||||
}
|
|
||||||
//fmt.Println(config.Show())
|
|
||||||
}
|
|
||||||
@@ -1,37 +0,0 @@
|
|||||||
package initial
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/zhufuyi/sponge/internal/config"
|
|
||||||
"github.com/zhufuyi/sponge/internal/rpcclient"
|
|
||||||
|
|
||||||
"github.com/zhufuyi/sponge/pkg/app"
|
|
||||||
"github.com/zhufuyi/sponge/pkg/tracer"
|
|
||||||
)
|
|
||||||
|
|
||||||
// RegisterClose 注册app需要释放的资源
|
|
||||||
func RegisterClose(servers []app.IServer) []app.Close {
|
|
||||||
var closes []app.Close
|
|
||||||
|
|
||||||
// 关闭服务
|
|
||||||
for _, s := range servers {
|
|
||||||
closes = append(closes, s.Stop)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 关闭rpc client连接
|
|
||||||
closes = append(closes, func() error {
|
|
||||||
return rpcclient.CloseServerNameExampleRPCConn()
|
|
||||||
})
|
|
||||||
|
|
||||||
// 关闭trace
|
|
||||||
if config.Get().App.EnableTracing {
|
|
||||||
closes = append(closes, func() error {
|
|
||||||
ctx, _ := context.WithTimeout(context.Background(), 2*time.Second) //nolint
|
|
||||||
return tracer.Close(ctx)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
return closes
|
|
||||||
}
|
|
||||||
@@ -1,83 +0,0 @@
|
|||||||
package initial
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"strconv"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/zhufuyi/sponge/internal/config"
|
|
||||||
"github.com/zhufuyi/sponge/internal/server"
|
|
||||||
|
|
||||||
"github.com/zhufuyi/sponge/pkg/app"
|
|
||||||
"github.com/zhufuyi/sponge/pkg/servicerd/registry"
|
|
||||||
"github.com/zhufuyi/sponge/pkg/servicerd/registry/consul"
|
|
||||||
"github.com/zhufuyi/sponge/pkg/servicerd/registry/etcd"
|
|
||||||
"github.com/zhufuyi/sponge/pkg/servicerd/registry/nacos"
|
|
||||||
)
|
|
||||||
|
|
||||||
// RegisterServers 注册app服务
|
|
||||||
func RegisterServers() []app.IServer {
|
|
||||||
var cfg = config.Get()
|
|
||||||
var servers []app.IServer
|
|
||||||
|
|
||||||
// 创建http服务
|
|
||||||
httpAddr := ":" + strconv.Itoa(cfg.HTTP.Port)
|
|
||||||
httpRegistry, httpInstance := registryService("http", cfg.App.Host, cfg.HTTP.Port)
|
|
||||||
httpServer := server.NewHTTPServer(httpAddr,
|
|
||||||
server.WithHTTPReadTimeout(time.Second*time.Duration(cfg.HTTP.ReadTimeout)),
|
|
||||||
server.WithHTTPWriteTimeout(time.Second*time.Duration(cfg.HTTP.WriteTimeout)),
|
|
||||||
server.WithHTTPRegistry(httpRegistry, httpInstance),
|
|
||||||
server.WithHTTPIsProd(cfg.App.Env == "prod"),
|
|
||||||
)
|
|
||||||
servers = append(servers, httpServer)
|
|
||||||
|
|
||||||
return servers
|
|
||||||
}
|
|
||||||
|
|
||||||
func registryService(scheme string, host string, port int) (registry.Registry, *registry.ServiceInstance) {
|
|
||||||
instanceEndpoint := fmt.Sprintf("%s://%s:%d", scheme, host, port)
|
|
||||||
cfg := config.Get()
|
|
||||||
|
|
||||||
switch cfg.App.RegistryDiscoveryType {
|
|
||||||
// 使用consul注册服务
|
|
||||||
case "consul":
|
|
||||||
iRegistry, instance, err := consul.NewRegistry(
|
|
||||||
cfg.Consul.Addr,
|
|
||||||
cfg.App.Name+"_"+scheme+"_"+host,
|
|
||||||
cfg.App.Name,
|
|
||||||
[]string{instanceEndpoint},
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
return iRegistry, instance
|
|
||||||
// 使用etcd注册服务
|
|
||||||
case "etcd":
|
|
||||||
iRegistry, instance, err := etcd.NewRegistry(
|
|
||||||
cfg.Etcd.Addrs,
|
|
||||||
cfg.App.Name+"_"+scheme+"_"+host,
|
|
||||||
cfg.App.Name,
|
|
||||||
[]string{instanceEndpoint},
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
return iRegistry, instance
|
|
||||||
// 使用nacos注册服务
|
|
||||||
case "nacos":
|
|
||||||
iRegistry, instance, err := nacos.NewRegistry(
|
|
||||||
cfg.NacosRd.IPAddr,
|
|
||||||
cfg.NacosRd.Port,
|
|
||||||
cfg.NacosRd.NamespaceID,
|
|
||||||
cfg.App.Name+"_"+scheme+"_"+host,
|
|
||||||
cfg.App.Name,
|
|
||||||
[]string{instanceEndpoint},
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
return iRegistry, instance
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/zhufuyi/sponge/cmd/serverNameExample_gwExample/initial"
|
|
||||||
|
|
||||||
"github.com/zhufuyi/sponge/pkg/app"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
initial.Config()
|
|
||||||
servers := initial.RegisterServers()
|
|
||||||
closes := initial.RegisterClose(servers)
|
|
||||||
|
|
||||||
a := app.New(servers, closes)
|
|
||||||
a.Run()
|
|
||||||
}
|
|
||||||
@@ -1,103 +0,0 @@
|
|||||||
package initial
|
|
||||||
|
|
||||||
import (
|
|
||||||
"flag"
|
|
||||||
"fmt"
|
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"github.com/zhufuyi/sponge/configs"
|
|
||||||
"github.com/zhufuyi/sponge/internal/config"
|
|
||||||
"github.com/zhufuyi/sponge/internal/model"
|
|
||||||
|
|
||||||
"github.com/zhufuyi/sponge/pkg/logger"
|
|
||||||
"github.com/zhufuyi/sponge/pkg/nacoscli"
|
|
||||||
"github.com/zhufuyi/sponge/pkg/stat"
|
|
||||||
"github.com/zhufuyi/sponge/pkg/tracer"
|
|
||||||
|
|
||||||
"github.com/jinzhu/copier"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
version string
|
|
||||||
configFile string
|
|
||||||
enableConfigCenter bool
|
|
||||||
)
|
|
||||||
|
|
||||||
// Config 初始化配置
|
|
||||||
func Config() {
|
|
||||||
initConfig()
|
|
||||||
cfg := config.Get()
|
|
||||||
|
|
||||||
// 初始化日志
|
|
||||||
_, _ = logger.Init(
|
|
||||||
logger.WithLevel(cfg.Logger.Level),
|
|
||||||
logger.WithFormat(cfg.Logger.Format),
|
|
||||||
logger.WithSave(cfg.Logger.IsSave),
|
|
||||||
)
|
|
||||||
|
|
||||||
// 初始化数据库
|
|
||||||
model.InitMysql()
|
|
||||||
model.InitCache(cfg.App.CacheType)
|
|
||||||
|
|
||||||
// 初始化链路跟踪
|
|
||||||
if cfg.App.EnableTracing {
|
|
||||||
tracer.InitWithConfig(
|
|
||||||
cfg.App.Name,
|
|
||||||
cfg.App.Env,
|
|
||||||
cfg.App.Version,
|
|
||||||
cfg.Jaeger.AgentHost,
|
|
||||||
strconv.Itoa(cfg.Jaeger.AgentPort),
|
|
||||||
cfg.App.TracingSamplingRate,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 初始化打印系统和进程资源
|
|
||||||
if cfg.App.EnableStat {
|
|
||||||
stat.Init(stat.WithLog(logger.Get()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 初始化配置
|
|
||||||
func initConfig() {
|
|
||||||
flag.StringVar(&version, "version", "", "service Version Number")
|
|
||||||
flag.BoolVar(&enableConfigCenter, "enable-cc", false, "whether to get from the configuration center, "+
|
|
||||||
"if true, the '-c' parameter indicates the configuration center")
|
|
||||||
flag.StringVar(&configFile, "c", "", "configuration file")
|
|
||||||
flag.Parse()
|
|
||||||
|
|
||||||
if enableConfigCenter {
|
|
||||||
// 从配置中心获取配置(先获取nacos配置,再根据nacos配置中心读取服务配置)
|
|
||||||
if configFile == "" {
|
|
||||||
configFile = configs.Path("serverNameExample_cc.yml")
|
|
||||||
}
|
|
||||||
nacosConfig, err := config.NewCenter(configFile)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
appConfig := &config.Config{}
|
|
||||||
params := &nacoscli.Params{}
|
|
||||||
_ = copier.Copy(params, &nacosConfig.Nacos)
|
|
||||||
err = nacoscli.Init(appConfig, params)
|
|
||||||
if err != nil {
|
|
||||||
panic(fmt.Sprintf("connect to configuration center err, %v", err))
|
|
||||||
}
|
|
||||||
if appConfig.App.Name == "" {
|
|
||||||
panic("read the config from center error, config data is empty")
|
|
||||||
}
|
|
||||||
config.Set(appConfig)
|
|
||||||
} else {
|
|
||||||
// 从本地配置文件获取配置
|
|
||||||
if configFile == "" {
|
|
||||||
configFile = configs.Path("serverNameExample.yml")
|
|
||||||
}
|
|
||||||
err := config.Init(configFile)
|
|
||||||
if err != nil {
|
|
||||||
panic("init config error: " + err.Error())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if version != "" {
|
|
||||||
config.Get().App.Version = version
|
|
||||||
}
|
|
||||||
//fmt.Println(config.Show())
|
|
||||||
}
|
|
||||||
@@ -1,44 +0,0 @@
|
|||||||
package initial
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/zhufuyi/sponge/internal/config"
|
|
||||||
"github.com/zhufuyi/sponge/internal/model"
|
|
||||||
|
|
||||||
"github.com/zhufuyi/sponge/pkg/app"
|
|
||||||
"github.com/zhufuyi/sponge/pkg/tracer"
|
|
||||||
)
|
|
||||||
|
|
||||||
// RegisterClose 注册app需要释放的资源
|
|
||||||
func RegisterClose(servers []app.IServer) []app.Close {
|
|
||||||
var closes []app.Close
|
|
||||||
|
|
||||||
// 关闭服务
|
|
||||||
for _, s := range servers {
|
|
||||||
closes = append(closes, s.Stop)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 关闭mysql
|
|
||||||
closes = append(closes, func() error {
|
|
||||||
return model.CloseMysql()
|
|
||||||
})
|
|
||||||
|
|
||||||
// 关闭redis
|
|
||||||
if config.Get().App.CacheType == "redis" {
|
|
||||||
closes = append(closes, func() error {
|
|
||||||
return model.CloseRedis()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// 关闭trace
|
|
||||||
if config.Get().App.EnableTracing {
|
|
||||||
closes = append(closes, func() error {
|
|
||||||
ctx, _ := context.WithTimeout(context.Background(), 2*time.Second) //nolint
|
|
||||||
return tracer.Close(ctx)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
return closes
|
|
||||||
}
|
|
||||||
@@ -1,83 +0,0 @@
|
|||||||
package initial
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"strconv"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/zhufuyi/sponge/internal/config"
|
|
||||||
"github.com/zhufuyi/sponge/internal/server"
|
|
||||||
|
|
||||||
"github.com/zhufuyi/sponge/pkg/app"
|
|
||||||
"github.com/zhufuyi/sponge/pkg/servicerd/registry"
|
|
||||||
"github.com/zhufuyi/sponge/pkg/servicerd/registry/consul"
|
|
||||||
"github.com/zhufuyi/sponge/pkg/servicerd/registry/etcd"
|
|
||||||
"github.com/zhufuyi/sponge/pkg/servicerd/registry/nacos"
|
|
||||||
)
|
|
||||||
|
|
||||||
// RegisterServers 注册app服务
|
|
||||||
func RegisterServers() []app.IServer {
|
|
||||||
var cfg = config.Get()
|
|
||||||
var servers []app.IServer
|
|
||||||
|
|
||||||
// 创建http服务
|
|
||||||
httpAddr := ":" + strconv.Itoa(cfg.HTTP.Port)
|
|
||||||
httpRegistry, httpInstance := registryService("http", cfg.App.Host, cfg.HTTP.Port)
|
|
||||||
httpServer := server.NewHTTPServer(httpAddr,
|
|
||||||
server.WithHTTPReadTimeout(time.Second*time.Duration(cfg.HTTP.ReadTimeout)),
|
|
||||||
server.WithHTTPWriteTimeout(time.Second*time.Duration(cfg.HTTP.WriteTimeout)),
|
|
||||||
server.WithHTTPRegistry(httpRegistry, httpInstance),
|
|
||||||
server.WithHTTPIsProd(cfg.App.Env == "prod"),
|
|
||||||
)
|
|
||||||
servers = append(servers, httpServer)
|
|
||||||
|
|
||||||
return servers
|
|
||||||
}
|
|
||||||
|
|
||||||
func registryService(scheme string, host string, port int) (registry.Registry, *registry.ServiceInstance) {
|
|
||||||
instanceEndpoint := fmt.Sprintf("%s://%s:%d", scheme, host, port)
|
|
||||||
cfg := config.Get()
|
|
||||||
|
|
||||||
switch cfg.App.RegistryDiscoveryType {
|
|
||||||
// 使用consul注册服务
|
|
||||||
case "consul":
|
|
||||||
iRegistry, instance, err := consul.NewRegistry(
|
|
||||||
cfg.Consul.Addr,
|
|
||||||
cfg.App.Name+"_"+scheme+"_"+host,
|
|
||||||
cfg.App.Name,
|
|
||||||
[]string{instanceEndpoint},
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
return iRegistry, instance
|
|
||||||
// 使用etcd注册服务
|
|
||||||
case "etcd":
|
|
||||||
iRegistry, instance, err := etcd.NewRegistry(
|
|
||||||
cfg.Etcd.Addrs,
|
|
||||||
cfg.App.Name+"_"+scheme+"_"+host,
|
|
||||||
cfg.App.Name,
|
|
||||||
[]string{instanceEndpoint},
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
return iRegistry, instance
|
|
||||||
// 使用nacos注册服务
|
|
||||||
case "nacos":
|
|
||||||
iRegistry, instance, err := nacos.NewRegistry(
|
|
||||||
cfg.NacosRd.IPAddr,
|
|
||||||
cfg.NacosRd.Port,
|
|
||||||
cfg.NacosRd.NamespaceID,
|
|
||||||
cfg.App.Name+"_"+scheme+"_"+host,
|
|
||||||
cfg.App.Name,
|
|
||||||
[]string{instanceEndpoint},
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
return iRegistry, instance
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/zhufuyi/sponge/cmd/serverNameExample_httpExample/initial"
|
|
||||||
|
|
||||||
"github.com/zhufuyi/sponge/pkg/app"
|
|
||||||
)
|
|
||||||
|
|
||||||
// @title serverNameExample api docs
|
|
||||||
// @description http server api docs
|
|
||||||
// @schemes http https
|
|
||||||
// @version v0.0.0
|
|
||||||
// @host localhost:8080
|
|
||||||
func main() {
|
|
||||||
initial.Config()
|
|
||||||
servers := initial.RegisterServers()
|
|
||||||
closes := initial.RegisterClose(servers)
|
|
||||||
|
|
||||||
a := app.New(servers, closes)
|
|
||||||
a.Run()
|
|
||||||
}
|
|
||||||
@@ -1,103 +0,0 @@
|
|||||||
package initial
|
|
||||||
|
|
||||||
import (
|
|
||||||
"flag"
|
|
||||||
"fmt"
|
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"github.com/zhufuyi/sponge/configs"
|
|
||||||
"github.com/zhufuyi/sponge/internal/config"
|
|
||||||
"github.com/zhufuyi/sponge/internal/model"
|
|
||||||
|
|
||||||
"github.com/zhufuyi/sponge/pkg/logger"
|
|
||||||
"github.com/zhufuyi/sponge/pkg/nacoscli"
|
|
||||||
"github.com/zhufuyi/sponge/pkg/stat"
|
|
||||||
"github.com/zhufuyi/sponge/pkg/tracer"
|
|
||||||
|
|
||||||
"github.com/jinzhu/copier"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
version string
|
|
||||||
configFile string
|
|
||||||
enableConfigCenter bool
|
|
||||||
)
|
|
||||||
|
|
||||||
// Config 初始化配置
|
|
||||||
func Config() {
|
|
||||||
initConfig()
|
|
||||||
cfg := config.Get()
|
|
||||||
|
|
||||||
// 初始化日志
|
|
||||||
_, _ = logger.Init(
|
|
||||||
logger.WithLevel(cfg.Logger.Level),
|
|
||||||
logger.WithFormat(cfg.Logger.Format),
|
|
||||||
logger.WithSave(cfg.Logger.IsSave),
|
|
||||||
)
|
|
||||||
|
|
||||||
// 初始化数据库
|
|
||||||
model.InitMysql()
|
|
||||||
model.InitCache(cfg.App.CacheType)
|
|
||||||
|
|
||||||
// 初始化链路跟踪
|
|
||||||
if cfg.App.EnableTracing {
|
|
||||||
tracer.InitWithConfig(
|
|
||||||
cfg.App.Name,
|
|
||||||
cfg.App.Env,
|
|
||||||
cfg.App.Version,
|
|
||||||
cfg.Jaeger.AgentHost,
|
|
||||||
strconv.Itoa(cfg.Jaeger.AgentPort),
|
|
||||||
cfg.App.TracingSamplingRate,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 初始化打印系统和进程资源
|
|
||||||
if cfg.App.EnableStat {
|
|
||||||
stat.Init(stat.WithLog(logger.Get()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 初始化配置
|
|
||||||
func initConfig() {
|
|
||||||
flag.StringVar(&version, "version", "", "service Version Number")
|
|
||||||
flag.BoolVar(&enableConfigCenter, "enable-cc", false, "whether to get from the configuration center, "+
|
|
||||||
"if true, the '-c' parameter indicates the configuration center")
|
|
||||||
flag.StringVar(&configFile, "c", "", "configuration file")
|
|
||||||
flag.Parse()
|
|
||||||
|
|
||||||
if enableConfigCenter {
|
|
||||||
// 从配置中心获取配置(先获取nacos配置,再根据nacos配置中心读取服务配置)
|
|
||||||
if configFile == "" {
|
|
||||||
configFile = configs.Path("serverNameExample_cc.yml")
|
|
||||||
}
|
|
||||||
nacosConfig, err := config.NewCenter(configFile)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
appConfig := &config.Config{}
|
|
||||||
params := &nacoscli.Params{}
|
|
||||||
_ = copier.Copy(params, &nacosConfig.Nacos)
|
|
||||||
err = nacoscli.Init(appConfig, params)
|
|
||||||
if err != nil {
|
|
||||||
panic(fmt.Sprintf("connect to configuration center err, %v", err))
|
|
||||||
}
|
|
||||||
if appConfig.App.Name == "" {
|
|
||||||
panic("read the config from center error, config data is empty")
|
|
||||||
}
|
|
||||||
config.Set(appConfig)
|
|
||||||
} else {
|
|
||||||
// 从本地配置文件获取配置
|
|
||||||
if configFile == "" {
|
|
||||||
configFile = configs.Path("serverNameExample.yml")
|
|
||||||
}
|
|
||||||
err := config.Init(configFile)
|
|
||||||
if err != nil {
|
|
||||||
panic("init config error: " + err.Error())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if version != "" {
|
|
||||||
config.Get().App.Version = version
|
|
||||||
}
|
|
||||||
//fmt.Println(config.Show())
|
|
||||||
}
|
|
||||||
@@ -1,44 +0,0 @@
|
|||||||
package initial
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/zhufuyi/sponge/internal/config"
|
|
||||||
"github.com/zhufuyi/sponge/internal/model"
|
|
||||||
|
|
||||||
"github.com/zhufuyi/sponge/pkg/app"
|
|
||||||
"github.com/zhufuyi/sponge/pkg/tracer"
|
|
||||||
)
|
|
||||||
|
|
||||||
// RegisterClose 注册app需要释放的资源
|
|
||||||
func RegisterClose(servers []app.IServer) []app.Close {
|
|
||||||
var closes []app.Close
|
|
||||||
|
|
||||||
// 关闭服务
|
|
||||||
for _, s := range servers {
|
|
||||||
closes = append(closes, s.Stop)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 关闭mysql
|
|
||||||
closes = append(closes, func() error {
|
|
||||||
return model.CloseMysql()
|
|
||||||
})
|
|
||||||
|
|
||||||
// 关闭redis
|
|
||||||
if config.Get().App.CacheType == "redis" {
|
|
||||||
closes = append(closes, func() error {
|
|
||||||
return model.CloseRedis()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// 关闭trace
|
|
||||||
if config.Get().App.EnableTracing {
|
|
||||||
closes = append(closes, func() error {
|
|
||||||
ctx, _ := context.WithTimeout(context.Background(), 2*time.Second) //nolint
|
|
||||||
return tracer.Close(ctx)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
return closes
|
|
||||||
}
|
|
||||||
@@ -1,93 +0,0 @@
|
|||||||
package initial
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"strconv"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/zhufuyi/sponge/internal/config"
|
|
||||||
"github.com/zhufuyi/sponge/internal/server"
|
|
||||||
|
|
||||||
"github.com/zhufuyi/sponge/pkg/app"
|
|
||||||
"github.com/zhufuyi/sponge/pkg/servicerd/registry"
|
|
||||||
"github.com/zhufuyi/sponge/pkg/servicerd/registry/consul"
|
|
||||||
"github.com/zhufuyi/sponge/pkg/servicerd/registry/etcd"
|
|
||||||
"github.com/zhufuyi/sponge/pkg/servicerd/registry/nacos"
|
|
||||||
)
|
|
||||||
|
|
||||||
// RegisterServers 注册app服务
|
|
||||||
func RegisterServers() []app.IServer {
|
|
||||||
var cfg = config.Get()
|
|
||||||
var servers []app.IServer
|
|
||||||
|
|
||||||
// 创建http服务
|
|
||||||
httpAddr := ":" + strconv.Itoa(cfg.HTTP.Port)
|
|
||||||
httpRegistry, httpInstance := registryService("http", cfg.App.Host, cfg.HTTP.Port)
|
|
||||||
httpServer := server.NewHTTPServer(httpAddr,
|
|
||||||
server.WithHTTPReadTimeout(time.Second*time.Duration(cfg.HTTP.ReadTimeout)),
|
|
||||||
server.WithHTTPWriteTimeout(time.Second*time.Duration(cfg.HTTP.WriteTimeout)),
|
|
||||||
server.WithHTTPRegistry(httpRegistry, httpInstance),
|
|
||||||
server.WithHTTPIsProd(cfg.App.Env == "prod"),
|
|
||||||
)
|
|
||||||
servers = append(servers, httpServer)
|
|
||||||
|
|
||||||
// 创建grpc服务
|
|
||||||
grpcAddr := ":" + strconv.Itoa(cfg.Grpc.Port)
|
|
||||||
grpcRegistry, grpcInstance := registryService("grpc", cfg.App.Host, cfg.Grpc.Port)
|
|
||||||
grpcServer := server.NewGRPCServer(grpcAddr,
|
|
||||||
server.WithGrpcReadTimeout(time.Duration(cfg.Grpc.ReadTimeout)*time.Second),
|
|
||||||
server.WithGrpcWriteTimeout(time.Duration(cfg.Grpc.WriteTimeout)*time.Second),
|
|
||||||
server.WithGrpcRegistry(grpcRegistry, grpcInstance),
|
|
||||||
)
|
|
||||||
servers = append(servers, grpcServer)
|
|
||||||
|
|
||||||
return servers
|
|
||||||
}
|
|
||||||
|
|
||||||
func registryService(scheme string, host string, port int) (registry.Registry, *registry.ServiceInstance) {
|
|
||||||
instanceEndpoint := fmt.Sprintf("%s://%s:%d", scheme, host, port)
|
|
||||||
cfg := config.Get()
|
|
||||||
|
|
||||||
switch cfg.App.RegistryDiscoveryType {
|
|
||||||
// 使用consul注册服务
|
|
||||||
case "consul":
|
|
||||||
iRegistry, instance, err := consul.NewRegistry(
|
|
||||||
cfg.Consul.Addr,
|
|
||||||
cfg.App.Name+"_"+scheme+"_"+host,
|
|
||||||
cfg.App.Name,
|
|
||||||
[]string{instanceEndpoint},
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
return iRegistry, instance
|
|
||||||
// 使用etcd注册服务
|
|
||||||
case "etcd":
|
|
||||||
iRegistry, instance, err := etcd.NewRegistry(
|
|
||||||
cfg.Etcd.Addrs,
|
|
||||||
cfg.App.Name+"_"+scheme+"_"+host,
|
|
||||||
cfg.App.Name,
|
|
||||||
[]string{instanceEndpoint},
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
return iRegistry, instance
|
|
||||||
// 使用nacos注册服务
|
|
||||||
case "nacos":
|
|
||||||
iRegistry, instance, err := nacos.NewRegistry(
|
|
||||||
cfg.NacosRd.IPAddr,
|
|
||||||
cfg.NacosRd.Port,
|
|
||||||
cfg.NacosRd.NamespaceID,
|
|
||||||
cfg.App.Name+"_"+scheme+"_"+host,
|
|
||||||
cfg.App.Name,
|
|
||||||
[]string{instanceEndpoint},
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
return iRegistry, instance
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/zhufuyi/sponge/cmd/serverNameExample_mixExample/initial"
|
|
||||||
|
|
||||||
"github.com/zhufuyi/sponge/pkg/app"
|
|
||||||
)
|
|
||||||
|
|
||||||
// @title serverNameExample api docs
|
|
||||||
// @description http server api docs
|
|
||||||
// @schemes http https
|
|
||||||
// @version v0.0.0
|
|
||||||
// @host localhost:8080
|
|
||||||
func main() {
|
|
||||||
initial.Config()
|
|
||||||
servers := initial.RegisterServers()
|
|
||||||
closes := initial.RegisterClose(servers)
|
|
||||||
|
|
||||||
a := app.New(servers, closes)
|
|
||||||
a.Run()
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user