refactor: store runtime identity and mileage in mysql metrics

This commit is contained in:
lingniu
2026-07-01 01:56:16 +08:00
parent b1190cd7c7
commit 0fc91f512c
23 changed files with 75 additions and 132 deletions

View File

@@ -1,8 +1,6 @@
package com.lingniu.ingest.identity.config;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lingniu.ingest.identity.FileVehicleIdentityService;
import com.lingniu.ingest.identity.InMemoryVehicleIdentityService;
import com.lingniu.ingest.identity.MySqlVehicleIdentityService;
import com.lingniu.ingest.identity.VehicleIdentityRegistry;
import com.lingniu.ingest.identity.VehicleIdentityResolver;
@@ -14,7 +12,6 @@ import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import javax.sql.DataSource;
import java.nio.file.Path;
@AutoConfiguration
@EnableConfigurationProperties(VehicleIdentityProperties.class)
@@ -28,16 +25,8 @@ public class VehicleIdentityAutoConfiguration {
@Bean
@ConditionalOnMissingBean({VehicleIdentityResolver.class, VehicleIdentityRegistry.class})
@ConditionalOnProperty(prefix = "lingniu.ingest.identity", name = "store", havingValue = "file")
public FileVehicleIdentityService fileVehicleIdentityService(VehicleIdentityProperties properties,
ObjectMapper objectMapper) {
// 文件实现同时提供 resolver 和 registry协议层既能查绑定也能在注册/鉴权时补写绑定。
return new FileVehicleIdentityService(Path.of(properties.getFile().getPath()), objectMapper);
}
@Bean
@ConditionalOnMissingBean({VehicleIdentityResolver.class, VehicleIdentityRegistry.class})
@ConditionalOnProperty(prefix = "lingniu.ingest.identity", name = "store", havingValue = "mysql")
@ConditionalOnProperty(prefix = "lingniu.ingest.identity", name = "store", havingValue = "mysql",
matchIfMissing = true)
public MySqlVehicleIdentityService mysqlVehicleIdentityService(VehicleIdentityProperties properties,
ObjectMapper objectMapper) {
VehicleIdentityProperties.Mysql mysql = properties.getMysql();
@@ -45,12 +34,4 @@ public class VehicleIdentityAutoConfiguration {
mysql.getJdbcUrl(), mysql.getUsername(), mysql.getPassword());
return new MySqlVehicleIdentityService(dataSource, mysql, objectMapper);
}
@Bean
@ConditionalOnMissingBean({VehicleIdentityResolver.class, VehicleIdentityRegistry.class})
@ConditionalOnProperty(prefix = "lingniu.ingest.identity", name = "store", havingValue = "memory",
matchIfMissing = true)
public InMemoryVehicleIdentityService vehicleIdentityService() {
return new InMemoryVehicleIdentityService();
}
}

View File

@@ -5,24 +5,14 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "lingniu.ingest.identity")
public class VehicleIdentityProperties {
private String store = "memory";
private File file = new File();
private String store = "mysql";
private Mysql mysql = new Mysql();
public String getStore() { return store; }
public void setStore(String store) { this.store = store; }
public File getFile() { return file; }
public void setFile(File file) { this.file = file; }
public Mysql getMysql() { return mysql; }
public void setMysql(Mysql mysql) { this.mysql = mysql; }
public static class File {
private String path = "./data/vehicle-identity.jsonl";
public String getPath() { return path; }
public void setPath(String path) { this.path = path; }
}
public static class Mysql {
private String jdbcUrl = "jdbc:mysql://127.0.0.1:3306/lingniu_vehicle?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai";
private String username = "root";

View File

@@ -3,42 +3,31 @@ package com.lingniu.ingest.identity.config;
import com.lingniu.ingest.identity.VehicleIdentityRegistry;
import com.lingniu.ingest.identity.VehicleIdentityResolver;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import java.nio.file.Path;
import static org.assertj.core.api.Assertions.assertThat;
class VehicleIdentityAutoConfigurationTest {
@TempDir
Path tempDir;
private final ApplicationContextRunner runner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(VehicleIdentityAutoConfiguration.class));
@Test
void createsDefaultIdentityService() {
runner.run(context -> {
assertThat(context).hasSingleBean(VehicleIdentityResolver.class);
assertThat(context).hasSingleBean(VehicleIdentityRegistry.class);
assertThat(context.getBean(VehicleIdentityResolver.class))
.isSameAs(context.getBean(VehicleIdentityRegistry.class));
});
}
@Test
void createsFileIdentityServiceWhenConfigured() {
void createsMysqlIdentityServiceByDefault() {
runner
.withPropertyValues(
"lingniu.ingest.identity.store=file",
"lingniu.ingest.identity.file.path=" + tempDir.resolve("identity.jsonl"))
"lingniu.ingest.identity.mysql.jdbc-url=jdbc:h2:mem:auto-identity-default;MODE=MySQL;DATABASE_TO_UPPER=false;DB_CLOSE_DELAY=-1",
"lingniu.ingest.identity.mysql.username=sa",
"lingniu.ingest.identity.mysql.password=",
"lingniu.ingest.identity.mysql.table-name=vehicle_identity_bindings")
.run(context -> {
assertThat(context).hasSingleBean(VehicleIdentityResolver.class);
assertThat(context).hasSingleBean(VehicleIdentityRegistry.class);
assertThat(context.getBean(VehicleIdentityResolver.class))
.isSameAs(context.getBean(VehicleIdentityRegistry.class));
assertThat(context.getBean(VehicleIdentityResolver.class).getClass().getSimpleName())
.isEqualTo("FileVehicleIdentityService");
.isEqualTo("MySqlVehicleIdentityService");
});
}
@@ -60,4 +49,14 @@ class VehicleIdentityAutoConfigurationTest {
.isEqualTo("MySqlVehicleIdentityService");
});
}
@Test
void unsupportedStoreModeDoesNotCreateRuntimeFallback() {
runner
.withPropertyValues("lingniu.ingest.identity.store=file")
.run(context -> {
assertThat(context).doesNotHaveBean(VehicleIdentityResolver.class);
assertThat(context).doesNotHaveBean(VehicleIdentityRegistry.class);
});
}
}