use of com.alipay.sofa.rpc.config.ConsumerConfig in project sofa-rpc by sofastack.
the class RejectedTest method testAll.
@Test
public void testAll() {
ServerConfig serverConfig = new ServerConfig().setStopTimeout(0).setPort(22222).setQueues(0).setCoreThreads(1).setMaxThreads(2);
// 发布一个服务,每个请求要执行1秒
ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl(1000)).setServer(serverConfig).setRegister(false);
providerConfig.export();
ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setTimeout(3000).setDirectUrl("bolt://127.0.0.1:22222").setRegister(false);
final HelloService helloService = consumerConfig.refer();
final AtomicInteger success = new AtomicInteger();
final AtomicInteger failure = new AtomicInteger();
int times = 3;
final CountDownLatch latch = new CountDownLatch(times);
for (int i = 0; i < times; i++) {
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
try {
helloService.sayHello("xxx", 22);
success.incrementAndGet();
} catch (Exception e) {
if (e instanceof SofaRpcException) {
Assert.assertEquals(((SofaRpcException) e).getErrorType(), RpcErrorType.SERVER_BUSY);
}
failure.incrementAndGet();
} finally {
latch.countDown();
}
}
}, "T1");
thread1.start();
}
try {
latch.await(10000, TimeUnit.MILLISECONDS);
} catch (InterruptedException ignore) {
}
Assert.assertEquals(success.get(), 2);
Assert.assertEquals(failure.get(), 1);
}
use of com.alipay.sofa.rpc.config.ConsumerConfig in project sofa-rpc by sofastack.
the class BootstrapsTest method from1.
@Test
public void from1() throws Exception {
ConsumerConfig consumerConfig = new ConsumerConfig().setProtocol("test").setBootstrap("test");
ConsumerBootstrap bootstrap = Bootstraps.from(consumerConfig);
Assert.assertEquals(TestConsumerBootstrap.class, bootstrap.getClass());
Assert.assertEquals(consumerConfig, bootstrap.getConsumerConfig());
// if not set bootstrap
consumerConfig = new ConsumerConfig().setProtocol("test");
bootstrap = Bootstraps.from(consumerConfig);
Assert.assertEquals(TestConsumerBootstrap.class, bootstrap.getClass());
Assert.assertEquals(consumerConfig, bootstrap.getConsumerConfig());
// if not set bootstrap and not exist
consumerConfig = new ConsumerConfig().setProtocol("xx");
bootstrap = Bootstraps.from(consumerConfig);
Assert.assertEquals(TestConsumerBootstrap.class, bootstrap.getClass());
Assert.assertEquals(consumerConfig, bootstrap.getConsumerConfig());
}
use of com.alipay.sofa.rpc.config.ConsumerConfig in project sofa-rpc by sofastack.
the class LoadBalancerFactoryTest method getLoadBalancer.
@Test
public void getLoadBalancer() throws Exception {
ConsumerConfig consumerConfig = new ConsumerConfig().setBootstrap("test").setLoadBalancer("test");
ConsumerBootstrap bootstrap = Bootstraps.from(consumerConfig);
Assert.assertEquals(LoadBalancerFactory.getLoadBalancer(bootstrap).getClass(), TestLoadBalancer.class);
boolean error = false;
try {
consumerConfig.setLoadBalancer("xasdsa");
LoadBalancerFactory.getLoadBalancer(bootstrap);
} catch (Exception e) {
error = true;
}
Assert.assertTrue(error);
}
use of com.alipay.sofa.rpc.config.ConsumerConfig in project sofa-rpc by sofastack.
the class ServerB method main.
public static void main(String[] args) {
// B服务里的C服务客户端
ConsumerConfig<ServiceC> consumerConfig = new ConsumerConfig<ServiceC>().setApplication(new ApplicationConfig().setAppName("BBB")).setInterfaceId(ServiceC.class.getName()).setDirectUrl("bolt://127.0.0.1:12299?appName=CCC").setRegister(false).setInvokeType(// 不设置,调用级别可设置
"callback").setTimeout(2000);
ServiceC serviceC = consumerConfig.refer();
ServerConfig serverConfig = new ServerConfig().setPort(12298).setDaemon(false);
ProviderConfig<ServiceB> providerConfig = new ProviderConfig<ServiceB>().setInterfaceId(ServiceB.class.getName()).setApplication(new ApplicationConfig().setAppName("BBB")).setRef(new ServiceBImpl(serviceC)).setServer(serverConfig).setRegister(false);
providerConfig.export();
}
use of com.alipay.sofa.rpc.config.ConsumerConfig in project sofa-rpc by sofastack.
the class DubboServerTest method testRegistrySync.
@Test
public // 同步调用,走服务注册中心
void testRegistrySync() {
// 只有1个线程 执行
ServerConfig serverConfig = new ServerConfig().setStopTimeout(10).setPort(20880).setProtocol("dubbo").setQueues(100).setCoreThreads(1).setMaxThreads(2).setHost(SystemInfo.getLocalHost());
// 发布一个服务,每个请求要执行1秒
ApplicationConfig serverApplacation = new ApplicationConfig();
serverApplacation.setAppName("server");
List<RegistryConfig> registryConfigs = new ArrayList<RegistryConfig>();
RegistryConfig registryConfig;
registryConfig = new RegistryConfig().setProtocol(RpcConstants.REGISTRY_PROTOCOL_ZK).setAddress("127.0.0.1:2181").setSubscribe(true).setRegister(true);
List<MethodConfig> methodConfigs = new ArrayList<MethodConfig>();
MethodConfig methodConfig = new MethodConfig();
methodConfig.setTimeout(3000);
methodConfig.setName("sayHello");
methodConfigs.add(methodConfig);
registryConfigs.add(registryConfig);
ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl()).setServer(serverConfig).setRegister(true).setBootstrap("dubbo").setRegistry(registryConfigs).setApplication(serverApplacation);
providerConfig.export();
ApplicationConfig clientApplication = new ApplicationConfig();
clientApplication.setAppName("client");
ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setTimeout(30000).setRegister(true).setProtocol("dubbo").setApplication(clientApplication).setRegistry(registryConfigs).setMethods(methodConfigs).setInJVM(false);
final HelloService demoService = consumerConfig.refer();
String result = demoService.sayHello("xxx", 22);
Assert.assertNotNull(result);
ConsumerBootstrap bootstrap = consumerConfig.getConsumerBootstrap();
Assert.assertTrue(bootstrap instanceof DubboConsumerBootstrap);
Assert.assertTrue(bootstrap.isSubscribed());
Assert.assertNotNull(bootstrap.getProxyIns());
bootstrap.unRefer();
try {
bootstrap.getCluster();
Assert.fail();
} catch (Exception e) {
Assert.assertTrue(e instanceof UnsupportedOperationException);
}
try {
bootstrap.subscribe();
Assert.fail();
} catch (Exception e) {
Assert.assertTrue(e instanceof UnsupportedOperationException);
}
}
Aggregations