Ver código fonte

search-service 中延迟初始化 GeoIpTool 和 GeoJson

reghao 2 meses atrás
pai
commit
3816806fda

+ 30 - 8
search/search-service/src/main/java/cn/reghao/tnb/search/app/log/service/NginxLogService.java

@@ -41,20 +41,42 @@ public class NginxLogService {
     private final String timeZone = "+08:00";
     private final NginxLogDocument nginxLogDocument;
     private final SearchService searchService;
-    private final GeoIpTool geoIpTool;
-    private final String geoJson;
+    private final AppProperties appProperties;
     private final SnowFlake idGenerator;
+    private GeoIpTool geoIpTool;
+    private String geoJson;
 
     public NginxLogService(NginxLogDocument nginxLogDocument, SearchService searchService,
-                           GeoIpTool geoIpTool, AppProperties appProperties) {
+                           AppProperties appProperties) {
         this.nginxLogDocument = nginxLogDocument;
         this.searchService = searchService;
-        this.geoIpTool = geoIpTool;
-        String geojsonPath = String.format("%s/%s", appProperties.getBaseDir(), appProperties.getGeojsonFilename());
-        this.geoJson = new TextFile().readFile(geojsonPath);
+        this.appProperties = appProperties;
         this.idGenerator = new SnowFlake(1, 1);
     }
 
+    private GeoIpTool getGeoIpTool() {
+        if (geoIpTool == null) {
+            String geoipPath = String.format("%s/%s", appProperties.getBaseDir(), appProperties.getGeoipFilename());
+            try {
+                this.geoIpTool = new GeoIpTool(geoipPath);
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+        }
+
+        return geoIpTool;
+    }
+
+    private String getGeoJson() {
+        if (geoJson == null) {
+            String geojsonPath = String.format("%s/%s", appProperties.getBaseDir(), appProperties.getGeojsonFilename());
+            geoJson = new TextFile().readFile(geojsonPath);
+        }
+
+        return geoJson;
+    }
+
+
     public void processNginxLogs(List<NginxLog> list) {
         if (list.isEmpty()) {
             return;
@@ -227,7 +249,7 @@ public class NginxLogService {
         Map<String, Map<String, Set<String>>> map = new HashMap<>();
         groupMap1.forEach((key, value) -> {
             String accessIp = key;
-            Location location = geoIpTool.getLocation(accessIp);
+            Location location = getGeoIpTool().getLocation(accessIp);
             String country0 = location.getCountry();
             String[] array = country0.split("–");
             int len = array.length;
@@ -258,7 +280,7 @@ public class NginxLogService {
         });
 
         Map<Integer, String> geoMap = new HashMap<>();
-        JsonObject jsonObject = JsonConverter.jsonToJsonElement(geoJson).getAsJsonObject();
+        JsonObject jsonObject = JsonConverter.jsonToJsonElement(getGeoJson()).getAsJsonObject();
         for (JsonElement jsonElement : jsonObject.get("features").getAsJsonArray()) {
             JsonObject propertiesObject = jsonElement.getAsJsonObject().get("properties").getAsJsonObject();
             int adcode = propertiesObject.get("adcode").getAsInt();

+ 0 - 19
search/search-service/src/main/java/cn/reghao/tnb/search/app/log/util/BeanConfig.java

@@ -1,19 +0,0 @@
-package cn.reghao.tnb.search.app.log.util;
-
-import cn.reghao.tnb.search.app.config.AppProperties;
-import cn.reghao.tnb.search.app.log.geoip.GeoIpTool;
-import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
-
-/**
- * @author reghao
- * @date 2026-01-08 16:56:47
- */
-@Configuration
-public class BeanConfig {
-    @Bean
-    public GeoIpTool ipTool(AppProperties appProperties) throws Exception {
-        String geoipPath = String.format("%s/%s", appProperties.getBaseDir(), appProperties.getGeoipFilename());
-        return new GeoIpTool(geoipPath);
-    }
-}