将Hutool的db.setting放在Nacos配置中心

hutool使用了DB.query作一个查询功能,但需要在resources下建立一个db.settting文件
但我使用了nacos,想需要把这个配置文件也同时放成Nacos上
示例代码如下:

 @SneakyThrows
    public static void main(String[] args) {
        //Nacos配置地址
        String serverAddr = "10.18.70.170:8848";
        //DateId
        String dataId = "db.setting";
        String group = "DEFAULT_GROUP";
        Properties properties = new Properties();
        properties.put(PropertyKeyConst.SERVER_ADDR, serverAddr);
        properties.put(PropertyKeyConst.NAMESPACE, "eblink");
        ConfigService configService = NacosFactory.createConfigService(properties);
        //读取NACOS配置内容,将内容转为读取NACOS配置内容
        Properties properties1=load(configService.getConfig(dataId, group, 5000));
        //转成HASHMAP
        Setting setting= Setting.create();
        Set keys=properties1.keySet();
        Map map=new HashMap<>();
        for (Object k : keys) {
            map.put((String)k, (String)properties1.get(k));
        }
        setting.putAll(map);
        //设置全局数据源
        GlobalDSFactory.set(DSFactory.create(setting));
        //查询测试
        List entities = Db.use().findAll("TMS_YQT_ACCOUNT");
        System.out.print(entities.size());

    }
    
    public static Properties load(String propertiesString) {
        System.out.println(propertiesString);
        Properties properties = new Properties();
        try {
            properties.load(new ByteArrayInputStream(propertiesString.getBytes()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return properties;
    }
 

                            
                        
                    
                    
                    

你可能感兴趣的:(将Hutool的db.setting放在Nacos配置中心)