| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- package subconfigs
-
- import "fmt"
-
- // RabbitMQConfig RabbitMQ配置
- type RabbitMQConfig struct {
- BaseConfig
- Host string `yaml:"host"`
- Port int `yaml:"port"`
- Username string `yaml:"username"`
- Password string `yaml:"password"`
- Vhost string `yaml:"vhost"`
- UseTLS bool `yaml:"use_tls"`
- CACert string `yaml:"ca_cert"`
- CertFile string `yaml:"cert_file"`
- KeyFile string `yaml:"key_file"`
- MaxOpenChannels int `yaml:"max_open_channels"`
- ReconnectDelay int `yaml:"reconnect_delay"`
- MaxReconnectAttempts int `yaml:"max_reconnect_attempts"`
- Heartbeat int `yaml:"heartbeat"`
- ChannelSize int `yaml:"channel_size"`
- DefaultExchange string `yaml:"default_exchange"`
- DefaultQueue string `yaml:"default_queue"`
- AutoAck bool `yaml:"auto_ack"`
- Mandatory bool `yaml:"mandatory"`
- Immediate bool `yaml:"immediate"`
- PrefetchCount int `yaml:"prefetch_count"`
- PrefetchSize int `yaml:"prefetch_size"`
- Global bool `yaml:"global"`
- PublisherConfirms bool `yaml:"publisher_confirms"`
- ConfirmTimeout int `yaml:"confirm_timeout"`
- }
-
- func NewRabbitMQConfig() *RabbitMQConfig {
- return &RabbitMQConfig{}
- }
-
- func (c *RabbitMQConfig) SetDefaults() {
- c.Port = 5672
- c.Username = "guest"
- c.Password = "guest"
- c.Vhost = "/"
- c.MaxOpenChannels = 10
- c.ReconnectDelay = 5000
- c.MaxReconnectAttempts = 10
- c.Heartbeat = 30
- c.ChannelSize = 100
- c.DefaultExchange = "amq.direct"
- c.PrefetchCount = 1
- c.ConfirmTimeout = 30
- }
-
- func (c *RabbitMQConfig) Load(data map[string]interface{}) error {
- return c.LoadFromYAML(data, c)
- }
-
- func (c *RabbitMQConfig) Validate() error {
- if c.Host == "" {
- return fmt.Errorf("rabbitmq host is required")
- }
- if c.Port <= 0 {
- return fmt.Errorf("invalid rabbitmq port: %d", c.Port)
- }
- return nil
- }
-
- func (c *RabbitMQConfig) IsConfigured() bool {
- return c.Host != ""
- }
-
- // 自动注册
- func init() {
- Register("rabbitmq", &RabbitMQConfig{})
- }
|