Search in sources :

Example 51 with ConsumerConfig

use of com.alipay.sofa.rpc.config.ConsumerConfig in project sofa-rpc by sofastack.

the class DubboConsumerBootstrapTest method setUp.

@Before
public void setUp() {
    ApplicationConfig clientApplication = new ApplicationConfig();
    clientApplication.setAppName("client");
    ConsumerConfig consumerConfig = new ConsumerConfig<DemoService>().setInterfaceId(DemoService.class.getName()).setDirectUrl("dubbo://127.0.0.1:20880").setTimeout(30000).setRegister(false).setProtocol("dubbo").setBootstrap("dubbo").setApplication(clientApplication).setInvokeType(RpcConstants.INVOKER_TYPE_ONEWAY);
    consumerConfig.setParameter("version", "1.0.1");
    dubboConsumerBootstrap = new DubboConsumerBootstrap<>(consumerConfig);
}
Also used : ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) Before(org.junit.Before)

Example 52 with ConsumerConfig

use of com.alipay.sofa.rpc.config.ConsumerConfig in project sofa-rpc by sofastack.

the class DubooServerTest method testOneWay.

@Test
public // 单向调用
void testOneWay() {
    // 只有1个线程 执行
    ServerConfig serverConfig = new ServerConfig().setStopTimeout(60000).setPort(20880).setProtocol("dubbo").setQueues(100).setCoreThreads(1).setMaxThreads(2);
    // 发布一个服务,每个请求要执行1秒
    ApplicationConfig serverApplacation = new ApplicationConfig();
    serverApplacation.setAppName("server");
    providerConfig = new ProviderConfig<DemoService>().setInterfaceId(DemoService.class.getName()).setRef(new DemoServiceImpl()).setServer(serverConfig).setBootstrap("dubbo").setRegister(false).setApplication(serverApplacation);
    providerConfig.export();
    ApplicationConfig clientApplication = new ApplicationConfig();
    clientApplication.setAppName("client");
    List<MethodConfig> methodConfigs = new ArrayList<MethodConfig>();
    MethodConfig methodConfig = new MethodConfig();
    methodConfig.setInvokeType(RpcConstants.INVOKER_TYPE_ONEWAY);
    methodConfig.setName("sayHello");
    methodConfigs.add(methodConfig);
    consumerConfig = new ConsumerConfig<DemoService>().setInterfaceId(DemoService.class.getName()).setDirectUrl("dubbo://127.0.0.1:20880").setTimeout(30000).setRegister(false).setProtocol("dubbo").setBootstrap("dubbo").setApplication(clientApplication).setInvokeType(RpcConstants.INVOKER_TYPE_ONEWAY).setMethods(methodConfigs);
    final DemoService demoService = consumerConfig.refer();
    String tmp = demoService.sayHello("xxx");
    Assert.assertEquals(null, tmp);
}
Also used : MethodConfig(com.alipay.sofa.rpc.config.MethodConfig) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ArrayList(java.util.ArrayList) DemoService(com.alipay.sofa.rpc.bootstrap.dubbo.demo.DemoService) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) DemoServiceImpl(com.alipay.sofa.rpc.bootstrap.dubbo.demo.DemoServiceImpl) Test(org.junit.Test)

Example 53 with ConsumerConfig

use of com.alipay.sofa.rpc.config.ConsumerConfig in project sofa-rpc by sofastack.

the class LocalRegistryTest method testAll.

