Java Code Examples for com.alibaba.dubbo.common.URL#addParameter()

The following examples show how to use com.alibaba.dubbo.common.URL#addParameter() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RouteServiceImpl.java    From dubbox with Apache License 2.0 6 votes vote down vote up
public void enableRoute(Long id) {
      if(id == null) {
          throw new IllegalStateException("no route id");
      }
      
      URL oldRoute = findRouteUrl(id);
      if(oldRoute == null) {
          throw new IllegalStateException("Route was changed!");
      }
      if(oldRoute.getParameter("enabled", true)) {
          return;
      }

registryService.unregister(oldRoute);
      URL newRoute= oldRoute.addParameter("enabled", true);
      registryService.register(newRoute);
      
  }
 
Example 2
Source File: DubboProtocol.java    From dubbo3 with Apache License 2.0 6 votes vote down vote up
private ExchangeServer createServer(URL url) {
    //默认开启server关闭时发送readonly事件
    url = url.addParameterIfAbsent(Constants.CHANNEL_READONLYEVENT_SENT_KEY, Boolean.TRUE.toString());
    //默认开启heartbeat
    url = url.addParameterIfAbsent(Constants.HEARTBEAT_KEY, String.valueOf(Constants.DEFAULT_HEARTBEAT));
    String str = url.getParameter(Constants.SERVER_KEY, Constants.DEFAULT_REMOTING_SERVER);

    if (str != null && str.length() > 0 && !ExtensionLoader.getExtensionLoader(Transporter.class).hasExtension(str))
        throw new RpcException("Unsupported server type: " + str + ", url: " + url);

    url = url.addParameter(Constants.CODEC_KEY, DubboCodec.NAME);
    ExchangeServer server;
    try {
        server = Exchangers.bind(url, requestHandler);
    } catch (RemotingException e) {
        throw new RpcException("Fail to start server(url: " + url + ") " + e.getMessage(), e);
    }
    str = url.getParameter(Constants.CLIENT_KEY);
    if (str != null && str.length() > 0) {
        Set<String> supportedTypes = ExtensionLoader.getExtensionLoader(Transporter.class).getSupportedExtensions();
        if (!supportedTypes.contains(str)) {
            throw new RpcException("Unsupported client type: " + str);
        }
    }
    return server;
}
 
Example 3
Source File: RouteServiceImpl.java    From open-capacity-platform with Apache License 2.0 6 votes vote down vote up
public void disableRoute(Long id) {
    if (id == null) {
        throw new IllegalStateException("no route id");
    }

    URL oldRoute = findRouteUrl(id);
    if (oldRoute == null) {
        throw new IllegalStateException("Route was changed!");
    }
    if (!oldRoute.getParameter("enabled", true)) {
        return;
    }

    URL newRoute = oldRoute.addParameter("enabled", false);
    registryService.unregister(oldRoute);
    registryService.register(newRoute);

}
 
Example 4
Source File: AbstractTracingCollectorFactory.java    From dubbo-plus with Apache License 2.0 6 votes vote down vote up
@Override
public TracingCollector getTracingCollector() {
    Collection<Registry> registries =  AbstractRegistryFactory.getRegistries();
    List<URL> urls = new ArrayList<URL>();
    for(Registry registry:registries){
        URL url = registry.getUrl();
        String protocolName = url.getProtocol();
        url=url.setProtocol(Constants.REGISTRY_PROTOCOL);
        url=url.addParameter(Constants.REGISTRY_KEY,protocolName);
        url=url.setPath(TracingCollector.class.getName());
        url=url.addParameter(Constants.INTERFACE_KEY,TracingCollector.class.getName());
        url=url.addParameter(Constants.REFERENCE_FILTER_KEY,"-dst");
        urls.add(url);
    }
    return createTracingCollector(urls);
}
 
Example 5
Source File: RegistryDirectory.java    From dubbox with Apache License 2.0 5 votes vote down vote up
/**
 * 合并url参数 顺序为override > -D >Consumer > Provider
 * @param providerUrl
 * @param overrides
 * @return
 */
