@Test public void route() throws Exception{ String serviceName = "com.sxp.TestService"; String application = "sxp-test-application"; String project = "sxp-test-project"; Map<String, String> parameters; Map<String, ServiceProvider> serviceProviders = new HashMap<String, ServiceProvider>(); ServiceProvider provider = new ServiceProvider(); String providerHost = "192.168.1.1"; int providerPort = 50001; provider.setHost(providerHost); provider.setPort(providerPort); parameters = new HashMap<>(MapUtils.capacity(5)); parameters.put(RegistryConstants.CATEGORY_KEY, RegistryConstants.PROVIDERS_CATEGORY); parameters.put(GlobalConstants.CommonKey.SIDE, RegistryConstants.PROVIDER_SIDE); parameters.put(GlobalConstants.Provider.Key.INTERFACE, serviceName); parameters.put(GlobalConstants.Provider.Key.APPLICATION, application); parameters.put(GlobalConstants.CommonKey.PROJECT, project); URL providerUrl = new URL(RegistryConstants.GRPC_PROTOCOL, providerHost, providerPort, parameters); provider.setUrl(providerUrl); serviceProviders.put(providerHost + ":" + providerPort, provider); parameters = new HashMap<>(MapUtils.capacity(5)); parameters.put(RegistryConstants.CATEGORY_KEY, RegistryConstants.CONSUMERS_CATEGORY); parameters.put(GlobalConstants.CommonKey.SIDE, RegistryConstants.CONSUMER_SIDE); parameters.put(GlobalConstants.Provider.Key.INTERFACE, serviceName); parameters.put(GlobalConstants.Provider.Key.APPLICATION, application); parameters.put(GlobalConstants.CommonKey.PROJECT, project); String consumerHost = "192.168.2.1"; int consumerPort = 0; URL consumerUrl = new URL(RegistryConstants.GRPC_PROTOCOL, consumerHost, consumerPort, parameters); provider.setUrl(providerUrl); serviceProviders.put(providerHost + ":" + providerPort, provider); Assert.assertTrue(serviceProviders.size() > 0); parameters = new HashMap<>(MapUtils.capacity(5)); parameters.put(RegistryConstants.CATEGORY_KEY, RegistryConstants.ROUTERS_CATEGORY); parameters.put(GlobalConstants.Provider.Key.INTERFACE, serviceName); parameters.put(GlobalConstants.Provider.Key.APPLICATION, application); parameters.put(GlobalConstants.CommonKey.PROJECT, project); parameters.put("rule", URLEncoder.encode("host=* => ", "UTF-8")); URL routerUrl = new URL(RegistryConstants.ROUTER_PROTOCOL, "0.0.0.0", 0, parameters); Router router = new ConditionRouter(routerUrl); serviceProviders = router.route(serviceProviders, consumerUrl); Assert.assertTrue(serviceProviders.size() == 0); }
public Map<String, ServiceProvider> route(Map<String, ServiceProvider> providers, URL url) throws RpcException { if (providers == null || providers.size() == 0) { return providers; } try { if (!matchWhen(url)) { return providers; } Map<String, ServiceProvider> result = new ConcurrentHashMap<String, ServiceProvider>(); if (thenCondition == null) { logger.log(Level.FINE, "The current consumer in the service blacklist. consumer: " + NetUtils.getLocalHost() + ", service: " + url.getServiceKey()); return result; } for (Map.Entry<String, ServiceProvider> entry : providers.entrySet()) { if (matchThen(entry.getValue().getUrl(), url)) { result.put(entry.getKey(), entry.getValue()); } } if (result.size() > 0) { return result; } else if (force) { logger.log(Level.FINE, "The route result is empty and force execute. consumer: " + NetUtils.getLocalHost() + ", service: " + url.getServiceKey() + ", router: " + url.getParameterAndDecoded(Constants.RULE_KEY)); return result; } } catch (Throwable t) { logger.log(Level.SEVERE, "Failed to execute condition router rule: " + getUrl() + ", invokers: " + providers + ", cause: " + t.getMessage(), t); } return providers; }
ConditionRouter implements Router, Comparable<Router> { public Map<String, ServiceProvider> route(Map<String, ServiceProvider> providers, URL url) throws RpcException { if (providers == null || providers.size() == 0) { return providers; } try { if (!matchWhen(url)) { return providers; } Map<String, ServiceProvider> result = new ConcurrentHashMap<String, ServiceProvider>(); if (thenCondition == null) { logger.log(Level.FINE, "The current consumer in the service blacklist. consumer: " + NetUtils.getLocalHost() + ", service: " + url.getServiceKey()); return result; } for (Map.Entry<String, ServiceProvider> entry : providers.entrySet()) { if (matchThen(entry.getValue().getUrl(), url)) { result.put(entry.getKey(), entry.getValue()); } } if (result.size() > 0) { return result; } else if (force) { logger.log(Level.FINE, "The route result is empty and force execute. consumer: " + NetUtils.getLocalHost() + ", service: " + url.getServiceKey() + ", router: " + url.getParameterAndDecoded(Constants.RULE_KEY)); return result; } } catch (Throwable t) { logger.log(Level.SEVERE, "Failed to execute condition router rule: " + getUrl() + ", invokers: " + providers + ", cause: " + t.getMessage(), t); } return providers; } }
ConditionRouter implements Router, Comparable<Router> { public Map<String, ServiceProvider> route(Map<String, ServiceProvider> providers, URL url) throws RpcException { if (providers == null || providers.size() == 0) { return providers; } try { if (!matchWhen(url)) { return providers; } Map<String, ServiceProvider> result = new ConcurrentHashMap<String, ServiceProvider>(); if (thenCondition == null) { logger.log(Level.FINE, "The current consumer in the service blacklist. consumer: " + NetUtils.getLocalHost() + ", service: " + url.getServiceKey()); return result; } for (Map.Entry<String, ServiceProvider> entry : providers.entrySet()) { if (matchThen(entry.getValue().getUrl(), url)) { result.put(entry.getKey(), entry.getValue()); } } if (result.size() > 0) { return result; } else if (force) { logger.log(Level.FINE, "The route result is empty and force execute. consumer: " + NetUtils.getLocalHost() + ", service: " + url.getServiceKey() + ", router: " + url.getParameterAndDecoded(Constants.RULE_KEY)); return result; } } catch (Throwable t) { logger.log(Level.SEVERE, "Failed to execute condition router rule: " + getUrl() + ", invokers: " + providers + ", cause: " + t.getMessage(), t); } return providers; } ConditionRouter(URL url); }
ConditionRouter implements Router, Comparable<Router> { public Map<String, ServiceProvider> route(Map<String, ServiceProvider> providers, URL url) throws RpcException { if (providers == null || providers.size() == 0) { return providers; } try { if (!matchWhen(url)) { return providers; } Map<String, ServiceProvider> result = new ConcurrentHashMap<String, ServiceProvider>(); if (thenCondition == null) { logger.log(Level.FINE, "The current consumer in the service blacklist. consumer: " + NetUtils.getLocalHost() + ", service: " + url.getServiceKey()); return result; } for (Map.Entry<String, ServiceProvider> entry : providers.entrySet()) { if (matchThen(entry.getValue().getUrl(), url)) { result.put(entry.getKey(), entry.getValue()); } } if (result.size() > 0) { return result; } else if (force) { logger.log(Level.FINE, "The route result is empty and force execute. consumer: " + NetUtils.getLocalHost() + ", service: " + url.getServiceKey() + ", router: " + url.getParameterAndDecoded(Constants.RULE_KEY)); return result; } } catch (Throwable t) { logger.log(Level.SEVERE, "Failed to execute condition router rule: " + getUrl() + ", invokers: " + providers + ", cause: " + t.getMessage(), t); } return providers; } ConditionRouter(URL url); Map<String, ServiceProvider> route(Map<String, ServiceProvider> providers, URL url); URL getUrl(); int compareTo(Router o); @Override boolean equals(Object o); @Override int hashCode(); boolean matchWhen(URL url); boolean matchThen(URL url, URL param); }
ConditionRouter implements Router, Comparable<Router> { public Map<String, ServiceProvider> route(Map<String, ServiceProvider> providers, URL url) throws RpcException { if (providers == null || providers.size() == 0) { return providers; } try { if (!matchWhen(url)) { return providers; } Map<String, ServiceProvider> result = new ConcurrentHashMap<String, ServiceProvider>(); if (thenCondition == null) { logger.log(Level.FINE, "The current consumer in the service blacklist. consumer: " + NetUtils.getLocalHost() + ", service: " + url.getServiceKey()); return result; } for (Map.Entry<String, ServiceProvider> entry : providers.entrySet()) { if (matchThen(entry.getValue().getUrl(), url)) { result.put(entry.getKey(), entry.getValue()); } } if (result.size() > 0) { return result; } else if (force) { logger.log(Level.FINE, "The route result is empty and force execute. consumer: " + NetUtils.getLocalHost() + ", service: " + url.getServiceKey() + ", router: " + url.getParameterAndDecoded(Constants.RULE_KEY)); return result; } } catch (Throwable t) { logger.log(Level.SEVERE, "Failed to execute condition router rule: " + getUrl() + ", invokers: " + providers + ", cause: " + t.getMessage(), t); } return providers; } ConditionRouter(URL url); Map<String, ServiceProvider> route(Map<String, ServiceProvider> providers, URL url); URL getUrl(); int compareTo(Router o); @Override boolean equals(Object o); @Override int hashCode(); boolean matchWhen(URL url); boolean matchThen(URL url, URL param);  }