Develop service discovery capabilities.

pull/161/head
chen.ma 3 years ago
parent 36890aae23
commit 689d66fcc4

@ -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);
}
}

@ -23,7 +23,7 @@ public class DiscoveryClient {
private final HttpAgent httpAgent; private final HttpAgent httpAgent;
private final InstanceInfo instanceInfo; private final InstanceConfig instanceConfig;
private volatile long lastSuccessfulHeartbeatTimestamp = -1; private volatile long lastSuccessfulHeartbeatTimestamp = -1;
@ -31,9 +31,9 @@ public class DiscoveryClient {
private String appPathIdentifier; private String appPathIdentifier;
public DiscoveryClient(HttpAgent httpAgent) { public DiscoveryClient(HttpAgent httpAgent, InstanceConfig instanceConfig) {
this.httpAgent = httpAgent; this.httpAgent = httpAgent;
this.instanceInfo = null; this.instanceConfig = instanceConfig;
heartbeatExecutor = ThreadPoolBuilder.builder() heartbeatExecutor = ThreadPoolBuilder.builder()
.poolThreadSize(1, 5) .poolThreadSize(1, 5)
.keepAliveTime(0, TimeUnit.SECONDS) .keepAliveTime(0, TimeUnit.SECONDS)
@ -54,26 +54,17 @@ public class DiscoveryClient {
initScheduledTasks(); initScheduledTasks();
} }
/**
*
*/
private void initScheduledTasks() { private void initScheduledTasks() {
scheduler.schedule(new HeartbeatThread(), 30, TimeUnit.SECONDS); scheduler.schedule(new HeartbeatThread(), 30, TimeUnit.SECONDS);
} }
/**
*
*
* @return
*/
boolean register() { boolean register() {
log.info("{}{} :: registering service...", PREFIX, appPathIdentifier); log.info("{}{} :: registering service...", PREFIX, appPathIdentifier);
String urlPath = "/apps/" + appPathIdentifier; String urlPath = "/apps/" + appPathIdentifier;
Result registerResult = null; Result registerResult = null;
try { try {
registerResult = httpAgent.httpPostByDiscovery(urlPath, instanceInfo); registerResult = httpAgent.httpPostByDiscovery(urlPath, instanceConfig);
} catch (Exception ex) { } catch (Exception ex) {
log.warn("{} {} - registration failed :: {}.", PREFIX, appPathIdentifier, ex.getMessage(), ex); log.warn("{} {} - registration failed :: {}.", PREFIX, appPathIdentifier, ex.getMessage(), ex);
throw ex; throw ex;
@ -86,10 +77,6 @@ public class DiscoveryClient {
return registerResult.isSuccess(); return registerResult.isSuccess();
} }
/**
* Server
*/
public class HeartbeatThread implements Runnable { public class HeartbeatThread implements Runnable {
@Override @Override
@ -98,13 +85,9 @@ public class DiscoveryClient {
lastSuccessfulHeartbeatTimestamp = System.currentTimeMillis(); lastSuccessfulHeartbeatTimestamp = System.currentTimeMillis();
} }
} }
} }
/**
*
*
* @return
*/
boolean renew() { boolean renew() {
return true; return true;

@ -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();
}

@ -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;
}
}
Loading…
Cancel
Save