You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by li...@apache.org on 2018/01/09 06:27:24 UTC
[incubator-servicecomb-java-chassis] 13/13: SCB-85 add boot
listener for start up output
This is an automated email from the ASF dual-hosted git repository.
liubao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-java-chassis.git
commit fc9dd152ef0090e6874028274ef704e82da96ca7
Author: zhengyangyong <ya...@huawei.com>
AuthorDate: Mon Jan 8 11:55:44 2018 +0800
SCB-85 add boot listener for start up output
Signed-off-by: zhengyangyong <ya...@huawei.com>
---
.../metrics-write-file/pom.xml | 16 +++
.../samples/mwf/MetricsBootListener.java | 41 ++++++
.../samples/mwf/WriteFileInitializer.java | 51 +++----
.../io/servicecomb/samples/mwf/TestWriteFile.java | 158 +++++++++++++++++++++
4 files changed, 237 insertions(+), 29 deletions(-)
diff --git a/samples/metrics-write-file-sample/metrics-write-file/pom.xml b/samples/metrics-write-file-sample/metrics-write-file/pom.xml
index fc497a8..acc3e2b 100644
--- a/samples/metrics-write-file-sample/metrics-write-file/pom.xml
+++ b/samples/metrics-write-file-sample/metrics-write-file/pom.xml
@@ -37,6 +37,22 @@
<groupId>io.servicecomb</groupId>
<artifactId>metrics-core</artifactId>
</dependency>
+
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.jmockit</groupId>
+ <artifactId>jmockit</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-core</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
diff --git a/samples/metrics-write-file-sample/metrics-write-file/src/main/java/io/servicecomb/samples/mwf/MetricsBootListener.java b/samples/metrics-write-file-sample/metrics-write-file/src/main/java/io/servicecomb/samples/mwf/MetricsBootListener.java
new file mode 100644
index 0000000..83a19ca
--- /dev/null
+++ b/samples/metrics-write-file-sample/metrics-write-file/src/main/java/io/servicecomb/samples/mwf/MetricsBootListener.java
@@ -0,0 +1,41 @@
+/*
+ * 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 io.servicecomb.samples.mwf;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import io.servicecomb.core.BootListener;
+
+@Component
+public class MetricsBootListener implements BootListener {
+
+ private final WriteFileInitializer initializer;
+
+ @Autowired
+ public MetricsBootListener(WriteFileInitializer initializer) {
+ this.initializer = initializer;
+ }
+
+ @Override
+ public void onBootEvent(BootEvent event) {
+ if (EventType.BEFORE_REGISTRY.equals(event.getEventType())) {
+ this.initializer.startOutput();
+ }
+ }
+}
diff --git a/samples/metrics-write-file-sample/metrics-write-file/src/main/java/io/servicecomb/samples/mwf/WriteFileInitializer.java b/samples/metrics-write-file-sample/metrics-write-file/src/main/java/io/servicecomb/samples/mwf/WriteFileInitializer.java
index 487eb80..fb606b9 100644
--- a/samples/metrics-write-file-sample/metrics-write-file/src/main/java/io/servicecomb/samples/mwf/WriteFileInitializer.java
+++ b/samples/metrics-write-file-sample/metrics-write-file/src/main/java/io/servicecomb/samples/mwf/WriteFileInitializer.java
@@ -56,8 +56,7 @@ public class WriteFileInitializer {
metricPoll = DynamicPropertyFactory.getInstance().getIntProperty(MetricsConfig.METRICS_POLLING_TIME, 5000).get();
this.fileWriter = fileWriter;
this.dataSource = dataSource;
-
- this.init();
+ this.convertor = new SimpleFileContentConvertor();
}
public WriteFileInitializer(MetricsFileWriter fileWriter, DataSource dataSource, String hostName, String filePrefix) {
@@ -66,42 +65,36 @@ public class WriteFileInitializer {
this.dataSource = dataSource;
this.hostName = hostName;
this.filePrefix = filePrefix;
+ this.convertor = new SimpleFileContentConvertor();
+ this.formatter = new SimpleFileContentFormatter(hostName, filePrefix);
}
- private void init() {
+ public void startOutput() {
+ if (StringUtils.isEmpty(filePrefix)) {
+ Microservice microservice = RegistryUtils.getMicroservice();
+ filePrefix = microservice.getAppId() + "." + microservice.getServiceName();
+ }
+ if (StringUtils.isEmpty(hostName)) {
+ hostName = NetUtils.getHostName();
+ if (StringUtils.isEmpty(hostName)) {
+ hostName = NetUtils.getHostAddress();
+ }
+ }
+
+ formatter = new SimpleFileContentFormatter(hostName, filePrefix);
+
final Runnable poller = this::run;
Executors.newScheduledThreadPool(1)
.scheduleWithFixedDelay(poller, 0, metricPoll, MILLISECONDS);
}
public void run() {
- //wait RegistryUtils init completed
- if (RegistryUtils.getServiceRegistry() != null) {
- if (StringUtils.isEmpty(filePrefix)) {
- Microservice microservice = RegistryUtils.getMicroservice();
- filePrefix = microservice.getAppId() + "." + microservice.getServiceName();
- }
- if (StringUtils.isEmpty(hostName)) {
- hostName = NetUtils.getHostName();
- if (StringUtils.isEmpty(hostName)) {
- hostName = NetUtils.getHostAddress();
- }
- }
+ RegistryMetric registryMetric = dataSource.getRegistryMetric();
+ Map<String, String> convertedMetrics = convertor.convert(registryMetric);
+ Map<String, String> formattedMetrics = formatter.format(convertedMetrics);
- if (convertor == null) {
- convertor = new SimpleFileContentConvertor();
- }
- if (formatter == null) {
- formatter = new SimpleFileContentFormatter(hostName, filePrefix);
- }
-
- RegistryMetric registryMetric = dataSource.getRegistryMetric();
- Map<String, String> convertedMetrics = convertor.convert(registryMetric);
- Map<String, String> formattedMetrics = formatter.format(convertedMetrics);
-
- for (String metricName : formattedMetrics.keySet()) {
- fileWriter.write(metricName, filePrefix, formattedMetrics.get(metricName));
- }
+ for (String metricName : formattedMetrics.keySet()) {
+ fileWriter.write(metricName, filePrefix, formattedMetrics.get(metricName));
}
}
}
diff --git a/samples/metrics-write-file-sample/metrics-write-file/src/test/java/io/servicecomb/samples/mwf/TestWriteFile.java b/samples/metrics-write-file-sample/metrics-write-file/src/test/java/io/servicecomb/samples/mwf/TestWriteFile.java
new file mode 100644
index 0000000..f3b089b
--- /dev/null
+++ b/samples/metrics-write-file-sample/metrics-write-file/src/test/java/io/servicecomb/samples/mwf/TestWriteFile.java
@@ -0,0 +1,158 @@
+/*
+ * 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 io.servicecomb.samples.mwf;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+import io.servicecomb.metrics.common.CallMetric;
+import io.servicecomb.metrics.common.ConsumerInvocationMetric;
+import io.servicecomb.metrics.common.RegistryMetric;
+import io.servicecomb.metrics.common.SystemMetric;
+import io.servicecomb.metrics.common.TimerMetric;
+import io.servicecomb.metrics.core.publish.DataSource;
+import io.servicecomb.serviceregistry.Features;
+import io.servicecomb.serviceregistry.RegistryUtils;
+import io.servicecomb.serviceregistry.ServiceRegistry;
+import io.servicecomb.serviceregistry.api.registry.Microservice;
+import io.servicecomb.serviceregistry.api.registry.MicroserviceInstance;
+import io.servicecomb.serviceregistry.cache.InstanceCacheManager;
+import io.servicecomb.serviceregistry.client.ServiceRegistryClient;
+import io.servicecomb.serviceregistry.consumer.AppManager;
+import mockit.Expectations;
+
+public class TestWriteFile {
+
+ @Test
+ public void test() {
+
+ new Expectations(RegistryUtils.class) {
+ {
+ RegistryUtils.getServiceRegistry();
+ result = new ServiceRegistry() {
+ @Override
+ public void init() {
+
+ }
+
+ @Override
+ public void run() {
+
+ }
+
+ @Override
+ public void destroy() {
+
+ }
+
+ @Override
+ public Set<String> getCombinedMicroserviceNames() {
+ return null;
+ }
+
+ @Override
+ public Microservice getMicroservice() {
+ return null;
+ }
+
+ @Override
+ public MicroserviceInstance getMicroserviceInstance() {
+ return null;
+ }
+
+ @Override
+ public ServiceRegistryClient getServiceRegistryClient() {
+ return null;
+ }
+
+ @Override
+ public AppManager getAppManager() {
+ return null;
+ }
+
+ @Override
+ public InstanceCacheManager getInstanceCacheManager() {
+ return null;
+ }
+
+ @Override
+ public List<MicroserviceInstance> findServiceInstance(String appId, String microserviceName,
+ String microserviceVersionRule) {
+ return null;
+ }
+
+ @Override
+ public boolean updateMicroserviceProperties(Map<String, String> properties) {
+ return false;
+ }
+
+ @Override
+ public boolean updateInstanceProperties(Map<String, String> instanceProperties) {
+ return false;
+ }
+
+ @Override
+ public Microservice getRemoteMicroservice(String microserviceId) {
+ return null;
+ }
+
+ @Override
+ public Features getFeatures() {
+ return null;
+ }
+ };
+ }
+ };
+
+ StringBuilder builder = new StringBuilder();
+
+ MetricsFileWriter writer = (loggerName, filePrefix, content) ->
+ builder.append(loggerName).append(filePrefix).append(content);
+
+ SystemMetric systemMetric = new SystemMetric(50, 10, 1, 2, 3,
+ 4, 5, 6, 7, 8);
+
+ Map<String, ConsumerInvocationMetric> consumerInvocationMetricMap = new HashMap<>();
+ consumerInvocationMetricMap.put("A", new ConsumerInvocationMetric("A", "A",
+ new TimerMetric("A1", 1, 2, 3, 4), new CallMetric("A2", 100, 999.44444)));
+
+ consumerInvocationMetricMap.put("B", new ConsumerInvocationMetric("B", "B",
+ new TimerMetric("B1", 1, 2, 3, 4), new CallMetric("B2", 100, 888.66666)));
+
+ RegistryMetric metric = new RegistryMetric(systemMetric, consumerInvocationMetricMap, new HashMap<>());
+
+ DataSource dataSource = Mockito.mock(DataSource.class);
+ Mockito.when(dataSource.getRegistryMetric()).thenReturn(metric);
+
+ WriteFileInitializer writeFileInitializer = new WriteFileInitializer(writer, dataSource,
+ "localhost", "appId.serviceName");
+
+ writeFileInitializer.run();
+
+ String sb = builder.toString();
+
+ Assert.assertTrue(sb.contains("999.4"));
+ Assert.assertTrue(sb.contains("888.7"));
+ }
+}
--
To stop receiving notification emails like this one, please contact
"commits@servicecomb.apache.org" <co...@servicecomb.apache.org>.