1
1
package motan
2
2
3
3
import (
4
- "encoding/json"
5
4
"flag"
6
5
"fmt"
7
6
"io/ioutil"
@@ -27,7 +26,7 @@ const (
27
26
28
27
type Agent struct {
29
28
ConfigFile string
30
- extFactory motan.ExtentionFactory
29
+ extFactory motan.ExtensionFactory
31
30
Context * motan.Context
32
31
33
32
agentServer motan.Server
@@ -47,10 +46,10 @@ type Agent struct {
47
46
manageHandlers map [string ]http.Handler
48
47
}
49
48
50
- func NewAgent (extfactory motan.ExtentionFactory ) * Agent {
49
+ func NewAgent (extfactory motan.ExtensionFactory ) * Agent {
51
50
var agent * Agent
52
51
if extfactory == nil {
53
- fmt .Println ("agent using default extentionFactory ." )
52
+ fmt .Println ("agent using default extensionFactory ." )
54
53
agent = & Agent {extFactory : GetDefaultExtFactory ()}
55
54
} else {
56
55
agent = & Agent {extFactory : extfactory }
@@ -106,6 +105,7 @@ func (a *Agent) initParam() {
106
105
logdir = "."
107
106
}
108
107
initLog (logdir )
108
+ registerSwitchers (a .Context )
109
109
110
110
port := * motan .Port
111
111
if port == 0 && section != nil && section ["port" ] != nil {
@@ -165,7 +165,7 @@ func (a *Agent) SetSanpshotConf() {
165
165
if snapshotDir == "" {
166
166
snapshotDir = registry .DefaultSnapshotDir
167
167
}
168
- registry .SetSanpshotConf (registry .DefaultSnapshotInterval , snapshotDir )
168
+ registry .SetSnapshotConf (registry .DefaultSnapshotInterval , snapshotDir )
169
169
}
170
170
171
171
func (a * Agent ) initAgentURL () {
@@ -215,7 +215,7 @@ func (a *Agent) startAgent() {
215
215
}
216
216
217
217
func (a * Agent ) registerAgent () {
218
- vlog .Infoln ("start agent regitstry ." )
218
+ vlog .Infoln ("start agent registry ." )
219
219
if reg , exit := a .agentURL .Parameters [motan .RegistryKey ]; exit {
220
220
if registryURL , regexit := a .Context .RegistryURLs [reg ]; regexit {
221
221
registry := a .extFactory .GetRegistry (registryURL )
@@ -232,7 +232,7 @@ func (a *Agent) registerAgent() {
232
232
}
233
233
}
234
234
} else {
235
- vlog .Warningf ("can not find agent registry in conf, so do not register. agent url:%s\n " , a .agentURL )
235
+ vlog .Warningf ("can not find agent registry in conf, so do not register. agent url:%s\n " , a .agentURL . GetIdentity () )
236
236
}
237
237
}
238
238
}
@@ -288,6 +288,7 @@ func (a *Agent) startServerAgent() {
288
288
application = a .agentURL .GetParam (motan .ApplicationKey , "" )
289
289
url .PutParam (motan .ApplicationKey , application )
290
290
}
291
+ url .ClearCachedInfo ()
291
292
exporter := & mserver.DefaultExporter {}
292
293
provider := a .extFactory .GetProvider (url )
293
294
if provider == nil {
@@ -296,7 +297,7 @@ func (a *Agent) startServerAgent() {
296
297
}
297
298
motan .CanSetContext (provider , globalContext )
298
299
motan .Initialize (provider )
299
- provider = mserver .WarperWithFilter (provider , a .extFactory )
300
+ provider = mserver .WrapWithFilter (provider , a .extFactory , globalContext )
300
301
exporter .SetProvider (provider )
301
302
server := a .agentPortServer [url .Port ]
302
303
if server == nil {
@@ -373,6 +374,14 @@ func initLog(logdir string) {
373
374
vlog .LogInit (nil )
374
375
}
375
376
377
+ func registerSwitchers (c * motan.Context ) {
378
+ switchers , _ := c .Config .GetSection (motan .SwitcherSection )
379
+ s := motan .GetSwitcherManager ()
380
+ for n , v := range switchers {
381
+ s .Register (n .(string ), v .(bool ))
382
+ }
383
+ }
384
+
376
385
func getDefaultResponse (requestid uint64 , errmsg string ) * motan.MotanResponse {
377
386
return motan .BuildExceptionResponse (requestid , & motan.Exception {ErrCode : 400 , ErrMsg : errmsg , ErrType : motan .ServiceException })
378
387
}
@@ -410,24 +419,15 @@ func (a *Agent) RegisterManageHandler(path string, handler http.Handler) {
410
419
}
411
420
412
421
func (a * Agent ) startMServer () {
413
- if _ , ok := a .manageHandlers ["/" ]; ! ok {
414
- a .manageHandlers ["/" ] = http .HandlerFunc (a .rootHandler )
415
- }
416
- if _ , ok := a .manageHandlers ["/503" ]; ! ok {
417
- a .manageHandlers ["/503" ] = http .HandlerFunc (a .StatusChangeHandler )
418
- }
419
- if _ , ok := a .manageHandlers ["/200" ]; ! ok {
420
- a .manageHandlers ["/200" ] = http .HandlerFunc (a .StatusChangeHandler )
421
- }
422
- if _ , ok := a .manageHandlers ["/getConfig" ]; ! ok {
423
- a .manageHandlers ["/getConfig" ] = http .HandlerFunc (a .getConfigHandler )
424
- }
425
- if _ , ok := a .manageHandlers ["/getReferService" ]; ! ok {
426
- a .manageHandlers ["/getReferService" ] = http .HandlerFunc (a .getReferServiceHandler )
422
+ handlers := make (map [string ]http.Handler , 16 )
423
+ for k , v := range GetDefaultManageHandlers () {
424
+ handlers [k ] = v
427
425
}
428
426
for k , v := range a .manageHandlers {
429
- http .Handle (k , v )
430
- vlog .Infof ("add manage server handle path:%s\n " , k )
427
+ handlers [k ] = v
428
+ }
429
+ for k , v := range handlers {
430
+ a .mhandle (k , v )
431
431
}
432
432
433
433
vlog .Infof ("start listen manage port %d ...\n " , a .mport )
@@ -438,60 +438,31 @@ func (a *Agent) startMServer() {
438
438
}
439
439
}
440
440
441
- type rpcService struct {
442
- Name string `json:"name"`
443
- Status bool `json:"status"`
444
- }
445
-
446
- type body struct {
447
- Service []rpcService `json:"service"`
448
- }
449
-
450
- type jsonRetData struct {
451
- Code int `json:"code"`
452
- Body body `json:"body"`
453
- }
454
-
455
- // return agent server status, e.g. 200 or 503
456
- func (a * Agent ) rootHandler (w http.ResponseWriter , r * http.Request ) {
457
- w .WriteHeader (a .status )
458
- w .Write ([]byte (http .StatusText (a .status )))
459
- }
460
-
461
- func (a * Agent ) getConfigHandler (w http.ResponseWriter , r * http.Request ) {
462
- data , err := ioutil .ReadFile (* motan .CfgFile )
463
- if err != nil {
464
- w .Write ([]byte ("error." ))
465
- } else {
466
- w .Write (data )
467
- }
468
- }
469
-
470
- func (a * Agent ) getReferServiceHandler (w http.ResponseWriter , r * http.Request ) {
471
-
472
- mbody := body {Service : []rpcService {}}
473
- for _ , cls := range a .clustermap {
474
- rpc := cls .GetURL ().Path
475
- available := cls .IsAvailable ()
476
- mbody .Service = append (mbody .Service , rpcService {Name : rpc , Status : available })
477
- }
478
- retData := & jsonRetData {Code : 200 , Body : mbody }
479
- if data , err := json .Marshal (& retData ); err == nil {
480
- w .Write (data )
481
- } else {
482
- w .Write ([]byte ("error." ))
441
+ func (a * Agent ) mhandle (k string , h http.Handler ) {
442
+ defer func () {
443
+ if err := recover (); err != nil {
444
+ vlog .Warningf ("manageHandler register fail. maybe the pattern '%s' already registered\n " , k )
445
+ }
446
+ }()
447
+ if sa , ok := h .(SetAgent ); ok {
448
+ sa .SetAgent (a )
483
449
}
450
+ http .HandleFunc (k , func (w http.ResponseWriter , r * http.Request ) {
451
+ if ! PermissionCheck (r ) {
452
+ w .Write ([]byte ("need permission!" ))
453
+ return
454
+ }
455
+ defer func () {
456
+ if err := recover (); err != nil {
457
+ fmt .Fprintf (w , "process request err: %s\n " , err )
458
+ }
459
+ }()
460
+ h .ServeHTTP (w , r )
461
+ })
462
+ vlog .Infof ("add manage server handle path:%s\n " , k )
484
463
}
485
464
486
- // StatusChangeHandler change agent server status, and set registed services available or unavailable.
487
- func (a * Agent ) StatusChangeHandler (w http.ResponseWriter , r * http.Request ) {
488
- switch r .RequestURI {
489
- case "/200" :
490
- availableService (a .serviceRegistries )
491
- a .status = http .StatusOK
492
- case "/503" :
493
- unavailableService (a .serviceRegistries )
494
- a .status = http .StatusServiceUnavailable
495
- }
496
- w .Write ([]byte ("ok." ))
465
+ func (a * Agent ) getConfigData () []byte {
466
+ data , _ := ioutil .ReadFile (* motan .CfgFile )
467
+ return data
497
468
}
0 commit comments