private URL mergeUrl(URL providerUrl){
    providerUrl = ClusterUtils.mergeUrl(providerUrl, queryMap); // 合并消费端参数
    
    List<Configurator> localConfigurators = this.configurators; // local reference
    if (localConfigurators != null && localConfigurators.size() > 0) {
        for (Configurator configurator : localConfigurators) {
            providerUrl = configurator.configure(providerUrl);
        }
    }
    
    providerUrl = providerUrl.addParameter(Constants.CHECK_KEY, String.valueOf(false)); // 不检查连接是否成功,总是创建Invoker!
    
    //directoryUrl 与 override 合并是在notify的最后,这里不能够处理
    this.overrideDirectoryUrl = this.overrideDirectoryUrl.addParametersIfAbsent(providerUrl.getParameters()); // 合并提供者参数        
    
    if ((providerUrl.getPath() == null || providerUrl.getPath().length() == 0)
            && "dubbo".equals(providerUrl.getProtocol())) { // 兼容1.0
        //fix by tony.chenl DUBBO-44
        String path = directoryUrl.getParameter(Constants.INTERFACE_KEY);
        if (path != null) {
            int i = path.indexOf('/');
            if (i >= 0) {
                path = path.substring(i + 1);
            }
            i = path.lastIndexOf(':');
            if (i >= 0) {
                path = path.substring(0, i);
            }
            providerUrl = providerUrl.setPath(path);
        }
    }
    return providerUrl;
}
 
Example 6
Source File: DubboProtocol.java    From dubbox with Apache License 2.0 5 votes vote down vote up
/**
 * 创建新连接.
 */
private ExchangeClient initClient(URL url) {
    
    // client type setting.
    String str = url.getParameter(Constants.CLIENT_KEY, url.getParameter(Constants.SERVER_KEY, Constants.DEFAULT_REMOTING_CLIENT));

    String version = url.getParameter(Constants.DUBBO_VERSION_KEY);
    boolean compatible = (version != null && version.startsWith("1.0."));
    url = url.addParameter(Constants.CODEC_KEY, Version.isCompatibleVersion() && compatible ? COMPATIBLE_CODEC_NAME : DubboCodec.NAME);
    //默认开启heartbeat
    url = url.addParameterIfAbsent(Constants.HEARTBEAT_KEY, String.valueOf(Constants.DEFAULT_HEARTBEAT));
    
    // BIO存在严重性能问题,暂时不允许使用
    if (str != null && str.length() > 0 && ! ExtensionLoader.getExtensionLoader(Transporter.class).hasExtension(str)) {
        throw new RpcException("Unsupported client type: " + str + "," +
                " supported client type is " + StringUtils.join(ExtensionLoader.getExtensionLoader(Transporter.class).getSupportedExtensions(), " "));
    }
    
    ExchangeClient client ;
    try {
        //设置连接应该是lazy的 
        if (url.getParameter(Constants.LAZY_CONNECT_KEY, false)){
            client = new LazyConnectExchangeClient(url ,requestHandler);
        } else {
            client = Exchangers.connect(url ,requestHandler);
        }
    } catch (RemotingException e) {
        throw new RpcException("Fail to create remoting client for service(" + url
                + "): " + e.getMessage(), e);
    }
    return client;
}
 
Example 7
Source File: HeartbeatHandlerTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Test
public void testClientHeartbeat() throws Exception {
    FakeChannelHandlers.setTestingChannelHandlers();
    URL serverURL = URL.valueOf("header://localhost:55555?transporter=netty4");
    TestHeartbeatHandler handler = new TestHeartbeatHandler();
    server = Exchangers.bind(serverURL, handler);
    System.out.println("Server bind successfully");

    FakeChannelHandlers.resetChannelHandlers();
    serverURL = serverURL.addParameter(Constants.HEARTBEAT_KEY, 1000);
    client = Exchangers.connect(serverURL);
    Thread.sleep(10000);
    Assert.assertTrue(handler.connectCount > 0);
    System.out.println("connect count " + handler.connectCount);
}
 
