Don’t say much, get up and do

Route Registration registration

The Admin service’s UpstreamCheckService class, which is a business layer decorated with @Component and injected as a bean, has a setUp() method

@postConstruct public void setup() {// Get plugin selector data from database, In the map PluginDO PluginDO = pluginMapper. SelectByName (PluginEnum. DIVIDE. GetName ()); if (pluginDO ! = null) { List<SelectorDO> selectorDOList = selectorMapper.findByPluginId(pluginDO.getId()); for (SelectorDO selectorDO : selectorDOList) { List<DivideUpstream> divideUpstreams = GsonUtils.getInstance().fromList(selectorDO.getHandle(), DivideUpstream.class); if (CollectionUtils.isNotEmpty(divideUpstreams)) { UPSTREAM_MAP.put(selectorDO.getName(), divideUpstreams); }} // check= @value ("${soul.upstream.check:true}") Brush regular access to the database data to the cache if (check) {new ScheduledThreadPoolExecutor (Runtime. The getRuntime (). AvailableProcessors (), SoulThreadFactory.create("scheduled-upstream-task", false)) .scheduleWithFixedDelay(this::scheduled, 10, scheduledTime, TimeUnit.SECONDS); }}Copy the code

The check() method provides the ability to resynchronize database data with cached data

private void check(final String selectorName, Final List<DivideUpstream> upstreamList) {// Build an empty List List<DivideUpstream> successList = Lists.newArrayListWithCapacity(upstreamList.size()); For (DivideUpstream DivideUpstream: Live and upstreamList) {/ / methods and final Boolean pass = UpstreamCheckUtils. CheckUrl (divideUpstream. GetUpstreamUrl ()); if (pass) { if (! divideUpstream.isStatus()) { divideUpstream.setTimestamp(System.currentTimeMillis()); divideUpstream.setStatus(true); log.info("UpstreamCacheManager check success the url: {}, host: {} ", divideUpstream.getUpstreamUrl(), divideUpstream.getUpstreamHost()); } successList.add(divideUpstream); } else { divideUpstream.setStatus(false); log.error("check the url={} is fail ", divideUpstream.getUpstreamUrl()); } } if (successList.size() == upstreamList.size()) { return; } // Update map if (successList.size() > 0) {upstream_map.put (selectorName, successList); updateSelectorHandler(selectorName, successList); } else { UPSTREAM_MAP.remove(selectorName); updateSelectorHandler(selectorName, null); }}Copy the code

In addition to updating the cache map, events are updated by publishing events and updating data

private void updateSelectorHandler(final String selectorName, final List<DivideUpstream> upstreams) { SelectorDO selector = selectorService.findByName(selectorName); if (Objects.nonNull(selector)) { SelectorData selectorData = selectorService.buildByName(selectorName); if (upstreams == null) { selector.setHandle(""); selectorData.setHandle(""); } else { String handler = GsonUtils.getInstance().toJson(upstreams); selector.setHandle(handler); selectorData.setHandle(handler); } selectorMapper.updateSelective(selector); / / the publish change event. / / release data change event eventPublisher. PublishEvent (new DataChangedEvent (ConfigGroupEnum. The SELECTOR, DataEventTypeEnum.UPDATE, Collections.singletonList(selectorData))); }}Copy the code

IP agents live

Live and in methods and UpstreamCheckUtils. CheckUrl (…). In, the logic of exploration is implemented

public static boolean checkUrl(final String url) { if (StringUtils.isBlank(url)) { return false; } if (checkIP(url)) { String[] hostPort; If (url.startswith (HTTP)) {final String[] HTTP = stringutils.split (url, "\\/\ /"); hostPort = StringUtils.split(http[1], Constants.COLONS); } else { hostPort = StringUtils.split(url, Constants.COLONS); Return isHostConnector(hostPort[0], integer.parseInt (hostPort[1])); } else { return isHostReachable(url); }}Copy the code

The final execution is isHostConnector(…) By means of

private static boolean isHostConnector(final String host, Final int port) {try (Socket Socket = new Socket()) {// new Socket Socket. Connect (new InetSocketAddress(host, port)); } catch (IOException e) { return false; } return true; } ` `Copy the code