@Test
public void testAll() throws Exception {
    // test for notifyConsumer
    notifyConsumerTest();
    int timeoutPerSub = 5000;
    ServerConfig serverConfig = new ServerConfig().setProtocol("bolt").setHost("0.0.0.0").setPort(12200);
    ProviderConfig<?> provider = new ProviderConfig();
    provider.setInterfaceId("com.alipay.xxx.TestService").setUniqueId("unique123Id").setApplication(new ApplicationConfig().setAppName("test-server")).setProxy("javassist").setRegister(true).setRegistry(registryConfig).setSerialization("hessian2").setServer(serverConfig).setWeight(222).setTimeout(3000);
    // 注册
    registry.register(provider);
    ConsumerConfig<?> consumer = new ConsumerConfig();
    consumer.setInterfaceId("com.alipay.xxx.TestService").setUniqueId("unique123Id").setApplication(new ApplicationConfig().setAppName("test-server")).setProxy("javassist").setSubscribe(true).setSerialization("java").setInvokeType("sync").setTimeout(4444);
    String tag0 = LocalRegistryHelper.buildListDataId(provider, serverConfig.getProtocol());
    String tag1 = LocalRegistryHelper.buildListDataId(consumer, consumer.getProtocol());
    Assert.assertEquals(tag1, tag0);
    String content = FileUtils.file2String(new File(file));
    Assert.assertTrue(content.startsWith(tag0));
    // 订阅
    LocalRegistryTest.MockProviderInfoListener providerInfoListener = new LocalRegistryTest.MockProviderInfoListener();
    consumer.setProviderInfoListener(providerInfoListener);
    List<ProviderGroup> groups = registry.subscribe(consumer);
    providerInfoListener.updateAllProviders(groups);
    Map<String, ProviderGroup> ps = providerInfoListener.getData();
    Assert.assertTrue(ps.size() > 0);
    Assert.assertNotNull(ps.get(RpcConstants.ADDRESS_DEFAULT_GROUP));
    Assert.assertTrue(ps.get(RpcConstants.ADDRESS_DEFAULT_GROUP).size() == 1);
    // 反注册
    CountDownLatch latch = new CountDownLatch(1);
    providerInfoListener.setCountDownLatch(latch);
    registry.unRegister(provider);
    latch.await(timeoutPerSub, TimeUnit.MILLISECONDS);
    Assert.assertTrue(ps.size() > 0);
    Assert.assertNotNull(ps.get(RpcConstants.ADDRESS_DEFAULT_GROUP));
    Assert.assertTrue(ps.get(RpcConstants.ADDRESS_DEFAULT_GROUP).size() == 0);
    // 一次发2个端口的再次注册
    latch = new CountDownLatch(1);
    providerInfoListener.setCountDownLatch(latch);
    provider.getServer().add(new ServerConfig().setProtocol("bolt").setHost("0.0.0.0").setPort(12201));
    registry.register(provider);
    latch.await(timeoutPerSub * 2, TimeUnit.MILLISECONDS);
    Assert.assertTrue(ps.size() > 0);
    Assert.assertNotNull(ps.get(RpcConstants.ADDRESS_DEFAULT_GROUP));
    Assert.assertTrue(ps.get(RpcConstants.ADDRESS_DEFAULT_GROUP).size() == 2);
    // 重复订阅
    ConsumerConfig<?> consumer2 = new ConsumerConfig();
    consumer2.setInterfaceId("com.alipay.xxx.TestService").setUniqueId("unique123Id").setApplication(new ApplicationConfig().setAppName("test-server")).setProxy("javassist").setSubscribe(true).setSerialization("java").setInvokeType("sync").setTimeout(4444);
    CountDownLatch latch2 = new CountDownLatch(1);
    LocalRegistryTest.MockProviderInfoListener providerInfoListener2 = new LocalRegistryTest.MockProviderInfoListener();
    providerInfoListener2.setCountDownLatch(latch2);
    consumer2.setProviderInfoListener(providerInfoListener2);
    List<ProviderGroup> groups2 = registry.subscribe(consumer2);
    providerInfoListener2.updateAllProviders(groups2);
    Map<String, ProviderGroup> ps2 = providerInfoListener2.getData();
    Assert.assertTrue(ps2.size() > 0);
    Assert.assertNotNull(ps2.get(RpcConstants.ADDRESS_DEFAULT_GROUP));
    Assert.assertTrue(ps2.get(RpcConstants.ADDRESS_DEFAULT_GROUP).size() == 2);
    Assert.assertTrue(registry.memoryCache.get(tag1).size() == 2);
    // 取消订阅者1
    registry.unSubscribe(consumer);
    List<ConsumerConfig> callback = registry.notifyListeners.get(tag1);
    Assert.assertFalse(callback.contains(consumer));
    Assert.assertTrue(callback.size() == 1);
    // 批量反注册,判断订阅者2的数据
    latch = new CountDownLatch(1);
    providerInfoListener2.setCountDownLatch(latch);
    List<ProviderConfig> providerConfigList = new ArrayList<ProviderConfig>();
    providerConfigList.add(provider);
    registry.batchUnRegister(providerConfigList);
    latch.await(timeoutPerSub, TimeUnit.MILLISECONDS);
    Assert.assertTrue(ps2.size() > 0);
    Assert.assertNotNull(ps2.get(RpcConstants.ADDRESS_DEFAULT_GROUP));
    Assert.assertTrue(ps2.get(RpcConstants.ADDRESS_DEFAULT_GROUP).size() == 0);
    Assert.assertTrue(registry.notifyListeners.size() == 1);
    // 批量取消订阅
    List<ConsumerConfig> consumerConfigList = new ArrayList<ConsumerConfig>();
    consumerConfigList.add(consumer2);
    registry.batchUnSubscribe(consumerConfigList);
    Assert.assertTrue(registry.notifyListeners.size() == 0);
}
Also used : ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) ProviderGroup(com.alipay.sofa.rpc.client.ProviderGroup) File(java.io.File) Test(org.junit.Test)