Example 8
Source File: HeartbeatHandlerTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Test
public void testHeartbeat() throws Exception {
    URL serverURL = URL.valueOf("header://localhost:55555");
    serverURL = serverURL.addParameter(Constants.HEARTBEAT_KEY, 1000);
    TestHeartbeatHandler handler = new TestHeartbeatHandler();
    server = Exchangers.bind(serverURL, handler);
    System.out.println("Server bind successfully");

    client = Exchangers.connect(serverURL);
    Thread.sleep(10000);
    System.err.println("++++++++++++++ disconnect count " + handler.disconnectCount);
    System.err.println("++++++++++++++ connect count " + handler.connectCount);
    Assert.assertTrue(handler.disconnectCount == 0);
    Assert.assertTrue(handler.connectCount == 1);
}
 
Example 9
Source File: ExtensionLoader_Adaptive_Test.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Test
public void test_getAdaptiveExtension_customizeAdaptiveKey() throws Exception {
    SimpleExt ext = ExtensionLoader.getExtensionLoader(SimpleExt.class).getAdaptiveExtension();

    Map<String, String> map = new HashMap<String, String>();
    map.put("key2", "impl2");
    URL url = new URL("p1", "1.2.3.4", 1010, "path1", map);

    String echo = ext.yell(url, "haha");
    assertEquals("Ext1Impl2-yell", echo);

    url = url.addParameter("key1", "impl3"); // 注意: URL是值类型
    echo = ext.yell(url, "haha");
    assertEquals("Ext1Impl3-yell", echo);
}
 
Example 10
Source File: CatRegistryFactoryWrapper.java    From dubbo-plus with Apache License 2.0 5 votes vote down vote up
private URL appendProviderAppName(URL url){
    String side = url.getParameter(Constants.SIDE_KEY);
    if(Constants.PROVIDER_SIDE.equals(side)){
        url=url.addParameter(CatConstants.PROVIDER_APPLICATION_NAME,url.getParameter(Constants.APPLICATION_KEY));
    }
    return url;
}
 
Example 11
Source File: TpsLimitFilterTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithoutCount() throws Exception {
    URL url = URL.valueOf("test://test");
    url = url.addParameter(Constants.INTERFACE_KEY,
                           "com.alibaba.dubbo.rpc.file.TpsService");
    url = url.addParameter(Constants.TPS_LIMIT_RATE_KEY, 5);
    Invoker<TpsLimitFilterTest> invoker = new MyInvoker<TpsLimitFilterTest>(url);
    Invocation invocation = new MockInvocation();
    filter.invoke(invoker, invocation);
}
 
Example 12
Source File: HeartbeatHandlerTest.java    From dubbo-remoting-netty4 with Apache License 2.0 5 votes vote down vote up
@Test
public void testHeartbeat() throws Exception {
    URL serverURL = URL.valueOf("header://localhost:55555?transporter=netty4");
    serverURL = serverURL.addParameter(Constants.HEARTBEAT_KEY, 1000);
    TestHeartbeatHandler handler = new TestHeartbeatHandler();
    server = Exchangers.bind(serverURL, handler);
    System.out.println("Server bind successfully");

    client = Exchangers.connect(serverURL);
    Thread.sleep(10000);
    System.err.println("++++++++++++++ disconnect count " + handler.disconnectCount);
    System.err.println("++++++++++++++ connect count " + handler.connectCount);
    Assert.assertTrue(handler.disconnectCount == 0);
    Assert.assertTrue(handler.connectCount == 1);
}
 
