前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >disconf-web原理分析

disconf-web原理分析

作者头像
Monica2333
发布2020-06-22 11:59:08
4120
发布2020-06-22 11:59:08
举报
文章被收录于专栏:码农知识点码农知识点

disconf-web是基于tomcat,springMVC构建的web应用,tomcat启动时,会加载webapp下面的web.xml

web.xml

可以看出spring profile默认为production,对应的applicationContext.xml文件bean加载为:

可以看出里面需要加载真实的mysql,redis和zookeeper配置。

此外,web.xml里面加载了两个自定义的StartupListenerContextListener,分别是用来设置本地Local环境加载spring的applicationContext的。

首先看一下mysql的数据表信息:

对外暴露的Controller如下:

image.png

UserController 从db校验通过登陆成功后,会把用户信息存放到session和redis中

代码语言:javascript
复制
 /**
     * 登录
     */
    @Override
    public void login(HttpServletRequest request, User user, int expireTime) {

        Visitor visitor = new Visitor();

        //
        //
        //
        visitor.setId(user.getId());
        visitor.setLoginUserId(user.getId());
        visitor.setLoginUserName(user.getName());
        visitor.setRoleId(user.getRoleId());
        visitor.setAppIds(user.getOwnApps());

        //
        // 更新session
        //
        updateSessionVisitor(request.getSession(), visitor);

        //
        // 更新Redis数据
        //
        updateRedisVisitor(visitor, request, expireTime);
    }

在之后的校验中通过LoginInterceptor进行登陆认证校验,通过注解@NoAuth进行权限校验

image.png

image.png

ConfigNewController

代码语言:javascript
复制
/**
     * 配置文件的新建(使用上传配置文件)
     */
    @ResponseBody
    @RequestMapping(value = "/file", method = RequestMethod.POST)
    public JsonObjectBase updateFile(@Valid ConfNewForm confNewForm, @RequestParam("myfilerar") MultipartFile file) {

        LOG.info(confNewForm.toString());

        //
        // 校验
        //
        int fileSize = 1024 * 1024 * 4;
        String[] allowExtName = {".properties", ".xml"};
        fileUploadValidator.validateFile(file, fileSize, allowExtName);

        //
        // 更新
        //
        String fileContent = "";
        try {

            fileContent = new String(file.getBytes(), "UTF-8");
            LOG.info("receive file: " + fileContent);

        } catch (Exception e) {

            LOG.error(e.toString());
            throw new FileUploadException("upload file error", e);
        }

        // 创建配置文件表格
        ConfNewItemForm confNewItemForm = new ConfNewItemForm(confNewForm);
        confNewItemForm.setKey(file.getOriginalFilename());
        confNewItemForm.setValue(fileContent);

        // 业务校验
        configValidator.validateNew(confNewItemForm, DisConfigTypeEnum.FILE);

        //
        configMgr.newConfig(confNewItemForm, DisConfigTypeEnum.FILE);

        return buildSuccess("创建成功");
    }

只会在数据库中创建config和config_history数据,如果开启邮件通知的话,会通知application.properties中设置的邮箱地址

ConfigUpdateController

代码语言:javascript
复制
  /**
     * 配置文件的更新
     */
    @ResponseBody
    @RequestMapping(value = "/file/{configId}", method = RequestMethod.POST)
    public JsonObjectBase updateFile(@PathVariable long configId, @RequestParam("myfilerar") MultipartFile file) {

        //
        // 校验
        //
        int fileSize = 1024 * 1024 * 4;
        String[] allowExtName = {".properties", ".xml"};
        fileUploadValidator.validateFile(file, fileSize, allowExtName);

        // 业务校验
        configValidator.validateUpdateFile(configId, file.getOriginalFilename());

        //
        // 更新
        //
        String emailNotification = "";
        try {

            String str = new String(file.getBytes(), "UTF-8");
            LOG.info("receive file: " + str);

            emailNotification = configMgr.updateItemValue(configId, str);
            LOG.info("update " + configId + " ok");

        } catch (Exception e) {

            LOG.error(e.toString());
            throw new FileUploadException("upload file error", e);
        }

        //
        // 通知ZK
        //
        configMgr.notifyZookeeper(configId);

        return buildSuccess(emailNotification);
    }

更新数据库,如果zk相关配置文件节点存在,会更新zk node

关于定时校验中心的配置与所有客户端配置的一致性 使用spring定时任务@Scheduled实现:

关于为什么使用mysql作为配置获取,而不是zk

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档