Search in sources :

Example 96 with ApplicationConfig

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

the class SofaRegistryTest method testAll.

@Test
public void testAll() throws Exception {
    int timeoutPerSub = 5000;
    serverConfig1 = new ServerConfig().setProtocol("bolt").setHost("0.0.0.0").setPort(12200);
    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(serverConfig1).setWeight(222).setTimeout(3000);
    // 注册
    registry.register(provider);
    consumer1 = new ConsumerConfig();
    consumer1.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 = SofaRegistryHelper.buildListDataId(provider, serverConfig1.getProtocol());
    String tag1 = SofaRegistryHelper.buildListDataId(consumer1, consumer1.getProtocol());
    Assert.assertEquals(tag1, tag0);
    // 订阅
    CountDownLatch latch = new CountDownLatch(2);
    MockProviderInfoListener providerInfoListener = new MockProviderInfoListener();
    providerInfoListener.setCountDownLatch(latch);
    consumer1.setProviderInfoListener(providerInfoListener);
    registry.subscribe(consumer1);
    latch.await(timeoutPerSub, TimeUnit.MILLISECONDS);
    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() > 0);
    // 反注册
    latch = new CountDownLatch(1);
    providerInfoListener.setCountDownLatch(latch);
    registry.unRegister(provider);
    latch.await(timeoutPerSub, TimeUnit.MILLISECONDS);
    Assert.assertEquals(ps.toString(), 1, ps.size());
    Assert.assertEquals(0, ps.get(RpcConstants.ADDRESS_DEFAULT_GROUP).size());
    // 一次发2个端口的再次注册
    latch = new CountDownLatch(2);
    providerInfoListener.setCountDownLatch(latch);
    serverConfig2 = new ServerConfig().setProtocol("bolt").setHost("0.0.0.0").setPort(12201);
    provider.getServer().add(serverConfig2);
    registry.register(provider);
    latch.await(timeoutPerSub * 2, TimeUnit.MILLISECONDS);
    Assert.assertTrue(ps.size() > 0);
    Assert.assertNotNull(ps.get(RpcConstants.ADDRESS_DEFAULT_GROUP));
    Assert.assertEquals(2, ps.get(RpcConstants.ADDRESS_DEFAULT_GROUP).size());
    // 重复订阅
    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);
    MockProviderInfoListener providerInfoListener2 = new MockProviderInfoListener();
    providerInfoListener2.setCountDownLatch(latch2);
    consumer2.setProviderInfoListener(providerInfoListener2);
    registry.subscribe(consumer2);
    latch2.await(timeoutPerSub * 2, TimeUnit.MILLISECONDS);
    Map<String, ProviderGroup> ps2 = providerInfoListener2.getData();
    Assert.assertTrue(ps2.size() > 0);
    Assert.assertNotNull(ps2.get(RpcConstants.ADDRESS_DEFAULT_GROUP));
    Assert.assertEquals(2, ps2.get(RpcConstants.ADDRESS_DEFAULT_GROUP).size());
    Assert.assertEquals(1, registry.subscribers.size());
    Assert.assertEquals(1, registry.configurators.size());
    // 取消订阅者1
    registry.unSubscribe(consumer1);
    SofaRegistrySubscribeCallback callback = (SofaRegistrySubscribeCallback) registry.subscribers.get(tag1).getDataObserver();
    Assert.assertFalse(callback.providerInfoListeners.contains(consumer1));
    Assert.assertEquals(1, registry.subscribers.size());
    Assert.assertEquals(1, registry.configurators.size());
    // 批量反注册,判断订阅者2的数据
    latch = new CountDownLatch(2);
    providerInfoListener2.setCountDownLatch(latch);
    List<ProviderConfig> providerConfigList = new ArrayList<ProviderConfig>();
    providerConfigList.add(provider);
    registry.batchUnRegister(providerConfigList);
    latch.await(timeoutPerSub * 2, TimeUnit.MILLISECONDS);
    Assert.assertEquals(ps2.toString(), 1, ps2.size());
    Assert.assertEquals(0, ps2.get(RpcConstants.ADDRESS_DEFAULT_GROUP).size());
    // 1个服务 订阅服务列表和服务配置 2个dataId
    Assert.assertEquals(1, registry.subscribers.size());
    Assert.assertEquals(1, registry.configurators.size());
    // 批量取消订阅
    List<ConsumerConfig> consumerConfigList = new ArrayList<ConsumerConfig>();
    consumerConfigList.add(consumer2);
    registry.batchUnSubscribe(consumerConfigList);
    Assert.assertEquals(0, registry.subscribers.size());
    Assert.assertEquals(0, registry.configurators.size());
}
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) Test(org.junit.Test)

