From 11a8a3d96c473ea12033f3aaf96cacc4ea13fb30 Mon Sep 17 00:00:00 2001 From: lanyuanxiaoyao Date: Thu, 21 Dec 2023 00:31:22 +0800 Subject: [PATCH] =?UTF-8?q?refactor(cloud-query):=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E6=9C=8D=E5=8A=A1=E5=88=97=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/cloud/CloudController.java | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/service-cloud-query/src/main/java/com/lanyuanxiaoyao/service/cloud/CloudController.java b/service-cloud-query/src/main/java/com/lanyuanxiaoyao/service/cloud/CloudController.java index 0263b35..a7a1b0e 100644 --- a/service-cloud-query/src/main/java/com/lanyuanxiaoyao/service/cloud/CloudController.java +++ b/service-cloud-query/src/main/java/com/lanyuanxiaoyao/service/cloud/CloudController.java @@ -1,12 +1,10 @@ package com.lanyuanxiaoyao.service.cloud; import cn.hutool.core.util.StrUtil; -import java.util.List; import org.eclipse.collections.api.factory.Lists; import org.eclipse.collections.api.list.ImmutableList; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; @@ -31,13 +29,23 @@ public class CloudController { } @GetMapping("services") - public ImmutableList infoQueryServices(@RequestParam("service_name") String serviceName) { + public ImmutableList infoQueryServices( + @RequestParam("service_name") String serviceName, + @RequestParam(value = "with_schema", defaultValue = "true") Boolean withSchema, + @RequestParam(value = "with_port", defaultValue = "true") Boolean withPort + ) { return Lists.immutable.ofAll(client.getServices()) .select(name -> StrUtil.equals(name, serviceName)) - .flatCollect(name -> { - return Lists.immutable.ofAll(client.getInstances(name)) - .collect(instance -> instance.getUri().toString()); - }) + .flatCollect(name -> + Lists.immutable.ofAll(client.getInstances(name)) + .collect(instance -> { + if (withSchema) + if (withPort) return instance.getUri().toString(); + else return StrUtil.format("{}://{}", instance.getScheme(), instance.getHost()); + else if (withPort) + return StrUtil.format("{}:{}", instance.getHost(), instance.getPort()); + else return instance.getHost(); + })) .toSortedList() .toImmutable(); }