Skip to content

Commit

Permalink
feat(helloworld): integrate consul for configuration management
Browse files Browse the repository at this point in the history
- Add consul client and configuration source to the helloworld service
- Implement context and signal handling in the NewApp function
- Update buildApp function to include context parameter
- Modify main function to use consul for configuration synchronization
- Add metrics middleware support in the configuration file
  • Loading branch information
godcong committed Oct 11, 2024
1 parent da847b6 commit fcfa985
Show file tree
Hide file tree
Showing 9 changed files with 525 additions and 241 deletions.
68 changes: 51 additions & 17 deletions cmd/service/helloworld/main.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
package main

import (
"context"
"flag"
"fmt"
"os"
"path/filepath"
"syscall"

"github.com/go-kratos/kratos/contrib/config/consul/v2"
"github.com/go-kratos/kratos/v2"
"github.com/go-kratos/kratos/v2/config"
"github.com/go-kratos/kratos/v2/config/file"
"github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/middleware/tracing"
"github.com/go-kratos/kratos/v2/transport/grpc"
"github.com/go-kratos/kratos/v2/transport/http"

"github.com/hashicorp/consul/api"
logger "github.com/origadmin/slog-kratos"
"github.com/origadmin/toolkits/codec"
_ "go.uber.org/automaxprocs"

"github.com/origadmin/basic-layout/internal/mods/helloworld/conf"
Expand All @@ -35,12 +40,14 @@ func init() {
flag.StringVar(&flagconf, "conf", "configs", "config path, eg: -conf config.yaml")
}

func NewApp(logger log.Logger, gs *grpc.Server, hs *http.Server) *kratos.App {
func NewApp(ctx context.Context, config *conf.Server, logger log.Logger, gs *grpc.Server, hs *http.Server) *kratos.App {
return kratos.New(
kratos.ID(id),
kratos.Name(Name),
kratos.Version(Version),
kratos.Metadata(map[string]string{}),
kratos.Context(ctx),
kratos.Signal(syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT),
kratos.Logger(logger),
kratos.Server(
gs,
Expand All @@ -51,24 +58,41 @@ func NewApp(logger log.Logger, gs *grpc.Server, hs *http.Server) *kratos.App {

func main() {
flag.Parse()
logger := log.With(log.NewStdLogger(os.Stdout),
"ts", log.DefaultTimestamp,
"caller", log.DefaultCaller,
"service.id", id,
"service.name", Name,
"service.version", Version,
"trace.id", tracing.TraceID(),
"span.id", tracing.SpanID(),
)

flagconf, _ = filepath.Abs(flagconf)
fmt.Println("load config at:", flagconf)

client, err := api.NewClient(&api.Config{
Address: "192.168.28.42:8500",
})
if err != nil {
panic(err)
}
fs := file.NewSource(flagconf)
kvs, err := fs.Load()
if err != nil {
panic(err)
}

for _, kv := range kvs {
fmt.Println("key:", kv.Key, "value:", string(kv.Value))
_, err := client.KV().Put(&api.KVPair{Key: "configs/" + kv.Key, Value: kv.Value}, nil)
if err != nil {
panic(err)
}
}

//consul.WithPath(testPath)
source, err := consul.New(client, consul.WithPath("configs/config.toml"))
if err != nil {
panic(err)
}
c := config.New(
config.WithSource(
file.NewSource(flagconf),
),
//config.WithSource(file.NewSource(flagconf), source),
config.WithSource(source),
config.WithDecoder(codec.SourceDecoder),
)
defer c.Close()

if err := c.Load(); err != nil {
panic(err)
}
Expand All @@ -77,9 +101,19 @@ func main() {
if err := c.Scan(&bc); err != nil {
panic(err)
}
fmt.Println("show bootstrap config:", bc)
logger := log.With(logger.NewLogger(),
"ts", log.DefaultTimestamp,
"caller", log.DefaultCaller,
"service.id", id,
"service.name", bc.ServiceName,
"service.version", bc.Version,
"trace.id", tracing.TraceID(),
"span.id", tracing.SpanID(),
)

app, cleanup, err := buildApp(bc.Server, bc.Data, logger)
fmt.Println("show bootstrap config:", bc)
ctx := context.Background()
app, cleanup, err := buildApp(ctx, bc.Server, bc.Data, logger)
if err != nil {
panic(err)
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/service/helloworld/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package main

import (
"context"

"github.com/origadmin/basic-layout/internal/mods/helloworld/biz"
"github.com/origadmin/basic-layout/internal/mods/helloworld/conf"
"github.com/origadmin/basic-layout/internal/mods/helloworld/dal"
Expand All @@ -19,6 +21,6 @@ import (
//go:generate go run -mod=mod --tags wireinject github.com/google/wire/cmd/wire

// buildApp init kratos application.
func buildApp(*conf.Server, *conf.Data, log.Logger) (*kratos.App, func(), error) {
func buildApp(context.Context, *conf.Server, *conf.Data, log.Logger) (*kratos.App, func(), error) {
panic(wire.Build(server.ProviderSet, dal.ProviderSet, biz.ProviderSet, service.ProviderSet, NewApp))
}
5 changes: 3 additions & 2 deletions cmd/service/helloworld/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 19 additions & 12 deletions configs/config.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
service_name = "helloworld_service_name"
version = "version_1.0.0"

[server.http]
addr = "0.0.0.0:8000"
timeout = "1s"
addr = "0.0.0.0:8000"
timeout = "1s"

[server.grpc]
addr = "0.0.0.0:9000"
timeout = "1s"

[server.grpc]
addr = "0.0.0.0:9000"
timeout = "1s"
[data.database]
driver = "mysql"
source = "root:root@tcp(127.0.0.1:3306)/test?parseTime=True&loc=Local"

[data.database]
driver = "mysql"
source = "root:root@tcp(127.0.0.1:3306)/test?parseTime=True&loc=Local"
[data.redis]
addr = "127.0.0.1:6379"
read_timeout = "0.2s"
write_timeout = "0.2s"

[data.redis]
addr = "127.0.0.1:6379"
read_timeout = "0.2s"
write_timeout = "0.2s"
[server.middleware.metrics]
enabled = true
name = "promtheus_metrics"
32 changes: 32 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ module github.com/origadmin/basic-layout
go 1.23.1

require (
github.com/go-kratos/kratos/contrib/config/consul/v2 v2.0.0-20241011140022-7cd86eb077dc
github.com/go-kratos/kratos/v2 v2.8.0
github.com/google/wire v0.6.0
github.com/hashicorp/consul/api v1.29.4
github.com/origadmin/slog-kratos v1.0.2
github.com/origadmin/toolkits v0.0.47
github.com/origadmin/toolkits/codec v0.0.12
go.uber.org/automaxprocs v1.6.0
google.golang.org/genproto/googleapis/api v0.0.0-20241007155032-5fefd90f89a9
google.golang.org/grpc v1.67.1
Expand All @@ -14,16 +18,44 @@ require (

require (
dario.cat/mergo v1.0.1 // indirect
github.com/BurntSushi/toml v1.4.0 // indirect
github.com/armon/go-metrics v0.4.1 // indirect
github.com/bytedance/sonic v1.12.3 // indirect
github.com/bytedance/sonic/loader v0.2.0 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/fatih/color v1.16.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-kratos/aegis v0.2.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-playground/form/v4 v4.2.1 // indirect
github.com/goexts/ggb v0.0.9 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/mux v1.8.1 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-hclog v1.5.0 // indirect
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-rootcerts v1.0.2 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/hashicorp/serf v0.10.1 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
go.opentelemetry.io/otel v1.30.0 // indirect
go.opentelemetry.io/otel/metric v1.30.0 // indirect
go.opentelemetry.io/otel/trace v1.30.0 // indirect
golang.org/x/arch v0.0.0-20210923205945-b76863e36670 // indirect
golang.org/x/exp v0.0.0-20241004190924-225e2abe05e6 // indirect
golang.org/x/net v0.30.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.26.0 // indirect
Expand Down
Loading

0 comments on commit fcfa985

Please sign in to comment.