infrastructure.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package infrastructure
  2. import (
  3. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/cache"
  4. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/cache/local"
  5. redisCache "git.sxidc.com/go-framework/baize/framework/core/infrastructure/cache/redis"
  6. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database"
  7. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/data_service"
  8. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/operations"
  9. messageQueueCommon "git.sxidc.com/go-framework/baize/framework/core/infrastructure/message_queue/common"
  10. mqttMessageQueue "git.sxidc.com/go-framework/baize/framework/core/infrastructure/message_queue/mqtt"
  11. redisMessageQueue "git.sxidc.com/go-framework/baize/framework/core/infrastructure/message_queue/redis"
  12. "git.sxidc.com/go-tools/utils/strutils"
  13. )
  14. type Config struct {
  15. DatabaseConfig
  16. CacheConfig
  17. MessageQueueConfig
  18. }
  19. type DatabaseConfig struct {
  20. Operations *operations.Config `json:"operations" yaml:"operations"`
  21. DataService *data_service.Config `json:"data_service" yaml:"data_service"`
  22. }
  23. type CacheConfig struct {
  24. Namespace string `json:"namespace" yaml:"namespace"`
  25. Redis *RedisConfig `json:"redis" yaml:"redis"`
  26. }
  27. type MessageQueueConfig struct {
  28. Redis *RedisConfig `json:"redis" yaml:"redis"`
  29. Mqtt *MqttConfig `json:"mqtt" yaml:"mqtt"`
  30. }
  31. type RedisConfig struct {
  32. Address string `json:"address" yaml:"address"`
  33. UserName string `json:"user_name" yaml:"user_name"`
  34. Password string `json:"password" yaml:"password"`
  35. DB int `json:"db" yaml:"db"`
  36. MaxLen int64 `json:"max_len" yaml:"max_len"`
  37. ConsumerNum int `json:"consumer_num" yaml:"consumer_num"`
  38. }
  39. type MqttConfig struct {
  40. Address string `json:"address" yaml:"address"`
  41. UserName string `json:"user_name" yaml:"user_name"`
  42. Password string `json:"password" yaml:"password"`
  43. }
  44. // Infrastructure 基础设施结构
  45. type Infrastructure struct {
  46. dbExecutor database.Executor
  47. localCache cache.Cache
  48. redisCache cache.Cache
  49. redisMessageQueue messageQueueCommon.MessageQueue
  50. mqttMessageQueue messageQueueCommon.MessageQueue
  51. }
  52. func NewInfrastructure(config Config) *Infrastructure {
  53. i := new(Infrastructure)
  54. // 数据库执行器多选一
  55. if config.DatabaseConfig.Operations != nil {
  56. op, err := operations.NewOperations(config.DatabaseConfig.Operations)
  57. if err != nil {
  58. panic("创建数据库操作失败: " + err.Error())
  59. }
  60. i.dbExecutor = op
  61. } else if config.DatabaseConfig.DataService != nil {
  62. executor, err := data_service.NewExecutor(config.DatabaseConfig.DataService)
  63. if err != nil {
  64. panic("创建数据服务执行器失败: " + err.Error())
  65. }
  66. i.dbExecutor = executor
  67. }
  68. // 初始化缓存
  69. if strutils.IsStringNotEmpty(config.CacheConfig.Namespace) {
  70. namespace := config.CacheConfig.Namespace
  71. i.localCache = local.New(namespace)
  72. if config.CacheConfig.Redis != nil {
  73. redisConf := config.CacheConfig.Redis
  74. newRedisCache, err := redisCache.New(redisConf.Address, redisConf.UserName, redisConf.Password, redisConf.DB, namespace)
  75. if err != nil {
  76. panic("初始化Redis缓存失败: " + err.Error())
  77. }
  78. i.redisCache = newRedisCache
  79. }
  80. }
  81. // 初始化Redis消息队列
  82. if config.MessageQueueConfig.Redis != nil {
  83. redisConf := config.MessageQueueConfig.Redis
  84. newRedisMessageQueue := redisMessageQueue.New(redisConf.Address, redisConf.UserName, redisConf.Password, redisConf.DB,
  85. redisMessageQueue.WithMaxLen(redisConf.MaxLen),
  86. redisMessageQueue.WithConsumerNum(redisConf.ConsumerNum))
  87. i.redisMessageQueue = newRedisMessageQueue
  88. }
  89. // 初始化Mqtt消息队列
  90. if config.MessageQueueConfig.Mqtt != nil {
  91. mqttConf := config.MessageQueueConfig.Mqtt
  92. newMqttMessageQueue, err := mqttMessageQueue.New(mqttConf.Address, mqttConf.UserName, mqttConf.Password)
  93. if err != nil {
  94. panic("初始化MQTT消息队列失败: " + err.Error())
  95. }
  96. i.mqttMessageQueue = newMqttMessageQueue
  97. }
  98. return i
  99. }
  100. func DestroyInfrastructure(i *Infrastructure) {
  101. if i == nil {
  102. return
  103. }
  104. if i.redisMessageQueue != nil {
  105. redisMessageQueue.Destroy(i.redisMessageQueue.(*redisMessageQueue.MessageQueue))
  106. }
  107. if i.redisCache != nil {
  108. err := redisCache.Destroy(i.redisCache.(*redisCache.Cache))
  109. if err != nil {
  110. panic("销毁Redis缓存失败: " + err.Error())
  111. }
  112. }
  113. if i.localCache != nil {
  114. local.Destroy(i.localCache.(*local.Cache))
  115. }
  116. if i.dbExecutor != nil {
  117. switch dbExecutor := i.dbExecutor.(type) {
  118. case *operations.Operations:
  119. err := operations.DestroyOperation(dbExecutor)
  120. if err != nil {
  121. panic("销毁数据库操作失败: " + err.Error())
  122. }
  123. case *data_service.Executor:
  124. err := data_service.DestroyExecutor(dbExecutor)
  125. if err != nil {
  126. panic("销毁数据库操作失败: " + err.Error())
  127. }
  128. default:
  129. panic("不支持的数据库执行器类型")
  130. }
  131. }
  132. return
  133. }
  134. // DBExecutor 获取数据库基础设施
  135. // 参数: 无
  136. // 返回值:
  137. // - 数据库基础设施的接口
  138. func (i Infrastructure) DBExecutor() database.Executor {
  139. return i.dbExecutor
  140. }
  141. // LocalCache 获取本地缓存基础设施
  142. // 参数: 无
  143. // 返回值:
  144. // - 缓存基础设施的接口
  145. func (i Infrastructure) LocalCache() cache.Cache {
  146. return i.localCache
  147. }
  148. // RedisCache 获取Redis缓存基础设施
  149. // 参数: 无
  150. // 返回值:
  151. // - 缓存基础设施的接口
  152. func (i Infrastructure) RedisCache() cache.Cache {
  153. return i.redisCache
  154. }
  155. // RedisMessageQueue 获取Redis消息队列基础设施
  156. // 参数: 无
  157. // 返回值:
  158. // - 消息队列基础设施的接口
  159. func (i Infrastructure) RedisMessageQueue() messageQueueCommon.MessageQueue {
  160. return i.redisMessageQueue
  161. }
  162. // MqttMessageQueue 获取Mqtt消息队列基础设施
  163. // 参数: 无
  164. // 返回值:
  165. // - 消息队列基础设施的接口
  166. func (i Infrastructure) MqttMessageQueue() messageQueueCommon.MessageQueue {
  167. return i.mqttMessageQueue
  168. }