Keine Beschreibung
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

service_config.go 1.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package subconfigs
  2. import "fmt"
  3. // ServiceConfig 微服务配置
  4. type ServiceConfig struct {
  5. BaseConfig
  6. ServiceName string `yaml:"service_name"`
  7. ServiceVersion string `yaml:"service_version"`
  8. ServiceTag string `yaml:"service_tags"`
  9. Port int `yaml:"port"`
  10. ReadTimeout int `yaml:"read_timeout"`
  11. WriteTimeout int `yaml:"write_timeout"`
  12. IdleTimeout int `yaml:"idle_timeout"`
  13. TrustedProxies string `yaml:"trusted_proxies"`
  14. InstanceName string `yaml:"instance-name"`
  15. }
  16. func NewServiceConfig() *ServiceConfig {
  17. return &ServiceConfig{}
  18. }
  19. func (c *ServiceConfig) SetDefaults() {
  20. c.ServiceName = "myService"
  21. c.Port = 8080
  22. c.ReadTimeout = 30
  23. c.WriteTimeout = 30
  24. c.IdleTimeout = 60
  25. }
  26. func (c *ServiceConfig) Load(data map[string]interface{}) error {
  27. return c.LoadFromYAML(data, c)
  28. }
  29. func (c *ServiceConfig) Validate() error {
  30. if c.ServiceName == "" {
  31. return fmt.Errorf("service name is required")
  32. }
  33. if c.Port <= 0 || c.Port > 65535 {
  34. return fmt.Errorf("invalid port: %d", c.Port)
  35. }
  36. return nil
  37. }
  38. // 自动注册
  39. func init() {
  40. Register("service", &ServiceConfig{})
  41. }