| | |
| | | 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())); |
| | |
| | | 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() + "\"}"; |