Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ MOON_RABBIT_ENVIRONMENT=PROD
MOON_RABBIT_ENABLE_CLIENT_CONFIG=true
MOON_RABBIT_ENABLE_SWAGGER=true
MOON_RABBIT_ENABLE_METRICS=true
MOON_RABBIT_USE_DATABASE=true
MOON_RABBIT_USE_DATABASE=false

# =============================================================================
# Server Configuration
# =============================================================================
MOON_RABBIT_NAME=moon.rabbit
MOON_RABBIT_USE_RANDOM_ID=false
MOON_RABBIT_METADATA_TAG=rabbit
MOON_RABBIT_METADATA_REPOSITORY=https://github.com/aide-family/rabbit
MOON_RABBIT_METADATA_AUTHOR=Aide Family
MOON_RABBIT_METADATA_EMAIL=aidecloud@163.com

# HTTP Server
MOON_RABBIT_HTTP_ADDRESS=0.0.0.0:8080
Expand All @@ -27,10 +30,10 @@ MOON_RABBIT_GRPC_ADDRESS=0.0.0.0:9090
MOON_RABBIT_GRPC_NETWORK=tcp
MOON_RABBIT_GRPC_TIMEOUT=10s

# eventBus Server
MOON_RABBIT_EVENT_BUS_ADDRESS=0.0.0.0:9091
MOON_RABBIT_EVENT_BUS_NETWORK=grpc
MOON_RABBIT_EVENT_BUS_TIMEOUT=10s
# Job Server (EventBus)
MOON_RABBIT_JOB_ADDRESS=0.0.0.0:9091
MOON_RABBIT_JOB_NETWORK=grpc
MOON_RABBIT_JOB_TIMEOUT=10s

# =============================================================================
# JWT Configuration
Expand All @@ -51,11 +54,11 @@ MOON_RABBIT_MAIN_DEBUG=false
MOON_RABBIT_MAIN_USE_SYSTEM_LOGGER=true

# =============================================================================
# Event Bus Configuration
# Job Core Configuration
# =============================================================================
MOON_RABBIT_EVENT_BUS_CORE_WORKER_TOTAL=10
MOON_RABBIT_EVENT_BUS_CORE_TIMEOUT=10s
MOON_RABBIT_EVENT_BUS_CORE_BUFFER_SIZE=1000
MOON_RABBIT_JOB_CORE_WORKER_TOTAL=10
MOON_RABBIT_JOB_CORE_TIMEOUT=10s
MOON_RABBIT_JOB_CORE_BUFFER_SIZE=1000

# =============================================================================
# Registry Configuration
Expand All @@ -65,8 +68,8 @@ MOON_RABBIT_REGISTRY_TYPE=
# =============================================================================
# Cluster Configuration
# =============================================================================
MOON_RABBIT_CLUSTER_NAME=rabbit
MOON_RABBIT_CLUSTER_ENDPOINTS=localhost:9090
MOON_RABBIT_CLUSTER_NAME=moon.rabbit
MOON_RABBIT_CLUSTER_ENDPOINTS=
MOON_RABBIT_CLUSTER_PROTOCOL=GRPC
MOON_RABBIT_CLUSTER_TIMEOUT=10s

Expand Down Expand Up @@ -100,9 +103,9 @@ MOON_RABBIT_METRICS_BASIC_AUTH_PASSWORD=rabbit.metrics
# =============================================================================
# Logging Configuration
# =============================================================================
MOON_RABBIT_CONFIG_PATHS=./datasource
MOON_RABBIT_CONFIG_PATHS=

