diff --git a/.gitignore b/.gitignore index 3c392ae..9f42455 100644 --- a/.gitignore +++ b/.gitignore @@ -21,4 +21,5 @@ tmp/ logs/ node_modules coverage-report -.vscode \ No newline at end of file +.vscode +*.factorypath \ No newline at end of file diff --git a/core/src/main/java/com/alipay/sofa/dashboard/client/ark/ArkBizLifecycleHandler.java b/core/src/main/java/com/alipay/sofa/dashboard/client/ark/ArkBizLifecycleHandler.java index 69650f1..149a30f 100644 --- a/core/src/main/java/com/alipay/sofa/dashboard/client/ark/ArkBizLifecycleHandler.java +++ b/core/src/main/java/com/alipay/sofa/dashboard/client/ark/ArkBizLifecycleHandler.java @@ -96,6 +96,6 @@ public void afterStarted(CuratorFramework client) { private String getBizPath() { return Constants.SOFA_BOOT_CLIENT_ROOT + Constants.SOFA_BOOT_CLIENT_BIZ + Constants.SEPARATOR + application.getAppName() + Constants.SEPARATOR - + application.getHostName(); + + application.getHostName() + Constants.COLON + application.getInternalHost(); } } diff --git a/core/src/main/java/com/alipay/sofa/dashboard/client/model/common/Application.java b/core/src/main/java/com/alipay/sofa/dashboard/client/model/common/Application.java index 611bfa1..7c0bd4c 100644 --- a/core/src/main/java/com/alipay/sofa/dashboard/client/model/common/Application.java +++ b/core/src/main/java/com/alipay/sofa/dashboard/client/model/common/Application.java @@ -19,6 +19,8 @@ import java.io.Serializable; import java.util.Objects; +import org.springframework.util.StringUtils; + /** * Application instance model definition * @@ -38,6 +40,12 @@ public class Application implements Serializable, Comparable { */ private String hostName; + /** + * 内部IP + * + */ + private String internalHost; + /** * Binding port */ @@ -64,6 +72,7 @@ public Application() { private Application(Builder builder) { setAppName(builder.appName); setHostName(builder.hostName); + setInternalHost(builder.internalHost); setPort(builder.port); setAppState(builder.appState); setStartTime(builder.startTime); @@ -78,7 +87,15 @@ public static Builder newBuilder() { public String toString() { return "Application{" + "appName='" + appName + '\'' + ", hostName='" + hostName + '\'' + ", port=" + port + ", appState='" + appState + '\'' + ", startTime=" + startTime - + ", lastRecover=" + lastRecover + '}'; + + ", lastRecover=" + lastRecover + "internalHost" + internalHost + '}'; + } + + public String getInternalHost() { + return internalHost; + } + + public void setInternalHost(String internalHost) { + this.internalHost = internalHost; } public String getAppName() { @@ -137,25 +154,37 @@ public boolean equals(Object o) { return false; Application that = (Application) o; return getPort() == that.getPort() && Objects.equals(getAppName(), that.getAppName()) - && Objects.equals(getHostName(), that.getHostName()); + && Objects.equals(getHostName(), that.getHostName()) + && Objects.equals(getInternalHost(), that.getInternalHost()); } @Override public int hashCode() { - return Objects.hash(getAppName(), getHostName(), getPort()); + return Objects.hash(getAppName(), getHostName(), getInternalHost(), getPort()); } @Override public int compareTo(Application o) { - int nameSign = Integer.compare(appName.compareTo(o.appName), 0) << 2; - int hostSign = Integer.compare(hostName.compareTo(o.hostName), 0) << 1; + int nameSign = Integer.compare(appName.compareTo(o.appName), 0) << 3; + int hostSign = Integer.compare(hostName.compareTo(o.hostName), 0) << 2; int portSign = Integer.compare(port, o.port); - return nameSign + hostSign + portSign; + boolean isTargetInternalHostNull = StringUtils.isEmpty(o.internalHost); + boolean isInternalHostNull = StringUtils.isEmpty(internalHost); + if (isInternalHostNull && isTargetInternalHostNull) { + return nameSign + hostSign + portSign; + } else if (isTargetInternalHostNull == false && isInternalHostNull == false) { + int internalSign = Integer.compare(internalHost.compareTo(o.internalHost), 0) << 1; + return nameSign + hostSign + internalSign + portSign; + } else { + return 1; + } } public static final class Builder { + private String appName; private String hostName; + private String internalHost; private int port; private String appState; private long startTime; @@ -164,6 +193,11 @@ public static final class Builder { private Builder() { } + public Builder internalHost(String internalHost) { + this.internalHost = internalHost; + return this; + } + public Builder appName(String appName) { this.appName = appName; return this; diff --git a/core/src/main/java/com/alipay/sofa/dashboard/client/model/common/HostAndPort.java b/core/src/main/java/com/alipay/sofa/dashboard/client/model/common/HostAndPort.java index f78cf11..838aa7d 100644 --- a/core/src/main/java/com/alipay/sofa/dashboard/client/model/common/HostAndPort.java +++ b/core/src/main/java/com/alipay/sofa/dashboard/client/model/common/HostAndPort.java @@ -19,6 +19,8 @@ import java.io.Serializable; import java.util.Objects; +import org.springframework.util.StringUtils; + /** * Host and port definition * @@ -26,69 +28,85 @@ */ public class HostAndPort implements Comparable, Serializable { - private static final int serialVersionUID = 0x11; - - private String host; - - private int port; - - public HostAndPort() { - } - - public HostAndPort(String host, int port) { - this.host = host; - this.port = port; - } - - /** - * Convert host & port to instance id - * - * @return instance id - */ - public String toInstanceId() { - return String.format("%s_%d", host, port); - } - - public String getHost() { - return host; - } - - public void setHost(String host) { - this.host = host; - } - - public int getPort() { - return port; - } - - public void setPort(int port) { - this.port = port; - } - - @Override - public int compareTo(HostAndPort o) { - int hostSign = Integer.compare(host.compareTo(o.host), 0) << 2; - int portSign = Integer.compare(port, o.port); - return hostSign + portSign; - } - - @Override - public String toString() { - return "HostAndPort{" + "host='" + host + '\'' + ", port=" + port + '}'; - } - - @Override - public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; - HostAndPort that = (HostAndPort) o; - return getPort() == that.getPort() && Objects.equals(getHost(), that.getHost()); - } - - @Override - public int hashCode() { - return Objects.hash(getHost(), getPort()); - } + private static final int serialVersionUID = 0x11; + + private String host; + + private String internalHost; + + private int port; + + // public HostAndPort() { + // } + + public HostAndPort(String host, String interHost, int port) { + this.host = host; + this.port = port; + this.internalHost = interHost; + } + + /** + * Convert host & port to instance id + * + * @return instance id + */ + public String toInstanceId() { + if (StringUtils.isEmpty(internalHost)) { + return String.format("%s_%d", host, port); + } else { + return String.format("%s_%s_%d", host, internalHost, port); + } + } + + public String getInternalHost() { + return "null".equals(internalHost) ? null : internalHost; + } + + public void setInternalHost(String internalHost) { + this.internalHost = internalHost; + } + + public String getHost() { + return host; + } + + public void setHost(String host) { + this.host = host; + } + + public int getPort() { + return port; + } + + public void setPort(int port) { + this.port = port; + } + + @Override + public int compareTo(HostAndPort o) { + int hostSign = Integer.compare(host.compareTo(o.host), 0) << 2; + int internalSign = Integer.compare(internalHost.compareTo(o.internalHost), 0) << 1; + int portSign = Integer.compare(port, o.port); + return hostSign + portSign + internalSign; + } + + @Override + public String toString() { + return "HostAndPort{" + "host='" + host + '\'' + ", internalHost='" + internalHost + '\'' + ", port=" + port + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; + HostAndPort that = (HostAndPort) o; + return getPort() == that.getPort() && Objects.equals(getHost(), that.getHost()) && Objects.equals(getInternalHost(), that.getInternalHost()); + } + + @Override + public int hashCode() { + return Objects.hash(getHost(), getInternalHost(), getPort()); + } } diff --git a/core/src/main/java/com/alipay/sofa/dashboard/client/registry/zookeeper/ZookeeperRegistryUtils.java b/core/src/main/java/com/alipay/sofa/dashboard/client/registry/zookeeper/ZookeeperRegistryUtils.java index 36a697a..0b52aa0 100644 --- a/core/src/main/java/com/alipay/sofa/dashboard/client/registry/zookeeper/ZookeeperRegistryUtils.java +++ b/core/src/main/java/com/alipay/sofa/dashboard/client/registry/zookeeper/ZookeeperRegistryUtils.java @@ -16,18 +16,20 @@ */ package com.alipay.sofa.dashboard.client.registry.zookeeper; -import com.alipay.sofa.dashboard.client.model.common.Application; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.lang.NonNull; -import org.springframework.lang.Nullable; - import java.io.UnsupportedEncodingException; import java.net.URI; import java.net.URLDecoder; import java.util.LinkedHashMap; import java.util.Map; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.lang.NonNull; +import org.springframework.lang.Nullable; +import org.springframework.util.StringUtils; + +import com.alipay.sofa.dashboard.client.model.common.Application; + /** * @author chpengzh@didiglobal.com * @date 2019-08-29 09:18 @@ -45,14 +47,15 @@ final class ZookeeperRegistryUtils { /** * Convert an instance definition into session node name * - * @param instance application instance + * @param instance + * application instance * @return session node name */ @NonNull static String toSessionNode(Application instance) { - String appId = String.format("%s:%d?startTime=%d&lastRecover=%d&state=%s", - instance.getHostName(), instance.getPort(), instance.getStartTime(), - instance.getLastRecover(), instance.getAppState()); + String appId = String.format("%s:%d?internalHost=%s&startTime=%d&lastRecover=%d&state=%s", + instance.getHostName(), instance.getPort(), instance.getInternalHost(), + instance.getStartTime(), instance.getLastRecover(), instance.getAppState()); String appName = instance.getAppName(); return String.format("%s/%s/%s", ZookeeperConstants.SOFA_BOOT_CLIENT_INSTANCE, appName, appId); @@ -61,7 +64,8 @@ static String toSessionNode(Application instance) { /** * Parse session node to application instance. * - * @param sessionNode session node path + * @param sessionNode + * session node path * @return {@code null}, if this session path is not a legal one */ @Nullable @@ -90,6 +94,7 @@ static Application parseSessionNode(String sessionNode) { Application application = new Application(); application.setAppName(appName); + application.setInternalHost(query.get("internalHost")); application.setHostName(instanceUri.getHost()); application.setPort(instanceUri.getPort()); application.setAppState(query.get("state")); diff --git a/extension-impl/redis-store/src/main/java/com/alipay/sofa/dashboard/redis/config/RedisStoreConfiguration.java b/extension-impl/redis-store/src/main/java/com/alipay/sofa/dashboard/redis/config/RedisStoreConfiguration.java index d541b9b..e70b919 100644 --- a/extension-impl/redis-store/src/main/java/com/alipay/sofa/dashboard/redis/config/RedisStoreConfiguration.java +++ b/extension-impl/redis-store/src/main/java/com/alipay/sofa/dashboard/redis/config/RedisStoreConfiguration.java @@ -16,11 +16,8 @@ */ package com.alipay.sofa.dashboard.redis.config; -import com.alipay.sofa.dashboard.redis.io.LettuceConnFactoryProvider; -import com.alipay.sofa.dashboard.redis.io.RedisRecordExporter; -import com.alipay.sofa.dashboard.redis.io.RedisRecordImporter; -import com.alipay.sofa.dashboard.redis.properties.SofaDashboardRedisProperties; -import io.lettuce.core.resource.DefaultClientResources; +import java.io.Closeable; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; @@ -31,7 +28,12 @@ import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Component; -import java.io.Closeable; +import com.alipay.sofa.dashboard.redis.io.LettuceConnFactoryProvider; +import com.alipay.sofa.dashboard.redis.io.RedisRecordExporter; +import com.alipay.sofa.dashboard.redis.io.RedisRecordImporter; +import com.alipay.sofa.dashboard.redis.properties.SofaDashboardRedisProperties; + +import io.lettuce.core.resource.DefaultClientResources; /** * @author chen.pengzhi (chpengzh@foxmail.com) diff --git a/extension-impl/redis-store/src/test/java/com/alipay/sofa/dashboard/redis/io/RedisStoreBasicTest.java b/extension-impl/redis-store/src/test/java/com/alipay/sofa/dashboard/redis/io/RedisStoreBasicTest.java index 3d21d16..24ce607 100644 --- a/extension-impl/redis-store/src/test/java/com/alipay/sofa/dashboard/redis/io/RedisStoreBasicTest.java +++ b/extension-impl/redis-store/src/test/java/com/alipay/sofa/dashboard/redis/io/RedisStoreBasicTest.java @@ -64,7 +64,7 @@ public static void recycleServer() { @Test public void writeRecordAndRead() { - final HostAndPort hostAndPort = new HostAndPort("127.0.0.1", 8080); + final HostAndPort hostAndPort = new HostAndPort("127.0.0.1", "10.41.0.1", 8080); final String schemeName = "test"; List samples = Lists.newArrayList(StoreRecord.newBuilder() .schemeName(schemeName).timestamp(System.currentTimeMillis()).value("aaaaa").build()); diff --git a/pom.xml b/pom.xml index 131ffde..c3433b5 100644 --- a/pom.xml +++ b/pom.xml @@ -1,399 +1,397 @@ - - 4.0.0 - pom + + 4.0.0 + pom - - com.alipay.sofa - sofaboot-dependencies - 3.1.4 - + + com.alipay.sofa + sofaboot-dependencies + 3.1.4 + - sofa-dashboard-client - 1.1.0-SNAPSHOT - SOFADashboard Client Projects - https://github.com/sofastack/sofa-dashboard-client + sofa-dashboard-client + 1.1.0-SNAPSHOT + SOFADashboard Client Projects + https://github.com/sofastack/sofa-dashboard-client - - core - sofa-boot-starter - extension-impl - dashboard-support - + + core + sofa-boot-starter + extension-impl + dashboard-support + - - - The Apache License, Version 2.0 - http://www.apache.org/licenses/LICENSE-2.0.txt - - + + + The Apache License, Version 2.0 + http://www.apache.org/licenses/LICENSE-2.0.txt + + - - scm:git:git://github.com/sofastack/sofa-dashboard-client.git - + + scm:git:git://github.com/sofastack/sofa-dashboard-client.git + scm:git:ssh://github.com/sofastack/sofa-dashboard-client.git - http://github.com/sofastack/sofa-dashboard-client/tree/master - + http://github.com/sofastack/sofa-dashboard-client/tree/master + - - - UTF-8 - 1.8 - 1.8 - 1.8 + + + UTF-8 + 1.8 + 1.8 + 1.8 - - 2.9.1 - 1.14 - 2.9.7 - 0.6 - Kay-SR14 + + 2.9.1 + 1.14 + 2.9.7 + 0.6 + Kay-SR14 - - 3.1.0 - 2.9.1 - 1.6.7 - 1.5 - + + 3.1.0 + 2.9.1 + 1.6.7 + 1.5 + - - - GuoLei Song - guolei.sgl@antfin.com - Ant Financial - https://www.alipay.com/ - - - Pengzhi Chen - chpengzh@foxmail.com - https://chpengzh.com - - + + + GuoLei Song + guolei.sgl@antfin.com + Ant Financial + https://www.alipay.com/ + + + Pengzhi Chen + chpengzh@foxmail.com + https://chpengzh.com + + - - - - - com.alipay.sofa - dashboard-client-core - 1.1.0-SNAPSHOT - - - com.alipay.sofa - dashboard-ext-redis-store - 1.1.0-SNAPSHOT - - - com.alipay.sofa - dashboard-client-sofa-boot-starter - 1.1.0-SNAPSHOT - + + + + + com.alipay.sofa + dashboard-client-core + 1.1.0-SNAPSHOT + + + com.alipay.sofa + dashboard-ext-redis-store + 1.1.0-SNAPSHOT + + + com.alipay.sofa + dashboard-client-sofa-boot-starter + 1.1.0-SNAPSHOT + - - com.alipay.sofa - sofa-ark-compatible-springboot2 - 1.0.0 - + + com.alipay.sofa + sofa-ark-compatible-springboot2 + 1.0.0 + - - - org.springframework.data - spring-data-releasetrain - ${spring-data-releasetrain.version} - import - pom - - - com.fasterxml.jackson - jackson-bom - ${jackson.version} - pom - import - - - org.apache.curator - apache-curator - ${curator.version} - pom - import - + + + org.springframework.data + spring-data-releasetrain + ${spring-data-releasetrain.version} + import + pom + + + com.fasterxml.jackson + jackson-bom + ${jackson.version} + pom + import + + + org.apache.curator + apache-curator + ${curator.version} + pom + import + - - - com.github.kstyrc - embedded-redis - ${embedded.redis.version} - test - - - org.jmockit - jmockit - ${jmockit.version} - test - - - org.mockito - mockito-core - test - - - org.jmockit - jmockit-coverage - ${jmockit.version} - test - - - + + + com.github.kstyrc + embedded-redis + ${embedded.redis.version} + test + + + org.jmockit + jmockit + ${jmockit.version} + test + + + org.mockito + mockito-core + test + + + org.jmockit + jmockit-coverage + ${jmockit.version} + test + + + - - - - maven-source-plugin - - - com.mycila - license-maven-plugin - - - com.googlecode.maven-java-formatter-plugin - maven-java-formatter-plugin - - - org.apache.maven.plugins - maven-surefire-plugin - - - - - - com.mycila - license-maven-plugin - 3.0 - - - generate-sources - - remove - format - - - - - true -
${user.dir}/tools/codestyle/HEADER
- - **/*.properties - *.sh - *.yml - .gitignore - **/*.md - **/*.xml - - true - - SLASHSTAR_STYLE - -
-
- - com.googlecode.maven-java-formatter-plugin - maven-java-formatter-plugin - 0.4 - - - - format - - - - - ${user.dir}/tools/codestyle/formatter.xml - UTF-8 - - - - org.apache.maven.plugins - maven-compiler-plugin - 3.1 - - ${java.compiler.source.version} - ${java.compiler.target.version} - ${project.build.sourceEncoding} - - - - org.apache.maven.plugins - maven-source-plugin - 3.0.1 - - - attach-sources - - jar - - - - - - org.eluder.coveralls - coveralls-maven-plugin - ${coveralls.maven.plugin} - - ${project.build.sourceEncoding} - - - - org.apache.maven.plugins - maven-surefire-plugin - 2.18.1 - - - - **/*Test.java - - - - - org.jacoco - jacoco-maven-plugin - 0.7.9 - - false - - - - default-prepare-agent - - prepare-agent - - - - default-report - test - - report-aggregate - - - - -
-
-
+ + + + maven-source-plugin + + + com.mycila + license-maven-plugin + + + com.googlecode.maven-java-formatter-plugin + maven-java-formatter-plugin + + + org.apache.maven.plugins + maven-surefire-plugin + + + + + + com.mycila + license-maven-plugin + 3.0 + + + generate-sources + + remove + format + + + + + true +
${user.dir}/tools/codestyle/HEADER
+ + **/*.properties + *.sh + *.yml + .gitignore + **/*.md + **/*.xml + + true + + SLASHSTAR_STYLE + +
+
+ + com.googlecode.maven-java-formatter-plugin + maven-java-formatter-plugin + 0.4 + + + + format + + + + + ${user.dir}/tools/codestyle/formatter.xml + UTF-8 + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.1 + + ${java.compiler.source.version} + ${java.compiler.target.version} + ${project.build.sourceEncoding} + + + + org.apache.maven.plugins + maven-source-plugin + 3.0.1 + + + attach-sources + + jar + + + + + + org.eluder.coveralls + coveralls-maven-plugin + ${coveralls.maven.plugin} + + ${project.build.sourceEncoding} + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.18.1 + + + + **/*Test.java + + + + + org.jacoco + jacoco-maven-plugin + 0.7.9 + + false + + + + default-prepare-agent + + prepare-agent + + + + default-report + test + + report-aggregate + + + + +
+
+
- - - release - - - - - org.apache.maven.plugins - maven-javadoc-plugin - ${maven.javadoc.plugin} - - - attach-javadocs - - jar - - - - - - org.sonatype.plugins - nexus-staging-maven-plugin - ${maven.staging.plugin} - true - - ossrh - https://oss.sonatype.org/ - false - - - - org.apache.maven.plugins - maven-gpg-plugin - ${maven.gpg.plugin} - - - sign-artifacts - verify - - sign - - - - - - - - - - ossrh - https://oss.sonatype.org/service/local/staging/deploy/maven2/ - - - ossrh - https://oss.sonatype.org/content/repositories/snapshots - - - + + + release + + + + + org.apache.maven.plugins + maven-javadoc-plugin + ${maven.javadoc.plugin} + + + attach-javadocs + + jar + + + + + + org.sonatype.plugins + nexus-staging-maven-plugin + ${maven.staging.plugin} + true + + ossrh + https://oss.sonatype.org/ + false + + + + org.apache.maven.plugins + maven-gpg-plugin + ${maven.gpg.plugin} + + + sign-artifacts + verify + + sign + + + + + + + + + + ossrh + https://oss.sonatype.org/service/local/staging/deploy/maven2/ + + + ossrh + https://oss.sonatype.org/content/repositories/snapshots + + + - - snapshot - - - - - org.apache.maven.plugins - maven-javadoc-plugin - ${maven.javadoc.plugin} - - - attach-javadocs - - jar - - - - - - org.sonatype.plugins - nexus-staging-maven-plugin - ${maven.staging.plugin} - true - - ossrh - https://oss.sonatype.org/ - false - - - - org.apache.maven.plugins - maven-gpg-plugin - ${maven.gpg.plugin} - - - sign-artifacts - verify - - sign - - - - - - - - - - ossrh - https://oss.sonatype.org/content/repositories/snapshots - - - - + + snapshot + + + + + org.apache.maven.plugins + maven-javadoc-plugin + ${maven.javadoc.plugin} + + + attach-javadocs + + jar + + + + + + org.sonatype.plugins + nexus-staging-maven-plugin + ${maven.staging.plugin} + true + + ossrh + https://oss.sonatype.org/ + false + + + + org.apache.maven.plugins + maven-gpg-plugin + ${maven.gpg.plugin} + + + sign-artifacts + verify + + sign + + + + + + + + + + ossrh + https://oss.sonatype.org/content/repositories/snapshots + + + +
diff --git a/sofa-boot-starter/src/main/java/com/alipay/sofa/dashboard/client/config/AppPublisherConfiguration.java b/sofa-boot-starter/src/main/java/com/alipay/sofa/dashboard/client/config/AppPublisherConfiguration.java index c47f507..161ddb1 100644 --- a/sofa-boot-starter/src/main/java/com/alipay/sofa/dashboard/client/config/AppPublisherConfiguration.java +++ b/sofa-boot-starter/src/main/java/com/alipay/sofa/dashboard/client/config/AppPublisherConfiguration.java @@ -44,77 +44,99 @@ */ @Configuration @ConditionalOnWebApplication -@EnableConfigurationProperties({ SofaDashboardClientProperties.class, - SofaDashboardZookeeperProperties.class }) +@EnableConfigurationProperties({ SofaDashboardClientProperties.class, SofaDashboardZookeeperProperties.class }) @ConditionalOnProperty(prefix = "com.alipay.sofa.dashboard.client", value = "enable", matchIfMissing = true) public class AppPublisherConfiguration { - private static final String KEY_SPRING_APP_NAME = "spring.application.name"; - - private static final String KEY_SERVER_PORT = "server.port"; - - private static final String DEFAULT_SERVER_PORT = "8080"; - - @Bean(name = "application") - @ConditionalOnMissingBean - public Application getApplicationInstance(Environment env, SofaDashboardClientProperties prop) { - long current = System.currentTimeMillis(); - - Application app = new Application(); - app.setAppName(env.getRequiredProperty(KEY_SPRING_APP_NAME)); - app.setHostName(getLocalIp(prop)); - app.setPort(Integer.parseInt(env.getProperty(KEY_SERVER_PORT, DEFAULT_SERVER_PORT))); - app.setStartTime(current); - app.setLastRecover(current); - return app; - } - - @Bean - @ConditionalOnMissingBean - public IntrospectBizEndpoint introspectBizEndpoint() { - return new IntrospectBizEndpoint(); - } - - @Bean - @ConditionalOnMissingBean - public ArkBizLifecycleHandler bizStateListener(IntrospectBizEndpoint endpoint, - Application application) { - return new ArkBizLifecycleHandler(endpoint, application); - } - - @Bean - @ConditionalOnMissingBean - public ZookeeperConfig getZookeeperRegistryConfig(SofaDashboardZookeeperProperties prop) { - ZookeeperConfig config = new ZookeeperConfig(); - config.setAddress(prop.getAddress()); - config.setBaseSleepTimeMs(prop.getBaseSleepTimeMs()); - config.setMaxRetries(prop.getMaxRetries()); - config.setSessionTimeoutMs(prop.getSessionTimeoutMs()); - config.setConnectionTimeoutMs(prop.getConnectionTimeoutMs()); - return config; - } - - @Bean(destroyMethod = "shutdown") - @ConditionalOnMissingBean - public ZookeeperClient zookeeperRegistryClient(SofaDashboardZookeeperProperties prop, - List provider) { - ZookeeperConfig config = getZookeeperRegistryConfig(prop); - ZookeeperClient client = new ZookeeperClient(config); - if (!CollectionUtils.isEmpty(provider)) { - provider.forEach(client::addLifecycleHandler); - } - return client; - } - - @Bean - @ConditionalOnMissingBean - public AppPublisher getAppPublisher(Application application, ZookeeperClient client) { - return new ZookeeperAppPublisher(application, client); - } - - private String getLocalIp(SofaDashboardClientProperties properties) { - NetworkAddressUtils.calculate(null, null); - return StringUtils.isEmpty(properties.getInstanceIp()) ? NetworkAddressUtils.getLocalIP() - : properties.getInstanceIp(); - } + private static final String KEY_SPRING_APP_NAME = "spring.application.name"; + + private static final String KEY_SERVER_PORT = "server.port"; + + private static final String DEFAULT_SERVER_PORT = "8080"; + + @Bean(name = "application") + @ConditionalOnMissingBean + public Application getApplicationInstance(Environment env, SofaDashboardClientProperties prop) { + long current = System.currentTimeMillis(); + + Application app = new Application(); + app.setAppName(env.getRequiredProperty(KEY_SPRING_APP_NAME)); + app.setHostName(getHostIp(prop)); + app.setInternalHost(getInteralHost(prop)); + app.setPort(getPort(prop,env)); + app.setStartTime(current); + app.setLastRecover(current); + return app; + } + + private String getInteralHost(SofaDashboardClientProperties properties) { + return properties.getInternalHost(); + } + + @Bean + @ConditionalOnMissingBean + public IntrospectBizEndpoint introspectBizEndpoint() { + return new IntrospectBizEndpoint(); + } + + @Bean + @ConditionalOnMissingBean + public ArkBizLifecycleHandler bizStateListener(IntrospectBizEndpoint endpoint, Application application) { + return new ArkBizLifecycleHandler(endpoint, application); + } + + @Bean + @ConditionalOnMissingBean + public ZookeeperConfig getZookeeperRegistryConfig(SofaDashboardZookeeperProperties prop) { + ZookeeperConfig config = new ZookeeperConfig(); + config.setAddress(prop.getAddress()); + config.setBaseSleepTimeMs(prop.getBaseSleepTimeMs()); + config.setMaxRetries(prop.getMaxRetries()); + config.setSessionTimeoutMs(prop.getSessionTimeoutMs()); + config.setConnectionTimeoutMs(prop.getConnectionTimeoutMs()); + return config; + } + + @Bean(destroyMethod = "shutdown") + @ConditionalOnMissingBean + public ZookeeperClient zookeeperRegistryClient(SofaDashboardZookeeperProperties prop, List provider) { + ZookeeperConfig config = getZookeeperRegistryConfig(prop); + ZookeeperClient client = new ZookeeperClient(config); + if (!CollectionUtils.isEmpty(provider)) { + provider.forEach(client::addLifecycleHandler); + } + return client; + } + + @Bean + @ConditionalOnMissingBean + public AppPublisher getAppPublisher(Application application, ZookeeperClient client) { + return new ZookeeperAppPublisher(application, client); + } + + private int getPort(SofaDashboardClientProperties properties,Environment env) { + String virtualPort = properties.getVirtualPort(); + if(StringUtils.isEmpty(virtualPort)) { + return Integer.parseInt(env.getProperty(KEY_SERVER_PORT, DEFAULT_SERVER_PORT)); + }else { + return Integer.valueOf(virtualPort); + } + } + + private String getHostIp(SofaDashboardClientProperties properties) { + NetworkAddressUtils.calculate(null, null); + String ip = null; + boolean isInstanceIpEmpty = StringUtils.isEmpty(properties.getInstanceIp()); + if (isInstanceIpEmpty) { + boolean isVirtualHostEmpty = StringUtils.isEmpty(properties.getVirtualHost()); + if (isVirtualHostEmpty) { + ip = NetworkAddressUtils.getLocalIP(); + } else { + ip = properties.getVirtualHost(); + } + } else { + ip = properties.getInstanceIp(); + } + return ip; + } } diff --git a/sofa-boot-starter/src/main/java/com/alipay/sofa/dashboard/client/config/DimensionStoreConfiguration.java b/sofa-boot-starter/src/main/java/com/alipay/sofa/dashboard/client/config/DimensionStoreConfiguration.java index a824d73..2836be0 100644 --- a/sofa-boot-starter/src/main/java/com/alipay/sofa/dashboard/client/config/DimensionStoreConfiguration.java +++ b/sofa-boot-starter/src/main/java/com/alipay/sofa/dashboard/client/config/DimensionStoreConfiguration.java @@ -63,10 +63,25 @@ public DimensionRecordingSchedule createStoreSchedule(List private HostAndPort getHostAndPort(Environment env, SofaDashboardClientProperties properties) { NetworkAddressUtils.calculate(null, null); - String ip = StringUtils.isEmpty(properties.getInstanceIp()) ? NetworkAddressUtils - .getLocalIP() : properties.getInstanceIp(); - int port = Integer.parseInt(env.getProperty(KEY_SERVER_PORT, DEFAULT_SERVER_PORT)); - return new HostAndPort(ip, port); + String ip = null; + boolean isInstanceIpEmpty = StringUtils.isEmpty(properties.getInstanceIp()); + if (isInstanceIpEmpty) { + boolean isVirtualHostEmpty = StringUtils.isEmpty(properties.getVirtualHost()); + if (isVirtualHostEmpty) { + ip = NetworkAddressUtils.getLocalIP(); + } else { + ip = properties.getVirtualHost(); + } + } else { + ip = properties.getInstanceIp(); + } + String virtualPort = properties.getVirtualPort(); + String internalHost = properties.getInternalHost(); + if(StringUtils.isEmpty(virtualPort)) { + return new HostAndPort(ip, internalHost, Integer.parseInt(env.getProperty(KEY_SERVER_PORT, DEFAULT_SERVER_PORT))); + }else { + return new HostAndPort(ip, internalHost, Integer.valueOf(virtualPort)); + } } /** diff --git a/sofa-boot-starter/src/main/java/com/alipay/sofa/dashboard/client/properties/SofaDashboardClientProperties.java b/sofa-boot-starter/src/main/java/com/alipay/sofa/dashboard/client/properties/SofaDashboardClientProperties.java index 765c328..00a6626 100644 --- a/sofa-boot-starter/src/main/java/com/alipay/sofa/dashboard/client/properties/SofaDashboardClientProperties.java +++ b/sofa-boot-starter/src/main/java/com/alipay/sofa/dashboard/client/properties/SofaDashboardClientProperties.java @@ -16,31 +16,60 @@ */ package com.alipay.sofa.dashboard.client.properties; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.stereotype.Component; +import org.springframework.core.env.Environment; +import org.springframework.util.StringUtils; + +import com.google.common.base.CaseFormat; @ConfigurationProperties(prefix = "com.alipay.sofa.dashboard.client") public class SofaDashboardClientProperties { + /** + * 兼容SOFA-RPC/SOFA-BOOT配置 + */ + public static final String SOFA_RPC_PREFIX = "com.alipay.sofa.rpc"; + + public static final String SOFA_DASHBOARD_PREFIX = "com.alipay.sofa.dashboard.client"; + + @Autowired + private Environment environment; + + /** + * virtual host for service publish(服务发布虚拟host) 主要用于向RPC注册 + */ + private String virtualHost; + + /** + * virtual port for service publish(服务发布虚拟端口) * 主要用于向RPC注册 + */ + private String virtualPort; + + /** + * 内部IP + */ + private String internalHost; + /** * 是否可用 */ - private boolean enable = true; + private boolean enable = true; /** * 实例地址 */ - private String instanceIp = ""; + private String instanceIp = ""; /** * Dashboard度量数据存储上报延迟期望(s) */ - private long storeInitDelayExp = 30; + private long storeInitDelayExp = 30; /** * Dashboard度量数据存储上报周期(s) */ - private long storeUploadPeriodExp = 60; + private long storeUploadPeriodExp = 60; public boolean isEnable() { return enable; @@ -74,10 +103,75 @@ public void setStoreUploadPeriodExp(long storeUploadPeriodExp) { this.storeUploadPeriodExp = storeUploadPeriodExp; } + public String getInternalHost() { + if (environment.containsProperty("rpc_register_internal_host")) { + return environment.getProperty("rpc_register_internal_host"); + } + return StringUtils.isEmpty(internalHost) ? getPropertiesByOrder(new Object() { + }.getClass().getEnclosingMethod().getName()) : internalHost; + } + + public void setInternalHost(String internalHost) { + this.internalHost = internalHost; + } + + public String getVirtualHost() { + if (environment.containsProperty("rpc_register_virtual_host")) { + return environment.getProperty("rpc_register_virtual_host"); + } + String name = new Object() { + }.getClass().getEnclosingMethod().getName(); + return StringUtils.isEmpty(virtualHost) ? getPropertiesByOrder(name) : virtualHost; + } + + public void setVirtualHost(String virtualHost) { + this.virtualHost = virtualHost; + } + + public String getVirtualPort() { + if (environment.containsProperty("rpc_register_virtual_port")) { + return environment.getProperty("rpc_register_virtual_port"); + } + return StringUtils.isEmpty(virtualPort) ? getPropertiesByOrder(new Object() { + }.getClass().getEnclosingMethod().getName()) : virtualPort; + } + + public void setVirtualPort(String virtualPort) { + this.virtualPort = virtualPort; + } + + public Environment getEnvironment() { + return environment; + } + + public void setEnvironment(Environment environment) { + this.environment = environment; + } + + private String getPropertiesByOrder(String enclosingMethodName) { + if (environment == null) { + return null; + } + String key = SOFA_DASHBOARD_PREFIX + "." + camelToDot(enclosingMethodName.substring(3)); + String dashboardPropertie = environment.getProperty(key); + if (StringUtils.isEmpty(dashboardPropertie)) { + String property = environment.getProperty(SOFA_RPC_PREFIX + "." + camelToDot(enclosingMethodName.substring(3))); + return property; + } else { + return dashboardPropertie; + } + } + + public String camelToDot(String camelCaseString) { + return CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, camelCaseString).replaceAll("-", + "."); + } + @Override public String toString() { return "SofaDashboardClientProperties{" + "enable=" + enable + ", instanceIp='" + instanceIp + '\'' + ", storeInitDelayExp=" + storeInitDelayExp - + ", storeUploadPeriodExp=" + storeUploadPeriodExp + '}'; + + ", storeUploadPeriodExp=" + storeUploadPeriodExp + "virtualHost=" + virtualHost + + "virtualPort=" + virtualPort + "internalHost=" + internalHost + '}'; } } \ No newline at end of file