@@ -176,7 +176,23 @@ func (s *Server) GetServiceWithCache(ctx context.Context, req *apiservice.Servic
176176
177177 if req .GetNamespace ().GetValue () != "" {
178178 revision , svcs = s .Cache ().Service ().ListServices (ctx , req .GetNamespace ().GetValue ())
179+ // 需要加上服务可见性处理
180+ visibleSvcs := s .caches .Service ().GetVisibleServicesInOtherNamespace (ctx , utils .MatchAll , req .GetNamespace ().GetValue ())
181+ revisions := make ([]string , 0 , len (visibleSvcs )+ 1 )
182+ revisions = append (revisions , revision )
183+ for i := range visibleSvcs {
184+ revisions = append (revisions , visibleSvcs [i ].Revision )
185+ }
186+ if rever , err := cachetypes .CompositeComputeRevision (revisions ); err != nil {
187+ // 如果计算失败,直接返回一个新的revision
188+ revision = utils .NewUUID ()
189+ } else {
190+ revision = rever
191+ }
192+ svcs = append (svcs , visibleSvcs ... )
193+ // 需要重新计算 revison
179194 } else {
195+ // 这里拉的是全部服务实例列表,如果客户端可以发起这个请求,应该是不需要
180196 revision , svcs = s .Cache ().Service ().ListAllServices (ctx )
181197 }
182198 if revision == "" {
@@ -212,14 +228,14 @@ func (s *Server) ServiceInstancesCache(ctx context.Context, filter *apiservice.D
212228 req * apiservice.Service ) * apiservice.DiscoverResponse {
213229
214230 resp := createCommonDiscoverResponse (req , apiservice .DiscoverResponse_INSTANCE )
215- serviceName := req .GetName ().GetValue ()
216- namespaceName := req .GetNamespace ().GetValue ()
231+ svcName := req .GetName ().GetValue ()
232+ nsName := req .GetNamespace ().GetValue ()
217233
218234 // 数据源都来自Cache,这里拿到的service,已经是源服务
219- aliasFor , visibleServices := s .findVisibleServices (serviceName , namespaceName , req )
235+ aliasFor , visibleServices := s .findVisibleServices (ctx , svcName , nsName , req )
220236 if len (visibleServices ) == 0 {
221237 log .Infof ("[Server][Service][Instance] not found name(%s) namespace(%s) service" ,
222- serviceName , namespaceName )
238+ svcName , nsName )
223239 return api .NewDiscoverInstanceResponse (apimodel .Code_NotFoundResource , req )
224240 }
225241
@@ -273,14 +289,15 @@ func (s *Server) ServiceInstancesCache(ctx context.Context, filter *apiservice.D
273289 return resp
274290}
275291
276- func (s * Server ) findVisibleServices (serviceName , namespaceName string , req * apiservice.Service ) (* model.Service , []* model.Service ) {
292+ func (s * Server ) findVisibleServices (ctx context.Context , serviceName , namespaceName string ,
293+ req * apiservice.Service ) (* model.Service , []* model.Service ) {
277294 visibleServices := make ([]* model.Service , 0 , 4 )
278295 // 数据源都来自Cache,这里拿到的service,已经是源服务
279296 aliasFor := s .getServiceCache (serviceName , namespaceName )
280297 if aliasFor != nil {
281298 visibleServices = append (visibleServices , aliasFor )
282299 }
283- ret := s .caches .Service ().GetVisibleServicesInOtherNamespace (serviceName , namespaceName )
300+ ret := s .caches .Service ().GetVisibleServicesInOtherNamespace (ctx , serviceName , namespaceName )
284301 if len (ret ) > 0 {
285302 visibleServices = append (visibleServices , ret ... )
286303 }
0 commit comments