在 Maven 中添加 InfluxDB 和 Logback 的依赖项:
xml
Copy code
<dependency>
<groupId>org.influxdb</groupId>
<artifactId>influxdb-java</artifactId>
<version>2.20</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
在 application.properties 文件中添加 InfluxDB 的配置:
properties
Copy code
influxdb.url=http://localhost:8086
influxdb.username=myusername
influxdb.password=mypassword
influxdb.database=mydatabase
influxdb.retentionPolicy=autogen
创建一个 InfluxDB 的配置类,用于从 application.properties 文件中读取 InfluxDB 的配置信息:
java
Copy code
@Configuration
@ConfigurationProperties(prefix = "influxdb")
public class InfluxDBConfig {
private String url;
private String username;
private String password;
private String database;
private String retentionPolicy;
// getters and setters
}
创建一个 InfluxDB 的连接池类,用于管理 InfluxDB 的连接:
java
Copy code
@Component
public class InfluxDBPool {
private final InfluxDBConfig config;
private final InfluxDB influxDB;
private final ThreadLocal<InfluxDB> threadLocal = new ThreadLocal<>();
public InfluxDBPool(InfluxDBConfig config) {
this.config = config;
this.influxDB = InfluxDBFactory.connect(config.getUrl(), config.getUsername(), config.getPassword());
this.influxDB.setDatabase(config.getDatabase());
this.influxDB.setRetentionPolicy(config.getRetentionPolicy());
}
public InfluxDB getConnection() {
InfluxDB conn = threadLocal.get();
if (conn == null) {
conn = influxDB;
threadLocal.set(conn);
}
return conn;
}
public void releaseConnection() {
threadLocal.remove();
}
}
创建一个 Logback 的 Appender 类,用于将日志数据存储到 InfluxDB 中:
java
Copy code
public class InfluxDBAppender extends AppenderBase<ILoggingEvent> {
private final InfluxDBPool pool;
private final String measurement;
public InfluxDBAppender(InfluxDBPool pool, String measurement) {
this.pool = pool;
this.measurement = measurement;
}
@Override
protected void append(ILoggingEvent event) {
InfluxDB conn = null;
try {
conn = pool.getConnection();
conn.write(Point.measurement(measurement)
.time(event.getTimeStamp(), TimeUnit.MILLISECONDS)
.addField("level", event.getLevel().levelStr)
.addField("logger", event.getLoggerName())
.addField("thread", event.getThreadName())
.addField("message", event.getFormattedMessage())
.build());
} finally {
if (conn != null) {
pool.releaseConnection();
}
}
在 logback.xml 中添加 InfluxDBAppender 的配置:
xml
Copy code
<configuration>
<appender name="influxdb" class="com.example.appender.InfluxDBAppender">
<pool>
<config>
<url>${influxdb.url}</url>
<username>${influxdb.username}</username>
<password>${influxdb.password}</password>
<database>${influxdb.database}</database>
<retentionPolicy>${influxdb.retentionPolicy}</retentionPolicy>
</config>
</pool>
<measurement>log</measurement>
</appender>
<root level="INFO">
<appender-ref ref="influxdb"/>
</root>
</configuration>
启动应用程序,并通过日志输出一些日志数据:
java
Copy code
@RestController
public class HelloController {
private final Logger logger = LoggerFactory.getLogger(HelloController.class);
@GetMapping("/hello")
public String hello() {
logger.info("Hello, world!");
return "Hello, world!";
}
}
登录 InfluxDB 并查询存储的日志数据:
bash
Copy code
$ influx
Connected to http://localhost:8086 version 2.0.9
InfluxDB shell version: 2.0.9
> use mydatabase
Using orgid 0000000000000001
Using bucket id=bucket-id
> select * from log
name: log
time level logger message thread
---- ----- ------ ------- ------
1658936500948 INFO hello.HelloController Hello, world! http-nio-8080-exec-1
在 Grafana 中创建一个 Dashboard,并添加一个 InfluxDB 的数据源,使用 InfluxQL 查询语言来查询存储的日志数据。
一个日志应用的例子可以是将应用程序的日志数据存储到 InfluxDB
猜你喜欢
转载自blog.csdn.net/heihei_100/article/details/130732058
今日推荐
周排行