Skip to content

Commit

Permalink
feat(micro): testing cmux with greeter project
Browse files Browse the repository at this point in the history
  • Loading branch information
xmlking committed Jun 12, 2020
1 parent c86a5dc commit cb6d229
Show file tree
Hide file tree
Showing 20 changed files with 402 additions and 519 deletions.
1 change: 1 addition & 0 deletions cmd/account/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func main() {
username := flag.String("username", "sumo", "username of user to be create")
email := flag.String("email", "[email protected]", "email of user to be create")
limit := flag.Uint64("limit", 10, "Limit number of results")
flag.Parse()

log.Debug().Str("username", *username).Str("email", *email).Uint64("limit", *limit).Msg("Flags Using:")

Expand Down
1 change: 1 addition & 0 deletions config/config.prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ features:
tls:
cert_file: /config/certs/upstream-cert.pem
key_file: /config/certs/upstream-key.pem
servername: "*"
2 changes: 1 addition & 1 deletion config/config.test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ email:

services:
account:
endpoint: mkit.service.account:8080
endpoint: tcp://account:8080
version: v0.1.0
deadline: 8888
14 changes: 8 additions & 6 deletions config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ database:
port: 9999
username: sumo
password: demo
database: "file:foobar?mode=memory&cache=shared"
# database: "file:foobar?mode=memory&cache=shared"
database: "file::memory:?cache=shared"
max_open_conns: 1
max_idle_conns: 1
conn_max_lifetime: 1h
Expand All @@ -18,16 +19,17 @@ email:

services:
account:
endpoint: mkit.service.account:8080
endpoint: tcp://account:8080
version: v0.1.0
greeter:
endpoint: mkit.service.greeter:8080
endpoint: tcp://greeter:8081
# endpoint: unix:///tmp/greeter.sock
version: v0.1.0
emailer:
endpoint: mkit.service.emailer:8080
endpoint: tcp://emailer:8082
version: v0.1.0
recorder:
endpoint: mkit.service.recorder:8080
endpoint: tcp://recorder:8083
version: v0.1.0

features:
Expand All @@ -43,7 +45,7 @@ features:
cert_file: /config/certs/upstream-localhost-cert.pem
key_file: /config/certs/upstream-localhost-key.pem
ca_file: /config/certs/upstream-ca-cert.pem
servername: "*"
servername: "localhost"
enabled: false
validator:
enabled: true
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ require (
github.com/rs/zerolog v1.19.0
github.com/sarulabs/di/v2 v2.4.0
github.com/satori/go.uuid v1.2.0
github.com/soheilhy/cmux v0.1.4
github.com/stretchr/testify v1.6.1
github.com/thoas/go-funk v0.6.0
github.com/xmlking/configor v0.0.0-20200609225939-ed6133420e9d
google.golang.org/genproto v0.0.0-20200608115520-7c474a2e3482
google.golang.org/genproto v0.0.0-20200611194920-44ba362f84c1
google.golang.org/grpc v1.27.0
)
2 changes: 1 addition & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98
google.golang.org/genproto v0.0.0-20190911173649-1774047e7e51/go.mod h1:IbNlFCBrqXvoKpeg0TB2l7cyZUmoaFKYIwrEpbDKLA8=
google.golang.org/genproto v0.0.0-20191216164720-4f79533eabd1/go.mod h1:n3cpQtvxv34hfy77yVDNjmbRyujviMdxYliBSkLhpCc=
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo=
google.golang.org/genproto v0.0.0-20200608115520-7c474a2e3482/go.mod h1:jDfRM7FcilCzHH/e9qn6dsT145K34l5v+OpcnNgKAAA=
google.golang.org/genproto v0.0.0-20200611194920-44ba362f84c1/go.mod h1:jDfRM7FcilCzHH/e9qn6dsT145K34l5v+OpcnNgKAAA=
google.golang.org/grpc v1.26.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk=
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
Expand Down
11 changes: 10 additions & 1 deletion service/account/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ Run the service

```bash
make run-account
make run-account ARGS="--server_address=:8080"
# or
go run service/account/main.go service/account/plugin.go
```
Expand All @@ -41,3 +40,13 @@ Build a docker image
```bash
make docker TARGET=account TYPE=service VERSION=v0.1.1
```

Test the service

```bash
micro call mkit.service.account UserService.Create \
'{"username": "sumo", "firstName": "sumo", "lastName": "demo", "email": "[email protected]"}'
micro call mkit.service.account UserService.Create \
'{"username": "sumo1", "firstName": "sumo1", "lastName": "demo1", "email": "[email protected]"}'
micro call mkit.service.account UserService.List '{}'
```
63 changes: 31 additions & 32 deletions service/account/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package main

import (
"github.com/micro/go-micro/v2"
sgrpc "github.com/micro/go-micro/v2/server/grpc"
"github.com/rs/zerolog/log"

// "github.com/micro/go-micro/v2/service/grpc"
"github.com/xmlking/micro-starter-kit/service/account/handler"
profilePB "github.com/xmlking/micro-starter-kit/service/account/proto/profile"
userPB "github.com/xmlking/micro-starter-kit/service/account/proto/user"
Expand All @@ -19,68 +19,66 @@ import (
validatorWrapper "github.com/xmlking/micro-starter-kit/shared/wrapper/validator"
)

const (
serviceName = constants.ACCOUNT_SERVICE
)
func main() {
serviceName := constants.ACCOUNT_SERVICE
cfg := config.GetConfig()

var (
cfg = config.GetConfig()
)
lis, err := config.GetListener(cfg.Services.Account.Endpoint)
if err != nil {
log.Fatal().Msgf("failed to create listener: %v", err)
}
println(lis.Addr())

func main() {
// New Service
service := micro.NewService(
micro.Server(sgrpc.NewServer(sgrpc.Listener(lis))), // KEEP-IT-FIRST
micro.Name(serviceName),
micro.Version(config.Version),
)

// Initialize service
service.Init(
micro.BeforeStart(func() (err error) {
return
}),
micro.BeforeStop(func() (err error) {
return
}),
)

// Initialize Features
var options []micro.Option

if cfg.Features.Tls.Enabled {
if tlsConf, err := config.CreateServerCerts(); err != nil {
log.Error().Err(err).Msg("unable to load certs")
} else {
log.Info().Msg("TLS Enabled")
options = append(options,
tls.WithTLS(tlsConf),
)
options = append(options, tls.WithTLS(tlsConf))
}
}

// Wrappers are invoked in the order as they added
if cfg.Features.Reqlogs.Enabled {
options = append(options,
micro.WrapHandler(logWrapper.NewHandlerWrapper()),
micro.WrapClient(logWrapper.NewClientWrapper()),
)
}
if cfg.Features.Validator.Enabled {
options = append(options,
micro.WrapHandler(validatorWrapper.NewHandlerWrapper()),
)
}
if cfg.Features.Translogs.Enabled {
topic := cfg.Features.Translogs.Topic
publisher := micro.NewEvent(topic, service.Client())
options = append(options,
micro.WrapHandler(transWrapper.NewHandlerWrapper(publisher)),
)
options = append(options, micro.WrapHandler(transWrapper.NewHandlerWrapper(publisher)))
}
if cfg.Features.Validator.Enabled {
options = append(options, micro.WrapHandler(validatorWrapper.NewHandlerWrapper()))
}

// Initialize Features
service.Init(
options...,
// Adding some optional lifecycle actions
options = append(options,
micro.BeforeStart(func() (err error) {
log.Debug().Msg("called BeforeStart")
return
}),
micro.BeforeStop(func() (err error) {
log.Debug().Msg("called BeforeStop")
return
}),
)

// Initialize service
service.Init(options...)

// Initialize DI Container
ctn, err := registry.NewContainer(cfg)
defer ctn.Clean()
Expand All @@ -104,6 +102,7 @@ func main() {
profilePB.RegisterProfileServiceHandler(service.Server(), profileHandler)

println(config.GetBuildInfo())

// Run service
if err := service.Run(); err != nil {
log.Fatal().Err(err).Msg("")
Expand Down
51 changes: 26 additions & 25 deletions service/emailer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"github.com/micro/go-micro/v2"
sgrpc "github.com/micro/go-micro/v2/server/grpc"

"github.com/rs/zerolog/log"

Expand All @@ -15,43 +16,34 @@ import (
// "github.com/xmlking/micro-starter-kit/service/emailer/subscriber"
)

const (
serviceName = constants.EMAILER_SERVICE
)
func main() {
serviceName := constants.EMAILER_SERVICE
cfg := config.GetConfig()

var (
cfg = config.GetConfig()
)
lis, err := config.GetListener(cfg.Services.Emailer.Endpoint)
if err != nil {
log.Fatal().Msgf("failed to create listener: %v", err)
}

func main() {
// New Service
service := micro.NewService(
micro.Server(sgrpc.NewServer(sgrpc.Listener(lis))), // KEEP-IT-FIRST
micro.Name(serviceName),
micro.Version(config.Version),
)

// Initialize service
service.Init(
micro.BeforeStart(func() (err error) {
return
}),
micro.BeforeStop(func() (err error) {
return
}),
)

// Initialize Features
var options []micro.Option

if cfg.Features.Tls.Enabled {
if tlsConf, err := config.CreateServerCerts(); err != nil {
log.Error().Err(err).Msg("unable to load certs")
} else {
log.Info().Msg("TLS Enabled")
options = append(options,
tls.WithTLS(tlsConf),
)
options = append(options, tls.WithTLS(tlsConf))
}
}

// Wrappers are invoked in the order as they added
if cfg.Features.Reqlogs.Enabled {
options = append(options, micro.WrapSubscriber(logWrapper.NewSubscriberWrapper()))
Expand All @@ -62,11 +54,21 @@ func main() {
options = append(options, micro.WrapSubscriber(transWrapper.NewSubscriberWrapper(publisher)))
}

// Initialize Features
service.Init(
options...,
// Adding some optional lifecycle actions
options = append(options,
micro.BeforeStart(func() (err error) {
log.Debug().Msg("called BeforeStart")
return
}),
micro.BeforeStop(func() (err error) {
log.Debug().Msg("called BeforeStop")
return
}),
)

// Initialize service
service.Init(options...)

// Initialize DI Container
ctn, err := registry.NewContainer(cfg)
defer ctn.Clean()
Expand All @@ -84,11 +86,10 @@ func main() {
// register subscriber with queue, each message is delivered to a unique subscriber
// micro.RegisterSubscriber("mkit.service.emailer-2", service.Server(), subscriber.Handler, server.SubscriberQueue("queue.pubsub"))

// PrintBuildInfo
println(config.GetBuildInfo())

// Run service
if err := service.Run(); err != nil {
log.Fatal().Err(err).Msg("")
log.Fatal().Err(err).Send()
}
}
16 changes: 7 additions & 9 deletions service/greeter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ This is the Greeter service

## Usage

A Makefile is included for convenience

Build the binary

```bash
make build TARGET=greeter TYPE=service
# then run with custom env
MICRO_SERVER_ADDRESS=0.0.0.0:9999 CONFIGOR_ENV_PREFIX=APP APP_FEATURES_TLS_ENABLED=true ./build/greeter-service
CONFIGOR_ENV_PREFIX=APP APP_FEATURES_TLS_ENABLED=true ./build/greeter-service
```

Run the service
Expand All @@ -34,18 +32,18 @@ Build a docker image
make docker TARGET=greeter TYPE=service VERSION=v0.1.1
```

### Test
Test the service

```bash
# start the server on fixed port
make run-greeter ARGS="--server_address=:8080 --broker_address=:10001"
# start greeter service first
make run-greeter

# test with grpc cli
grpcurl -plaintext -proto service/greeter/proto/greeter/greeter.proto list
grpcurl -plaintext -proto service/greeter/proto/greeter/greeter.proto describe
grpcurl -plaintext -proto service/greeter/proto/greeter/greeter.proto -d '{"name": "sumo"}' localhost:8080 mkit.service.greeter.Greeter/Hello
grpcurl -plaintext -proto service/greeter/proto/greeter/greeter.proto -d '{"name": "sumo"}' localhost:8081 mkit.service.greeter.v1.GreeterService/Hello
# testing via micro-cli
micro --client=grpc call --metadata trans-id=1234 mkit.service.greeter Greeter.Hello '{"name": "John"}'
micro call mkit.service.greeter GreeterService.Hello '{"name": "John"}'

# start REST gateway
micro api --enable_rpc=true
Expand All @@ -55,5 +53,5 @@ curl --request POST \
--url http://localhost:8080/rpc \
--header 'accept: application/json' \
--header 'content-type: application/json' \
--data '{"service": "mkit.service.greeter", "method": "Greeter.Hello","request": {"name": "sumo"}}'
--data '{"service": "mkit.service.greeter", "method": "GreeterService.Hello","request": {"name": "sumo"}}'
```
Loading

0 comments on commit cb6d229

Please sign in to comment.