From 392746fcf000149e3babcd687d977dfe4f025934 Mon Sep 17 00:00:00 2001 From: MemphyChan Date: Fri, 26 Mar 2021 08:22:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B8=8D=E6=94=AF=E6=8C=81eu?= =?UTF-8?q?reka=E9=AB=98=E5=8F=AF=E7=94=A8=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../impl/RegistryServiceEurekaImpl.java | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/service/impl/RegistryServiceEurekaImpl.java b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/service/impl/RegistryServiceEurekaImpl.java index 34a5b7f1..111e1116 100644 --- a/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/service/impl/RegistryServiceEurekaImpl.java +++ b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/service/impl/RegistryServiceEurekaImpl.java @@ -18,6 +18,8 @@ import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; /** * eureka接口实现 @@ -29,11 +31,11 @@ public class RegistryServiceEurekaImpl implements RegistryService { private OkHttpClient client = new OkHttpClient(); @Value("${eureka.client.serviceUrl.defaultZone:}") - private String eurekaUrl; + private String eurekaUrls; @Override public List listAllService(int pageNo, int pageSize) throws Exception { - if (StringUtils.isBlank(eurekaUrl)) { + if (StringUtils.isBlank(eurekaUrls)) { throw new IllegalArgumentException("未指定eureka.client.serviceUrl.defaultZone参数"); } String json = this.requestEurekaServer(EurekaUri.QUERY_APPS); @@ -81,7 +83,7 @@ public class RegistryServiceEurekaImpl implements RegistryService { } private String requestEurekaServer(EurekaUri eurekaUri, String... args) throws IOException { - Request request = eurekaUri.getRequest(this.eurekaUrl, args); + Request request = eurekaUri.getRequest(getFirstDefaultZoneServiceUrl(), args); Response response = client.newCall(request).execute(); if (response.isSuccessful()) { return response.body().string(); @@ -91,4 +93,22 @@ public class RegistryServiceEurekaImpl implements RegistryService { } } + private List getDefaultZoneServiceUrls() { + if (!StringUtils.isEmpty(eurekaUrls)) { + return Stream.of(org.springframework.util.StringUtils.commaDelimitedListToStringArray(eurekaUrls)) + .map(url -> !url.endsWith("/") ? (url += "/").trim() : url.trim()) + .collect(Collectors.toList()); + } else { + return Collections.emptyList(); + } + } + + private String getFirstDefaultZoneServiceUrl() { + List serviceUrls = getDefaultZoneServiceUrls(); + if (CollectionUtils.isEmpty(serviceUrls)) { + throw new IllegalArgumentException("未指定eureka.client.serviceUrl.defaultZone参数"); + } + return serviceUrls.get(0); + } + } -- Gitee