Example 13
Source File: RegistryProtocol.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public void notify(List<URL> urls) {
	List<URL> result = null;
	for (URL url : urls) {
		URL overrideUrl = url;
		if (url.getParameter(Constants.CATEGORY_KEY) == null
				&& Constants.OVERRIDE_PROTOCOL.equals(url.getProtocol())) {
			// 兼容旧版本
			overrideUrl = url.addParameter(Constants.CATEGORY_KEY, Constants.CONFIGURATORS_CATEGORY);
		}
		if (! UrlUtils.isMatch(subscribeUrl, overrideUrl)) {
			if (result == null) {
				result = new ArrayList<URL>(urls);
			}
			result.remove(url);
			logger.warn("Subsribe category=configurator, but notifed non-configurator urls. may be registry bug. unexcepted url: " + url);
		}
	}
	if (result != null) {
		urls = result;
	}
	this.configurators = RegistryDirectory.toConfigurators(urls);
    List<ExporterChangeableWrapper<?>> exporters = new ArrayList<ExporterChangeableWrapper<?>>(bounds.values());
    for (ExporterChangeableWrapper<?> exporter : exporters){
        Invoker<?> invoker = exporter.getOriginInvoker();
        final Invoker<?> originInvoker ;
        if (invoker instanceof InvokerDelegete){
            originInvoker = ((InvokerDelegete<?>)invoker).getInvoker();
        }else {
            originInvoker = invoker;
        }
        
        URL originUrl = RegistryProtocol.this.getProviderUrl(originInvoker);
        URL newUrl = getNewInvokerUrl(originUrl, urls);
        
        if (! originUrl.equals(newUrl)){
            RegistryProtocol.this.doChangeLocalExport(originInvoker, newUrl);
        }
    }
}
 
Example 14
Source File: HeartbeatHandlerTest.java    From dubbo3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testHeartbeat() throws Exception {
    URL serverURL = URL.valueOf("header://localhost:55555");
    serverURL = serverURL.addParameter(Constants.HEARTBEAT_KEY, 1000);
    TestHeartbeatHandler handler = new TestHeartbeatHandler();
    server = Exchangers.bind(serverURL, handler);
    System.out.println("Server bind successfully");

    client = Exchangers.connect(serverURL);
    Thread.sleep(10000);
    System.err.println("++++++++++++++ disconnect count " + handler.disconnectCount);
    System.err.println("++++++++++++++ connect count " + handler.connectCount);
    Assert.assertTrue(handler.disconnectCount == 0);
    Assert.assertTrue(handler.connectCount == 1);
}
 
Example 15
Source File: RegistryDirectory.java    From dubbox with Apache License 2.0 5 votes vote down vote up
/**
 * 合并url参数 顺序为override > -D >Consumer > Provider
 * @param providerUrl
 * @param overrides
 * @return
 */
private URL mergeUrl(URL providerUrl){
    providerUrl = ClusterUtils.mergeUrl(providerUrl, queryMap); // 合并消费端参数
    
    List<Configurator> localConfigurators = this.configurators; // local reference
    if (localConfigurators != null && localConfigurators.size() > 0) {
        for (Configurator configurator : localConfigurators) {
            providerUrl = configurator.configure(providerUrl);
        }
    }
    
    providerUrl = providerUrl.addParameter(Constants.CHECK_KEY, String.valueOf(false)); // 不检查连接是否成功,总是创建Invoker!
    
    //directoryUrl 与 override 合并是在notify的最后,这里不能够处理
    this.overrideDirectoryUrl = this.overrideDirectoryUrl.addParametersIfAbsent(providerUrl.getParameters()); // 合并提供者参数        
    
    if ((providerUrl.getPath() == null || providerUrl.getPath().length() == 0)
            && "dubbo".equals(providerUrl.getProtocol())) { // 兼容1.0
        //fix by tony.chenl DUBBO-44
        String path = directoryUrl.getParameter(Constants.INTERFACE_KEY);
        if (path != null) {
            int i = path.indexOf('/');
            if (i >= 0) {
                path = path.substring(i + 1);
            }
            i = path.lastIndexOf(':');
            if (i >= 0) {
                path = path.substring(0, i);
            }
            providerUrl = providerUrl.setPath(path);
        }
    }
    return providerUrl;
}
 
Example 16
Source File: DubboProtocol.java    From dubbo3 with Apache License 2.0 5 votes vote down vote up
/**
 * 创建新连接.
 */
