github.com/polarismesh/polaris@v1.17.8/service/default.go (about) 1 /** 2 * Tencent is pleased to support the open source community by making Polaris available. 3 * 4 * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. 5 * 6 * Licensed under the BSD 3-Clause License (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * https://opensource.org/licenses/BSD-3-Clause 11 * 12 * Unless required by applicable law or agreed to in writing, software distributed 13 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 14 * CONDITIONS OF ANY KIND, either express or implied. See the License for the 15 * specific language governing permissions and limitations under the License. 16 */ 17 18 package service 19 20 import ( 21 "context" 22 "errors" 23 "sync" 24 25 "golang.org/x/sync/singleflight" 26 27 "github.com/polarismesh/polaris/auth" 28 "github.com/polarismesh/polaris/common/eventhub" 29 "github.com/polarismesh/polaris/common/model" 30 "github.com/polarismesh/polaris/plugin" 31 ) 32 33 const ( 34 // MaxBatchSize max batch size 35 MaxBatchSize = 100 36 // MaxQuerySize max query size 37 MaxQuerySize = 100 38 ) 39 40 const ( 41 // SystemNamespace polaris system namespace 42 SystemNamespace = "Polaris" 43 // DefaultNamespace default namespace 44 DefaultNamespace = "default" 45 // ProductionNamespace default namespace 46 ProductionNamespace = "Production" 47 // DefaultTLL default ttl 48 DefaultTLL = 5 49 ) 50 51 var ( 52 server DiscoverServer 53 namingServer *Server = new(Server) 54 once = sync.Once{} 55 finishInit = false 56 ) 57 58 // Config 核心逻辑层配置 59 type Config struct { 60 Auth map[string]interface{} `yaml:"auth"` 61 Batch map[string]interface{} `yaml:"batch"` 62 } 63 64 // Initialize 初始化 65 func Initialize(ctx context.Context, namingOpt *Config, opts ...InitOption) error { 66 var err error 67 once.Do(func() { 68 err = initialize(ctx, namingOpt, opts...) 69 }) 70 71 if err != nil { 72 return err 73 } 74 75 finishInit = true 76 return nil 77 } 78 79 // GetServer 获取已经初始化好的Server 80 func GetServer() (DiscoverServer, error) { 81 if !finishInit { 82 return nil, errors.New("server has not done InitializeServer") 83 } 84 85 return server, nil 86 } 87 88 // GetOriginServer 获取已经初始化好的Server 89 func GetOriginServer() (*Server, error) { 90 if !finishInit { 91 return nil, errors.New("server has not done InitializeServer") 92 } 93 94 return namingServer, nil 95 } 96 97 // 内部初始化函数 98 func initialize(ctx context.Context, namingOpt *Config, opts ...InitOption) error { 99 // l5service 100 namingServer.l5service = &l5service{} 101 namingServer.createServiceSingle = &singleflight.Group{} 102 namingServer.subCtxs = make([]*eventhub.SubscribtionContext, 0, 4) 103 104 for i := range opts { 105 opts[i](namingServer) 106 } 107 108 // 插件初始化 109 pluginInitialize() 110 111 userMgn, err := auth.GetUserServer() 112 if err != nil { 113 return err 114 } 115 strategyMgn, err := auth.GetStrategyServer() 116 if err != nil { 117 return err 118 } 119 120 server = newServerAuthAbility(namingServer, userMgn, strategyMgn) 121 122 return nil 123 } 124 125 type PluginInstanceEventHandler struct { 126 *BaseInstanceEventHandler 127 subscriber plugin.DiscoverChannel 128 } 129 130 func (p *PluginInstanceEventHandler) OnEvent(ctx context.Context, any2 any) error { 131 e := any2.(model.InstanceEvent) 132 p.subscriber.PublishEvent(e) 133 return nil 134 } 135 136 // 插件初始化 137 func pluginInitialize() { 138 // 获取CMDB插件 139 namingServer.cmdb = plugin.GetCMDB() 140 if namingServer.cmdb == nil { 141 log.Warnf("Not Found CMDB Plugin") 142 } 143 144 // 获取History插件,注意:插件的配置在bootstrap已经设置好 145 namingServer.history = plugin.GetHistory() 146 if namingServer.history == nil { 147 log.Warnf("Not Found History Log Plugin") 148 } 149 150 // 获取限流插件 151 namingServer.ratelimit = plugin.GetRatelimit() 152 if namingServer.ratelimit == nil { 153 log.Warnf("Not found Ratelimit Plugin") 154 } 155 156 subscriber := plugin.GetDiscoverEvent() 157 if subscriber == nil { 158 log.Warnf("Not found DiscoverEvent Plugin") 159 return 160 } 161 162 eventHandler := &PluginInstanceEventHandler{ 163 BaseInstanceEventHandler: NewBaseInstanceEventHandler(namingServer), 164 subscriber: subscriber, 165 } 166 subCtx, err := eventhub.Subscribe(eventhub.InstanceEventTopic, eventHandler) 167 if err != nil { 168 log.Warnf("register DiscoverEvent into eventhub:%s %v", subscriber.Name(), err) 169 } 170 namingServer.subCtxs = append(namingServer.subCtxs, subCtx) 171 }