@@ -1,65 +0,0 @@ | |||
package com.telpo.dipperposition.config.db; | |||
import org.springframework.beans.factory.annotation.Autowired; | |||
import org.springframework.context.annotation.Bean; | |||
import org.springframework.data.mongodb.MongoDbFactory; | |||
import org.springframework.data.mongodb.core.SimpleMongoClientDbFactory; | |||
import org.springframework.stereotype.Component; | |||
import org.springframework.util.CollectionUtils; | |||
import javax.annotation.PostConstruct; | |||
import java.util.HashMap; | |||
import java.util.Iterator; | |||
import java.util.Map; | |||
/** | |||
* @program: DataPushServer | |||
* @description: mongdb数据库连接上下文 | |||
* @author: linwl | |||
* @create: 2020-07-11 14:31 | |||
*/ | |||
@Component | |||
public class MongoDbContext { | |||
private static final Map<String, MongoDbFactory> MONGO_CLIENT_DB_FACTORY_MAP = new HashMap<>(); | |||
private static final ThreadLocal<MongoDbFactory> MONGO_DB_FACTORY_THREAD_LOCAL = new ThreadLocal<>(); | |||
@Autowired | |||
MongoListProperties mongoListProperties; | |||
public static MongoDbFactory getMongoDbFactory() { | |||
return MONGO_DB_FACTORY_THREAD_LOCAL.get(); | |||
} | |||
public static void setMongoDbFactory(String name) { | |||
MONGO_DB_FACTORY_THREAD_LOCAL.set(MONGO_CLIENT_DB_FACTORY_MAP.get(name)); | |||
} | |||
public static void removeMongoDbFactory() { | |||
MONGO_DB_FACTORY_THREAD_LOCAL.remove(); | |||
} | |||
@PostConstruct | |||
public void afterPropertiesSet() { | |||
if (!CollectionUtils.isEmpty(mongoListProperties.getDblist())) { | |||
mongoListProperties | |||
.getDblist() | |||
.forEach( | |||
info -> { | |||
MONGO_CLIENT_DB_FACTORY_MAP.put( | |||
info.getDatabase(), new SimpleMongoClientDbFactory(info.getUri())); | |||
}); | |||
} | |||
} | |||
@Bean(name = "mongoTemplate") | |||
public MultiMongoTemplate dynamicMongoTemplate() { | |||
Iterator<MongoDbFactory> iterator = MONGO_CLIENT_DB_FACTORY_MAP.values().iterator(); | |||
return new MultiMongoTemplate(iterator.next()); | |||
} | |||
@Bean(name = "mongoDbFactory") | |||
public MongoDbFactory mongoDbFactory() { | |||
Iterator<MongoDbFactory> iterator = MONGO_CLIENT_DB_FACTORY_MAP.values().iterator(); | |||
return iterator.next(); | |||
} | |||
} |
@@ -1,30 +0,0 @@ | |||
package com.telpo.dipperposition.config.db; | |||
import lombok.Data; | |||
import lombok.Getter; | |||
import lombok.Setter; | |||
import lombok.ToString; | |||
import org.springframework.boot.context.properties.ConfigurationProperties; | |||
import java.util.List; | |||
/** | |||
* @program: DataPushServer | |||
* @description: mongo连接配置类 | |||
* @author: linwl | |||
* @create: 2020-07-11 14:41 | |||
*/ | |||
@Getter | |||
@Setter | |||
@ToString | |||
@ConfigurationProperties(prefix = "mongo.datasource") | |||
public class MongoListProperties { | |||
private List<MongoList> dblist; | |||
@Data | |||
public static class MongoList { | |||
private String uri; | |||
private String database; | |||
} | |||
} |
@@ -1,26 +0,0 @@ | |||
package com.telpo.dipperposition.config.db; | |||
import com.mongodb.client.MongoDatabase; | |||
import lombok.extern.slf4j.Slf4j; | |||
import org.springframework.data.mongodb.MongoDbFactory; | |||
import org.springframework.data.mongodb.core.MongoTemplate; | |||
/** | |||
* @program: DataPushServer | |||
* @description: 多mongo数据源配置 | |||
* @author: linwl | |||
* @create: 2020-07-11 14:21 | |||
*/ | |||
@Slf4j | |||
public class MultiMongoTemplate extends MongoTemplate { | |||
public MultiMongoTemplate(MongoDbFactory mongoDbFactory) { | |||
super(mongoDbFactory); | |||
} | |||
@Override | |||
protected MongoDatabase doGetDatabase() { | |||
MongoDbFactory mongoDbFactory = MongoDbContext.getMongoDbFactory(); | |||
return mongoDbFactory == null ? super.doGetDatabase() : mongoDbFactory.getDb(); | |||
} | |||
} |
@@ -68,7 +68,7 @@ public class DipperAstPosAsyncTaskServiceImpl implements IDipperAstPosAsyncTaskS | |||
dataMap.put("key", ipPositionRequestKey); | |||
StringBuffer buffer = okHttpUtil.getQueryString(ipPositionRequestPath, dataMap); | |||
JSONObject paramObject = new JSONObject(); | |||
String result = okHttpUtil.postJsonParams(buffer.toString(), paramObject.toJSONString()); | |||
String result = okHttpUtil.getJsonParams(buffer.toString(), paramObject.toJSONString()); | |||
if (ObjectUtils.isNotEmpty(result)) { | |||
if (StringUtils.isEmpty(result)) { | |||
log.debug("IP省份获取错误,结果为空"); | |||