private ExchangeClient initClient(URL url) {

    // client type setting.
    String str = url.getParameter(Constants.CLIENT_KEY, url.getParameter(Constants.SERVER_KEY, Constants.DEFAULT_REMOTING_CLIENT));
    url = url.addParameter(Constants.CODEC_KEY, DubboCodec.NAME);
    //默认开启heartbeat
    url = url.addParameterIfAbsent(Constants.HEARTBEAT_KEY, String.valueOf(Constants.DEFAULT_HEARTBEAT));

    // BIO存在严重性能问题,暂时不允许使用
    if (str != null && str.length() > 0 && !ExtensionLoader.getExtensionLoader(Transporter.class).hasExtension(str)) {
        throw new RpcException("Unsupported client type: " + str + "," +
                " supported client type is " + StringUtils.join(ExtensionLoader.getExtensionLoader(Transporter.class).getSupportedExtensions(), " "));
    }

    ExchangeClient client;
    try {
        //设置连接应该是lazy的
        if (url.getParameter(Constants.LAZY_CONNECT_KEY, false)) {
            client = new LazyConnectExchangeClient(url, requestHandler);
        } else {
            client = Exchangers.connect(url, requestHandler);
        }
    } catch (RemotingException e) {
        throw new RpcException("Fail to create remoting client for service(" + url + "): " + e.getMessage(), e);
    }
    return client;
}
 
Example 17
Source File: ClusterUtilsTest.java    From dubbo3 with Apache License 2.0 4 votes vote down vote up
@Test
public void testMergeUrl() throws Exception {
    URL providerURL = URL.valueOf("dubbo://localhost:55555");
    providerURL = providerURL.setPath("path")
        .setUsername("username")
        .setPassword("password");

    providerURL = providerURL.addParameter(Constants.GROUP_KEY, "dubbo")
        .addParameter(Constants.VERSION_KEY, "1.2.3")
        .addParameter(Constants.DUBBO_VERSION_KEY, "2.3.7")
        .addParameter(Constants.THREADPOOL_KEY, "fixed")
        .addParameter(Constants.THREADS_KEY, Integer.MAX_VALUE)
        .addParameter(Constants.THREAD_NAME_KEY, "test")
        .addParameter(Constants.CORE_THREADS_KEY, Integer.MAX_VALUE)
        .addParameter(Constants.QUEUES_KEY, Integer.MAX_VALUE)
        .addParameter(Constants.ALIVE_KEY, Integer.MAX_VALUE)
        .addParameter(Constants.DEFAULT_KEY_PREFIX + Constants.THREADS_KEY, Integer.MAX_VALUE)
        .addParameter(Constants.DEFAULT_KEY_PREFIX + Constants.THREADPOOL_KEY, "fixed")
        .addParameter(Constants.DEFAULT_KEY_PREFIX + Constants.CORE_THREADS_KEY, Integer.MAX_VALUE)
        .addParameter(Constants.DEFAULT_KEY_PREFIX + Constants.QUEUES_KEY, Integer.MAX_VALUE)
        .addParameter(Constants.DEFAULT_KEY_PREFIX + Constants.ALIVE_KEY, Integer.MAX_VALUE)
        .addParameter(Constants.DEFAULT_KEY_PREFIX + Constants.THREAD_NAME_KEY, "test");

    URL consumerURL = URL.valueOf("dubbo://localhost:55555");
    consumerURL = consumerURL.addParameter(Constants.PID_KEY, "1234");
 consumerURL = consumerURL.addParameter(Constants.THREADPOOL_KEY, "foo");

    URL url = ClusterUtils.mergeUrl(providerURL, consumerURL.getParameters());

    Assert.assertFalse(url.hasParameter(Constants.THREADS_KEY));
    Assert.assertFalse(url.hasParameter(Constants.DEFAULT_KEY_PREFIX + Constants.THREADS_KEY));

    Assert.assertFalse(url.hasParameter(Constants.DEFAULT_KEY_PREFIX + Constants.THREADPOOL_KEY));

    Assert.assertFalse(url.hasParameter(Constants.CORE_THREADS_KEY));
    Assert.assertFalse(url.hasParameter(Constants.DEFAULT_KEY_PREFIX + Constants.CORE_THREADS_KEY));

    Assert.assertFalse(url.hasParameter(Constants.QUEUES_KEY));
    Assert.assertFalse(url.hasParameter(Constants.DEFAULT_KEY_PREFIX + Constants.QUEUES_KEY));

    Assert.assertFalse(url.hasParameter(Constants.ALIVE_KEY));
    Assert.assertFalse(url.hasParameter(Constants.DEFAULT_KEY_PREFIX + Constants.ALIVE_KEY));

    Assert.assertFalse(url.hasParameter(Constants.THREAD_NAME_KEY));
    Assert.assertFalse(url.hasParameter(Constants.DEFAULT_KEY_PREFIX + Constants.THREAD_NAME_KEY));

    Assert.assertEquals(url.getPath(), "path");
    Assert.assertEquals(url.getUsername(), "username");
    Assert.assertEquals(url.getPassword(), "password");
    Assert.assertEquals(url.getParameter(Constants.PID_KEY), "1234");
 Assert.assertEquals(url.getParameter(Constants.THREADPOOL_KEY), "foo");
}
 
