Search in sources :

Example 46 with ConsumerConfig

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);
}
Also used : ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) HelloService(com.alipay.sofa.rpc.test.HelloService) HelloServiceImpl(com.alipay.sofa.rpc.test.HelloServiceImpl) CountDownLatch(java.util.concurrent.CountDownLatch) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Example 47 with ConsumerConfig

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());
}
Also used : ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) Test(org.junit.Test)

Example 48 with ConsumerConfig

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);
}
Also used : ConsumerBootstrap(com.alipay.sofa.rpc.bootstrap.ConsumerBootstrap) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) Test(org.junit.Test)

Example 49 with ConsumerConfig

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();
}
Also used : ServiceC(com.alipay.sofa.rpc.asynchain.ServiceC) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ServiceB(com.alipay.sofa.rpc.asynchain.ServiceB) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ServiceBImpl(com.alipay.sofa.rpc.asynchain.ServiceBImpl) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig)

Example 50 with ConsumerConfig

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);
    }
}
Also used : RegistryConfig(com.alipay.sofa.rpc.config.RegistryConfig) ArrayList(java.util.ArrayList) HelloService(com.alipay.sofa.rpc.test.HelloService) HelloServiceImpl(com.alipay.sofa.rpc.test.HelloServiceImpl) MethodConfig(com.alipay.sofa.rpc.config.MethodConfig) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ConsumerBootstrap(com.alipay.sofa.rpc.bootstrap.ConsumerBootstrap) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) Test(org.junit.Test) BaseZkTest(com.alipay.sofa.rpc.registry.base.BaseZkTest)

Aggregations

ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)139 Test (org.junit.Test)86 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)68 ApplicationConfig (com.alipay.sofa.rpc.config.ApplicationConfig)61 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)44 ActivelyDestroyTest (com.alipay.sofa.rpc.test.ActivelyDestroyTest)40 HelloService (com.alipay.sofa.rpc.test.HelloService)38 CountDownLatch (java.util.concurrent.CountDownLatch)27 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)21 HelloServiceImpl (com.alipay.sofa.rpc.test.HelloServiceImpl)21 ArrayList (java.util.ArrayList)19 ProviderGroup (com.alipay.sofa.rpc.client.ProviderGroup)18 SofaResponseCallback (com.alipay.sofa.rpc.core.invoke.SofaResponseCallback)14 RegistryConfig (com.alipay.sofa.rpc.config.RegistryConfig)12 RequestBase (com.alipay.sofa.rpc.core.request.RequestBase)12 MethodConfig (com.alipay.sofa.rpc.config.MethodConfig)10 ProviderInfo (com.alipay.sofa.rpc.client.ProviderInfo)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 ConsumerBootstrap (com.alipay.sofa.rpc.bootstrap.ConsumerBootstrap)8 SofaTimeOutException (com.alipay.sofa.rpc.core.exception.SofaTimeOutException)8