From d57bc4398215c0e7849af60c30f1dc9f017e3a48 Mon Sep 17 00:00:00 2001 From: linwl <304115325@qq.com> Date: Wed, 20 Jan 2021 19:42:59 +0800 Subject: [PATCH] =?UTF-8?q?mongodb=E8=AE=BF=E9=97=AE=E6=96=B9=E5=BC=8F?= =?UTF-8?q?=E5=8F=98=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/db/MongoDbContext.java | 65 ------------------- .../config/db/MongoListProperties.java | 30 --------- .../config/db/MultiMongoTemplate.java | 26 -------- .../DipperAstPosAsyncTaskServiceImpl.java | 2 +- 4 files changed, 1 insertion(+), 122 deletions(-) delete mode 100644 src/main/java/com/telpo/dipperposition/config/db/MongoDbContext.java delete mode 100644 src/main/java/com/telpo/dipperposition/config/db/MongoListProperties.java delete mode 100644 src/main/java/com/telpo/dipperposition/config/db/MultiMongoTemplate.java diff --git a/src/main/java/com/telpo/dipperposition/config/db/MongoDbContext.java b/src/main/java/com/telpo/dipperposition/config/db/MongoDbContext.java deleted file mode 100644 index 14fbcd8..0000000 --- a/src/main/java/com/telpo/dipperposition/config/db/MongoDbContext.java +++ /dev/null @@ -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 MONGO_CLIENT_DB_FACTORY_MAP = new HashMap<>(); - private static final ThreadLocal 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 iterator = MONGO_CLIENT_DB_FACTORY_MAP.values().iterator(); - return new MultiMongoTemplate(iterator.next()); - } - - @Bean(name = "mongoDbFactory") - public MongoDbFactory mongoDbFactory() { - Iterator iterator = MONGO_CLIENT_DB_FACTORY_MAP.values().iterator(); - return iterator.next(); - } -} diff --git a/src/main/java/com/telpo/dipperposition/config/db/MongoListProperties.java b/src/main/java/com/telpo/dipperposition/config/db/MongoListProperties.java deleted file mode 100644 index 804eff3..0000000 --- a/src/main/java/com/telpo/dipperposition/config/db/MongoListProperties.java +++ /dev/null @@ -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 dblist; - - @Data - public static class MongoList { - private String uri; - private String database; - } -} diff --git a/src/main/java/com/telpo/dipperposition/config/db/MultiMongoTemplate.java b/src/main/java/com/telpo/dipperposition/config/db/MultiMongoTemplate.java deleted file mode 100644 index df79df4..0000000 --- a/src/main/java/com/telpo/dipperposition/config/db/MultiMongoTemplate.java +++ /dev/null @@ -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(); - } -} diff --git a/src/main/java/com/telpo/dipperposition/service/impl/DipperAstPosAsyncTaskServiceImpl.java b/src/main/java/com/telpo/dipperposition/service/impl/DipperAstPosAsyncTaskServiceImpl.java index c122805..3384ab6 100644 --- a/src/main/java/com/telpo/dipperposition/service/impl/DipperAstPosAsyncTaskServiceImpl.java +++ b/src/main/java/com/telpo/dipperposition/service/impl/DipperAstPosAsyncTaskServiceImpl.java @@ -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省份获取错误,结果为空");