Example 97 with ApplicationConfig

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

the class SofaRegistryHelperTest method convertProviderToUrls.

@Test
public void convertProviderToUrls() throws Exception {
    ServerConfig serverConfig = new ServerConfig().setHost("0.0.0.0").setPort(22000).setProtocol("bolt");
    ServerConfig serverConfig2 = new ServerConfig().setHost("127.0.0.1").setPort(12200).setProtocol("tr");
    ServerConfig serverConfig3 = new ServerConfig().setHost("192.1.1.1").setPort(8080).setProtocol("xfire");
    ProviderConfig<?> providerConfig = new ProviderConfig();
    providerConfig.setInterfaceId("com.alipay.sofa.rpc.test.TestService").setUniqueId("qqqq").setApplication(new ApplicationConfig().setAppName("xxxx")).setTimeout(4444).setWeight(250).setServer(Arrays.asList(serverConfig, serverConfig2));
    MethodConfig methodConfig = new MethodConfig().setName("echo").setTimeout(3333);
    MethodConfig methodConfig2 = new MethodConfig().setName("xx").setTimeout(2222);
    providerConfig.setMethods(Arrays.asList(methodConfig, methodConfig2));
    String s1 = SofaRegistryHelper.convertProviderToUrls(providerConfig, serverConfig);
    Assert.assertNotNull(s1);
    ProviderInfo providerInfo = SofaRegistryHelper.parseProviderInfo(s1);
    Assert.assertEquals(SystemInfo.getLocalHost(), providerInfo.getHost());
    Assert.assertEquals(serverConfig.getPort(), providerInfo.getPort());
    Assert.assertEquals(providerConfig.getAppName(), providerInfo.getAttr(ProviderInfoAttrs.ATTR_APP_NAME));
    Assert.assertEquals(providerConfig.getTimeout(), providerInfo.getDynamicAttr(ProviderInfoAttrs.ATTR_TIMEOUT));
    String s2 = SofaRegistryHelper.convertProviderToUrls(providerConfig, serverConfig2);
    Assert.assertNotNull(s2);
    ProviderInfo providerInfo2 = SofaRegistryHelper.parseProviderInfo(s2);
    Assert.assertEquals(SystemInfo.getLocalHost(), providerInfo.getHost());
    Assert.assertEquals(serverConfig2.getPort(), providerInfo2.getPort());
    Assert.assertEquals(providerConfig.getAppName(), providerInfo2.getAttr(ProviderInfoAttrs.ATTR_APP_NAME));
    Assert.assertEquals(providerConfig.getTimeout(), providerInfo2.getDynamicAttr(ProviderInfoAttrs.ATTR_TIMEOUT));
    String s3 = SofaRegistryHelper.convertProviderToUrls(providerConfig, serverConfig3);
    Assert.assertNotNull(s3);
    ProviderInfo providerInfo3 = SofaRegistryHelper.parseProviderInfo(s3);
    Assert.assertEquals(serverConfig3.getHost(), providerInfo3.getHost());
    Assert.assertEquals(serverConfig3.getPort(), providerInfo3.getPort());
    Assert.assertEquals(providerConfig.getAppName(), providerInfo3.getAttr(ProviderInfoAttrs.ATTR_APP_NAME));
    Assert.assertEquals(providerConfig.getTimeout(), providerInfo3.getDynamicAttr(ProviderInfoAttrs.ATTR_TIMEOUT));
}
Also used : MethodConfig(com.alipay.sofa.rpc.config.MethodConfig) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ProviderInfo(com.alipay.sofa.rpc.client.ProviderInfo) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) Test(org.junit.Test)

Example 98 with ApplicationConfig

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

the class ZookeeperRegistryTest method testConfigObserver.

/**
 * 测试Zookeeper Config Observer
 *
 * @throws Exception
 */
