本小节测试一下限流的功能,首先我们先定义一个简单的类,配置一下它单位时间最大的调用次数
package org.laopopo.example.generic.test_5;
import org.laopopo.client.annotation.RPCService;
import org.laopopo.example.demo.service.HelloSerivce;
public class HelloServiceFlowControllerImpl implements HelloSerivce {
@Override
@RPCService(responsibilityName="xiaoy",serviceName="LAOPOPO.TEST.SAYHELLO",maxCallCountInMinute = 40)
public String sayHello(String str) {
return "hello "+ str;
}
}
package org.laopopo.example.generic.test_5;
import org.laopopo.client.provider.DefaultProvider;
import org.laopopo.common.exception.remoting.RemotingException;
/**
*
* @author BazingaLyn
* @description 测试单位时间的限流
* @time 2016年9月14日
* @modifytime
*/
public class ProviderTest {
public static void main(String[] args) throws InterruptedException, RemotingException {
DefaultProvider defaultProvider = new DefaultProvider();
defaultProvider.serviceListenPort(8899) //暴露服务的地址
.publishService(new HelloServiceFlowControllerImpl()) //暴露的服务
.start(); //启动服务
}
}
消费者的代码基本上也不用改变:
package org.laopopo.example.generic.test_5;
import org.laopopo.client.consumer.ConsumerClient;
import org.laopopo.client.consumer.proxy.ProxyFactory;
import org.laopopo.common.utils.UnresolvedAddress;
/**
*
* @author BazingaLyn
* @description 测试
* @time
* @modifytime
*/
public class ConsumerTest {
public static void main(String[] args) throws Exception {
ConsumerClient client = new ConsumerClient();
client.start();
UnresolvedAddress addresses = new UnresolvedAddress("127.0.0.1", 8899);
HelloService helloService = ProxyFactory.factory(HelloService.class).consumer(client).addProviderAddress(addresses).timeoutMillis(3000l).newProxyInstance();
for(int index = 1;index < 45;index++){
String str = helloService.sayHello("Lyncc");
System.out.println("当前调用的次数是:" + index);
System.out.println(str);
}
}
}
先运行服务提供者之后,再运行服务消费者:
基本上是没有问题的~