Search in sources :

Example 16 with Protocol

use of org.apache.dubbo.rpc.Protocol in project dubbo by alibaba.

the class ThriftProtocolTest method testThriftProtocol.

@Test
public void testThriftProtocol() throws TException {
    DemoServiceImpl server = new DemoServiceImpl();
    ProxyFactory proxyFactory = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension();
    Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension();
    int port = NetUtils.getAvailablePort();
    URL url = URL.valueOf(org.apache.dubbo.rpc.protocol.nativethrift.ThriftProtocol.NAME + "://127.0.0.1:" + port + "/" + DemoService.Iface.class.getName() + "?version=1.0.0&nativethrift.overload.method=true");
    Exporter<DemoService.Iface> exporter = protocol.export(proxyFactory.getInvoker(server, DemoService.Iface.class, url));
    Invoker<DemoService.Iface> invoker = protocol.refer(DemoService.Iface.class, url);
    DemoService.Iface client = proxyFactory.getProxy(invoker);
    String result = client.sayHello("haha");
    Assertions.assertTrue(server.isCalled());
    Assertions.assertEquals("Hello, haha", result);
    invoker.destroy();
    exporter.unexport();
}
Also used : ProxyFactory(org.apache.dubbo.rpc.ProxyFactory) Protocol(org.apache.dubbo.rpc.Protocol) URL(org.apache.dubbo.common.URL) Test(org.junit.jupiter.api.Test)

Example 17 with Protocol

use of org.apache.dubbo.rpc.Protocol in project dubbo by alibaba.

the class MockClusterInvokerTest method testMockInvokerInvoke_normal.

/**
 * Test if mock policy works fine: fail-mock
 */
