Dubbo 并发调优的几个参数

https://blog.csdn.net/JDream314/article/details/44590937

 

欢迎加入DUBBO交流群:259566260
消费端调优: 
一、connections 
这个参数可以在服务提供端发布服务的时候配置,也可以在消费端引用服务的时候配置,但是这个值是只对消费端生效的,所以一般是服务提供端不建议配置,如果配置,请斟酌一下,详情请查看《对connections参数的设置 》。不管是在消费端或者服务提供端,如果对某个服务配置了connections参数,并且该参数大于1,那么就会导致消费端在创建该服务的远程socketclient的时候(如果是dubbo协议的话)将会给该服务初始化一个私有的socketclient。所以一般不建议对这个值进行调整。 
服务端优化调整: 
相对余消费端,服务端调优的参数相对多一些,但是配置的时候也需要谨慎。 
一、executes 
这个参数是可以精确到方法级别的参数,就是可以指定调用远程接口某个方法的是该参数的值。具体是怎么配置的可以到官方文档里面去看看那,这里只是描述这个参数实际意义以及使用的时候应该注意点。 

要说这个参数,就要所介绍ExecuteLimitFilter,他是这个参数使用者,看到Filter大家就应该懂了,就是在每个方法请求前后加上业务逻辑。下面贴出里面的代码: 

 

 
  1. @Activate(group = Constants.PROVIDER, value = Constants.EXECUTES_KEY)

  2. public class ExecuteLimitFilter implements Filter {

  3.  
  4. public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {

  5. URL url = invoker.getUrl();

  6. String methodName = invocation.getMethodName();

  7. int max = url.getMethodParameter(methodName, Constants.EXECUTES_KEY, 0);

  8. if (max > 0) {

  9. RpcStatus count = RpcStatus.getStatus(url, invocation.getMethodName());

  10. if (count.getActive() >= max) {

  11. throw new RpcException("Failed to invoke method " + invocation.getMethodName() + " in provider " + url + ", cause: The service using threads greater than <dubbo:service executes=\"" + max + "\" /> limited.");

  12. }

  13. }

  14. long begin = System.currentTimeMillis();

  15. boolean isException = false;

  16. RpcStatus.beginCount(url, methodName);

  17. try {

  18. Result result = invoker.invoke(invocation);

  19. return result;

  20. } catch (Throwable t) {

  21. isException = true;

  22. if(t instanceof RuntimeException) {

  23. throw (RuntimeException) t;

  24. }

  25. else {

  26. throw new RpcException("unexpected exception when ExecuteLimitFilter", t);

  27. }

  28. }

  29. finally {

  30. RpcStatus.endCount(url, methodName, System.currentTimeMillis() - begin, isException);

  31. }

  32. }

  33.  
  34. }


