001/**
002 * Copyright (c) 2015-2022, Michael Yang 杨福海 (fuhai999@gmail.com).
003 * <p>
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * <p>
008 * http://www.apache.org/licenses/LICENSE-2.0
009 * <p>
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package io.jboot.app.config.support.nacos;
017
018import com.alibaba.nacos.api.NacosFactory;
019import com.alibaba.nacos.api.config.ConfigService;
020import com.jfinal.log.Log;
021import io.jboot.app.config.JbootConfigKit;
022import io.jboot.app.config.JbootConfigManager;
023import io.jboot.utils.ConfigUtil;
024
025import java.io.IOException;
026import java.io.StringReader;
027import java.util.Map;
028import java.util.Objects;
029import java.util.Properties;
030
031/**
032 * @author michael yang (fuhai999@gmail.com)
033 * @Date: 2020/2/8
034 */
035public class NacosConfigManager {
036
037    private static final Log LOG = Log.getLog(NacosConfigManager.class);
038    private static final NacosConfigManager ME = new NacosConfigManager();
039
040    public static NacosConfigManager me() {
041        return ME;
042    }
043
044
045    private Properties contentProperties;
046
047    /**
048     * 初始化 nacos 配置监听
049     */
050    public void init(JbootConfigManager configManager) {
051        Map<String, NacosServerConfig> configModels = ConfigUtil.getConfigModels(configManager, NacosServerConfig.class);
052        configModels.forEach((s, nacosConfig) -> initConfig(configManager, nacosConfig));
053    }
054
055    private void initConfig(JbootConfigManager configManager, NacosServerConfig nacosServerConfig) {
056        if (nacosServerConfig == null
057                || !nacosServerConfig.isEnable()
058                || !nacosServerConfig.isConfigOk()) {
059            return;
060        }
061
062        try {
063
064            ConfigService configService = NacosFactory.createConfigService(nacosServerConfig.toProperties());
065            String content = configService.getConfig(nacosServerConfig.getDataId()
066                    , nacosServerConfig.getGroup(), 3000);
067
068            if (JbootConfigKit.isNotBlank(content)) {
069                contentProperties = str2Properties(content);
070                if (contentProperties != null) {
071                    configManager.setRemoteProperties(contentProperties);
072                }
073            }
074
075            new NacosConfigInitializer(this, configManager).initListener(configService, nacosServerConfig);
076
077        } catch (Exception e) {
078
079            LOG.error(e.toString(), e);
080        }
081    }
082
083
084    /**
085     * 接收到 nacos 服务器消息
086     *
087     * @param configManager
088     * @param configInfo
089     */
090    public void onReceiveConfigInfo(JbootConfigManager configManager, String configInfo) {
091        Properties properties = str2Properties(configInfo);
092        if (properties != null) {
093            if (contentProperties == null) {
094                contentProperties = properties;
095                configManager.setRemoteProperties(properties);
096            } else {
097                for (Object key : properties.keySet()) {
098                    String newValue = properties.getProperty(key.toString());
099                    String oldValue = contentProperties.getProperty(key.toString());
100
101                    if (!Objects.equals(newValue, oldValue)) {
102                        contentProperties.put(key, newValue);
103                        configManager.setRemoteProperty(key.toString(), newValue);
104
105                        configManager.notifyChangeListeners(key.toString(), newValue, oldValue);
106                    }
107                }
108            }
109        }
110
111
112    }
113
114
115    private Properties str2Properties(String content) {
116        try {
117            Properties properties = new Properties();
118            properties.load(new StringReader(content));
119            return properties;
120        } catch (IOException e) {
121            e.printStackTrace();
122        }
123        return null;
124    }
125}