From 85437548880ebc71237fcf6a4aeb8b3cf62d6a2e Mon Sep 17 00:00:00 2001 From: tanghc Date: Sat, 27 Mar 2021 14:47:22 +0800 Subject: [PATCH] 4.3.2 --- .../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); + } + }