mirror of https://github.com/longtai-cn/hippo4j
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);
|
||||
}
|
||||
|
||||
}
|
@ -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…
Reference in new issue