github.com/polarismesh/polaris@v1.17.8/apiserver/httpserver/discover/v1/client_access.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 v1 19 20 import ( 21 "context" 22 "fmt" 23 24 "github.com/emicklei/go-restful/v3" 25 apimodel "github.com/polarismesh/specification/source/go/api/v1/model" 26 apiservice "github.com/polarismesh/specification/source/go/api/v1/service_manage" 27 "go.uber.org/zap" 28 29 httpcommon "github.com/polarismesh/polaris/apiserver/httpserver/utils" 30 api "github.com/polarismesh/polaris/common/api/v1" 31 "github.com/polarismesh/polaris/common/utils" 32 ) 33 34 // ReportClient 客户端上报信息 35 func (h *HTTPServerV1) ReportClient(req *restful.Request, rsp *restful.Response) { 36 handler := &httpcommon.Handler{ 37 Request: req, 38 Response: rsp, 39 } 40 client := &apiservice.Client{} 41 ctx, err := handler.Parse(client) 42 if err != nil { 43 handler.WriteHeaderAndProto(api.NewResponseWithMsg(apimodel.Code_ParseException, err.Error())) 44 return 45 } 46 47 handler.WriteHeaderAndProto(h.namingServer.ReportClient(ctx, client)) 48 } 49 50 // RegisterInstance 注册服务实例 51 func (h *HTTPServerV1) RegisterInstance(req *restful.Request, rsp *restful.Response) { 52 handler := &httpcommon.Handler{ 53 Request: req, 54 Response: rsp, 55 } 56 57 instance := &apiservice.Instance{} 58 ctx, err := handler.Parse(instance) 59 if err != nil { 60 handler.WriteHeaderAndProto(api.NewResponseWithMsg(apimodel.Code_ParseException, err.Error())) 61 return 62 } 63 // 客户端请求中带了 token 的,优先已请求中的为准 64 if instance.GetServiceToken().GetValue() != "" { 65 ctx = context.WithValue(ctx, utils.ContextAuthTokenKey, instance.GetServiceToken().GetValue()) 66 } 67 68 handler.WriteHeaderAndProto(h.namingServer.RegisterInstance(ctx, instance)) 69 } 70 71 // DeregisterInstance 反注册服务实例 72 func (h *HTTPServerV1) DeregisterInstance(req *restful.Request, rsp *restful.Response) { 73 handler := &httpcommon.Handler{ 74 Request: req, 75 Response: rsp, 76 } 77 78 instance := &apiservice.Instance{} 79 ctx, err := handler.Parse(instance) 80 if err != nil { 81 handler.WriteHeaderAndProto(api.NewResponseWithMsg(apimodel.Code_ParseException, err.Error())) 82 return 83 } 84 // 客户端请求中带了 token 的,优先已请求中的为准 85 if instance.GetServiceToken().GetValue() != "" { 86 ctx = context.WithValue(ctx, utils.ContextAuthTokenKey, instance.GetServiceToken().GetValue()) 87 } 88 handler.WriteHeaderAndProto(h.namingServer.DeregisterInstance(ctx, instance)) 89 } 90 91 // Discover 统一发现接口 92 func (h *HTTPServerV1) Discover(req *restful.Request, rsp *restful.Response) { 93 handler := &httpcommon.Handler{ 94 Request: req, 95 Response: rsp, 96 } 97 98 discoverRequest := &apiservice.DiscoverRequest{} 99 ctx, err := handler.Parse(discoverRequest) 100 if err != nil { 101 handler.WriteHeaderAndProto(api.NewResponseWithMsg(apimodel.Code_ParseException, err.Error())) 102 return 103 } 104 105 msg := fmt.Sprintf("receive http discover request: %s", discoverRequest.Service.String()) 106 namingLog.Info(msg, 107 zap.String("type", apiservice.DiscoverRequest_DiscoverRequestType_name[int32(discoverRequest.Type)]), 108 zap.String("client-address", req.Request.RemoteAddr), 109 zap.String("user-agent", req.HeaderParameter("User-Agent")), 110 utils.ZapRequestID(req.HeaderParameter("Request-Id")), 111 ) 112 113 var ret *apiservice.DiscoverResponse 114 switch discoverRequest.Type { 115 case apiservice.DiscoverRequest_INSTANCE: 116 ret = h.namingServer.ServiceInstancesCache(ctx, discoverRequest.Service) 117 case apiservice.DiscoverRequest_ROUTING: 118 ret = h.namingServer.GetRoutingConfigWithCache(ctx, discoverRequest.Service) 119 case apiservice.DiscoverRequest_RATE_LIMIT: 120 ret = h.namingServer.GetRateLimitWithCache(ctx, discoverRequest.Service) 121 case apiservice.DiscoverRequest_CIRCUIT_BREAKER: 122 ret = h.namingServer.GetCircuitBreakerWithCache(ctx, discoverRequest.Service) 123 case apiservice.DiscoverRequest_SERVICES: 124 ret = h.namingServer.GetServiceWithCache(ctx, discoverRequest.Service) 125 case apiservice.DiscoverRequest_FAULT_DETECTOR: 126 ret = h.namingServer.GetFaultDetectWithCache(ctx, discoverRequest.Service) 127 default: 128 ret = api.NewDiscoverRoutingResponse(apimodel.Code_InvalidDiscoverResource, discoverRequest.Service) 129 } 130 131 handler.WriteHeaderAndProto(ret) 132 } 133 134 // Heartbeat 服务实例心跳 135 func (h *HTTPServerV1) Heartbeat(req *restful.Request, rsp *restful.Response) { 136 handler := &httpcommon.Handler{ 137 Request: req, 138 Response: rsp, 139 } 140 141 instance := &apiservice.Instance{} 142 ctx, err := handler.Parse(instance) 143 if err != nil { 144 handler.WriteHeaderAndProto(api.NewResponseWithMsg(apimodel.Code_ParseException, err.Error())) 145 return 146 } 147 148 handler.WriteHeaderAndProto(h.healthCheckServer.Report(ctx, instance)) 149 }