Example 54 with ConsumerConfig

use of com.alipay.sofa.rpc.config.ConsumerConfig in project sofa-rpc by sofastack.

the class LocalRegistryTest method testLoadFile.

@Test
public void testLoadFile() {
    ServerConfig serverConfig = new ServerConfig().setProtocol("bolt").setHost("0.0.0.0").setPort(12200);
    ProviderConfig<?> provider = new ProviderConfig();
    provider.setInterfaceId("com.alipay.xxx.TestService").setUniqueId("unique123Id").setRegister(true).setRegistry(registryConfig).setServer(serverConfig);
    registry.register(provider);
    registry.destroy();
    // registry 关闭,但是 provider 信息保存到本地
    Assert.assertTrue(new File(file).exists());
    // 创建一个新的 localRegistry,会立即加载到缓存
    RegistryConfig newRegistryConfig = new RegistryConfig().setProtocol("local").setSubscribe(true).setFile(file).setRegister(true);
    LocalRegistry newRegistry = (LocalRegistry) RegistryFactory.getRegistry(newRegistryConfig);
    newRegistry.init();
    Assert.assertFalse(newRegistry.memoryCache.isEmpty());
    // consumer 订阅时应该能立刻读到数据
    ConsumerConfig<?> consumer = new ConsumerConfig();
    consumer.setInterfaceId("com.alipay.xxx.TestService").setUniqueId("unique123Id").setRegistry(registryConfig).setSubscribe(true);
    List<ProviderGroup> subscribe = newRegistry.subscribe(consumer);
    Assert.assertFalse(subscribe.isEmpty());
    Assert.assertFalse(subscribe.get(0).getProviderInfos().isEmpty());
}
Also used : RegistryConfig(com.alipay.sofa.rpc.config.RegistryConfig) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) ProviderGroup(com.alipay.sofa.rpc.client.ProviderGroup) File(java.io.File) Test(org.junit.Test)

Example 55 with ConsumerConfig

use of com.alipay.sofa.rpc.config.ConsumerConfig in project sofa-rpc by sofastack.

the class MeshRouter method needToLoad.

@Override
public boolean needToLoad(ConsumerBootstrap consumerBootstrap) {
    ConsumerConfig consumerConfig = consumerBootstrap.getConsumerConfig();
    // 不是直连,且从注册中心订阅配置
    final boolean isDirect = StringUtils.isNotBlank(consumerConfig.getDirectUrl());
    final List<RegistryConfig> registrys = consumerConfig.getRegistry();
    boolean isMesh = false;
    if (registrys != null) {
        for (RegistryConfig registry : registrys) {
            if (registry.getProtocol().equalsIgnoreCase(RpcConstants.REGISTRY_PROTOCOL_MESH)) {
                isMesh = true;
                break;
            }
        }
    }
    return !isDirect && isMesh;
}
Also used : RegistryConfig(com.alipay.sofa.rpc.config.RegistryConfig) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig)

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