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

servicecontext.go 2.0 kB

2 years ago
2 years ago
2 years ago
2 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. Copyright (c) [2023] [pcm]
  3. [pcm-coordinator] is licensed under Mulan PSL v2.
  4. You can use this software according to the terms and conditions of the Mulan PSL v2.
  5. You may obtain a copy of Mulan PSL v2 at:
  6. http://license.coscl.org.cn/MulanPSL2
  7. THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
  8. EITHER EXPaRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
  9. MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
  10. See the Mulan PSL v2 for more details.
  11. */
  12. package svc
  13. import (
  14. "github.com/go-redis/redis/v8"
  15. _ "github.com/go-sql-driver/mysql"
  16. "github.com/zeromicro/go-zero/core/logx"
  17. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils"
  18. "gitlink.org.cn/JointCloud/pcm-coordinator/rpc/internal/config"
  19. "gorm.io/driver/mysql"
  20. "gorm.io/gorm"
  21. "gorm.io/gorm/logger"
  22. "gorm.io/gorm/schema"
  23. "time"
  24. )
  25. type ServiceContext struct {
  26. Config config.Config
  27. DbEngin *gorm.DB
  28. RedisClient *redis.Client
  29. }
  30. func NewServiceContext(c config.Config) *ServiceContext {
  31. //启动Gorm支持
  32. dbEngin, _ := gorm.Open(mysql.Open(c.DB.DataSource), &gorm.Config{
  33. NamingStrategy: schema.NamingStrategy{
  34. SingularTable: true, // 使用单数表名,启用该选项,此时,`User` 的表名应该是 `t_user`
  35. },
  36. Logger: logger.Default.LogMode(logger.Warn),
  37. })
  38. sqlDB, err := dbEngin.DB()
  39. // SetMaxIdleConns 设置空闲连接池中连接的最大数量
  40. sqlDB.SetMaxIdleConns(10)
  41. // SetMaxOpenConns 设置打开数据库连接的最大数量。
  42. sqlDB.SetMaxOpenConns(50)
  43. // SetConnMaxLifetime 设置了连接可复用的最大时间。
  44. sqlDB.SetConnMaxLifetime(time.Hour)
  45. //添加snowflake支持
  46. err = utils.InitSnowflake(c.SnowflakeConf.MachineId)
  47. if err != nil {
  48. logx.Errorf("InitSnowflake err: ", err)
  49. panic("InitSnowflake err")
  50. }
  51. return &ServiceContext{
  52. Config: c,
  53. DbEngin: dbEngin,
  54. RedisClient: redis.NewClient(&redis.Options{
  55. Addr: c.RedisConf.Host,
  56. Password: c.RedisConf.Pass,
  57. DB: 0, // use default DB
  58. }),
  59. }
  60. }

PCM is positioned as Software stack over Cloud, aiming to build the standards and ecology of heterogeneous cloud collaboration for JCC in a non intrusive and autonomous peer-to-peer manner.