RestTemplate restTemplate = new RestTemplate(); SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); requestFactory.setConnectTimeout(5000); // 设置连接超时时间为5秒 requestFactory.setReadTimeout(5000); // 设置读取超时时间为5秒 restTemplate.setRequestFactory(requestFactory);
/** * * <p> * 检查数据源状态 * </p> * * @Title: checkDataSouceStatus * @param: @return * @author: jlcui * @date: 2023年9月14日 下午4:12:12 */ private void checkDataSouceStatus(ConfigNetauthDataSourceBean bean,Integer num) { try { SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); requestFactory.setConnectTimeout(500); requestFactory.setReadTimeout(500); RestTemplate restTemplate = HttpClientUtils.gethTttpRestTemplate(); restTemplate.setRequestFactory(requestFactory); String url = "http://10.210.21.123:8082/actuator"; UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url); URI uri = builder.build().encode().toUri(); ResponseEntity<JSONObject> forEntity = restTemplate.getForEntity(uri, JSONObject.class); if (HttpStatus.OK.equals(forEntity.getStatusCode())) { // 如果获取数据成功后,判断是否时延迟或者正常状态 if (num == Const.CONST_INTEGER_0) { bean.setStatus(Const.CONST_INTEGER_2); } else { bean.setStatus(Const.CONST_INTEGER_3); } } else { bean.setStatus(Const.CONST_INTEGER_4); } configNetauthDataSourceService.update(bean); } catch (Exception e) { // 连接超时或读取超时抛出的异常 if (e.getCause() instanceof SocketTimeoutException) { // 超时请求超过三次时则判断故障 if (num < Const.CONST_INTEGER_3) { num = num + Const.CONST_INTEGER_1; checkDataSouceStatus(bean,num); } else { bean.setStatus(Const.CONST_INTEGER_4); configNetauthDataSourceService.update(bean); } } else { bean.setStatus(Const.CONST_INTEGER_4); configNetauthDataSourceService.update(bean); e.printStackTrace(); } } }
标签:Const,spring,CONST,boot,RestTemplate,bean,num,INTEGER,requestFactory From: https://www.cnblogs.com/cuijinlong/p/17702941.html