perf:优化ip2region,改为从服务器下载db文件,而不是打包到项目中
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -4,3 +4,4 @@ rebel.xml
|
||||
/.idea/
|
||||
*.iml
|
||||
/static/
|
||||
/ipdb/
|
||||
|
||||
2
pom.xml
2
pom.xml
@@ -18,7 +18,7 @@
|
||||
</parent>
|
||||
|
||||
<modules>
|
||||
<module>demo</module>
|
||||
<module>tiesheng-ademo</module>
|
||||
<module>tiesheng-db-migration</module>
|
||||
<module>tiesheng-login</module>
|
||||
<module>tiesheng-web</module>
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
<version>0.0.6</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>demo</artifactId>
|
||||
<artifactId>tiesheng-ademo</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
@@ -1,10 +1,15 @@
|
||||
package com.tiesheng.demo.controller;
|
||||
|
||||
import cn.hutool.log.LogFactory;
|
||||
import com.tiesheng.annotation.token.TokenIgnore;
|
||||
import com.tiesheng.util.ip2region.DataBlock;
|
||||
import com.tiesheng.util.ip2region.Ip2Region;
|
||||
import com.tiesheng.util.pojos.ApiResp;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @author hao
|
||||
*/
|
||||
@@ -15,6 +15,7 @@ import com.tiesheng.platform.config.ding.bean.DingUserInfo;
|
||||
import com.tiesheng.platform.config.wxmp.PlatformWxmpConfig;
|
||||
import com.tiesheng.platform.config.wxmp.bean.WxJsapiSignature;
|
||||
import com.tiesheng.platform.config.wxmp.bean.WxUserInfo;
|
||||
import com.tiesheng.util.config.GlobalConfig;
|
||||
import com.tiesheng.util.exception.ApiException;
|
||||
import com.tiesheng.util.pojos.ApiResp;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -42,6 +43,8 @@ public class LoginController {
|
||||
LoginService loginService;
|
||||
@Autowired
|
||||
TsTokenConfig tsTokenConfig;
|
||||
@Autowired
|
||||
GlobalConfig globalConfig;
|
||||
|
||||
|
||||
/**
|
||||
@@ -105,7 +108,7 @@ public class LoginController {
|
||||
map.put("extra", extra);
|
||||
loginService.checkLoginConfigurer(s -> {
|
||||
String query = URLUtil.buildQuery(map, Charset.defaultCharset());
|
||||
String configUrl = loginService.buildPath("/ding/index.html?" + query);
|
||||
String configUrl = globalConfig.buildPath("/ding/index.html?" + query);
|
||||
try {
|
||||
response.sendRedirect(configUrl);
|
||||
} catch (IOException ignored) {
|
||||
@@ -162,7 +165,7 @@ public class LoginController {
|
||||
if (StrUtil.isEmpty(extra)) {
|
||||
extra = "";
|
||||
}
|
||||
String configUrl = loginService.buildPath("/auth/wxmp/oauth2/" + service + "?extra=" + extra);
|
||||
String configUrl = globalConfig.buildPath("/auth/wxmp/oauth2/" + service + "?extra=" + extra);
|
||||
String authorizationUrl = platformWxmpConfig.buildAuthorizationUrl(service, configUrl, "snsapi_userinfo");
|
||||
response.sendRedirect(authorizationUrl);
|
||||
}
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
package com.tiesheng.login.service;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import com.tiesheng.login.config.TieshengLoginConfigurer;
|
||||
import com.tiesheng.util.exception.ApiException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
@@ -16,8 +13,6 @@ import java.util.function.Consumer;
|
||||
@Service
|
||||
public class LoginService {
|
||||
|
||||
@Value("${tiesheng.global.host}")
|
||||
String globalHost;
|
||||
@Autowired(required = false)
|
||||
TieshengLoginConfigurer tieshengLoginConfigurer;
|
||||
|
||||
@@ -32,26 +27,4 @@ public class LoginService {
|
||||
consumer.accept(tieshengLoginConfigurer);
|
||||
}
|
||||
|
||||
public String getContextPath() {
|
||||
String context = SpringUtil.getProperty("server.servlet.context-path");
|
||||
if (StrUtil.isEmpty(context)) {
|
||||
context = "";
|
||||
}
|
||||
|
||||
return context;
|
||||
}
|
||||
|
||||
public String buildPath(String path) {
|
||||
if (StrUtil.isEmpty(path)) {
|
||||
path = "";
|
||||
}
|
||||
|
||||
if (!StrUtil.isEmpty(this.globalHost) && !StrUtil.startWith(path, "http")) {
|
||||
path = this.globalHost + this.getContextPath() + path;
|
||||
}
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.tiesheng.util;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
/**
|
||||
* @author hao
|
||||
*/
|
||||
@ComponentScan({
|
||||
"com.tiesheng.util.**.*",
|
||||
})
|
||||
@ComponentScan("cn.hutool.extra.spring")
|
||||
public class UtilAutoConfigurer {
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.tiesheng.core.config.global;
|
||||
package com.tiesheng.util.config;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
@@ -0,0 +1,68 @@
|
||||
package com.tiesheng.util.config;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import cn.hutool.log.LogFactory;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.RandomAccessFile;
|
||||
|
||||
/**
|
||||
* @author hao
|
||||
*/
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "tiesheng.ip2region")
|
||||
public class Ip2regionConfig {
|
||||
|
||||
private String dbUrl = "http://git.kepai365.com/zeng_wenhao/kepai-repo/raw/master/ipdb/ip2region.db";
|
||||
private String dbPath = System.getProperty("user.dir") + "/ipdb/ip2region.db";
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// 逻辑方法
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
|
||||
@PostConstruct
|
||||
public void downloadDbFile() {
|
||||
if (!FileUtil.exist(dbPath)) {
|
||||
LogFactory.get().info("download ip2region file start");
|
||||
HttpUtil.downloadFile(dbUrl, dbPath);
|
||||
LogFactory.get().info("download ip2region file finish");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取db文件
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public RandomAccessFile getDbAccessFile() throws FileNotFoundException {
|
||||
if (!FileUtil.exist(dbPath)) {
|
||||
downloadDbFile();
|
||||
}
|
||||
return new RandomAccessFile(dbPath, "r");
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// setter\getter
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
|
||||
public String getDbUrl() {
|
||||
return dbUrl;
|
||||
}
|
||||
|
||||
public void setDbUrl(String dbUrl) {
|
||||
this.dbUrl = dbUrl;
|
||||
}
|
||||
|
||||
public String getDbPath() {
|
||||
return dbPath;
|
||||
}
|
||||
|
||||
public void setDbPath(String dbPath) {
|
||||
this.dbPath = dbPath;
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,9 @@
|
||||
package com.tiesheng.util.ip2region;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.io.resource.ClassPathResource;
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import com.tiesheng.util.config.Ip2regionConfig;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.RandomAccessFile;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
@@ -51,15 +48,8 @@ public class Ip2Region {
|
||||
public Ip2Region() {
|
||||
this.dbConfig = new DbConfig();
|
||||
try {
|
||||
ClassPathResource cpr = new ClassPathResource("ipdb/ip2region.db");
|
||||
InputStream inputStream = cpr.getStream();
|
||||
File tempFile = File.createTempFile("ip2region", ".db");
|
||||
try {
|
||||
FileUtil.writeFromStream(inputStream, tempFile);
|
||||
} finally {
|
||||
IoUtil.close(inputStream);
|
||||
}
|
||||
raf = new RandomAccessFile(tempFile, "r");
|
||||
Ip2regionConfig ip2regionConfig = SpringUtil.getBean(Ip2regionConfig.class);
|
||||
raf = ip2regionConfig.getDbAccessFile();
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
Binary file not shown.
@@ -2,6 +2,7 @@ package com.tiesheng.core;
|
||||
|
||||
import com.tiesheng.login.LoginAutoImportSelector;
|
||||
import com.tiesheng.migration.MigrationAutoImportSelector;
|
||||
import com.tiesheng.util.UtilAutoConfigurer;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
@@ -14,10 +15,10 @@ import java.lang.annotation.*;
|
||||
@Target({ElementType.TYPE})
|
||||
@Documented
|
||||
@Import({
|
||||
UtilAutoConfigurer.class,
|
||||
CoreAutoImportSelector.class,
|
||||
LoginAutoImportSelector.class,
|
||||
MigrationAutoImportSelector.class
|
||||
})
|
||||
@ComponentScan("cn.hutool.extra.spring")
|
||||
public @interface EnableTieshengWeb {
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user