From c423ffa4483f89d381fe1764fa43ed31494643e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A9=AC=E7=A7=B0=20Ma=20Chen?= Date: Tue, 1 Nov 2022 23:18:31 +0800 Subject: [PATCH] Client instance registration information code refactoring (#888) (#889) --- .../config/DiscoveryConfiguration.java | 49 +---------- .../provider/InstanceInfoProviderFactory.java | 83 +++++++++++++++++++ 2 files changed, 85 insertions(+), 47 deletions(-) create mode 100644 hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/provider/InstanceInfoProviderFactory.java 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 0449b241..5a5c47a1 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,26 +17,15 @@ 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.core.DiscoveryClient; +import cn.hippo4j.springboot.starter.provider.InstanceInfoProviderFactory; 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; - /** * Dynamic thread-pool discovery config. */ @@ -49,43 +38,9 @@ public class DiscoveryConfiguration { private final InetUtils hippo4JInetUtils; - static { - DynamicThreadPoolServiceLoader.register(ClientNetworkService.class); - } - @Bean - @SneakyThrows public InstanceInfo instanceConfig() { - String namespace = bootstrapProperties.getNamespace(); - String itemId = bootstrapProperties.getItemId(); - String port = environment.getProperty("server.port", "8080"); - String applicationName = environment.getProperty("spring.dynamic.thread-pool.item-id"); - String active = environment.getProperty("spring.profiles.active", "UNKNOWN"); - InstanceInfo instanceInfo = new InstanceInfo(); - String instanceId = CloudCommonIdUtil.getDefaultInstanceId(environment, hippo4JInetUtils); - instanceId = new StringBuilder() - .append(instanceId) - .append(IDENTIFY_SLICER_SYMBOL) - .append(CLIENT_IDENTIFICATION_VALUE) - .toString(); - String contextPath = environment.getProperty("server.servlet.context-path", ""); - instanceInfo.setInstanceId(instanceId) - .setIpApplicationName(CloudCommonIdUtil.getIpApplicationName(environment, hippo4JInetUtils)) - .setHostName(InetAddress.getLocalHost().getHostAddress()) - .setAppName(applicationName) - .setPort(port) - .setClientBasePath(contextPath) - .setGroupKey(ContentUtil.getGroupKey(itemId, namespace)); - 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); - instanceInfo.setIdentify(identify); - instanceInfo.setActive(active.toUpperCase()); - return instanceInfo; + return InstanceInfoProviderFactory.getInstance(environment, bootstrapProperties, hippo4JInetUtils); } @Bean diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/provider/InstanceInfoProviderFactory.java b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/provider/InstanceInfoProviderFactory.java new file mode 100644 index 00000000..41a20e9e --- /dev/null +++ b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/provider/InstanceInfoProviderFactory.java @@ -0,0 +1,83 @@ +/* + * 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.springboot.starter.provider; + +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.config.BootstrapProperties; +import cn.hippo4j.springboot.starter.toolkit.CloudCommonIdUtil; +import lombok.SneakyThrows; +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; + +/** + * Instance info provider factory. + */ +public final class InstanceInfoProviderFactory { + + static { + DynamicThreadPoolServiceLoader.register(ClientNetworkService.class); + } + + /** + * Create client registration instance information. + * + * @param environment configurable environment + * @param bootstrapProperties bootstrap properties + * @param inetUtils inet utils + * @return + */ + @SneakyThrows + public static InstanceInfo getInstance(final ConfigurableEnvironment environment, + final BootstrapProperties bootstrapProperties, + final InetUtils inetUtils) { + String namespace = bootstrapProperties.getNamespace(); + String itemId = bootstrapProperties.getItemId(); + String port = environment.getProperty("server.port", "8080"); + String applicationName = environment.getProperty("spring.dynamic.thread-pool.item-id"); + String active = environment.getProperty("spring.profiles.active", "UNKNOWN"); + InstanceInfo instanceInfo = new InstanceInfo(); + String instanceId = CloudCommonIdUtil.getDefaultInstanceId(environment, inetUtils); + instanceId = new StringBuilder() + .append(instanceId).append(IDENTIFY_SLICER_SYMBOL).append(CLIENT_IDENTIFICATION_VALUE).toString(); + String contextPath = environment.getProperty("server.servlet.context-path", ""); + instanceInfo.setInstanceId(instanceId) + .setIpApplicationName(CloudCommonIdUtil.getIpApplicationName(environment, inetUtils)) + .setHostName(InetAddress.getLocalHost().getHostAddress()).setAppName(applicationName) + .setPort(port).setClientBasePath(contextPath).setGroupKey(ContentUtil.getGroupKey(itemId, namespace)); + 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, inetUtils); + instanceInfo.setIdentify(identify); + instanceInfo.setActive(active.toUpperCase()); + return instanceInfo; + } +}