@Test
public void testConfigObserver() throws InterruptedException {
    ServerConfig serverConfig = new ServerConfig().setProtocol("bolt").setHost("0.0.0.0").setPort(12200);
    ProviderConfig<?> providerConfig = new ProviderConfig();
    providerConfig.setInterfaceId(TEST_SERVICE_NAME).setUniqueId("unique123Id").setApplication(new ApplicationConfig().setAppName("test-server")).setProxy("javassist").setRegister(true).setRegistry(registryConfig).setSerialization("hessian2").setServer(serverConfig).setWeight(222).setTimeout(3000);
    // 注册Provider Config
    registry.register(providerConfig);
    // 订阅Provider Config
    CountDownLatch latch = new CountDownLatch(1);
    MockConfigListener configListener = new MockConfigListener();
    configListener.setCountDownLatch(latch);
    registry.subscribeConfig(providerConfig, configListener);
    configListener.attrUpdated(Collections.singletonMap("timeout", "2000"));
    Map<String, String> configData = configListener.getData();
    Assert.assertEquals(1, configData.size());
    configListener.attrUpdated(Collections.singletonMap("uniqueId", "unique234Id"));
    configData = configListener.getData();
    Assert.assertEquals(2, configData.size());
    ConsumerConfig<?> consumerConfig = new ConsumerConfig();
    consumerConfig.setInterfaceId(TEST_SERVICE_NAME).setUniqueId("unique123Id").setApplication(new ApplicationConfig().setAppName("test-server")).setProxy("javassist").setSubscribe(true).setSerialization("java").setInvokeType("sync").setTimeout(4444);
    // 订阅Consumer Config
    latch = new CountDownLatch(1);
    configListener = new MockConfigListener();
    configListener.setCountDownLatch(latch);
    registry.subscribeConfig(consumerConfig, configListener);
    configListener.attrUpdated(Collections.singletonMap(RpcConstants.CONFIG_KEY_TIMEOUT, "3333"));
    configData = configListener.getData();
    Assert.assertEquals(1, configData.size());
    configListener.attrUpdated(Collections.singletonMap("uniqueId", "unique234Id"));
    configData = configListener.getData();
    Assert.assertEquals(2, configData.size());
    latch.await(2000, TimeUnit.MILLISECONDS);
    Assert.assertEquals(2, configData.size());
    registry.unRegister(providerConfig);
}
Also used : ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) CountDownLatch(java.util.concurrent.CountDownLatch) BaseZkTest(com.alipay.sofa.rpc.registry.zk.base.BaseZkTest) Test(org.junit.Test)

Example 99 with ApplicationConfig

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

the class ZookeeperRegistryTest method testOverrideObserver.

/**
 * 测试Zookeeper Override Observer
 *
 * @throws Exception
 */
@Test
public void testOverrideObserver() throws InterruptedException {
    ConsumerConfig<?> consumerConfig = new ConsumerConfig();
    consumerConfig.setInterfaceId(TEST_SERVICE_NAME).setUniqueId("unique123Id").setApplication(new ApplicationConfig().setAppName("test-server")).setProxy("javassist").setSubscribe(true).setSerialization("java").setInvokeType("sync").setTimeout(4444);
    // 订阅Consumer Config
    CountDownLatch latch = new CountDownLatch(1);
    MockConfigListener configListener = new MockConfigListener();
    configListener.setCountDownLatch(latch);
    registry.subscribeOverride(consumerConfig, configListener);
    Map<String, String> attributes = new ConcurrentHashMap<String, String>();
    attributes.put(RpcConstants.CONFIG_KEY_TIMEOUT, "3333");
    attributes.put(RpcConstants.CONFIG_KEY_APP_NAME, "test-server");
    attributes.put(RpcConstants.CONFIG_KEY_SERIALIZATION, "java");
    configListener.attrUpdated(attributes);
    Map<String, String> configData = configListener.getData();
    Assert.assertEquals(3, configData.size());
    consumerConfig.setInterfaceId(TEST_SERVICE_NAME).setUniqueId("unique123Id").setApplication(new ApplicationConfig().setAppName("test-server1")).setProxy("javassist").setSubscribe(true).setSerialization("java").setInvokeType("sync").setTimeout(5555);
    configListener = new MockConfigListener();
    configListener.setCountDownLatch(latch);
    registry.subscribeOverride(consumerConfig, configListener);
    attributes.put(RpcConstants.CONFIG_KEY_TIMEOUT, "4444");
    attributes.put(RpcConstants.CONFIG_KEY_APP_NAME, "test-server2");
    configListener.attrUpdated(attributes);
    configData = configListener.getData();
    Assert.assertEquals(3, configData.size());
    latch.await(2000, TimeUnit.MILLISECONDS);
    Assert.assertEquals(3, configData.size());
}
Also used : ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) CountDownLatch(java.util.concurrent.CountDownLatch) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) BaseZkTest(com.alipay.sofa.rpc.registry.zk.base.BaseZkTest) Test(org.junit.Test)

Example 100 with ApplicationConfig

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

the class SingleTripleDemo method main.