@Test
public void testMockInvokerInvoke_normal() {
    URL url = URL.valueOf("remote://1.2.3.4/" + IHelloService.class.getName());
    url = url.addParameter(MOCK_KEY, "fail").addParameter(REFER_KEY, URL.encode(PATH_KEY + "=" + IHelloService.class.getName()));
    Invoker<IHelloService> cluster = getClusterInvoker(url);
    URL mockUrl = URL.valueOf("mock://localhost/" + IHelloService.class.getName() + "?getSomething.mock=return aa");
    Protocol protocol = new MockProtocol();
    Invoker<IHelloService> mInvoker1 = protocol.refer(IHelloService.class, mockUrl);
    invokers.add(mInvoker1);
    // Configured with mock
    RpcInvocation invocation = new RpcInvocation();
    invocation.setMethodName("getSomething");
    Result ret = cluster.invoke(invocation);
    Assertions.assertEquals("something", ret.getValue());
    // If no mock was configured, return null directly
    invocation = new RpcInvocation();
    invocation.setMethodName("sayHello");
    ret = cluster.invoke(invocation);
    Assertions.assertNull(ret.getValue());
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Protocol(org.apache.dubbo.rpc.Protocol) MockProtocol(org.apache.dubbo.rpc.support.MockProtocol) MockProtocol(org.apache.dubbo.rpc.support.MockProtocol) URL(org.apache.dubbo.common.URL) Result(org.apache.dubbo.rpc.Result) Test(org.junit.jupiter.api.Test)

Example 18 with Protocol

use of org.apache.dubbo.rpc.Protocol in project dubbo by alibaba.

the class MockClusterInvokerTest method testMockInvokerInvoke_forcemock_defaultreturn.

@Test
public void testMockInvokerInvoke_forcemock_defaultreturn() {
    URL url = URL.valueOf("remote://1.2.3.4/" + IHelloService.class.getName());
    url = url.addParameter(MOCK_KEY, "force").addParameter(REFER_KEY, URL.encode(PATH_KEY + "=" + IHelloService.class.getName()));
    Invoker<IHelloService> cluster = getClusterInvoker(url);
    URL mockUrl = URL.valueOf("mock://localhost/" + IHelloService.class.getName() + "?getSomething.mock=return aa&getSomething3xx.mock=return xx&sayHello.mock=return ").addParameters(url.getParameters());
    Protocol protocol = new MockProtocol();
    Invoker<IHelloService> mInvoker1 = protocol.refer(IHelloService.class, mockUrl);
    invokers.add(mInvoker1);
    RpcInvocation invocation = new RpcInvocation();
    invocation.setMethodName("sayHello");
    Result ret = cluster.invoke(invocation);
    Assertions.assertNull(ret.getValue());
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Protocol(org.apache.dubbo.rpc.Protocol) MockProtocol(org.apache.dubbo.rpc.support.MockProtocol) MockProtocol(org.apache.dubbo.rpc.support.MockProtocol) URL(org.apache.dubbo.common.URL) Result(org.apache.dubbo.rpc.Result) Test(org.junit.jupiter.api.Test)

Example 19 with Protocol

use of org.apache.dubbo.rpc.Protocol in project dubbo by alibaba.

the class GenericServiceTest method testGeneric2.

@Test
public void testGeneric2() {
    DemoService server = new DemoServiceImpl();
    ProxyFactory proxyFactory = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension();
    Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension();
    URL url = URL.valueOf("dubbo://127.0.0.1:5342/" + DemoService.class.getName() + "?version=1.0.0&generic=true$timeout=3000");
    Exporter<DemoService> exporter = protocol.export(proxyFactory.getInvoker(server, DemoService.class, url));
    Invoker<GenericService> invoker = protocol.refer(GenericService.class, url);
    GenericService client = proxyFactory.getProxy(invoker, true);
    Object result = client.$invoke("sayHello", new String[] { "java.lang.String" }, new Object[] { "haha" });
    Assertions.assertEquals("hello haha", result);
    Invoker<DemoService> invoker2 = protocol.refer(DemoService.class, url);
    GenericService client2 = (GenericService) proxyFactory.getProxy(invoker2, true);
    Object result2 = client2.$invoke("sayHello", new String[] { "java.lang.String" }, new Object[] { "haha" });
    Assertions.assertEquals("hello haha", result2);
    invoker.destroy();
    exporter.unexport();
}
Also used : GenericService(org.apache.dubbo.rpc.service.GenericService) ProxyFactory(org.apache.dubbo.rpc.ProxyFactory) DemoService(org.apache.dubbo.service.DemoService) ComplexObject(org.apache.dubbo.service.ComplexObject) Protocol(org.apache.dubbo.rpc.Protocol) URL(org.apache.dubbo.common.URL) DemoServiceImpl(org.apache.dubbo.service.DemoServiceImpl) Test(org.junit.jupiter.api.Test)

Example 20 with Protocol

use of org.apache.dubbo.rpc.Protocol in project dubbo by alibaba.

the class GenericServiceTest method testGenericCompatible.

@Test
public void testGenericCompatible() {
    DemoService server = new DemoServiceImpl();
    ProxyFactory proxyFactory = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension();
    Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension();
    URL url = URL.valueOf("dubbo://127.0.0.1:5342/" + DemoService.class.getName() + "?version=1.0.0&generic=true$timeout=3000");
    Exporter<DemoService> exporter = protocol.export(proxyFactory.getInvoker(server, DemoService.class, url));
    // simulate normal invoke
    ReferenceConfig<com.alibaba.dubbo.rpc.service.GenericService> oldReferenceConfig = new ReferenceConfig<>();
    oldReferenceConfig.setGeneric(true);
    oldReferenceConfig.setInterface(DemoService.class.getName());
    oldReferenceConfig.checkAndUpdateSubConfigs();
    Invoker invoker = protocol.refer(oldReferenceConfig.getInterfaceClass(), url);
    com.alibaba.dubbo.rpc.service.GenericService client = (com.alibaba.dubbo.rpc.service.GenericService) proxyFactory.getProxy(invoker, true);
    Object result = client.$invoke("sayHello", new String[] { "java.lang.String" }, new Object[] { "haha" });
    Assertions.assertEquals("hello haha", result);
    invoker.destroy();
    exporter.unexport();
}
Also used : GenericService(org.apache.dubbo.rpc.service.GenericService) ProxyFactory(org.apache.dubbo.rpc.ProxyFactory) DemoService(org.apache.dubbo.service.DemoService) URL(org.apache.dubbo.common.URL) Invoker(org.apache.dubbo.rpc.Invoker) ReferenceConfig(com.alibaba.dubbo.config.ReferenceConfig) ComplexObject(org.apache.dubbo.service.ComplexObject) Protocol(org.apache.dubbo.rpc.Protocol) DemoServiceImpl(org.apache.dubbo.service.DemoServiceImpl) Test(org.junit.jupiter.api.Test)

Aggregations

Protocol (org.apache.dubbo.rpc.Protocol)37 Test (org.junit.jupiter.api.Test)33 URL (org.apache.dubbo.common.URL)32 ProxyFactory (org.apache.dubbo.rpc.ProxyFactory)25 GenericService (org.apache.dubbo.rpc.service.GenericService)10 RpcInvocation (org.apache.dubbo.rpc.RpcInvocation)6 DemoService (org.apache.dubbo.service.DemoService)6 DemoServiceImpl (org.apache.dubbo.service.DemoServiceImpl)6 Result (org.apache.dubbo.rpc.Result)5 DubboProtocol (org.apache.dubbo.rpc.protocol.dubbo.DubboProtocol)5 ComplexObject (org.apache.dubbo.service.ComplexObject)5 RpcException (org.apache.dubbo.rpc.RpcException)4 MockProtocol (org.apache.dubbo.rpc.support.MockProtocol)4 HashMap (java.util.HashMap)3 MetricsService (org.apache.dubbo.monitor.MetricsService)3 RegistryProtocol (org.apache.dubbo.registry.integration.RegistryProtocol)3 IMetricManager (com.alibaba.metrics.IMetricManager)2 MetricObject (com.alibaba.metrics.common.MetricObject)2 Gson (com.google.gson.Gson)2 TypeToken (com.google.gson.reflect.TypeToken)2