Açıklama Yok
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

service.go 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package myservice
  2. import (
  3. "fmt"
  4. "log"
  5. "time"
  6. "git.x2erp.com/qdy/go-base/config"
  7. "github.com/go-micro/plugins/v4/registry/consul"
  8. "go-micro.dev/v4/registry"
  9. "go-micro.dev/v4/web"
  10. )
  11. // StartWithRegistry starts microservice with registry (e.g., Consul)
  12. func StartWithRegistry(cfg config.IConfig) web.Service {
  13. // Get service information from config
  14. serviceConfig := cfg.GetService()
  15. microConfig := cfg.GetMicro()
  16. // 1. Create Consul registry
  17. consulRegistry := consul.NewRegistry(
  18. registry.Addrs(microConfig.RegistryAddress),
  19. )
  20. // 2. Create HTTP web service (for external HTTP access)
  21. service := web.NewService(
  22. web.Name(serviceConfig.ServiceName),
  23. web.Version(serviceConfig.ServiceVersion),
  24. web.Address(fmt.Sprintf(":%d", serviceConfig.Port)),
  25. web.Registry(consulRegistry),
  26. web.RegisterTTL(30*time.Second), // Health check interval
  27. web.RegisterInterval(20*time.Second), // Registration interval
  28. )
  29. // 3. Initialize service
  30. service.Init()
  31. log.Printf("Microservice started (with registry)")
  32. log.Printf(" • Service name: %s", serviceConfig.ServiceName)
  33. log.Printf(" • Version: %s", serviceConfig.ServiceVersion)
  34. log.Printf(" • Port: %d", serviceConfig.Port)
  35. log.Printf(" • Registry: %s", microConfig.RegistryAddress)
  36. log.Printf(" • IdleTimeout: %d秒", serviceConfig.IdleTimeout)
  37. log.Printf(" • ReadTimeout: %d秒", serviceConfig.ReadTimeout)
  38. log.Printf(" • WriteTimeout: %d秒", serviceConfig.WriteTimeout)
  39. // Run in background
  40. go func() {
  41. if err := service.Run(); err != nil {
  42. log.Printf("Microservice stopped: %v", err)
  43. }
  44. }()
  45. return service
  46. }
  47. // StartStandalone starts standalone microservice (not registered to registry)
  48. func StartStandalone(cfg config.IConfig) web.Service {
  49. // Get service information from config
  50. serviceConfig := cfg.GetService()
  51. // Create HTTP web service (standalone operation)
  52. service := web.NewService(
  53. web.Name(serviceConfig.ServiceName),
  54. web.Version(serviceConfig.ServiceVersion),
  55. web.Address(fmt.Sprintf(":%d", serviceConfig.Port)),
  56. // No Registry specified = standalone service
  57. )
  58. // Initialize service
  59. service.Init()
  60. log.Printf("Standalone microservice started")
  61. log.Printf(" • Service name: %s", serviceConfig.ServiceName)
  62. log.Printf(" • Version: %s", serviceConfig.ServiceVersion)
  63. log.Printf(" • Port: %d", serviceConfig.Port)
  64. log.Printf(" • Mode: Standalone (no service discovery)")
  65. // Run in background
  66. go func() {
  67. if err := service.Run(); err != nil {
  68. log.Printf("Microservice stopped: %v", err)
  69. }
  70. }()
  71. return service
  72. }
  73. // Start maintains compatibility with original interface, uses registry version by default
  74. func Start(cfg config.IConfig) web.Service {
  75. return StartWithRegistry(cfg)
  76. }