Skip to content

Commit

Permalink
enhance nacos openapi logic (#1227)
Browse files Browse the repository at this point in the history
  • Loading branch information
chickenlj committed Sep 1, 2023
1 parent c9bf378 commit 014da62
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ server.port=38080
dubbo.protocol.port=30880
dubbo.application.qos-port=32222

# centers in dubbo2.7, if you want to add parameters, please add them to the url
# centers in dubbo, if you want to add parameters, please add them to the url
admin.registry.address=zookeeper://127.0.0.1:2181
admin.config-center=zookeeper://127.0.0.1:2181
admin.metadata-report.address=zookeeper://127.0.0.1:2181
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@
*/
package org.apache.dubbo.admin.registry.nacos;

import org.apache.dubbo.admin.controller.AccessesController;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.utils.StringUtils;

import com.alibaba.fastjson2.JSON;
import net.bytebuddy.implementation.bytecode.Throw;

import java.io.BufferedReader;
import java.io.IOException;
Expand All @@ -30,6 +34,9 @@
import java.util.List;

public class NacosOpenapiUtil {

private static final Logger logger = LoggerFactory.getLogger(NacosOpenapiUtil.class);

public static List<NacosData> getSubscribeAddressesWithHttpEndpoint(URL url, String serviceName) {
// 定义Nacos OpenAPI的URL
String nacosUrl = "http://" + url.getAddress() + "/nacos/v1/ns/service/subscribers?serviceName=" + serviceName;
Expand Down Expand Up @@ -60,23 +67,25 @@ public static List<NacosData> getSubscribeAddressesWithHttpEndpoint(URL url, Str
// 发送请求并获取响应状态码
int responseCode = connection.getResponseCode();

// 读取响应内容
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
StringBuilder response = new StringBuilder();
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
if (responseCode == 200) {
// 读取响应内容
StringBuilder response = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
} catch (Throwable t) {
logger.error("Error requesting nacos openapi, " + nacosUrl, t);
}

// 打印响应结果
System.out.println("Response Code: " + responseCode);
System.out.println("Response Body: " + response.toString());

NacosResponse nacosResponse = JSON.parseObject(response.toString(), NacosResponse.class);
return nacosResponse.getSubscribers();
NacosResponse nacosResponse = JSON.parseObject(response.toString(), NacosResponse.class);
return nacosResponse.getSubscribers();
} else {
logger.error("Error requesting nacos openapi, " + nacosUrl + ", error code is " + responseCode);
}
} catch (IOException e) {
e.printStackTrace();
logger.error("Error requesting nacos openapi, " + nacosUrl, e);
} finally {
// 关闭连接
if (connection != null) {
Expand Down
2 changes: 1 addition & 1 deletion dubbo-admin-ui/public/dubbo-admin-info.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"version": "0.5.0-SNAPSHOT"
"version": "0.6.0"
}

0 comments on commit 014da62

Please sign in to comment.