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 1.9 kB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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/robfig/cron/v3"
  17. "github.com/zeromicro/go-zero/core/logx"
  18. "gitlink.org.cn/jcce-pcm/pcm-coordinator/pkg/utils"
  19. "gitlink.org.cn/jcce-pcm/pcm-coordinator/rpc/internal/config"
  20. "gorm.io/driver/mysql"
  21. "gorm.io/gorm"
  22. "gorm.io/gorm/logger"
  23. "gorm.io/gorm/schema"
  24. )
  25. type ServiceContext struct {
  26. Config config.Config
  27. DbEngin *gorm.DB
  28. Cron *cron.Cron
  29. RedisClient *redis.Client
  30. }
  31. func NewServiceContext(c config.Config) *ServiceContext {
  32. //启动Gorm支持
  33. dbEngin, err := gorm.Open(mysql.Open(c.DB.DataSource), &gorm.Config{
  34. NamingStrategy: schema.NamingStrategy{
  35. SingularTable: true, // 使用单数表名,启用该选项,此时,`User` 的表名应该是 `t_user`
  36. },
  37. Logger: logger.Default.LogMode(logger.Warn),
  38. })
  39. if err != nil {
  40. logx.Error("gorm初始化错误:", err.Error())
  41. return nil
  42. }
  43. //添加snowflake支持
  44. err = utils.InitSnowflake(c.SnowflakeConf.MachineId)
  45. if err != nil {
  46. logx.Errorf("InitSnowflake err: ", err)
  47. panic("InitSnowflake err")
  48. }
  49. return &ServiceContext{
  50. Cron: cron.New(cron.WithSeconds()),
  51. Config: c,
  52. DbEngin: dbEngin,
  53. RedisClient: redis.NewClient(&redis.Options{
  54. Addr: c.RedisConf.Host,
  55. Password: c.RedisConf.Pass,
  56. DB: 0, // use default DB
  57. }),
  58. }
  59. }

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.