Example 18
Source File: ThriftProtocol.java    From dubbox with Apache License 2.0 4 votes vote down vote up
private ExchangeClient initClient(URL url) {

        ExchangeClient client ;

        url = url.addParameter( Constants.CODEC_KEY, ThriftCodec.NAME );

        try {
            client = Exchangers.connect( url );
        } catch ( RemotingException e ) {
            throw new RpcException( "Fail to create remoting client for service(" + url
                                            + "): " + e.getMessage(), e );
        }

        return client;

    }
 
Example 19
Source File: ThriftProtocol.java    From dubbox with Apache License 2.0 4 votes vote down vote up
private ExchangeClient initClient(URL url) {

        ExchangeClient client ;

        url = url.addParameter( Constants.CODEC_KEY, ThriftCodec.NAME );

        try {
            client = Exchangers.connect( url );
        } catch ( RemotingException e ) {
            throw new RpcException( "Fail to create remoting client for service(" + url
                                            + "): " + e.getMessage(), e );
        }

        return client;

    }
 
Example 20
Source File: AbstractInterfaceConfig.java    From dubbox with Apache License 2.0 4 votes vote down vote up
protected List<URL> loadRegistries(boolean provider) {
    checkRegistry();
    List<URL> registryList = new ArrayList<URL>();
    if (registries != null && registries.size() > 0) {
        for (RegistryConfig config : registries) {
            String address = config.getAddress();
            if (address == null || address.length() == 0) {
            	address = Constants.ANYHOST_VALUE;
            }
            String sysaddress = System.getProperty("dubbo.registry.address");
            if (sysaddress != null && sysaddress.length() > 0) {
                address = sysaddress;
            }
            if (address != null && address.length() > 0 
                    && ! RegistryConfig.NO_AVAILABLE.equalsIgnoreCase(address)) {
                Map<String, String> map = new HashMap<String, String>();
                appendParameters(map, application);
                appendParameters(map, config);
                map.put("path", RegistryService.class.getName());
                map.put("dubbo", Version.getVersion());
                map.put(Constants.TIMESTAMP_KEY, String.valueOf(System.currentTimeMillis()));
                if (ConfigUtils.getPid() > 0) {
                    map.put(Constants.PID_KEY, String.valueOf(ConfigUtils.getPid()));
                }
                if (! map.containsKey("protocol")) {
                    if (ExtensionLoader.getExtensionLoader(RegistryFactory.class).hasExtension("remote")) {
                        map.put("protocol", "remote");
                    } else {
                        map.put("protocol", "dubbo");
                    }
                }
                List<URL> urls = UrlUtils.parseURLs(address, map);
                for (URL url : urls) {
                    url = url.addParameter(Constants.REGISTRY_KEY, url.getProtocol());
                    url = url.setProtocol(Constants.REGISTRY_PROTOCOL);
                    if ((provider && url.getParameter(Constants.REGISTER_KEY, true))
                            || (! provider && url.getParameter(Constants.SUBSCRIBE_KEY, true))) {
                        registryList.add(url);
                    }
                }
            }
        }
    }
    return registryList;
}