diff --git a/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/config/DiscoveryConfig.java b/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/config/DiscoveryConfig.java new file mode 100644 index 00000000..5f9d5782 --- /dev/null +++ b/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/config/DiscoveryConfig.java @@ -0,0 +1,41 @@ +package com.github.dynamic.threadpool.starter.config; + +import com.github.dynamic.threadpool.starter.core.DiscoveryClient; +import com.github.dynamic.threadpool.starter.core.InstanceConfig; +import com.github.dynamic.threadpool.starter.core.InstanceInfo; +import com.github.dynamic.threadpool.starter.remote.HttpAgent; +import lombok.AllArgsConstructor; +import org.springframework.context.annotation.Bean; +import org.springframework.core.env.ConfigurableEnvironment; + +import static com.github.dynamic.threadpool.starter.toolkit.CloudCommonIdUtil.getDefaultInstanceId; + +/** + * Dynamic Tp Discovery Config. + * + * @author chen.ma + * @date 2021/8/6 21:35 + */ +@AllArgsConstructor +public class DiscoveryConfig { + + private ConfigurableEnvironment environment; + + @Bean + public InstanceConfig instanceConfig() { + InstanceInfo instanceInfo = new InstanceInfo(); + instanceInfo.setInstanceId(getDefaultInstanceId(environment)); + + String hostNameKey = "eureka.instance.hostname"; + String hostNameVal = environment.containsProperty(hostNameKey) ? environment.getProperty(hostNameKey) : ""; + instanceInfo.setHostName(hostNameVal); + + return instanceInfo; + } + + @Bean + public DiscoveryClient discoveryClient(HttpAgent httpAgent, InstanceConfig instanceConfig) { + return new DiscoveryClient(httpAgent, instanceConfig); + } + +} diff --git a/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/core/DiscoveryClient.java b/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/core/DiscoveryClient.java index b69d061a..2983b251 100644 --- a/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/core/DiscoveryClient.java +++ b/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/core/DiscoveryClient.java @@ -23,7 +23,7 @@ public class DiscoveryClient { private final HttpAgent httpAgent; - private final InstanceInfo instanceInfo; + private final InstanceConfig instanceConfig; private volatile long lastSuccessfulHeartbeatTimestamp = -1; @@ -31,9 +31,9 @@ public class DiscoveryClient { private String appPathIdentifier; - public DiscoveryClient(HttpAgent httpAgent) { + public DiscoveryClient(HttpAgent httpAgent, InstanceConfig instanceConfig) { this.httpAgent = httpAgent; - this.instanceInfo = null; + this.instanceConfig = instanceConfig; heartbeatExecutor = ThreadPoolBuilder.builder() .poolThreadSize(1, 5) .keepAliveTime(0, TimeUnit.SECONDS) @@ -54,26 +54,17 @@ public class DiscoveryClient { initScheduledTasks(); } - /** - * 初始化所有计划任务 - */ private void initScheduledTasks() { scheduler.schedule(new HeartbeatThread(), 30, TimeUnit.SECONDS); - } - /** - * 注册实例到服务端 - * - * @return - */ boolean register() { log.info("{}{} :: registering service...", PREFIX, appPathIdentifier); String urlPath = "/apps/" + appPathIdentifier; Result registerResult = null; try { - registerResult = httpAgent.httpPostByDiscovery(urlPath, instanceInfo); + registerResult = httpAgent.httpPostByDiscovery(urlPath, instanceConfig); } catch (Exception ex) { log.warn("{} {} - registration failed :: {}.", PREFIX, appPathIdentifier, ex.getMessage(), ex); throw ex; @@ -86,10 +77,6 @@ public class DiscoveryClient { return registerResult.isSuccess(); } - - /** - * 与 Server 端保持心跳续约 - */ public class HeartbeatThread implements Runnable { @Override @@ -98,13 +85,9 @@ public class DiscoveryClient { lastSuccessfulHeartbeatTimestamp = System.currentTimeMillis(); } } + } - /** - * 心跳续约 - * - * @return - */ boolean renew() { return true; diff --git a/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/core/InstanceConfig.java b/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/core/InstanceConfig.java new file mode 100644 index 00000000..dfdd123e --- /dev/null +++ b/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/core/InstanceConfig.java @@ -0,0 +1,25 @@ +package com.github.dynamic.threadpool.starter.core; + +/** + * Dynamic thread pool instance configuration. + * + * @author chen.ma + * @date 2021/8/6 21:31 + */ +public interface InstanceConfig { + + /** + * get Host Name. + * + * @return + */ + String getHostName(); + + /** + * get Instance Id. + * + * @return + */ + String getInstanceId(); + +} diff --git a/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/toolkit/CloudCommonIdUtil.java b/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/toolkit/CloudCommonIdUtil.java new file mode 100644 index 00000000..6ac32383 --- /dev/null +++ b/dynamic-threadpool-spring-boot-starter/src/main/java/com/github/dynamic/threadpool/starter/toolkit/CloudCommonIdUtil.java @@ -0,0 +1,36 @@ +package com.github.dynamic.threadpool.starter.toolkit; + +import org.springframework.core.env.PropertyResolver; + +/** + * Cloud Common Id Util. + * + * @author chen.ma + * @date 2021/8/6 21:02 + */ +public class CloudCommonIdUtil { + + private static final String SEPARATOR = ":"; + + public static String getDefaultInstanceId(PropertyResolver resolver) { + String hostname = resolver.getProperty("spring.cloud.client.hostname"); + String appName = resolver.getProperty("spring.application.name"); + String namePart = combineParts(hostname, SEPARATOR, appName); + String indexPart = resolver.getProperty("spring.application.instance_id", resolver.getProperty("server.port")); + return combineParts(namePart, SEPARATOR, indexPart); + } + + public static String combineParts(String firstPart, String separator, + String secondPart) { + String combined = null; + if (firstPart != null && secondPart != null) { + combined = firstPart + separator + secondPart; + } else if (firstPart != null) { + combined = firstPart; + } else if (secondPart != null) { + combined = secondPart; + } + return combined; + } + +}