feat(check): 增加环境检验工具
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
package com.lanyuanxiaoyao.service.check;
|
||||
|
||||
import com.lanyuanxiaoyao.service.check.actions.Checker;
|
||||
import java.util.Map;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
/**
|
||||
* 启动类
|
||||
*
|
||||
* @author lanyuanxiaoyao
|
||||
* @date 2024-02-22
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class CheckApplication implements ApplicationRunner {
|
||||
private static final Logger logger = LoggerFactory.getLogger(CheckApplication.class);
|
||||
private final ApplicationContext applicationContext;
|
||||
|
||||
public CheckApplication(ApplicationContext applicationContext) {this.applicationContext = applicationContext;}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(CheckApplication.class, args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(ApplicationArguments args) throws Exception {
|
||||
Map<String, Checker> beans = applicationContext.getBeansOfType(Checker.class);
|
||||
for (Checker checker : beans.values()) {
|
||||
logger.info("Execute checker: {}", checker.description());
|
||||
checker.check();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.lanyuanxiaoyao.service.check.actions;
|
||||
|
||||
import cn.hutool.core.net.NetUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* 检查
|
||||
*
|
||||
* @author lanyuanxiaoyao
|
||||
* @date 2024-02-22
|
||||
*/
|
||||
public abstract class Checker {
|
||||
private static final Logger logger = LoggerFactory.getLogger(Checker.class);
|
||||
|
||||
public abstract void check() throws Exception;
|
||||
|
||||
public abstract String description();
|
||||
|
||||
protected boolean ping(String url) {
|
||||
String[] split = url.split(":");
|
||||
if (split.length != 2) {
|
||||
logger.error("Error url {}", url);
|
||||
return false;
|
||||
}
|
||||
InetSocketAddress address = NetUtil.createAddress(split[0], Integer.parseInt(split[1]));
|
||||
try {
|
||||
String command = StrUtil.format("ping {} -c 2 -w 2 -q", address.getHostString());
|
||||
Process exec = Runtime.getRuntime().exec(command);
|
||||
if (exec.waitFor(5, TimeUnit.SECONDS)) {
|
||||
return exec.exitValue() == 0;
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.lanyuanxiaoyao.service.check.actions;
|
||||
|
||||
import com.lanyuanxiaoyao.service.check.configuration.MysqlConfigurationProperties;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.Statement;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author lanyuanxiaoyao
|
||||
* @date 2024-02-22
|
||||
*/
|
||||
@Component
|
||||
public class MysqlChecker extends Checker {
|
||||
private static final Logger logger = LoggerFactory.getLogger(MysqlChecker.class);
|
||||
|
||||
private final MysqlConfigurationProperties mysqlConfigurationProperties;
|
||||
|
||||
public MysqlChecker(MysqlConfigurationProperties mysqlConfigurationProperties) {this.mysqlConfigurationProperties = mysqlConfigurationProperties;}
|
||||
|
||||
@Override
|
||||
public void check() throws Exception {
|
||||
Class.forName("com.mysql.cj.jdbc.Driver");
|
||||
for (MysqlConfigurationProperties.MysqlInfo target : mysqlConfigurationProperties.getTargets()) {
|
||||
try (Connection connection = DriverManager.getConnection(target.getUrl(), target.getUsername(), target.getPassword())) {
|
||||
if (connection.isClosed()) {
|
||||
logger.warn("Database connect failure {}", target.getUrl());
|
||||
continue;
|
||||
}
|
||||
try (Statement statement = connection.createStatement()) {
|
||||
try (ResultSet resultSet = statement.executeQuery("select version()")) {
|
||||
if (resultSet.next()) {
|
||||
logger.info("Database version {}", resultSet.getString(1));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String description() {
|
||||
return "Check mysql";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package com.lanyuanxiaoyao.service.check.actions;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.lanyuanxiaoyao.service.configuration.entity.pulsar.PulsarInfo;
|
||||
import org.apache.pulsar.client.admin.PulsarAdmin;
|
||||
import org.apache.pulsar.client.api.PulsarClient;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 主机相关检查
|
||||
*
|
||||
* @author lanyuanxiaoyao
|
||||
* @date 2024-02-22
|
||||
*/
|
||||
@Component
|
||||
public class PulsarChecker extends Checker {
|
||||
private static final Logger logger = LoggerFactory.getLogger(PulsarChecker.class);
|
||||
|
||||
private void checkConnect(String url) {
|
||||
if (!ping(url)) {
|
||||
logger.warn("{} ping failure", url);
|
||||
}
|
||||
}
|
||||
|
||||
private String adminUrl(PulsarInfo info) {
|
||||
return StrUtil.format("http://{}/admin/v2", info.getAdmin());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void check() throws Exception {
|
||||
for (PulsarInfo pulsarInfo : PulsarInfo.DEFAULT_INFOS) {
|
||||
logger.info("Check pulsar {}", pulsarInfo.getName());
|
||||
if (StrUtil.isNotBlank(pulsarInfo.getAdmin())) {
|
||||
logger.info("Check pulsar admin {}", pulsarInfo.getAdmin());
|
||||
if (ping(pulsarInfo.getAdmin())) {
|
||||
// noinspection EmptyTryBlock
|
||||
try (PulsarAdmin ignored = PulsarAdmin.builder().serviceHttpUrl(adminUrl(pulsarInfo)).build()) {
|
||||
} catch (Exception exception) {
|
||||
logger.warn("Pulsar admin login failure", exception);
|
||||
}
|
||||
} else {
|
||||
logger.warn("{} ping failure", pulsarInfo.getAdmin());
|
||||
}
|
||||
} else {
|
||||
logger.warn("Pulsar {} admin is empty", pulsarInfo.getName());
|
||||
}
|
||||
logger.info("Check pulsar nodes");
|
||||
for (String broker : pulsarInfo.getBrokers()) {
|
||||
if (!ping(broker)) {
|
||||
logger.warn("{} ping failure", broker);
|
||||
}
|
||||
}
|
||||
String brokerUrl = "pulsar://" + pulsarInfo.getBrokers().makeString(",");
|
||||
try (PulsarClient client = PulsarClient.builder()
|
||||
.serviceUrl(brokerUrl)
|
||||
.build()) {
|
||||
if (client.isClosed()) {
|
||||
logger.warn("Pulsar client failure connect {}", brokerUrl);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String description() {
|
||||
return "Check Pulsar";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package com.lanyuanxiaoyao.service.check.configuration;
|
||||
|
||||
import java.util.List;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author lanyuanxiaoyao
|
||||
* @date 2024-02-22
|
||||
*/
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "checker.mysql")
|
||||
public class MysqlConfigurationProperties {
|
||||
private List<MysqlInfo> targets;
|
||||
|
||||
public List<MysqlInfo> getTargets() {
|
||||
return targets;
|
||||
}
|
||||
|
||||
public void setTargets(List<MysqlInfo> targets) {
|
||||
this.targets = targets;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "MysqlConfigurationProperties{" +
|
||||
"targets=" + targets +
|
||||
'}';
|
||||
}
|
||||
|
||||
public static final class MysqlInfo {
|
||||
private String url;
|
||||
private String username;
|
||||
private String password;
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "MysqlInfo{" +
|
||||
"url='" + url + '\'' +
|
||||
", username='" + username + '\'' +
|
||||
", password='" + password + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
}
|
||||
11
service-check/src/main/resources/application.yml
Normal file
11
service-check/src/main/resources/application.yml
Normal file
@@ -0,0 +1,11 @@
|
||||
spring:
|
||||
application:
|
||||
name: service-check
|
||||
profiles:
|
||||
include: common
|
||||
checker:
|
||||
mysql:
|
||||
targets:
|
||||
- url: jdbc:mysql://132.121.204.217:17906/hudi_collect_build_2?useSSL=false
|
||||
username: odcp
|
||||
password: wFg_fR492#&
|
||||
17
service-check/src/main/resources/logback-spring.xml
Normal file
17
service-check/src/main/resources/logback-spring.xml
Normal file
@@ -0,0 +1,17 @@
|
||||
<configuration>
|
||||
<conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
|
||||
<conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />
|
||||
<conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" />
|
||||
|
||||
<appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %clr(%5p) %clr([${HOSTNAME}]){yellow} %clr([%t]){magenta} %clr(%logger{40}){cyan} #@# %m%n%wEx</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<logger name="org.apache.pulsar.client" level="WARN"/>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="Console"/>
|
||||
</root>
|
||||
</configuration>
|
||||
Reference in New Issue
Block a user