上面这段代码主要是看两个地方,分别是第7行和第10行,第7行是获取配置的executes的值,是一个int类型的,描述数量,然后第10行是获取当前请求方法当前的状态,里面既有一个active属性(该属性是AtomacInteger类型的,大家应该懂了为什么用这个类型),表示当前请求的方法处于执行状态的线程数量,如果这个值大于或者等于配置的值那么直接抛出异常,那么消费端就会收到一个RPC的异常导致调用服务失败,这是这个参数最终导致的效果。   
二、actives   
这个参数基本上和excetes一样,但是有一点不同,在说这不同之前,还是看看另一个Filter,看名字你们应该就知道它是做什么的了—— ActiveLimitFilter,下面同样贴出代码:  

 

 

 
  1. @Activate(group = Constants.CONSUMER, value = Constants.ACTIVES_KEY)

  2. public class ActiveLimitFilter implements Filter {

  3.  
  4. public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {

  5. URL url = invoker.getUrl();

  6. String methodName = invocation.getMethodName();

  7. int max = invoker.getUrl().getMethodParameter(methodName, Constants.ACTIVES_KEY, 0);

  8. RpcStatus count = RpcStatus.getStatus(invoker.getUrl(), invocation.getMethodName());

  9. if (max > 0) {

  10. long timeout = invoker.getUrl().getMethodParameter(invocation.getMethodName(), Constants.TIMEOUT_KEY, 0);

  11. long start = System.currentTimeMillis();

  12. long remain = timeout;

  13. int active = count.getActive();

  14. if (active >= max) {

  15. synchronized (count) {

  16. while ((active = count.getActive()) >= max) {

  17. try {

  18. count.wait(remain);

  19. } catch (InterruptedException e) {

  20. }

  21. long elapsed = System.currentTimeMillis() - start;

  22. remain = timeout - elapsed;

  23. if (remain <= 0) {

  24. throw new RpcException("Waiting concurrent invoke timeout in client-side for service: "

  25. + invoker.getInterface().getName() + ", method: "

  26. + invocation.getMethodName() + ", elapsed: " + elapsed

  27. + ", timeout: " + timeout + ". concurrent invokes: " + active

  28. + ". max concurrent invoke limit: " + max);

  29. }

  30. }

  31. }

  32. }

  33. }

  34. try {

  35. long begin = System.currentTimeMillis();

  36. RpcStatus.beginCount(url, methodName);

  37. try {

  38. Result result = invoker.invoke(invocation);

  39. RpcStatus.endCount(url, methodName, System.currentTimeMillis() - begin, true);

  40. return result;

  41. } catch (RuntimeException t) {

  42. RpcStatus.endCount(url, methodName, System.currentTimeMillis() - begin, false);

  43. throw t;

  44. }

  45. } finally {

  46. if(max>0){

  47. synchronized (count) {

  48. count.notify();

  49. }

  50. }

  51. }

  52. }

  53.  
  54. }

上面代码大致上和executes一样,唯一不同的就是多了一个等待时间,当当前执行该方法的线程超出了最大限制,那么可以等待一个timeout时间,如果时间过了还是超出了最大限制,那么就抛出异常。这个相对余executes来说温柔那么点。这就是那点不同!   
三、accepts 
在看代码之前先看看这个参数的意思,这个参数是告知服务提供端能接收多少个消费端连接该服务提供方。下面接着上代码,这个参数的体现是在类AbstractServer中。代码如下:  

 

要说这个参数,就要所介绍ExecuteLimitFilter,他是这个参数使用者,看到Filter大家就应该懂了,就是在每个方法请求前后加上业务逻辑。下面贴出里面的代码: 

 

 
  1. @Override

  2. public void connected(Channel ch) throws RemotingException {

  3. Collection<Channel> channels = getChannels();

  4. if (accepts > 0 && channels.size() > accepts) {

  5. logger.error("Close channel " + ch + ", cause: The server " + ch.getLocalAddress() + " connections greater than max config " + accepts);

  6. ch.close();

  7. return;

  8. }

  9. super.connected(ch);

  10. }


这个方法是每个消费端向服务提供端创建一个socket连接的时候都会触发,上面可以清晰看到如果连接当前服务端的消费端数量超出了配置的值,那么将会关闭当前消费端连接的请求。这个只是对socket连接的数量限制,而不是像上面两个参数对调用线程的配置。   


以上归纳出的几个参数建议服务端生效的在服务端配置,消费端生效的在消费端配置,不然会导致一些不可控的现象出现。这也叫改哪里的东西就应该在哪里,而不能乱放。

 

个人分类: Java专栏

JavaScript大神之路第一季

从0开始学习JavaScript,从入门到进阶高级编程

  • qq_31309517

    qq_313095172016-04-26 14:53:39#1楼

    个人感觉actives是控制客户端线程数的,executes是控制server端线程数的,这是本质区别。但是如果在server端设置actives参数,不知道dubbo是不是通过注册中心将该参数传给client端生效的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值