diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/api/ClientNetworkService.java b/hippo4j-common/src/main/java/cn/hippo4j/common/api/ClientNetworkService.java new file mode 100644 index 00000000..6db7b13d --- /dev/null +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/api/ClientNetworkService.java @@ -0,0 +1,34 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 cn.hippo4j.common.api; + +import org.springframework.core.env.ConfigurableEnvironment; + +/** + * Client network service. + */ +public interface ClientNetworkService { + + /** + * Get network ip port. return as an array 127.0.0.1,8080 + * + * @param environment environment + * @return network ip port + */ + String[] getNetworkIpPort(ConfigurableEnvironment environment); +} diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/IoUtil.java b/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/IoUtil.java index f777b35b..73948b09 100644 --- a/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/IoUtil.java +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/IoUtil.java @@ -173,7 +173,7 @@ public class IoUtil { public static long copy(Reader input, Writer output) throws IOException { char[] buffer = new char[1 << 12]; long count = 0; - for (int n = 0; (n = input.read(buffer)) >= 0; ) { + for (int n = 0; (n = input.read(buffer)) >= 0;) { output.write(buffer, 0, n); count += n; } diff --git a/hippo4j-common/src/test/java/cn/hippo4j/common/executor/ThreadPoolManagerTest.java b/hippo4j-common/src/test/java/cn/hippo4j/common/executor/ThreadPoolManagerTest.java index d508d347..94cf6964 100644 --- a/hippo4j-common/src/test/java/cn/hippo4j/common/executor/ThreadPoolManagerTest.java +++ b/hippo4j-common/src/test/java/cn/hippo4j/common/executor/ThreadPoolManagerTest.java @@ -17,7 +17,6 @@ package cn.hippo4j.common.executor; - import cn.hippo4j.common.toolkit.ReflectUtil; import org.junit.Assert; import org.junit.Test; @@ -30,16 +29,16 @@ import java.util.concurrent.ScheduledExecutorService; public class ThreadPoolManagerTest { - // tenantId = schedule,group = schedule + // tenantId = schedule,group = schedule ScheduledExecutorService executorService1 = Executors.newScheduledThreadPool(1); - // tenantId = schedule,group = schedule + // tenantId = schedule,group = schedule ScheduledExecutorService executorService2 = Executors.newScheduledThreadPool(10); - // tenantId = executor,group = executor + // tenantId = executor,group = executor ExecutorService executorService3 = Executors.newFixedThreadPool(8); - // tenantId = executor,group = executor + // tenantId = executor,group = executor ExecutorService executorService4 = Executors.newFixedThreadPool(16); static final String schedule = "schedule"; diff --git a/hippo4j-core/src/main/java/cn/hippo4j/core/toolkit/IdentifyUtil.java b/hippo4j-core/src/main/java/cn/hippo4j/core/toolkit/IdentifyUtil.java index 0975b773..b6c24c37 100644 --- a/hippo4j-core/src/main/java/cn/hippo4j/core/toolkit/IdentifyUtil.java +++ b/hippo4j-core/src/main/java/cn/hippo4j/core/toolkit/IdentifyUtil.java @@ -17,13 +17,18 @@ package cn.hippo4j.core.toolkit; +import cn.hippo4j.common.api.ClientNetworkService; import cn.hippo4j.common.config.ApplicationContextHolder; -import cn.hippo4j.common.toolkit.*; +import cn.hippo4j.common.spi.DynamicThreadPoolServiceLoader; +import cn.hippo4j.common.toolkit.CollectionUtil; +import cn.hippo4j.common.toolkit.IdUtil; +import cn.hippo4j.common.toolkit.Joiner; +import cn.hippo4j.common.toolkit.StringUtil; +import cn.hippo4j.common.toolkit.ThreadUtil; import cn.hippo4j.core.toolkit.inet.InetUtils; import org.springframework.core.env.ConfigurableEnvironment; import java.util.ArrayList; -import java.util.UUID; import static cn.hippo4j.common.constant.Constants.GROUP_KEY_DELIMITER; import static cn.hippo4j.common.constant.Constants.IDENTIFY_SLICER_SYMBOL; @@ -37,34 +42,47 @@ public class IdentifyUtil { public static final String CLIENT_IDENTIFICATION_VALUE = IdUtil.simpleUUID(); + static { + DynamicThreadPoolServiceLoader.register(ClientNetworkService.class); + } + /** * Generate identify. * - * @param environment - * @param hippo4JInetUtils - * @return + * @param environment environment + * @param inetUtil inet util + * @return identify */ - public static synchronized String generate(ConfigurableEnvironment environment, InetUtils hippo4JInetUtils) { + public static synchronized String generate(ConfigurableEnvironment environment, InetUtils inetUtil) { if (StringUtil.isNotBlank(IDENTIFY)) { return IDENTIFY; } - String ip = hippo4JInetUtils.findFirstNonLoopBackHostInfo().getIpAddress(); - String port = environment.getProperty("server.port", "8080"); - String identification = new StringBuilder() + String[] customerNetwork = DynamicThreadPoolServiceLoader.getSingletonServiceInstances(ClientNetworkService.class) + .stream().findFirst().map(each -> each.getNetworkIpPort(environment)).orElse(null); + String ip; + String port; + if (customerNetwork != null && customerNetwork.length > 0) { + ip = customerNetwork[0]; + port = customerNetwork[1]; + } else { + ip = inetUtil.findFirstNonLoopBackHostInfo().getIpAddress(); + port = environment.getProperty("server.port", "8080"); + } + String identify = new StringBuilder() .append(ip) .append(":") .append(port) .append(IDENTIFY_SLICER_SYMBOL) .append(CLIENT_IDENTIFICATION_VALUE) .toString(); - IDENTIFY = identification; - return identification; + IDENTIFY = identify; + return identify; } /** * Get identify. * - * @return + * @return identify */ public static String getIdentify() { while (StringUtil.isBlank(IDENTIFY)) { @@ -82,10 +100,10 @@ public class IdentifyUtil { /** * Get thread pool identify. * - * @param threadPoolId - * @param itemId - * @param namespace - * @return + * @param threadPoolId thread pool id + * @param itemId item id + * @param namespace namespace + * @return identify */ public static String getThreadPoolIdentify(String threadPoolId, String itemId, String namespace) { ArrayList params = CollectionUtil.newArrayList(threadPoolId, itemId, namespace, getIdentify()); diff --git a/hippo4j-example/hippo4j-example-core/src/main/java/cn/hippo4j/example/core/handler/CustomerClientNetworkService.java b/hippo4j-example/hippo4j-example-core/src/main/java/cn/hippo4j/example/core/handler/CustomerClientNetworkService.java new file mode 100644 index 00000000..3070a043 --- /dev/null +++ b/hippo4j-example/hippo4j-example-core/src/main/java/cn/hippo4j/example/core/handler/CustomerClientNetworkService.java @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 cn.hippo4j.example.core.handler; + +import cn.hippo4j.common.api.ClientNetworkService; +import org.springframework.core.env.ConfigurableEnvironment; + +/** + * Customer client network service. + */ +public class CustomerClientNetworkService implements ClientNetworkService { + + @Override + public String[] getNetworkIpPort(ConfigurableEnvironment environment) { + String[] network = new String[2]; + network[0] = "127.0.0.1"; + network[1] = environment.getProperty("server.port", "1994"); + return network; + } +} diff --git a/hippo4j-example/hippo4j-spring-boot-starter-adapter-spring-cloud-stream-rocketmq-example/src/main/resources/META-INF/services/cn.hippo4j.common.api.ClientNetworkService b/hippo4j-example/hippo4j-spring-boot-starter-adapter-spring-cloud-stream-rocketmq-example/src/main/resources/META-INF/services/cn.hippo4j.common.api.ClientNetworkService new file mode 100644 index 00000000..76cecf59 --- /dev/null +++ b/hippo4j-example/hippo4j-spring-boot-starter-adapter-spring-cloud-stream-rocketmq-example/src/main/resources/META-INF/services/cn.hippo4j.common.api.ClientNetworkService @@ -0,0 +1 @@ +cn.hippo4j.example.core.handler.CustomerClientNetworkService \ No newline at end of file diff --git a/hippo4j-example/hippo4j-spring-boot-starter-example/src/main/resources/META-INF/services/cn.hippo4j.common.api.ClientNetworkService b/hippo4j-example/hippo4j-spring-boot-starter-example/src/main/resources/META-INF/services/cn.hippo4j.common.api.ClientNetworkService new file mode 100644 index 00000000..76cecf59 --- /dev/null +++ b/hippo4j-example/hippo4j-spring-boot-starter-example/src/main/resources/META-INF/services/cn.hippo4j.common.api.ClientNetworkService @@ -0,0 +1 @@ +cn.hippo4j.example.core.handler.CustomerClientNetworkService \ No newline at end of file diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/config/DiscoveryConfiguration.java b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/config/DiscoveryConfiguration.java index f46f102b..737ab8a0 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/config/DiscoveryConfiguration.java +++ b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/config/DiscoveryConfiguration.java @@ -17,19 +17,22 @@ package cn.hippo4j.springboot.starter.config; +import cn.hippo4j.common.api.ClientNetworkService; import cn.hippo4j.common.model.InstanceInfo; +import cn.hippo4j.common.spi.DynamicThreadPoolServiceLoader; import cn.hippo4j.common.toolkit.ContentUtil; import cn.hippo4j.core.toolkit.IdentifyUtil; import cn.hippo4j.core.toolkit.inet.InetUtils; -import cn.hippo4j.springboot.starter.toolkit.CloudCommonIdUtil; import cn.hippo4j.springboot.starter.core.DiscoveryClient; import cn.hippo4j.springboot.starter.remote.HttpAgent; +import cn.hippo4j.springboot.starter.toolkit.CloudCommonIdUtil; import lombok.AllArgsConstructor; import lombok.SneakyThrows; import org.springframework.context.annotation.Bean; import org.springframework.core.env.ConfigurableEnvironment; import java.net.InetAddress; +import java.util.Optional; import static cn.hippo4j.common.constant.Constants.IDENTIFY_SLICER_SYMBOL; import static cn.hippo4j.core.toolkit.IdentifyUtil.CLIENT_IDENTIFICATION_VALUE; @@ -46,6 +49,10 @@ public class DiscoveryConfiguration { private final InetUtils hippo4JInetUtils; + static { + DynamicThreadPoolServiceLoader.register(ClientNetworkService.class); + } + @Bean @SneakyThrows public InstanceInfo instanceConfig() { @@ -69,8 +76,10 @@ public class DiscoveryConfiguration { .setPort(port) .setClientBasePath(contextPath) .setGroupKey(ContentUtil.getGroupKey(itemId, namespace)); - String callBackUrl = new StringBuilder().append(instanceInfo.getHostName()).append(":") - .append(port).append(instanceInfo.getClientBasePath()) + String[] customerNetwork = DynamicThreadPoolServiceLoader.getSingletonServiceInstances(ClientNetworkService.class) + .stream().findFirst().map(each -> each.getNetworkIpPort(environment)).orElse(null); + String callBackUrl = new StringBuilder().append(Optional.ofNullable(customerNetwork).map(each -> each[0]).orElse(instanceInfo.getHostName())).append(":") + .append(Optional.ofNullable(customerNetwork).map(each -> each[1]).orElse(port)).append(instanceInfo.getClientBasePath()) .toString(); instanceInfo.setCallBackUrl(callBackUrl); String identify = IdentifyUtil.generate(environment, hippo4JInetUtils); diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/controller/ThreadPoolAdapterController.java b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/controller/ThreadPoolAdapterController.java index 1ca73e1a..6bc1bad8 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/controller/ThreadPoolAdapterController.java +++ b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/controller/ThreadPoolAdapterController.java @@ -20,6 +20,9 @@ package cn.hippo4j.springboot.starter.controller; import cn.hippo4j.adapter.base.ThreadPoolAdapter; import cn.hippo4j.adapter.base.ThreadPoolAdapterParameter; import cn.hippo4j.adapter.base.ThreadPoolAdapterState; +import cn.hippo4j.common.api.ClientNetworkService; +import cn.hippo4j.common.spi.DynamicThreadPoolServiceLoader; +import cn.hippo4j.common.toolkit.StringUtil; import cn.hippo4j.common.web.base.Result; import cn.hippo4j.common.web.base.Results; import cn.hippo4j.core.toolkit.IdentifyUtil; @@ -56,7 +59,14 @@ public class ThreadPoolAdapterController { ThreadPoolAdapterState threadPoolState = each.getThreadPoolState(requestParameter.getThreadPoolKey()); String active = environment.getProperty("spring.profiles.active", "UNKNOWN"); threadPoolState.setActive(active.toUpperCase()); - String clientAddress = CloudCommonIdUtil.getClientIpPort(environment, hippo4JInetUtils); + String[] customerNetwork = DynamicThreadPoolServiceLoader.getSingletonServiceInstances(ClientNetworkService.class) + .stream().findFirst().map(network -> network.getNetworkIpPort(environment)).orElse(null); + String clientAddress; + if (customerNetwork != null) { + clientAddress = StringUtil.newBuilder(customerNetwork[0], ":", customerNetwork[1]); + } else { + clientAddress = CloudCommonIdUtil.getClientIpPort(environment, hippo4JInetUtils); + } threadPoolState.setClientAddress(clientAddress); threadPoolState.setIdentify(IdentifyUtil.getIdentify()); return threadPoolState;