Geen omschrijving
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.

get_service_configs.go 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package servicemanagement
  2. import (
  3. "context"
  4. "fmt"
  5. "git.x2erp.com/qdy/go-base/ctx"
  6. "git.x2erp.com/qdy/go-base/logger"
  7. "git.x2erp.com/qdy/go-base/model/response"
  8. "git.x2erp.com/qdy/go-base/util"
  9. "git.x2erp.com/qdy/go-db/factory/database"
  10. )
  11. // ServiceConfig 微服务配置项
  12. type ServiceConfig struct {
  13. ID string `json:"id"`
  14. ServiceName string `json:"service_name"`
  15. ConfigName string `json:"config_name"`
  16. YamlName string `json:"yaml_name"`
  17. YamlValue string `json:"yaml_value"`
  18. Creator string `json:"creator,omitempty"`
  19. CreatedAt string `json:"created_at,omitempty"`
  20. }
  21. // GetServiceConfigs 获取微服务配置项
  22. func GetServiceConfigs(ctx context.Context, dbFactory *database.DBFactory, serviceName string, reqCtx *ctx.RequestContext) *response.QueryResult[[]ServiceConfig] {
  23. logger.Debug(fmt.Sprintf("GetServiceConfigs-开始获取微服务配置项: %s", serviceName))
  24. // 获取数据库连接
  25. db := dbFactory.GetDB()
  26. // 查询指定微服务的所有配置
  27. query := `
  28. SELECT
  29. id,
  30. service_name,
  31. config_name,
  32. yaml_name,
  33. yaml_value,
  34. creator,
  35. created_at
  36. FROM config_startup_svc
  37. WHERE service_name = ?
  38. ORDER BY config_name, yaml_name
  39. `
  40. rows, err := db.QueryxContext(ctx, query, serviceName)
  41. if err != nil {
  42. logger.ErrorC(reqCtx, fmt.Sprintf("查询微服务配置失败: %v", err))
  43. return util.CreateErrorResult[[]ServiceConfig](fmt.Sprintf("查询微服务配置失败: %v", err), reqCtx)
  44. }
  45. defer rows.Close()
  46. var configs []ServiceConfig
  47. for rows.Next() {
  48. var config ServiceConfig
  49. var creator, createdAt interface{}
  50. err := rows.Scan(&config.ID, &config.ServiceName, &config.ConfigName, &config.YamlName, &config.YamlValue, &creator, &createdAt)
  51. if err != nil {
  52. logger.ErrorC(reqCtx, fmt.Sprintf("扫描微服务配置数据失败: %v", err))
  53. continue
  54. }
  55. // 处理可能为nil的字段
  56. if creator != nil {
  57. config.Creator = fmt.Sprintf("%v", creator)
  58. }
  59. if createdAt != nil {
  60. config.CreatedAt = fmt.Sprintf("%v", createdAt)
  61. }
  62. configs = append(configs, config)
  63. }
  64. if err := rows.Err(); err != nil {
  65. logger.ErrorC(reqCtx, fmt.Sprintf("遍历微服务配置数据失败: %v", err))
  66. return util.CreateErrorResult[[]ServiceConfig](fmt.Sprintf("遍历微服务配置数据失败: %v", err), reqCtx)
  67. }
  68. logger.Debug(fmt.Sprintf("获取到微服务 %s 的 %d 个配置项", serviceName, len(configs)))
  69. return util.CreateSuccessResultData[[]ServiceConfig](configs, reqCtx)
  70. }