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.

client.go 2.8 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package client
  18. import (
  19. "github.com/seata/seata-go/pkg/remoting/getty"
  20. "sync"
  21. "github.com/seata/seata-go/pkg/datasource"
  22. at "github.com/seata/seata-go/pkg/datasource/sql"
  23. "github.com/seata/seata-go/pkg/datasource/sql/exec/config"
  24. "github.com/seata/seata-go/pkg/integration"
  25. remoteConfig "github.com/seata/seata-go/pkg/remoting/config"
  26. "github.com/seata/seata-go/pkg/remoting/processor/client"
  27. "github.com/seata/seata-go/pkg/rm"
  28. "github.com/seata/seata-go/pkg/rm/tcc"
  29. "github.com/seata/seata-go/pkg/tm"
  30. "github.com/seata/seata-go/pkg/util/log"
  31. )
  32. // Init seata client client
  33. func Init() {
  34. InitPath("")
  35. }
  36. // InitPath init client with config path
  37. func InitPath(configFilePath string) {
  38. cfg := LoadPath(configFilePath)
  39. initRmClient(cfg)
  40. initTmClient(cfg)
  41. initDatasource()
  42. }
  43. var (
  44. onceInitTmClient sync.Once
  45. onceInitRmClient sync.Once
  46. onceInitDatasource sync.Once
  47. )
  48. // InitTmClient init client tm client
  49. func initTmClient(cfg *Config) {
  50. onceInitTmClient.Do(func() {
  51. tm.InitTm(cfg.ClientConfig.TmConfig)
  52. })
  53. }
  54. // initRemoting init rpc client
  55. func initRemoting(cfg *Config) {
  56. getty.InitRpcClient(&cfg.GettyConfig, &remoteConfig.SeataConfig{
  57. ApplicationID: cfg.ApplicationID,
  58. TxServiceGroup: cfg.TxServiceGroup,
  59. ServiceVgroupMapping: cfg.ServiceConfig.VgroupMapping,
  60. ServiceGrouplist: cfg.ServiceConfig.Grouplist,
  61. LoadBalanceType: cfg.GettyConfig.LoadBalanceType,
  62. })
  63. }
  64. // InitRmClient init client rm client
  65. func initRmClient(cfg *Config) {
  66. onceInitRmClient.Do(func() {
  67. log.Init()
  68. initRemoting(cfg)
  69. rm.InitRm(rm.RmConfig{
  70. Config: cfg.ClientConfig.RmConfig,
  71. ApplicationID: cfg.ApplicationID,
  72. TxServiceGroup: cfg.TxServiceGroup,
  73. })
  74. config.Init(cfg.ClientConfig.RmConfig.LockConfig)
  75. client.RegisterProcessor()
  76. integration.Init()
  77. tcc.InitTCC()
  78. at.InitAT(cfg.ClientConfig.UndoConfig, cfg.AsyncWorkerConfig)
  79. at.InitXA(cfg.ClientConfig.XaConfig)
  80. })
  81. }
  82. func initDatasource() {
  83. onceInitDatasource.Do(func() {
  84. datasource.Init()
  85. })
  86. }