Commit 101a83f0 authored by Node- 门 忠鑫's avatar Node- 门 忠鑫

# 添加enochAgent

parent cf71cf88
/target/
!.mvn/wrapper/maven-wrapper.jar
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
/build/
<?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.1</version>
</parent>
<groupId>cn.quantgroup</groupId>
<artifactId>enoch-agent-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>enoch-agent-spring-boot-starter</name>
<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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</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.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* @author: amen
* @date: 2019-02-26
*/
@Component
@EnableScheduling
public class TimerTask {
@Autowired
private IReportService reportService;
/**
* 1毫秒采集一次数据
*/
@Scheduled(fixedRate = 1000)
public void collect() {
reportService.collect();
}
/**
* 30毫秒上报一次数据
*/
@Scheduled(fixedRate = 30000)
public void report() {
reportService.report();
}
}
\ No newline at end of file
package cn.quantgroup.tech.enoch.agent.configuration;
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.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
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 java.util.HashMap;
/**
* @author: amen
* @date: 2019-02-26
*/
@Slf4j
@Configuration
@EnableConfigurationProperties({EnochAgentProperties.class})
public class EnochAgentAutoConfiguration {
@Autowired
private EnochAgentProperties enochAgentProperties;
@Bean
@ConditionalOnBean(EnochAgentProperties.class)
@ConditionalOnMissingBean
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);
}
}
\ 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;
private Long reportRate = 30000L;
}
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 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.HealthEndpoint;
import org.springframework.boot.actuate.endpoint.MetricsEndpoint;
import org.springframework.boot.actuate.endpoint.mvc.EnvironmentMvcEndpoint;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
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 HealthEndpoint healthEndpoint;
@Autowired
private MetricsEndpoint metricsEndpoint;
@Autowired
private KafkaTemplate<String, String> kafkaTemplate;
@Autowired
private EnochAgentProperties enochAgentProperties;
@Autowired
private EnvironmentMvcEndpoint environmentMvcEndpoint;
private final ArrayBlockingQueue<Map<String, Object>> arrayBlockingQueue = new ArrayBlockingQueue<>(600);
private final ApplicationInfo applicationInfo = new ApplicationInfo();
@Override
public void report() {
log.info("enoch.agent report");
int size = arrayBlockingQueue.size();
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));
}
@Override
public void collect() {
log.info("enoch.agent collect");
//todo 添加timestamp
HashMap<String, Object> endPoints = new HashMap<>(16);
endPoints.put("health", healthEndpoint.invoke());
endPoints.put("metrics", metricsEndpoint.invoke());
endPoints.put("timestamp", System.nanoTime());
try {
arrayBlockingQueue.put(endPoints);
} catch (InterruptedException e) {
log.error("enoch.agent collect data error: {}", e);
}
}
private ApplicationInfo initApplicationInfo() {
applicationInfo.setAppName(environmentMvcEndpoint.value("spring.application.name").toString());
applicationInfo.setIp(environmentMvcEndpoint.value("spring.cloud.client.ipAddress").toString());
return applicationInfo;
}
@Data
public static class ApplicationInfo {
private String ip;
private String appName;
}
}
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
cn.quantgroup.tech.enoch.agent.configuration.EnochAgentAutoConfiguration
\ No newline at end of file
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
<module>shutdown-spring-boot-starter</module> <module>shutdown-spring-boot-starter</module>
<module>brave-spring-boot-starter</module> <module>brave-spring-boot-starter</module>
<module>idgenerator-spring-boot-starter</module> <module>idgenerator-spring-boot-starter</module>
<module>enoch-agent-spring-boot-starter</module>
</modules> </modules>
<packaging>pom</packaging> <packaging>pom</packaging>
......
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