public static void main(String[] args) {
    RpcRunningState.setDebugMode(true);
    ApplicationConfig clientApp = new ApplicationConfig().setAppName("triple-client");
    ApplicationConfig serverApp = new ApplicationConfig().setAppName("triple-server");
    int port = 50052;
    if (args.length != 0) {
        LOGGER.debug("first arg is {}", args[0]);
        port = Integer.valueOf(args[0]);
    }
    RegistryConfig registryConfig = new RegistryConfig().setProtocol("zookeeper").setAddress("127.0.0.1:2181");
    ServerConfig serverConfig = new ServerConfig().setProtocol(RpcConstants.PROTOCOL_TYPE_TRIPLE).setPort(port);
    ProviderConfig<SofaGreeterTriple.IGreeter> providerConfig = new ProviderConfig<SofaGreeterTriple.IGreeter>().setApplication(serverApp).setBootstrap(RpcConstants.PROTOCOL_TYPE_TRIPLE).setInterfaceId(SofaGreeterTriple.IGreeter.class.getName()).setRef(new TripleGreeterImpl()).setServer(serverConfig).setRegistry(registryConfig);
    providerConfig.export();
    ConsumerConfig<SofaGreeterTriple.IGreeter> consumerConfig = new ConsumerConfig<SofaGreeterTriple.IGreeter>();
    consumerConfig.setInterfaceId(SofaGreeterTriple.IGreeter.class.getName()).setProtocol(RpcConstants.PROTOCOL_TYPE_TRIPLE).setRegistry(registryConfig).setApplication(clientApp);
    SofaGreeterTriple.IGreeter greeterBlockingStub = consumerConfig.refer();
    LOGGER.info("Grpc stub bean successful: {}", greeterBlockingStub.getClass().getName());
    LOGGER.info("Will try to greet " + "world" + " ...");
    HelloRequest.DateTime dateTime = HelloRequest.DateTime.newBuilder().setDate("2018-12-28").setTime("11:13:00").build();
    HelloRequest request = HelloRequest.newBuilder().setName("world").build();
    HelloReply reply = null;
    while (true) {
        try {
            try {
                HelloRequest.DateTime reqDateTime = HelloRequest.DateTime.newBuilder(dateTime).setTime("").build();
                request = HelloRequest.newBuilder(request).setName("world").setDateTime(reqDateTime).build();
                reply = greeterBlockingStub.sayHello(request);
                LOGGER.info("Invoke Success,Greeting: {}, {}", reply.getMessage(), reply.getDateTime().getDate());
            } catch (StatusRuntimeException e) {
                LOGGER.error("RPC failed: {}", e.getStatus());
            } catch (Throwable e) {
                LOGGER.error("Unexpected RPC call breaks", e);
            }
        } catch (Exception e) {
            LOGGER.error("Unexpected RPC call breaks", e);
        }
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
Also used : RegistryConfig(com.alipay.sofa.rpc.config.RegistryConfig) SofaGreeterTriple(io.grpc.examples.helloworld.SofaGreeterTriple) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) HelloRequest(io.grpc.examples.helloworld.HelloRequest) StatusRuntimeException(io.grpc.StatusRuntimeException) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) StatusRuntimeException(io.grpc.StatusRuntimeException) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) HelloReply(io.grpc.examples.helloworld.HelloReply)

Aggregations

ApplicationConfig (com.alipay.sofa.rpc.config.ApplicationConfig)113 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)77 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)64 Test (org.junit.Test)53 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)52 HelloService (com.alipay.sofa.rpc.test.HelloService)30 ActivelyDestroyTest (com.alipay.sofa.rpc.test.ActivelyDestroyTest)21 CountDownLatch (java.util.concurrent.CountDownLatch)20 HelloServiceImpl (com.alipay.sofa.rpc.test.HelloServiceImpl)15 MethodConfig (com.alipay.sofa.rpc.config.MethodConfig)14 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)14 SofaResponseCallback (com.alipay.sofa.rpc.core.invoke.SofaResponseCallback)13 ArrayList (java.util.ArrayList)12 RegistryConfig (com.alipay.sofa.rpc.config.RegistryConfig)11 RequestBase (com.alipay.sofa.rpc.core.request.RequestBase)11 ProviderGroup (com.alipay.sofa.rpc.client.ProviderGroup)10 EchoService (com.alipay.sofa.rpc.test.EchoService)10 EchoRequest (com.alipay.sofa.rpc.server.bolt.pb.EchoRequest)8 DemoServiceImpl (com.alipay.sofa.rpc.bootstrap.dubbo.demo.DemoServiceImpl)7 Filter (com.alipay.sofa.rpc.filter.Filter)7