# =============================================================================
# Message Log Path Configuration
# =============================================================================
MOON_RABBIT_MESSAGE_LOG_PATH=./messages
MOON_RABBIT_MESSAGE_LOG_PATH=
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,6 @@ go.work.sum
deploy/*/data
description.txt
.rabbit/
*.log
*.log
messages
message_logs
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ build: all
# run the rabbit binary in development mode
dev:
@echo "Running rabbit in development mode"
go run . run
go run . run all

.PHONY: test
# run the tests
Expand Down
94 changes: 94 additions & 0 deletions cmd/run/all/all.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Package all is the all command for the Rabbit service
package all

import (
"github.com/aide-family/magicbox/hello"
"github.com/go-kratos/kratos/v2"
klog "github.com/go-kratos/kratos/v2/log"
"github.com/spf13/cobra"

"github.com/aide-family/rabbit/cmd"
"github.com/aide-family/rabbit/cmd/run"
"github.com/aide-family/rabbit/internal/conf"
"github.com/aide-family/rabbit/internal/data"
"github.com/aide-family/rabbit/internal/server"
)

const cmdAllLong = `Start the Rabbit messaging service with all services (HTTP, gRPC, and Job).

The server command starts all services together:
• HTTP service: Provides RESTful API interfaces for message delivery and management
• gRPC service: Provides high-performance gRPC API interfaces for inter-service communication
• Job service: Provides asynchronous message processing capabilities via EventBus

Rabbit is a distributed messaging platform built on the Kratos framework, supporting unified
management and delivery of multiple message channels (email, Webhook, SMS, etc.). It implements
multi-tenant isolation through namespaces and supports both file-based and database storage modes
to meet different deployment requirements.

Key Features:
• Multi-channel messaging: Unified management of email, Webhook, SMS, and other message channels
• Template-based delivery: Support for message template configuration with dynamic content rendering and reuse
• Asynchronous processing: Queue-based asynchronous message delivery for improved throughput and reliability
• Configuration management: Centralized management of channel configurations (email servers, Webhook endpoints, etc.)
• Multi-tenant isolation: Namespace-based isolation of configurations and data for different businesses or tenants

Use Cases:
• All-in-one deployment: Deploy all services together for simple deployment scenarios
• Development and testing: Quick start for development and testing environments
• Small to medium deployments: Suitable for deployments that don't require service separation

Note: For production environments requiring service separation, consider using the http, grpc, or job
commands to start services independently for better scalability and resource management.

After starting the service, Rabbit will listen on the configured ports:
• HTTP: Default 0.0.0.0:8080 (configurable via --http-address)
• gRPC: Default 0.0.0.0:9090 (configurable via --grpc-address)
• Job: Default 0.0.0.0:9091 (configurable via --job-address)`

func NewCmd() *cobra.Command {
runCmd := &cobra.Command{
Use: "all",
Short: "Run the Rabbit all services",
Long: cmdAllLong,
Annotations: map[string]string{
"group": cmd.ServiceCommands,
},
Run: runAll,
}

flags.addFlags(runCmd)
return runCmd
}

func runAll(_ *cobra.Command, _ []string) {
flags.applyToBootstrap()

run.StartServer("all", wireApp)
}

func newApp(d *data.Data, srvs server.Servers, bc *conf.Bootstrap, helper *klog.Helper) (*kratos.App, error) {
defer hello.Hello()
opts := []kratos.Option{
kratos.Logger(helper.Logger()),
kratos.Server(srvs...),
kratos.Version(hello.Version()),
kratos.ID(hello.ID()),
kratos.Name(hello.Name()),
kratos.Metadata(hello.Metadata()),
}

if registry := d.Registry(); registry != nil {
opts = append(opts, kratos.Registrar(registry))
}

srvs.BindSwagger(bc, helper)
srvs.BindMetrics(bc, helper)

// 生成客户端配置
if err := generateClientConfig(bc, srvs, helper); err != nil {
helper.Warnw("msg", "generate client config failed", "error", err)
}

return kratos.New(opts...), nil
}
8 changes: 4 additions & 4 deletions cmd/run/client.go → cmd/run/all/client.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package run
package all

import (
"fmt"
Expand All @@ -7,16 +7,16 @@ import (
"strings"
"time"

"github.com/aide-family/magicbox/load"
"github.com/aide-family/magicbox/strutil"
"github.com/aide-family/magicbox/strutil/cnst"
"github.com/go-kratos/kratos/v2/encoding"
klog "github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/transport/grpc"
"github.com/go-kratos/kratos/v2/transport/http"
jwtv5 "github.com/golang-jwt/jwt/v5"
"google.golang.org/protobuf/types/known/durationpb"

"github.com/aide-family/magicbox/load"
"github.com/aide-family/magicbox/strutil"
"github.com/aide-family/magicbox/strutil/cnst"
"github.com/aide-family/rabbit/internal/conf"
"github.com/aide-family/rabbit/internal/server"
"github.com/aide-family/rabbit/pkg/config"
Expand Down
87 changes: 87 additions & 0 deletions cmd/run/all/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package all

import (
"strconv"
"time"

"github.com/aide-family/magicbox/pointer"
"github.com/aide-family/magicbox/strutil"
"github.com/spf13/cobra"
"google.golang.org/protobuf/types/known/durationpb"

"github.com/aide-family/rabbit/cmd/run"
)

type Flags struct {
run.RunFlags

httpTimeout string
grpcTimeout string
jobTimeout string
jobCoreTimeout string
enableClientConfig bool
enableSwagger bool
enableSwaggerBasicAuth bool
enableMetrics bool
enableMetricsBasicAuth bool
}

var flags Flags

func (f *Flags) addFlags(c *cobra.Command) {
f.RunFlags = run.GetRunFlags()
c.Flags().StringVar(&f.Server.Http.Address, "http-address", f.Server.Http.Address, `Example: --http-address="0.0.0.0:8080", --http-address=":8080"`)
c.Flags().StringVar(&f.Server.Http.Network, "http-network", f.Server.Http.Network, `Example: --http-network="tcp"`)
c.Flags().StringVar(&f.httpTimeout, "http-timeout", f.Server.Http.Timeout.AsDuration().String(), `Example: --http-timeout="10s", --http-timeout="1m", --http-timeout="1h", --http-timeout="1d"`)
enableSwagger, _ := strconv.ParseBool(f.SwaggerBasicAuth.Enabled)
c.Flags().BoolVar(&f.enableSwagger, "enable-swagger", enableSwagger, `Example: --enable-swagger`)
enableSwaggerBasicAuth, _ := strconv.ParseBool(f.SwaggerBasicAuth.Enabled)
c.Flags().BoolVar(&f.enableSwaggerBasicAuth, "enable-swagger-basic-auth", enableSwaggerBasicAuth, `Example: --enable-swagger-basic-auth`)
c.Flags().StringVar(&f.SwaggerBasicAuth.Username, "swagger-basic-auth-username", f.SwaggerBasicAuth.Username, `Example: --swagger-basic-auth-username="username"`)
c.Flags().StringVar(&f.SwaggerBasicAuth.Password, "swagger-basic-auth-password", f.SwaggerBasicAuth.Password, `Example: --swagger-basic-auth-password="password"`)
enableMetrics, _ := strconv.ParseBool(f.MetricsBasicAuth.Enabled)
c.Flags().BoolVar(&f.enableMetrics, "enable-metrics", enableMetrics, `Example: --enable-metrics`)
enableMetricsBasicAuth, _ := strconv.ParseBool(f.MetricsBasicAuth.Enabled)
c.Flags().BoolVar(&f.enableMetricsBasicAuth, "enable-metrics-basic-auth", enableMetricsBasicAuth, `Example: --enable-metrics-basic-auth`)
c.Flags().StringVar(&f.MetricsBasicAuth.Username, "metrics-basic-auth-username", f.MetricsBasicAuth.Username, `Example: --metrics-basic-auth-username="username"`)
c.Flags().StringVar(&f.MetricsBasicAuth.Password, "metrics-basic-auth-password", f.MetricsBasicAuth.Password, `Example: --metrics-basic-auth-password="password"`)

c.Flags().StringVar(&f.Server.Grpc.Address, "grpc-address", f.Server.Grpc.Address, `Example: --grpc-address="0.0.0.0:9090", --grpc-address=":9090"`)
c.Flags().StringVar(&f.Server.Grpc.Network, "grpc-network", f.Server.Grpc.Network, `Example: --grpc-network="tcp"`)
c.Flags().StringVar(&f.grpcTimeout, "grpc-timeout", f.Server.Grpc.Timeout.AsDuration().String(), `Example: --grpc-timeout="10s", --grpc-timeout="1m", --grpc-timeout="1h", --grpc-timeout="1d"`)

c.Flags().StringVar(&f.Server.Job.Address, "job-address", f.Server.Job.Address, `Example: --job-address="0.0.0.0:9091", --job-address=":9091"`)
c.Flags().StringVar(&f.Server.Job.Network, "job-network", f.Server.Job.Network, `Example: --job-network="tcp"`)
c.Flags().StringVar(&f.jobTimeout, "job-timeout", f.Server.Job.Timeout.AsDuration().String(), `Example: --job-timeout="10s", --job-timeout="1m", --job-timeout="1h", --job-timeout="1d"`)

c.Flags().Int32Var(&f.JobCore.WorkerTotal, "job-core-worker-total", f.JobCore.WorkerTotal, `Example: --job-core-worker-total=10"`)
c.Flags().StringVar(&f.jobCoreTimeout, "job-core-timeout", f.JobCore.Timeout.AsDuration().String(), `Example: --job-core-timeout="10s", --job-core-timeout="1m", --job-core-timeout="1h", --job-core-timeout="1d"`)
c.Flags().Uint32Var(&f.JobCore.BufferSize, "job-core-buffer-size", f.JobCore.BufferSize, `Example: --job-core-buffer-size=1000"`)

enableClientConfig, _ := strconv.ParseBool(f.EnableClientConfig)
c.Flags().BoolVar(&f.enableClientConfig, "enable-client-config", enableClientConfig, `Example: --enable-client-config`)
}

func (f *Flags) applyToBootstrap() {
if strutil.IsNotEmpty(f.httpTimeout) {
if timeout, err := time.ParseDuration(f.httpTimeout); pointer.IsNil(err) {
f.Server.Http.Timeout = durationpb.New(timeout)
}
}
if strutil.IsNotEmpty(f.grpcTimeout) {
if timeout, err := time.ParseDuration(f.grpcTimeout); pointer.IsNil(err) {
f.Server.Grpc.Timeout = durationpb.New(timeout)
}
}
if strutil.IsNotEmpty(f.jobTimeout) {
if timeout, err := time.ParseDuration(f.jobTimeout); pointer.IsNil(err) {
f.Server.Job.Timeout = durationpb.New(timeout)
}
}

if strutil.IsNotEmpty(f.jobCoreTimeout) {
if timeout, err := time.ParseDuration(f.jobCoreTimeout); pointer.IsNil(err) {
f.JobCore.Timeout = durationpb.New(timeout)
}
}
}
6 changes: 3 additions & 3 deletions cmd/run/wire.go → cmd/run/all/wire.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//go:build wireinject
// +build wireinject

// Package run is the run command for the Rabbit service
package run
// Package all is the all command for the Rabbit service
package all

import (
"github.com/go-kratos/kratos/v2"
Expand All @@ -19,7 +19,7 @@ import (

func wireApp(bc *conf.Bootstrap, helper *klog.Helper) (*kratos.App, func(), error) {
panic(wire.Build(
server.ProviderSetServer,
server.ProviderSetServerAll,
service.ProviderSetService,
biz.ProviderSetBiz,
impl.ProviderSetImpl,
Expand Down
Loading