fs-danaus
2021-11-29 fc5b4fb816d05222ee7609a926d46ecfbb87e0fb
增加数据源解析多域名可以绑定多个IP
2个文件已修改
73 ■■■■■ 已修改文件
src/com/yc/action/panval/InvGet.java 51 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/com/yc/multiData/MultiDataSource.java 22 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/com/yc/action/panval/InvGet.java
@@ -542,40 +542,57 @@
            return;
        }
    }
    /**
     * 解析数据源定义的所有域名对应的IP
     * @param dataSourceEntity
     * @return
     */
    private  void processDomainToIP(DataSourceEntity dataSourceEntity){
        StringBuilder sb=new StringBuilder();
        if(StringUtils.isNotBlank(dataSourceEntity.getDomain())) {
             Object[] domainList=  Arrays.stream(dataSourceEntity.getDomain().replaceAll(":.*?\\d+", "").split(";")).distinct().toArray();
            for(Object domain:domainList){
                try {
                    InetAddress addresses = InetAddress.getByName(String.valueOf(domain));
                    //本机IP跳过
                    if("127.0.0.1".equals(addresses.getHostAddress())) continue;
                    sb.append(addresses.getHostAddress());
                        sb.append(";");
                }catch (Exception ex){
                    ex.printStackTrace();
                    continue;
                }
            }
        }
        if(sb.length()>0) {
            String str=sb.toString();
            int index=str.lastIndexOf(";");
            dataSourceEntity.setDomainIpList(str.substring(0,index));
        }
    }
    @RequestMapping("/demo/refreshIp.do")
    public  void freshDomainTOIP(HttpServletRequest request,HttpServletResponse response){
        //通过域名查出ip
        try {
            //取第一个域名
            StringBuilder sql=new StringBuilder();
            StringBuilder error=new StringBuilder();
            final Map<String, DataSourceEntity> dataSourceMaps = MultiDataSource.getDataSourceMaps();
            Map<String, DataSourceEntity> concurrentHashMap =new ConcurrentHashMap<>();
            concurrentHashMap.putAll(dataSourceMaps);
            for (Map.Entry<String, DataSourceEntity> entry : concurrentHashMap.entrySet()) {
                DataSourceEntity dataSourceEntity = entry.getValue();
                final String hostName = dataSourceEntity.getDomain().split(";")[0].replaceAll(":.*?\\d+", "");
                try {
                    InetAddress addresses = InetAddress.getByName(hostName);
                    String ip = addresses.getHostAddress();
                    dataSourceEntity.setDomainIpList(ip);
                    sql.append(String.format(" update a set a.domainIpList='%s' from gsystem a where a.id=%d \n ", ip, dataSourceEntity.getDbId()));
                    //刷新数据源
                    refreshDataSource(dataSourceEntity.getDbId() + "", ActionEnum.REFRESH);
                }catch (Exception ex){
                    ex.printStackTrace();
                    error.append(hostName).append(";");
                    continue;
                }
                    processDomainToIP(dataSourceEntity);
                    sql.append(String.format(" update a set a.domainIpList='%s' from gsystem a where a.id=%d \n ", dataSourceEntity.getDomainIpList(), dataSourceEntity.getDbId()));
            }
                //更新到数据库
                SpObserver.setDBtoDemo();
            if(StringUtils.isNotBlank(sql)) {
                BaseService baseService = (BaseService) FactoryBean.getBean("BaseService");
                baseService.getSimpleJdbcTemplate().execute(sql.toString());
                //刷新数据源
                refreshDataSource("", ActionEnum.REFRESH);
                this.print(response, "所有数据源域名重新绑定IP成功!");
            }
            this.print(response, "如下域名解析IP出错:"+error.toString());
        } catch (Exception e) {
            e.printStackTrace();
            this.print(response, new CallBackMessage().sendErrorMessage(e.getMessage()));
@@ -652,6 +669,8 @@
                    invitationCode = dIfc.getInvitationCode();
                    datasoure.setInvitationCode(invitationCode);
                }
                //解析域名对应的IP
                processDomainToIP(datasoure);
                dbid = dIfc.updateDemo(datasoure);
                refreshDataSource(dbid + "", DataSourceActionEntity.ActionEnum.REFRESH); // 直接刷新本机
                String datav = "{\"dbid\":" + dbid + ",\"invitationCode\":\"" + datasoure.getInvitationCode() + "\"}";
src/com/yc/multiData/MultiDataSource.java
@@ -821,10 +821,16 @@
            //表示需要创建最小的连接数,也就是jdbc.properties配置文件定义
            // 且执行localHostBindIPs的过滤
            if (StringUtils.isNotBlank(localHostBindIPs) && StringUtils.isNotBlank(dataSourceEntity.getDomainIpList())) {
                if ((";" + localHostBindIPs + ";").contains(";" + dataSourceEntity.getDomainIpList() + ";")) {
                    //判断当前数据源所绑定的IP是否属于定义的IP名单里面
                    isMiniConection = true;
                } else {
                //处理一个数据源对应多个域名情况,yingchen.onbus.cn;mp.onbus.cn
                String[] ipList=dataSourceEntity.getDomainIpList().split(";");
                for(String ip:ipList) {
                    if ((";" + localHostBindIPs + ";").contains(";" + ip + ";")) {
                        //判断当前数据源所绑定的IP是否属于定义的IP名单里面
                        isMiniConection = true;
                        break;
                    }
                }
                if(!isMiniConection) {
                    return false;//不属于当前服务器的不需要生成数据源
                }
            } else {
@@ -834,7 +840,9 @@
        }
        //---9001
        else if (StringUtils.isNotBlank(localHostBindIPs) && StringUtils.isNotBlank(dataSourceEntity.getDomainIpList())) {
                if ((";" + localHostBindIPs + ";").contains(";" + dataSourceEntity.getDomainIpList() + ";")) {
            String[] ipList=dataSourceEntity.getDomainIpList().split(";");
            for(String ip:ipList) {
                if ((";" + localHostBindIPs + ";").contains(";" + ip + ";")) {
                    //默认取数据源定义的配置,如果没配置再取配置文件的值
                    dataSourceEntity.setDataSourceMaxIdleTime(
                            dataSourceEntity.getDataSourceMaxIdleTime() != null ? dataSourceEntity.getDataSourceMaxIdleTime() : InvGet.maxIdleTime);
@@ -852,7 +860,7 @@
                            dataSourceEntity.getDataSourceIdleConnectionTestPeriod() != null ? dataSourceEntity.getDataSourceIdleConnectionTestPeriod() : InvGet.idleConnectionTestPeriod);
                    return true;
                }
            }
        }else{
            //什么也不配置,加载所有
            isMiniConection=true;
@@ -916,7 +924,7 @@
            }
            dataSourceMap.remove(dbId);   //这个只是个集合,可以先删除然后再新增
            dataSourceMap.put(dbId, dataSourceEntity);
            System.out.println("已加载数据源:"+dataSourceEntity.getSystemID());
            //System.out.println("已加载数据源:"+dataSourceEntity.getSystemID());
            //System.out.println(this.getClass()+" setDataSourceByMap() dbid:" +dbid+ " expireddays:" + (dataSourceEntity.getExpiredDays()==null?"null":dataSourceEntity.getExpiredDays()) );
        } catch (Exception e) {
            log.error(e.getMessage());