Commit 7498b554 authored by Node- 门 忠鑫's avatar Node- 门 忠鑫

Merge branch 'enochAgent' into 'master'

Enoch agent



See merge request !1
parents cf71cf88 8404fc51
......@@ -5,7 +5,7 @@
<parent>
<artifactId>commons-parent</artifactId>
<groupId>cn.quantgroup</groupId>
<version>0.2.5.1</version>
<version>0.2.5.4</version>
</parent>
<modelVersion>4.0.0</modelVersion>
......
......@@ -5,7 +5,7 @@
<parent>
<artifactId>commons-parent</artifactId>
<groupId>cn.quantgroup</groupId>
<version>0.2.5.1</version>
<version>0.2.5.4</version>
</parent>
<modelVersion>4.0.0</modelVersion>
......
......@@ -5,7 +5,7 @@
<parent>
<artifactId>commons-parent</artifactId>
<groupId>cn.quantgroup</groupId>
<version>0.2.5.1</version>
<version>0.2.5.4</version>
</parent>
<modelVersion>4.0.0</modelVersion>
......
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>commons-parent</artifactId>
<groupId>cn.quantgroup</groupId>
<version>0.2.5.4</version>
</parent>
<groupId>cn.quantgroup</groupId>
<artifactId>enoch-agent-spring-boot-starter</artifactId>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
<version>2.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>cn.quantgroup</groupId>
<artifactId>commons-spring</artifactId>
</dependency>
</dependencies>
</project>
package cn.quantgroup.tech.enoch.agent;
import cn.quantgroup.tech.enoch.agent.report.IReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* @author: amen
* @date: 2019-02-26
*/
@Lazy
@Component
@EnableScheduling
public class AgentReportTask {
@Autowired
private IReportService reportService;
/**
* 1毫秒采集一次数据
*/
@Scheduled(fixedRate = 1000)
public void collect() {
reportService.collect();
}
/**
* 1毫秒上报一次数据
*/
@Scheduled(fixedRate = 1000)
public void report() {
reportService.report();
}
}
\ No newline at end of file
package cn.quantgroup.tech.enoch.agent.configuration;
import cn.quantgroup.tech.enoch.agent.AgentReportTask;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.serialization.StringSerializer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.core.ProducerFactory;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import java.util.HashMap;
import java.util.concurrent.ScheduledThreadPoolExecutor;
/**
* @author: amen
* @date: 2019-02-26
*/
@Slf4j
@Configuration
@ComponentScan(basePackageClasses = AgentReportTask.class)
@EnableConfigurationProperties(EnochAgentProperties.class)
@ConditionalOnProperty(prefix = "tech.enoch", name = "enabled", havingValue = "true")
public class EnochAgentAutoConfiguration {
@Autowired
private EnochAgentProperties enochAgentProperties;
@Autowired
private AgentReportTask agentReportTask;
@Bean
public ProducerFactory<String, String> producerFactory() {
HashMap<String, Object> config = new HashMap<>(16);
config.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, enochAgentProperties.getKafkaHost());
config.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
config.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
return new DefaultKafkaProducerFactory(config);
}
@Bean
@ConditionalOnBean(ProducerFactory.class)
public KafkaTemplate<String, String> kafkaTemplate(ProducerFactory<String, String> producerFactory) {
return new KafkaTemplate<>(producerFactory);
}
@Configuration
public static class ScheduleConfig implements SchedulingConfigurer {
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(2);
taskRegistrar.setScheduler(executor);
}
}
}
\ No newline at end of file
package cn.quantgroup.tech.enoch.agent.configuration;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* @author: amen
* @date: 2019-02-26
*/
@Data
@ConfigurationProperties(prefix = "tech.enoch")
public class EnochAgentProperties {
private String kafkaHost;
private String kafkaTopic;
}
package cn.quantgroup.tech.enoch.agent.report;
/**
* @author: amen
* @date: 2019-02-26
*/
public interface IReportService {
/**
* 上报节点信息
*
* @return
*/
void report();
/**
* 采集节点信息
*/
void collect();
}
package cn.quantgroup.tech.enoch.agent.report.impl;
import cn.quantgroup.tech.enoch.agent.configuration.EnochAgentProperties;
import cn.quantgroup.tech.enoch.agent.report.IReportService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.endpoint.MetricReaderPublicMetrics;
import org.springframework.boot.actuate.endpoint.MetricsEndpoint;
import org.springframework.boot.actuate.endpoint.mvc.EnvironmentMvcEndpoint;
import org.springframework.boot.actuate.endpoint.mvc.HealthMvcEndpoint;
import org.springframework.boot.actuate.health.Health;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
/**
* @author: amen
* @date: 2019-02-26
*/
@Slf4j
@Service
public class ReportServiceImpl implements IReportService {
@Autowired
private HealthMvcEndpoint healthMvcEndpoint;
@Autowired
private MetricsEndpoint metricsEndpoint;
@Autowired
private KafkaTemplate<String, String> kafkaTemplate;
@Autowired
private EnochAgentProperties enochAgentProperties;
@Autowired
private EnvironmentMvcEndpoint environmentMvcEndpoint;
@Autowired
private MetricReaderPublicMetrics metricReaderPublicMetrics;
private final ArrayBlockingQueue<Map<String, Object>> arrayBlockingQueue = new ArrayBlockingQueue<>(600);
private final ApplicationInfo applicationInfo = new ApplicationInfo();
@Override
public void report() {
int size = arrayBlockingQueue.size();
if (size <= 0) {
return;
}
List<Map<String, Object>> slice = new ArrayList<>(size);
arrayBlockingQueue.drainTo(slice, size);
Map<String, Object> pushPackage = new HashMap<>(6);
if (StringUtils.isBlank(applicationInfo.getAppName())) {
initApplicationInfo();
}
pushPackage.put("ip", applicationInfo.getIp());
pushPackage.put("appName", applicationInfo.getAppName());
pushPackage.put("endPoints", slice);
//todo 若发kafka失败,添加本地持久化操作,待下次提交时,一同push
kafkaTemplate.send(enochAgentProperties.getKafkaTopic(), JSON.toJSONString(pushPackage, SerializerFeature.DisableCircularReferenceDetect));
}
@Override
public void collect() {
HashMap<String, Object> endPoints = new HashMap<>(16);
Health health = healthMvcEndpoint.getDelegate().invoke();
Health.status(health.getStatus()).build();
endPoints.put("health", health);
endPoints.put("metrics", metricsEndpoint.invoke());
endPoints.put("timestamp", System.currentTimeMillis());
try {
arrayBlockingQueue.put(endPoints);
} catch (InterruptedException e) {
log.error("enoch.agent collect data error: {}", e);
}
}
private ApplicationInfo initApplicationInfo() {
Map<String, String> appName = (HashMap) environmentMvcEndpoint.value("spring.application.name");
Map<String, String> ip = (HashMap) environmentMvcEndpoint.value("spring.cloud.client.ipAddress");
applicationInfo.setAppName(appName.get("spring.application.name"));
applicationInfo.setIp(ip.get("spring.cloud.client.ipAddress"));
return applicationInfo;
}
@PostConstruct
public void init(){
//注释掉不需要收集器
metricsEndpoint.unregisterPublicMetrics(metricReaderPublicMetrics);
}
@Data
public static class ApplicationInfo {
private String ip;
private String appName;
}
}
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
cn.quantgroup.tech.enoch.agent.configuration.EnochAgentAutoConfiguration
......@@ -5,7 +5,7 @@
<parent>
<artifactId>commons-parent</artifactId>
<groupId>cn.quantgroup</groupId>
<version>0.2.5.1</version>
<version>0.2.5.4</version>
</parent>
<modelVersion>4.0.0</modelVersion>
......
{
"lockfileVersion": 1
}
......@@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>cn.quantgroup</groupId>
<artifactId>commons-parent</artifactId>
<version>0.2.5.1</version>
<version>0.2.5.4</version>
<parent>
<groupId>org.springframework.boot</groupId>
......@@ -20,6 +20,7 @@
<module>shutdown-spring-boot-starter</module>
<module>brave-spring-boot-starter</module>
<module>idgenerator-spring-boot-starter</module>
<module>enoch-agent-spring-boot-starter</module>
</modules>
<packaging>pom</packaging>
......@@ -33,7 +34,7 @@
<lombok.version>1.16.20</lombok.version>
<guava.version>23.0</guava.version>
<apollo.client.version>0.10.2</apollo.client.version>
<common.parent.version>0.2.5.1</common.parent.version>
<common.parent.version>0.2.5.4</common.parent.version>
</properties>
<dependencies>
......@@ -89,6 +90,11 @@
<artifactId>idgenerator-spring-boot-starter</artifactId>
<version>${common.parent.version}</version>
</dependency>
<dependency>
<groupId>cn.quantgroup</groupId>
<artifactId>enoch-agent-spring-boot-starter</artifactId>
<version>${common.parent.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
......
......@@ -5,7 +5,7 @@
<parent>
<artifactId>commons-parent</artifactId>
<groupId>cn.quantgroup</groupId>
<version>0.2.5.1</version>
<version>0.2.5.4</version>
</parent>
<modelVersion>4.0.0</modelVersion>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment