@@ -31,11 +31,12 @@ import (
31
31
)
32
32
33
33
const (
34
- ParamAppId string = "appId"
35
- ParamInstId string = "instId"
36
- ParamValue string = "value"
37
- ParamVip string = "vipAddress"
38
- ParamSVip string = "svipAddress"
34
+ ParamAppId string = "appId"
35
+ ParamInstId string = "instId"
36
+ ParamValue string = "value"
37
+ ParamVip string = "vipAddress"
38
+ ParamSVip string = "svipAddress"
39
+ HeaderNamespace string = "x-namespace"
39
40
)
40
41
41
42
// GetEurekaServer eureka web server
@@ -104,7 +105,7 @@ func (h *EurekaServer) addDiscoverAccess(ws *restful.WebService) {
104
105
ws .Route (ws .GET (fmt .Sprintf ("/instances/{%s}" , ParamInstId )).To (h .GetInstance )).
105
106
Param (ws .PathParameter (ParamInstId , "instanceId" ).DataType ("string" ))
106
107
// Update metadata
107
- ws .Route (ws .GET (fmt .Sprintf ("/apps/{%s}/{%s}/metadata" , ParamAppId , ParamInstId )).To (h .UpdateMetadata )).
108
+ ws .Route (ws .PUT (fmt .Sprintf ("/apps/{%s}/{%s}/metadata" , ParamAppId , ParamInstId )).To (h .UpdateMetadata )).
108
109
Param (ws .PathParameter (ParamAppId , "applicationId" ).DataType ("string" )).
109
110
Param (ws .PathParameter (ParamInstId , "instanceId" ).DataType ("string" ))
110
111
// Query for all instances under a particular vip address
@@ -131,7 +132,8 @@ func parseAcceptValue(acceptValue string) map[string]bool {
131
132
132
133
// GetAllApplications 全量拉取服务实例信息
133
134
func (h * EurekaServer ) GetAllApplications (req * restful.Request , rsp * restful.Response ) {
134
- appsRespCache := h .worker .GetCachedAppsWithLoad ()
135
+ namespace := readNamespaceFromRequest (req , h .namespace )
136
+ appsRespCache := h .workers .Get (namespace ).GetCachedAppsWithLoad ()
135
137
remoteAddr := req .Request .RemoteAddr
136
138
acceptValue := getParamFromEurekaRequestHeader (req , restful .HEADER_Accept )
137
139
if err := writeResponse (parseAcceptValue (acceptValue ), appsRespCache , req , rsp ); nil != err {
@@ -148,7 +150,8 @@ func (h *EurekaServer) GetApplication(req *restful.Request, rsp *restful.Respons
148
150
appId := readAppIdFromRequest (req )
149
151
150
152
remoteAddr := req .Request .RemoteAddr
151
- appsRespCache := h .worker .GetCachedAppsWithLoad ()
153
+ namespace := readNamespaceFromRequest (req , h .namespace )
154
+ appsRespCache := h .workers .Get (namespace ).GetCachedAppsWithLoad ()
152
155
apps := appsRespCache .AppsResp .Applications
153
156
app := apps .GetApplication (appId )
154
157
if app == nil {
@@ -190,7 +193,8 @@ func (h *EurekaServer) GetAppInstance(req *restful.Request, rsp *restful.Respons
190
193
writeHeader (http .StatusBadRequest , rsp )
191
194
return
192
195
}
193
- appsRespCache := h .worker .GetCachedAppsWithLoad ()
196
+ namespace := readNamespaceFromRequest (req , h .namespace )
197
+ appsRespCache := h .workers .Get (namespace ).GetCachedAppsWithLoad ()
194
198
apps := appsRespCache .AppsResp .Applications
195
199
app := apps .GetApplication (appId )
196
200
if app == nil {
@@ -271,13 +275,15 @@ func writeResponse(acceptValues map[string]bool, appsRespCache *ApplicationsResp
271
275
272
276
// GetDeltaApplications 增量拉取服务实例信息
273
277
func (h * EurekaServer ) GetDeltaApplications (req * restful.Request , rsp * restful.Response ) {
274
- appsRespCache := h .worker .GetDeltaApps ()
278
+ namespace := readNamespaceFromRequest (req , h .namespace )
279
+ work := h .workers .Get (namespace )
280
+ appsRespCache := work .GetDeltaApps ()
275
281
if nil == appsRespCache {
276
- ctx := h . worker .StartWorker ()
282
+ ctx := work .StartWorker ()
277
283
if nil != ctx {
278
284
<- ctx .Done ()
279
285
}
280
- appsRespCache = h . worker .GetDeltaApps ()
286
+ appsRespCache = work .GetDeltaApps ()
281
287
}
282
288
remoteAddr := req .Request .RemoteAddr
283
289
acceptValue := getParamFromEurekaRequestHeader (req , restful .HEADER_Accept )
@@ -378,18 +384,24 @@ func (h *EurekaServer) RegisterApplication(req *restful.Request, rsp *restful.Re
378
384
379
385
ctx := context .WithValue (context .Background (), utils .ContextAuthTokenKey , token )
380
386
381
- log .Infof ("[EUREKA-SERVER]received instance register request, client: %s, instId: %s, appId: %s, ipAddr: %s" ,
382
- remoteAddr , registrationRequest .Instance .InstanceId , appId , registrationRequest .Instance .IpAddr )
383
- code := h .registerInstances (ctx , appId , registrationRequest .Instance , false )
387
+ namespace := readNamespaceFromRequest (req , h .namespace )
388
+ log .Infof (
389
+ "[EUREKA-SERVER]received instance register request, " +
390
+ "client: %s, namespace: %s, instId: %s, appId: %s, ipAddr: %s" ,
391
+ remoteAddr , namespace , registrationRequest .Instance .InstanceId , appId , registrationRequest .Instance .IpAddr )
392
+ code := h .registerInstances (ctx , namespace , appId , registrationRequest .Instance , false )
384
393
if code == api .ExecuteSuccess || code == api .ExistedResource || code == api .SameInstanceRequest {
385
- log .Infof ("[EUREKA-SERVER]instance (instId=%s, appId=%s) has been registered successfully, code is %d" ,
386
- registrationRequest .Instance .InstanceId , appId , code )
394
+ log .Infof (
395
+ "[EUREKA-SERVER]instance (namespace=%s, instId=%s, appId=%s) has been registered successfully," +
396
+ " code is %d" ,
397
+ namespace , registrationRequest .Instance .InstanceId , appId , code )
387
398
writePolarisStatusCode (req , code )
388
399
writeHeader (http .StatusNoContent , rsp )
389
400
return
390
401
}
391
- log .Errorf ("[EUREKA-SERVER]instance (instId=%s, appId=%s) has been registered failed, code is %d" ,
392
- registrationRequest .Instance .InstanceId , appId , code )
402
+ log .Errorf ("[EUREKA-SERVER]instance (namespace=%s, instId=%s, appId=%s) has been registered failed, " +
403
+ "code is %d" ,
404
+ namespace , registrationRequest .Instance .InstanceId , appId , code )
393
405
writePolarisStatusCode (req , code )
394
406
writeHeader (int (code / 1000 ), rsp )
395
407
}
@@ -414,23 +426,27 @@ func (h *EurekaServer) UpdateStatus(req *restful.Request, rsp *restful.Response)
414
426
return
415
427
}
416
428
status := req .QueryParameter (ParamValue )
417
- log .Infof ("[EUREKA-SERVER]received instance updateStatus request, client: %s, instId: %s, appId: %s, status: %s" ,
418
- remoteAddr , instId , appId , status )
429
+ namespace := readNamespaceFromRequest (req , h .namespace )
430
+ log .Infof ("[EUREKA-SERVER]received instance updateStatus request, " +
431
+ "client: %s, namespace: %s, instId: %s, appId: %s, status: %s" ,
432
+ remoteAddr , namespace , instId , appId , status )
419
433
// check status
420
434
if status == StatusUnknown {
421
435
writePolarisStatusCode (req , api .ExecuteSuccess )
422
436
writeHeader (http .StatusOK , rsp )
423
437
return
424
438
}
425
- code := h .updateStatus (context .Background (), appId , instId , status , false )
439
+ code := h .updateStatus (context .Background (), namespace , appId , instId , status , false )
426
440
writePolarisStatusCode (req , code )
427
441
if code == api .ExecuteSuccess {
428
- log .Infof ("[EUREKA-SERVER]instance (instId=%s, appId=%s) has been updated successfully" , instId , appId )
442
+ log .Infof ("[EUREKA-SERVER]instance (namespace=%s, instId=%s, appId=%s) has been updated successfully" ,
443
+ namespace , instId , appId )
429
444
writeHeader (http .StatusOK , rsp )
430
445
return
431
446
}
432
- log .Errorf ("[EUREKA-SERVER]instance (instId=%s, appId=%s) has been updated failed, code is %d" ,
433
- instId , appId , code )
447
+ log .Errorf ("[EUREKA-SERVER]instance ((namespace=%s, instId=%s, appId=%s) has been updated failed, " +
448
+ "code is %d" ,
449
+ namespace , instId , appId , code )
434
450
if code == api .NotFoundResource {
435
451
writeHeader (http .StatusNotFound , rsp )
436
452
return
@@ -458,19 +474,24 @@ func (h *EurekaServer) DeleteStatus(req *restful.Request, rsp *restful.Response)
458
474
return
459
475
}
460
476
461
- log .Infof ("[EUREKA-SERVER]received instance status delete request, client: %s, instId=%s, appId=%s" ,
462
- remoteAddr , instId , appId )
477
+ namespace := readNamespaceFromRequest (req , h .namespace )
463
478
464
- code := h .updateStatus (context .Background (), appId , instId , StatusUp , false )
479
+ log .Infof ("[EUREKA-SERVER]received instance status delete request, " +
480
+ "client: %s,namespace=%s, instId=%s, appId=%s" ,
481
+ remoteAddr , namespace , instId , appId )
482
+
483
+ code := h .updateStatus (context .Background (), namespace , appId , instId , StatusUp , false )
465
484
writePolarisStatusCode (req , code )
466
485
if code == api .ExecuteSuccess {
467
- log .Infof ("[EUREKA-SERVER]instance status (instId=%s, appId=%s) has been deleted successfully" ,
468
- instId , appId )
486
+ log .Infof ("[EUREKA-SERVER]instance status (namespace=%s, instId=%s, appId=%s) " +
487
+ "has been deleted successfully" ,
488
+ namespace , instId , appId )
469
489
writeHeader (http .StatusOK , rsp )
470
490
return
471
491
}
472
- log .Errorf ("[EUREKA-SERVER]instance status (instId=%s, appId=%s) has been deleted failed, code is %d" ,
473
- instId , appId , code )
492
+ log .Errorf ("[EUREKA-SERVER]instance status (namespace=%s, instId=%s, appId=%s) " +
493
+ "has been deleted failed, code is %d" ,
494
+ namespace , instId , appId , code )
474
495
if code == api .NotFoundResource {
475
496
writeHeader (http .StatusNotFound , rsp )
476
497
return
@@ -497,14 +518,15 @@ func (h *EurekaServer) RenewInstance(req *restful.Request, rsp *restful.Response
497
518
writeHeader (http .StatusBadRequest , rsp )
498
519
return
499
520
}
500
- code := h .renew (context .Background (), appId , instId , false )
521
+ namespace := readNamespaceFromRequest (req , h .namespace )
522
+ code := h .renew (context .Background (), namespace , appId , instId , false )
501
523
writePolarisStatusCode (req , code )
502
524
if code == api .ExecuteSuccess || code == api .HeartbeatExceedLimit {
503
525
writeHeader (http .StatusOK , rsp )
504
526
return
505
527
}
506
- log .Errorf ("[EUREKA-SERVER]instance (instId=%s, appId=%s) heartbeat failed, code is %d" ,
507
- instId , appId , code )
528
+ log .Errorf ("[EUREKA-SERVER]instance (namespace=%s, instId=%s, appId=%s) heartbeat failed, code is %d" ,
529
+ namespace , instId , appId , code )
508
530
if code == api .NotFoundResource {
509
531
writeHeader (http .StatusNotFound , rsp )
510
532
return
@@ -531,18 +553,22 @@ func (h *EurekaServer) CancelInstance(req *restful.Request, rsp *restful.Respons
531
553
writeHeader (http .StatusBadRequest , rsp )
532
554
return
533
555
}
534
- log .Infof ("[EUREKA-SERVER]received instance deregistered request, client: %s, instId: %s, appId: %s" ,
535
- remoteAddr , instId , appId )
536
- code := h .deregisterInstance (context .Background (), appId , instId , false )
556
+ namespace := readNamespaceFromRequest (req , h .namespace )
557
+ log .Infof ("[EUREKA-SERVER]received instance deregistered request, " +
558
+ "client: %s, namespace: %s, instId: %s, appId: %s" ,
559
+ remoteAddr , namespace , instId , appId )
560
+ code := h .deregisterInstance (context .Background (), namespace , appId , instId , false )
537
561
writePolarisStatusCode (req , code )
538
562
if code == api .ExecuteSuccess || code == api .NotFoundResource || code == api .SameInstanceRequest {
539
563
writeHeader (http .StatusOK , rsp )
540
- log .Infof ("[EUREKA-SERVER]instance (instId=%s, appId=%s) has been deregistered successfully, code is %d" ,
541
- instId , appId , code )
564
+ log .Infof ("[EUREKA-SERVER]instance (namespace=%s, instId=%s, appId=%s) " +
565
+ "has been deregistered successfully, code is %d" ,
566
+ namespace , instId , appId , code )
542
567
return
543
568
}
544
- log .Errorf ("[EUREKA-SERVER]instance (instId=%s, appId=%s) has been deregistered failed, code is %d" ,
545
- instId , appId , code )
569
+ log .Errorf ("[EUREKA-SERVER]instance (namespace=%s, instId=%s, appId=%s) has been deregistered failed," +
570
+ " code is %d" ,
571
+ namespace , instId , appId , code )
546
572
writeHeader (int (code / 1000 ), rsp )
547
573
}
548
574
@@ -557,7 +583,8 @@ func (h *EurekaServer) GetInstance(req *restful.Request, rsp *restful.Response)
557
583
writeHeader (http .StatusBadRequest , rsp )
558
584
return
559
585
}
560
- appsRespCache := h .worker .GetCachedAppsWithLoad ()
586
+ namespace := readNamespaceFromRequest (req , h .namespace )
587
+ appsRespCache := h .workers .Get (namespace ).GetCachedAppsWithLoad ()
561
588
apps := appsRespCache .AppsResp .Applications
562
589
instance := apps .GetInstance (instId )
563
590
if nil == instance {
@@ -596,6 +623,7 @@ func (h *EurekaServer) UpdateMetadata(req *restful.Request, rsp *restful.Respons
596
623
writeHeader (http .StatusBadRequest , rsp )
597
624
return
598
625
}
626
+ namespace := readNamespaceFromRequest (req , h .namespace )
599
627
queryValues := req .Request .URL .Query ()
600
628
metadataMap := make (map [string ]string , len (queryValues ))
601
629
for key , values := range queryValues {
@@ -605,16 +633,17 @@ func (h *EurekaServer) UpdateMetadata(req *restful.Request, rsp *restful.Respons
605
633
}
606
634
metadataMap [key ] = values [0 ]
607
635
}
608
- code := h .updateMetadata (context .Background (), appId , instId , metadataMap )
636
+ code := h .updateMetadata (context .Background (), namespace , appId , instId , metadataMap )
609
637
writePolarisStatusCode (req , code )
610
638
if code == api .ExecuteSuccess {
611
- log .Infof ("[EUREKA-SERVER]instance metadata (instId=%s, appId=%s) has been updated successfully" ,
612
- instId , appId )
639
+ log .Infof ("[EUREKA-SERVER]instance metadata (namespace=%s, instId=%s, appId=%s) has been updated successfully" ,
640
+ namespace , instId , appId )
613
641
writeHeader (http .StatusOK , rsp )
614
642
return
615
643
}
616
- log .Errorf ("[EUREKA-SERVER]instance metadata (instId=%s, appId=%s) has been updated failed, code is %d" ,
617
- instId , appId , code )
644
+ log .Errorf ("[EUREKA-SERVER]instance metadata (namespace=%s, instId=%s, appId=%s) has been updated failed, " +
645
+ "code is %d" ,
646
+ namespace , instId , appId , code )
618
647
if code == api .NotFoundResource {
619
648
writeHeader (http .StatusNotFound , rsp )
620
649
return
@@ -633,7 +662,9 @@ func (h *EurekaServer) QueryByVipAddress(req *restful.Request, rsp *restful.Resp
633
662
writeHeader (http .StatusBadRequest , rsp )
634
663
return
635
664
}
636
- appsRespCache := h .worker .GetVipApps (VipCacheKey {
665
+
666
+ namespace := readNamespaceFromRequest (req , h .namespace )
667
+ appsRespCache := h .workers .Get (namespace ).GetVipApps (VipCacheKey {
637
668
entityType : entityTypeVip ,
638
669
targetVipAddress : formatReadName (vipAddress ),
639
670
})
@@ -654,7 +685,8 @@ func (h *EurekaServer) QueryBySVipAddress(req *restful.Request, rsp *restful.Res
654
685
writeHeader (http .StatusBadRequest , rsp )
655
686
return
656
687
}
657
- appsRespCache := h .worker .GetVipApps (VipCacheKey {
688
+ namespace := readNamespaceFromRequest (req , h .namespace )
689
+ appsRespCache := h .workers .Get (namespace ).GetVipApps (VipCacheKey {
658
690
entityType : entityTypeSVip ,
659
691
targetVipAddress : formatReadName (vipAddress ),
660
692
})
0 commit comments