disconf上托管的json文件使用

step1.在applicationContext.xml中引入配置

 

<!-- 使用托管方式的disconf配置(无代码侵入, 配置更改会自动reload) -->
<bean id="configproperties_disconf"
      class="com.baidu.disconf.client.addons.properties.ReloadablePropertiesFactoryBean">
    <property name="locations">
        <list>
            <value>classpath:/user_acl_template.json</value>
        </list>
    </property>
</bean>

step2.配置类,UserAclTemplateConfig

 

/**
 *
 * @author 周宁
 * @Date 2018-08-08 11:39
 */
@Data
@Configuration
@DisconfFile(filename = "user_acl_template.json",targetDirPath = "com/gysoft/im/common/core/disconf")
public class UserAclTemplateConfig {
}

step3.文件更新回调类,UserAclTemplateCallback实现IDisconfUpdate接口

 

/**
 * 更新user_acl_template.json回调方法
 * @author 周宁
 * @Date 2018-08-08 11:11
 */
@Service
@DisconfUpdateService(classes={UserAclTemplateConfig.class})
public class UserAclTemplateCallback implements IDisconfUpdate {

    @Override
    public void reload() throws Exception {
        UserAclService.updateMqttAcls();
    }

}

UserAclService.java

 

/**
 * @author 周宁
 * @Date 2018-08-08 15:41
 */
public class UserAclService {
    /**
     * disconf上配置的acl规则模板<br/>
     * 只会初始化一次或者在更新disconf上配置时更新<br/>
     */
    private static List<MqttAcl> mqttAcls = new ArrayList<>();

    /**
     * 获取用户的acl规则配置
     *
     * @param username
     * @param clientid
     * @param topic
     * @return
     */
    public static List<MqttAcl> userMqttAcls(String username, String clientid, String topic) {
        synchronized (mqttAcls) {
            if (EmptyUtils.isEmpty(mqttAcls)) {
                updateMqttAcls();
            }
            List<MqttAcl> result = new ArrayList<>();
            mqttAcls.forEach(mqttAcl -> {
                MqttAcl userMqttAcl = new MqttAcl();
                userMqttAcl.setAccess(mqttAcl.getAccess());
                userMqttAcl.setAllow(mqttAcl.getAllow());
                userMqttAcl.setUsername(mqttAcl.getUsername().replace("%username%", username));
                userMqttAcl.setTopic(mqttAcl.getTopic().replace("%topic%", topic));
                userMqttAcl.setClientId(mqttAcl.getClientId().replace("%clientId%", clientid));
                result.add(userMqttAcl);
            });
            return result;
        }

    }

    /**
     * 更新mqttAcl配置规则
     */
    public static void updateMqttAcls() {
        synchronized (mqttAcls) {
            String JsonContext = readJson(OsUtil.pathJoin(ClassLoaderUtil.getClassPath(), "com/gysoft/im/common/core/disconf") + "/user_acl_template.json");
            JSONArray jsonArray = JSONArray.fromObject(JsonContext);
            if (jsonArray.size() > 0) {
                mqttAcls.clear();
                jsonArray.stream().forEach(obj -> mqttAcls.add((MqttAcl) JSONObject.toBean((JSONObject) obj, MqttAcl.class)));
            } else {
                throw new RuntimeException("未找到配置【user_acl_template.json】");
            }
        }

    }

    /**
     * 读取user_acl_template.json文件
     *
     * @param path
     * @return String
     */
    private static String readJson(String path) {
        BufferedReader reader = null;
        String result = "";
        try {
            FileInputStream fileInputStream = new FileInputStream(path);
            InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
            reader = new BufferedReader(inputStreamReader);
            String tempString = null;
            while ((tempString = reader.readLine()) != null) {
                result += tempString;
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值