-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c6e8c9f
commit 9766103
Showing
25 changed files
with
2,194 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
264 changes: 264 additions & 0 deletions
264
naming/src/main/java/com/alibaba/nacos/naming/controllers/v3/ClientControllerV3.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,264 @@ | ||
/* | ||
* Copyright 1999-$toady.year Alibaba Group Holding Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.alibaba.nacos.naming.controllers.v3; | ||
|
||
import com.alibaba.nacos.api.annotation.NacosApi; | ||
import com.alibaba.nacos.api.common.Constants; | ||
import com.alibaba.nacos.api.exception.api.NacosApiException; | ||
import com.alibaba.nacos.api.model.v2.ErrorCode; | ||
import com.alibaba.nacos.api.model.v2.Result; | ||
import com.alibaba.nacos.auth.annotation.Secured; | ||
import com.alibaba.nacos.common.utils.JacksonUtils; | ||
import com.alibaba.nacos.core.paramcheck.ExtractorManager; | ||
import com.alibaba.nacos.core.remote.Connection; | ||
import com.alibaba.nacos.core.remote.ConnectionManager; | ||
import com.alibaba.nacos.core.remote.ConnectionMeta; | ||
import com.alibaba.nacos.naming.core.v2.client.Client; | ||
import com.alibaba.nacos.naming.core.v2.client.impl.ConnectionBasedClient; | ||
import com.alibaba.nacos.naming.core.v2.client.manager.ClientManager; | ||
import com.alibaba.nacos.naming.core.v2.index.ClientServiceIndexesManager; | ||
import com.alibaba.nacos.naming.core.v2.pojo.BatchInstancePublishInfo; | ||
import com.alibaba.nacos.naming.core.v2.pojo.InstancePublishInfo; | ||
import com.alibaba.nacos.naming.core.v2.pojo.Service; | ||
import com.alibaba.nacos.naming.misc.UtilsAndCommons; | ||
import com.alibaba.nacos.naming.paramcheck.NamingDefaultHttpParamExtractor; | ||
import com.alibaba.nacos.naming.pojo.Subscriber; | ||
import com.alibaba.nacos.plugin.auth.constant.ActionTypes; | ||
import com.alibaba.nacos.plugin.auth.constant.ApiType; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Client controller. | ||
* | ||
* @author Nacos | ||
*/ | ||
@NacosApi | ||
@RestController | ||
@RequestMapping(UtilsAndCommons.CLIENT_CONTROLLER_V3_ADMIN_PATH) | ||
@ExtractorManager.Extractor(httpExtractor = NamingDefaultHttpParamExtractor.class) | ||
public class ClientControllerV3 { | ||
|
||
private final ClientManager clientManager; | ||
|
||
private final ConnectionManager connectionManager; | ||
|
||
private final ClientServiceIndexesManager clientServiceIndexesManager; | ||
|
||
public ClientControllerV3(ClientManager clientManager, ConnectionManager connectionManager, | ||
ClientServiceIndexesManager clientServiceIndexesManager) { | ||
this.clientManager = clientManager; | ||
this.connectionManager = connectionManager; | ||
this.clientServiceIndexesManager = clientServiceIndexesManager; | ||
} | ||
|
||
/** | ||
* Query all clients. | ||
*/ | ||
@GetMapping("/list") | ||
@Secured(resource = UtilsAndCommons.CLIENT_CONTROLLER_V3_ADMIN_PATH, action = ActionTypes.READ, apiType = ApiType.ADMIN_API) | ||
public Result<List<String>> getClientList() { | ||
return Result.success(new ArrayList<>(clientManager.allClientId())); | ||
} | ||
|
||
/** | ||
* Query client by clientId. | ||
*/ | ||
@GetMapping() | ||
@Secured(resource = UtilsAndCommons.CLIENT_CONTROLLER_V3_ADMIN_PATH, action = ActionTypes.READ, apiType = ApiType.ADMIN_API) | ||
public Result<ObjectNode> getClientDetail(@RequestParam("clientId") String clientId) throws NacosApiException { | ||
checkClientId(clientId); | ||
Client client = clientManager.getClient(clientId); | ||
|
||
ObjectNode result = JacksonUtils.createEmptyJsonNode(); | ||
result.put("clientId", client.getClientId()); | ||
result.put("ephemeral", client.isEphemeral()); | ||
result.put("lastUpdatedTime", client.getLastUpdatedTime()); | ||
|
||
if (client instanceof ConnectionBasedClient) { | ||
// 2.x client | ||
result.put("clientType", "connection"); | ||
Connection connection = connectionManager.getConnection(clientId); | ||
ConnectionMeta connectionMetaInfo = connection.getMetaInfo(); | ||
result.put("connectType", connectionMetaInfo.getConnectType()); | ||
result.put("appName", connectionMetaInfo.getAppName()); | ||
result.put("version", connectionMetaInfo.getVersion()); | ||
result.put("clientIp", connectionMetaInfo.getClientIp()); | ||
result.put("clientPort", clientId.substring(clientId.lastIndexOf('_') + 1)); | ||
|
||
} | ||
|
||
return Result.success(result); | ||
} | ||
|
||
/** | ||
* Query the services registered by the specified client. | ||
*/ | ||
@GetMapping("/publish/list") | ||
@Secured(resource = UtilsAndCommons.CLIENT_CONTROLLER_V3_ADMIN_PATH, action = ActionTypes.READ, apiType = ApiType.ADMIN_API) | ||
public Result<List<ObjectNode>> getPublishedServiceList(@RequestParam("clientId") String clientId) | ||
throws NacosApiException { | ||
checkClientId(clientId); | ||
Client client = clientManager.getClient(clientId); | ||
Collection<Service> allPublishedService = client.getAllPublishedService(); | ||
ArrayList<ObjectNode> res = new ArrayList<>(); | ||
for (Service service : allPublishedService) { | ||
InstancePublishInfo instancePublishInfo = client.getInstancePublishInfo(service); | ||
if (instancePublishInfo instanceof BatchInstancePublishInfo) { | ||
List<InstancePublishInfo> instancePublishInfos = ((BatchInstancePublishInfo) instancePublishInfo).getInstancePublishInfos(); | ||
for (InstancePublishInfo publishInfo : instancePublishInfos) { | ||
res.add(wrapSingleInstanceNode(publishInfo, service)); | ||
} | ||
} else { | ||
res.add(wrapSingleInstanceNode(instancePublishInfo, service)); | ||
} | ||
} | ||
|
||
return Result.success(res); | ||
} | ||
|
||
private ObjectNode wrapSingleInstanceNode(InstancePublishInfo instancePublishInfo, Service service) { | ||
ObjectNode item = JacksonUtils.createEmptyJsonNode(); | ||
item.put("namespace", service.getNamespace()); | ||
item.put("group", service.getGroup()); | ||
item.put("serviceName", service.getName()); | ||
item.set("registeredInstance", wrapSingleInstance(instancePublishInfo)); | ||
return item; | ||
} | ||
|
||
private ObjectNode wrapSingleInstance(InstancePublishInfo instancePublishInfo) { | ||
ObjectNode instanceInfo = JacksonUtils.createEmptyJsonNode(); | ||
instanceInfo.put("ip", instancePublishInfo.getIp()); | ||
instanceInfo.put("port", instancePublishInfo.getPort()); | ||
instanceInfo.put("cluster", instancePublishInfo.getCluster()); | ||
return instanceInfo; | ||
} | ||
|
||
/** | ||
* Query the services to which the specified client subscribes. | ||
*/ | ||
@GetMapping("/subscribe/list") | ||
@Secured(resource = UtilsAndCommons.CLIENT_CONTROLLER_V3_ADMIN_PATH, action = ActionTypes.READ, apiType = ApiType.ADMIN_API) | ||
public Result<List<ObjectNode>> getSubscribeServiceList(@RequestParam("clientId") String clientId) | ||
throws NacosApiException { | ||
checkClientId(clientId); | ||
Client client = clientManager.getClient(clientId); | ||
Collection<Service> allSubscribeService = client.getAllSubscribeService(); | ||
ArrayList<ObjectNode> res = new ArrayList<>(); | ||
for (Service service : allSubscribeService) { | ||
ObjectNode item = JacksonUtils.createEmptyJsonNode(); | ||
item.put("namespace", service.getNamespace()); | ||
item.put("group", service.getGroup()); | ||
item.put("serviceName", service.getName()); | ||
Subscriber subscriber = client.getSubscriber(service); | ||
ObjectNode subscriberInfo = JacksonUtils.createEmptyJsonNode(); | ||
subscriberInfo.put("app", subscriber.getApp()); | ||
subscriberInfo.put("agent", subscriber.getAgent()); | ||
subscriberInfo.put("addr", subscriber.getAddrStr()); | ||
item.set("subscriberInfo", subscriberInfo); | ||
res.add(item); | ||
} | ||
|
||
return Result.success(res); | ||
} | ||
|
||
/** | ||
* Query the clients that have registered the specified service. | ||
*/ | ||
@GetMapping("/service/publisher/list") | ||
@Secured(resource = UtilsAndCommons.CLIENT_CONTROLLER_V3_ADMIN_PATH, action = ActionTypes.READ, apiType = ApiType.ADMIN_API) | ||
public Result<List<ObjectNode>> getPublishedClientList( | ||
@RequestParam(value = "namespaceId", required = false, defaultValue = Constants.DEFAULT_NAMESPACE_ID) String namespaceId, | ||
@RequestParam(value = "groupName", required = false, defaultValue = Constants.DEFAULT_GROUP) String groupName, | ||
@RequestParam(value = "ephemeral", required = false, defaultValue = "true") Boolean ephemeral, | ||
@RequestParam("serviceName") String serviceName, @RequestParam(value = "ip", required = false) String ip, | ||
@RequestParam(value = "port", required = false) Integer port) { | ||
Service service = Service.newService(namespaceId, groupName, serviceName, ephemeral); | ||
Collection<String> allClientsRegisteredService = clientServiceIndexesManager | ||
.getAllClientsRegisteredService(service); | ||
ArrayList<ObjectNode> res = new ArrayList<>(); | ||
for (String clientId : allClientsRegisteredService) { | ||
Client client = clientManager.getClient(clientId); | ||
InstancePublishInfo instancePublishInfo = client.getInstancePublishInfo(service); | ||
if (instancePublishInfo instanceof BatchInstancePublishInfo) { | ||
List<InstancePublishInfo> list = ((BatchInstancePublishInfo) instancePublishInfo).getInstancePublishInfos(); | ||
for (InstancePublishInfo info : list) { | ||
if (!Objects.equals(info.getIp(), ip) || !Objects | ||
.equals(port, info.getPort())) { | ||
continue; | ||
} | ||
res.add(wrapSingleInstance(info).put("clientId", clientId)); | ||
} | ||
} else { | ||
if (!Objects.equals(instancePublishInfo.getIp(), ip) || !Objects | ||
.equals(port, instancePublishInfo.getPort())) { | ||
continue; | ||
} | ||
res.add(wrapSingleInstance(instancePublishInfo).put("clientId", clientId)); | ||
} | ||
} | ||
|
||
return Result.success(res); | ||
} | ||
|
||
/** | ||
* Query the clients that are subscribed to the specified service. | ||
*/ | ||
@GetMapping("/service/subscriber/list") | ||
@Secured(resource = UtilsAndCommons.CLIENT_CONTROLLER_V3_ADMIN_PATH, action = ActionTypes.READ, apiType = ApiType.ADMIN_API) | ||
public Result<List<ObjectNode>> getSubscribeClientList( | ||
@RequestParam(value = "namespaceId", required = false, defaultValue = Constants.DEFAULT_NAMESPACE_ID) String namespaceId, | ||
@RequestParam(value = "groupName", required = false, defaultValue = Constants.DEFAULT_GROUP) String groupName, | ||
@RequestParam(value = "ephemeral", required = false, defaultValue = "true") Boolean ephemeral, | ||
@RequestParam("serviceName") String serviceName, @RequestParam(value = "ip", required = false) String ip, | ||
@RequestParam(value = "port", required = false) Integer port) { | ||
Service service = Service.newService(namespaceId, groupName, serviceName, ephemeral); | ||
Collection<String> allClientsSubscribeService = clientServiceIndexesManager | ||
.getAllClientsSubscribeService(service); | ||
ArrayList<ObjectNode> res = new ArrayList<>(); | ||
for (String clientId : allClientsSubscribeService) { | ||
Client client = clientManager.getClient(clientId); | ||
Subscriber subscriber = client.getSubscriber(service); | ||
if (!Objects.equals(subscriber.getIp(), ip) || !Objects.equals(port, subscriber.getPort())) { | ||
continue; | ||
} | ||
ObjectNode item = JacksonUtils.createEmptyJsonNode(); | ||
item.put("clientId", clientId); | ||
item.put("ip", subscriber.getIp()); | ||
item.put("port", subscriber.getPort()); | ||
res.add(item); | ||
} | ||
|
||
return Result.success(res); | ||
} | ||
|
||
private void checkClientId(String clientId) throws NacosApiException { | ||
if (!clientManager.contains(clientId)) { | ||
throw new NacosApiException(HttpStatus.NOT_FOUND.value(), ErrorCode.RESOURCE_NOT_FOUND, | ||
"clientId [ " + clientId + " ] not exist"); | ||
} | ||
} | ||
} |
Oops, something went wrong.
9766103
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add management api naming.
9766103
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello friends, my phone is Android Samsung, not iPhone
9766103
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's why I say Samsung Android because I used Samsung Android from the beginning
9766103
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I checked all the accounts, someone hacked into my phone with an iPhone while I always used Samsung Android
9766103
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please merge