-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathservice_instance.go
156 lines (138 loc) · 5.07 KB
/
service_instance.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
* This file is part of Golaxy Distributed Service Development Framework.
*
* Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 2.1 of the License, or
* (at your option) any later version.
*
* Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Golaxy Distributed Service Development Framework. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright (c) 2024 pangdogs.
*/
package framework
import (
"git.golaxy.org/core"
"git.golaxy.org/core/runtime"
"git.golaxy.org/core/service"
"git.golaxy.org/core/utils/exception"
"git.golaxy.org/core/utils/reinterpret"
"git.golaxy.org/framework/addins/broker"
"git.golaxy.org/framework/addins/conf"
"git.golaxy.org/framework/addins/dentq"
"git.golaxy.org/framework/addins/discovery"
"git.golaxy.org/framework/addins/dsvc"
"git.golaxy.org/framework/addins/dsync"
"git.golaxy.org/framework/addins/rpc"
"github.com/spf13/viper"
"sync"
)
// GetServiceInstance 获取服务实例
func GetServiceInstance(provider runtime.ConcurrentContextProvider) IServiceInstance {
return reinterpret.Cast[IServiceInstance](service.Current(provider))
}
// IServiceInstance 服务实例接口
type IServiceInstance interface {
service.Context
// GetConf 获取配置插件
GetConf() conf.IConfig
// GetRegistry 获取服务发现插件
GetRegistry() discovery.IRegistry
// GetBroker 获取消息队列中间件插件
GetBroker() broker.IBroker
// GetDistSync 获取分布式同步插件
GetDistSync() dsync.IDistSync
// GetDistService 获取分布式服务插件
GetDistService() dsvc.IDistService
// GetDistEntityQuerier 获取分布式实体查询插件
GetDistEntityQuerier() dentq.IDistEntityQuerier
// GetRPC 获取RPC支持插件
GetRPC() rpc.IRPC
// GetStartupNo 获取启动序号
GetStartupNo() int
// GetStartupConf 获取启动参数配置
GetStartupConf() *viper.Viper
// GetMemKV 获取服务内存KV数据库
GetMemKV() *sync.Map
// CreateRuntime 创建运行时
CreateRuntime() RuntimeCreator
// CreateEntityPT 创建实体原型
CreateEntityPT(prototype string) core.EntityPTCreator
// CreateEntityAsync 创建实体
CreateEntityAsync(prototype string) EntityCreatorAsync
}
// ServiceInstance 服务实例
type ServiceInstance struct {
service.ContextBehavior
runtimeGeneric RuntimeGeneric
}
// GetConf 获取配置插件
func (inst *ServiceInstance) GetConf() conf.IConfig {
return conf.Using(inst)
}
// GetRegistry 获取服务发现插件
func (inst *ServiceInstance) GetRegistry() discovery.IRegistry {
return discovery.Using(inst)
}
// GetBroker 获取消息队列中间件插件
func (inst *ServiceInstance) GetBroker() broker.IBroker {
return broker.Using(inst)
}
// GetDistSync 获取分布式同步插件
func (inst *ServiceInstance) GetDistSync() dsync.IDistSync {
return dsync.Using(inst)
}
// GetDistService 获取分布式服务插件
func (inst *ServiceInstance) GetDistService() dsvc.IDistService {
return dsvc.Using(inst)
}
// GetDistEntityQuerier 获取分布式实体查询插件
func (inst *ServiceInstance) GetDistEntityQuerier() dentq.IDistEntityQuerier {
return dentq.Using(inst)
}
// GetRPC 获取RPC支持插件
func (inst *ServiceInstance) GetRPC() rpc.IRPC {
return rpc.Using(inst)
}
// GetStartupNo 获取启动序号
func (inst *ServiceInstance) GetStartupNo() int {
v, _ := inst.GetMemKV().Load("startup.no")
if v == nil {
exception.Panicf("%w: service memory kv startup.no not existed", ErrFramework)
}
return v.(int)
}
// GetStartupConf 获取启动参数配置
func (inst *ServiceInstance) GetStartupConf() *viper.Viper {
v, _ := inst.GetMemKV().Load("startup.conf")
if v == nil {
exception.Panicf("%w: service memory kv startup.conf not existed", ErrFramework)
}
return v.(*viper.Viper)
}
// GetMemKV 获取服务内存KV数据库
func (inst *ServiceInstance) GetMemKV() *sync.Map {
memKV, _ := inst.Value("mem_kv").(*sync.Map)
if memKV == nil {
exception.Panicf("%w: service memory not existed", ErrFramework)
}
return memKV
}
// CreateRuntime 创建运行时
func (inst *ServiceInstance) CreateRuntime() RuntimeCreator {
return CreateRuntime(service.UnsafeContext(inst).GetOptions().InstanceFace.Iface).Setup(&inst.runtimeGeneric)
}
// CreateEntityPT 创建实体原型
func (inst *ServiceInstance) CreateEntityPT(prototype string) core.EntityPTCreator {
return core.CreateEntityPT(service.UnsafeContext(inst).GetOptions().InstanceFace.Iface, prototype)
}
// CreateEntityAsync 创建实体
func (inst *ServiceInstance) CreateEntityAsync(prototype string) EntityCreatorAsync {
return CreateEntityAsync(service.UnsafeContext(inst).GetOptions().InstanceFace.Iface, prototype).RuntimeCreator(inst.CreateRuntime())
}