Search in sources :

Example 1 with MethodConfig

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

the class BaggageAsyncChainTest method doTest.

@Override
public void doTest() throws Exception {
    ServerConfig serverConfig = new ServerConfig().setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setPort(12299);
    // C服务的服务端
    // C服务的服务端
    CSampleServiceImpl refC = new CSampleServiceImpl();
    ProviderConfig<SampleService> serviceBeanC = new ProviderConfig<SampleService>();
    serviceBeanC.setInterfaceId(SampleService.class.getName());
    serviceBeanC.setApplication(new ApplicationConfig().setAppName("CCC"));
    serviceBeanC.setUniqueId("C5");
    serviceBeanC.setRef(refC);
    serviceBeanC.setServer(serverConfig);
    serviceBeanC.setRegister(false);
    serviceBeanC.export();
    // D服务的服务端
    DSampleServiceImpl refD = new DSampleServiceImpl();
    ProviderConfig<SampleService> serviceBeanD = new ProviderConfig<SampleService>();
    serviceBeanD.setInterfaceId(SampleService.class.getName());
    serviceBeanD.setApplication(new ApplicationConfig().setAppName("DDD"));
    serviceBeanD.setUniqueId("D5");
    serviceBeanD.setRef(refD);
    serviceBeanD.setServer(serverConfig);
    serviceBeanD.setRegister(false);
    serviceBeanD.export();
    // B服务里的C服务客户端
    ConsumerConfig referenceBeanC = new ConsumerConfig();
    referenceBeanC.setApplication(new ApplicationConfig().setAppName("BBB"));
    referenceBeanC.setInterfaceId(SampleService.class.getName());
    referenceBeanC.setUniqueId("C5");
    referenceBeanC.setDirectUrl("localhost:12299");
    referenceBeanC.setTimeout(1000);
    MethodConfig methodConfigC = new MethodConfig().setName("hello").setInvokeType(RpcConstants.INVOKER_TYPE_CALLBACK);
    referenceBeanC.setMethods(Collections.singletonList(methodConfigC));
    SampleService sampleServiceC = (SampleService) referenceBeanC.refer();
    // B服务里的D服务客户端
    ConsumerConfig referenceBeanD = new ConsumerConfig();
    referenceBeanD.setApplication(new ApplicationConfig().setAppName("BBB"));
    referenceBeanD.setInterfaceId(SampleService.class.getName());
    referenceBeanD.setUniqueId("D5");
    referenceBeanD.setDirectUrl("localhost:12299?p=1&v=4.0");
    referenceBeanD.setTimeout(1000);
    SampleService sampleServiceD = (SampleService) referenceBeanD.refer();
    // B服务的服务端
    BAsyncChainSampleServiceImpl refB = new BAsyncChainSampleServiceImpl(sampleServiceC, sampleServiceD);
    ProviderConfig<SampleService> ServiceBeanB = new ProviderConfig<SampleService>();
    ServiceBeanB.setInterfaceId(SampleService.class.getName());
    ServiceBeanB.setApplication(new ApplicationConfig().setAppName("BBB"));
    ServiceBeanB.setUniqueId("B5");
    ServiceBeanB.setRef(refB);
    ServiceBeanB.setServer(serverConfig);
    ServiceBeanB.setRegister(false);
    ServiceBeanB.export();
    // A 服务
    final String[] str = new String[1];
    final CountDownLatch[] latch = new CountDownLatch[] { new CountDownLatch(1) };
    final RpcInvokeContext[] contexts = new RpcInvokeContext[1];
    ConsumerConfig referenceBeanA = new ConsumerConfig();
    referenceBeanA.setApplication(new ApplicationConfig().setAppName("AAA"));
    referenceBeanA.setUniqueId("B5");
    referenceBeanA.setInterfaceId(SampleService.class.getName());
    referenceBeanA.setDirectUrl("localhost:12299");
    referenceBeanA.setTimeout(3000);
    MethodConfig methodConfigA = new MethodConfig().setName("hello").setInvokeType(RpcConstants.INVOKER_TYPE_CALLBACK);
    final Exception[] exp = new Exception[1];
    methodConfigA.setOnReturn(new SofaResponseCallback() {

        @Override
        public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
            try {
                // 主线程和子线程中的 Context 必须不是同一个,否则有并发问题
                Assert.assertNotSame(RpcInvokeContext.getContext(), contexts[0]);
                str[0] = (String) appResponse;
                latch[0].countDown();
            } catch (Exception e) {
                exp[0] = e;
            }
        }

        @Override
        public void onAppException(Throwable t, String methodName, RequestBase request) {
            Assert.assertEquals("sampleService", t.getMessage());
            Assert.assertEquals("sayException", methodName);
        }

        @Override
        public void onSofaException(SofaRpcException sofaException, String methodName, RequestBase request) {
            // never go to this
            Assert.assertEquals("sampleService", sofaException.getMessage());
            Assert.assertEquals("sayException", methodName);
        }
    });
    referenceBeanA.setMethods(Collections.singletonList(methodConfigA));
    SampleService service = (SampleService) referenceBeanA.refer();
    // 开始测试
    RpcInvokeContext context = RpcInvokeContext.getContext();
    contexts[0] = context;
    context.putRequestBaggage("reqBaggageB", "a2bbb");
    context.putRequestBaggage("reqBaggageC", "a2ccc");
    context.putRequestBaggage("reqBaggageD", "a2ddd");
    LOGGER.info("-----A1-----" + RpcInvokeContext.getContext());
    // 测试传递数据
    String ret = service.hello();
    Assert.assertEquals(ret, null);
    latch[0].await(5000, TimeUnit.MILLISECONDS);
    if (exp[0] != null) {
        throw exp[0];
    }
    ret = str[0];
    Assert.assertEquals(ret, "hello world chello world d");
    Assert.assertEquals(refB.getReqBaggage(), "a2bbb");
    Assert.assertEquals(refC.getReqBaggage(), "a2ccc");
    Assert.assertEquals(refD.getReqBaggage(), "a2ddd");
    Assert.assertNotNull(context.getResponseBaggage("respBaggageB_useful1"));
    Assert.assertNotNull(context.getResponseBaggage("respBaggageB_useful2"));
    Assert.assertNull(context.getResponseBaggage("respBaggageB_useless1"));
    Assert.assertNull(context.getResponseBaggage("respBaggageB_useless2"));
    // 支持
    Assert.assertEquals(context.getResponseBaggage("respBaggageB"), "b2aaa");
    // 支持
    Assert.assertEquals(context.getResponseBaggage("respBaggageC"), "c2aaa");
    // 支持
    Assert.assertEquals(context.getResponseBaggage("respBaggageD"), "d2aaa");
    Assert.assertNull(context.getResponseBaggage("respBaggageB_force"));
    Assert.assertNull(context.getResponseBaggage("respBaggageC_force"));
    Assert.assertNull(context.getResponseBaggage("respBaggageD_force"));
    RpcInvokeContext.removeContext();
    context = RpcInvokeContext.getContext();
    contexts[0] = context;
    latch[0] = new CountDownLatch(1);
    str[0] = null;
    ret = null;
    LOGGER.info("-----A3-----" + RpcInvokeContext.getContext());
    // 测试不传数据
    ret = service.hello();
    Assert.assertEquals(ret, null);
    latch[0].await(5000, TimeUnit.MILLISECONDS);
    ret = str[0];
    Assert.assertEquals(ret, "hello world chello world d");
    Assert.assertNull(refB.getReqBaggage());
    Assert.assertNull(refC.getReqBaggage());
    Assert.assertNull(refD.getReqBaggage());
    Assert.assertNotNull(context.getResponseBaggage("respBaggageB_useful1"));
    Assert.assertNotNull(context.getResponseBaggage("respBaggageB_useful2"));
    Assert.assertNull(context.getResponseBaggage("respBaggageB_useless1"));
    Assert.assertNull(context.getResponseBaggage("respBaggageB_useless2"));
    Assert.assertNull(context.getResponseBaggage("respBaggageB"));
    Assert.assertNull(context.getResponseBaggage("respBaggageC"));
    Assert.assertNull(context.getResponseBaggage("respBaggageD"));
    Assert.assertEquals(context.getResponseBaggage("respBaggageB_force"), "b2aaaff");
    Assert.assertEquals(context.getResponseBaggage("respBaggageC_force"), "c2aaaff");
    Assert.assertEquals(context.getResponseBaggage("respBaggageD_force"), "d2aaaff");
}
Also used : RpcInvokeContext(com.alipay.sofa.rpc.context.RpcInvokeContext) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) SofaResponseCallback(com.alipay.sofa.rpc.core.invoke.SofaResponseCallback) CountDownLatch(java.util.concurrent.CountDownLatch) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) RequestBase(com.alipay.sofa.rpc.core.request.RequestBase) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) MethodConfig(com.alipay.sofa.rpc.config.MethodConfig) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig)

Example 2 with MethodConfig

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

the class BaggageOnewayTest method doTest.

@Override
void doTest() {
    ServerConfig serverConfig = new ServerConfig().setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setPort(12299);
    // C服务的服务端
    CSampleServiceImpl refC = new CSampleServiceImpl();
    ProviderConfig<SampleService> serviceBeanC = new ProviderConfig<SampleService>();
    serviceBeanC.setInterfaceId(SampleService.class.getName());
    serviceBeanC.setApplication(new ApplicationConfig().setAppName("CCC"));
    serviceBeanC.setUniqueId("C4");
    serviceBeanC.setRef(refC);
    serviceBeanC.setServer(serverConfig);
    serviceBeanC.setRegister(false);
    serviceBeanC.export();
    // D服务的服务端
    DSampleServiceImpl refD = new DSampleServiceImpl();
    ProviderConfig<SampleService> serviceBeanD = new ProviderConfig<SampleService>();
    serviceBeanD.setInterfaceId(SampleService.class.getName());
    serviceBeanD.setApplication(new ApplicationConfig().setAppName("DDD"));
    serviceBeanD.setUniqueId("D4");
    serviceBeanD.setRef(refD);
    serviceBeanD.setServer(serverConfig);
    serviceBeanD.setRegister(false);
    serviceBeanD.export();
    // B服务里的C服务客户端
    ConsumerConfig referenceBeanC = new ConsumerConfig();
    referenceBeanC.setApplication(new ApplicationConfig().setAppName("BBB"));
    referenceBeanC.setInterfaceId(SampleService.class.getName());
    referenceBeanC.setUniqueId("C4");
    referenceBeanC.setDirectUrl("localhost:12299");
    referenceBeanC.setTimeout(1000);
    SampleService sampleServiceC = (SampleService) referenceBeanC.refer();
    // B服务里的D服务客户端
    ConsumerConfig referenceBeanD = new ConsumerConfig();
    referenceBeanD.setApplication(new ApplicationConfig().setAppName("BBB"));
    referenceBeanD.setInterfaceId(SampleService.class.getName());
    referenceBeanD.setUniqueId("D4");
    referenceBeanD.setDirectUrl("localhost:12299?p=1&v=4.0");
    referenceBeanD.setTimeout(1000);
    SampleService sampleServiceD = (SampleService) referenceBeanD.refer();
    // B服务的服务端
    BSampleServiceImpl refB = new BSampleServiceImpl(sampleServiceC, sampleServiceD);
    ProviderConfig<SampleService> ServiceBeanB = new ProviderConfig<SampleService>();
    ServiceBeanB.setInterfaceId(SampleService.class.getName());
    ServiceBeanB.setApplication(new ApplicationConfig().setAppName("BBB"));
    ServiceBeanB.setUniqueId("B4");
    ServiceBeanB.setRef(refB);
    ServiceBeanB.setServer(serverConfig);
    ServiceBeanB.setRegister(false);
    ServiceBeanB.export();
    // A 服务
    ConsumerConfig referenceBeanA = new ConsumerConfig();
    referenceBeanA.setApplication(new ApplicationConfig().setAppName("AAA"));
    referenceBeanA.setUniqueId("B4");
    referenceBeanA.setInterfaceId(SampleService.class.getName());
    referenceBeanA.setDirectUrl("localhost:12299");
    referenceBeanA.setTimeout(3000);
    MethodConfig methodConfigA = new MethodConfig().setName("hello").setInvokeType(RpcConstants.INVOKER_TYPE_ONEWAY);
    referenceBeanA.setMethods(Collections.singletonList(methodConfigA));
    SampleService service = (SampleService) referenceBeanA.refer();
    // 开始测试
    RpcInvokeContext context = RpcInvokeContext.getContext();
    context.putRequestBaggage("reqBaggageB", "a2bbb");
    context.putRequestBaggage("reqBaggageC", "a2ccc");
    context.putRequestBaggage("reqBaggageD", "a2ddd");
    String ret = service.hello();
    Assert.assertNull(ret);
    try {
        Thread.sleep(1000);
    } catch (Exception e) {
        e.printStackTrace();
    }
    Assert.assertEquals(refB.getReqBaggage(), "a2bbb");
    Assert.assertEquals(refC.getReqBaggage(), "a2ccc");
    Assert.assertEquals(refD.getReqBaggage(), "a2ddd");
    // 反方向不行
    Assert.assertNull(context.getResponseBaggage("respBaggageB"));
    Assert.assertNull(context.getResponseBaggage("respBaggageC"));
    Assert.assertNull(context.getResponseBaggage("respBaggageD"));
    Assert.assertNull(context.getResponseBaggage("respBaggageB_force"));
    Assert.assertNull(context.getResponseBaggage("respBaggageC_force"));
    Assert.assertNull(context.getResponseBaggage("respBaggageD_force"));
    RpcInvokeContext.removeContext();
    context = RpcInvokeContext.getContext();
    ret = service.hello();
    Assert.assertNull(ret);
    try {
        Thread.sleep(1000);
    } catch (Exception e) {
        e.printStackTrace();
    }
    Assert.assertNull(refB.getReqBaggage());
    Assert.assertNull(refC.getReqBaggage());
    Assert.assertNull(refD.getReqBaggage());
    // 反方向不行
    Assert.assertNull(context.getResponseBaggage("respBaggageB"));
    Assert.assertNull(context.getResponseBaggage("respBaggageC"));
    Assert.assertNull(context.getResponseBaggage("respBaggageD"));
    Assert.assertNull(context.getResponseBaggage("respBaggageB_force"));
    Assert.assertNull(context.getResponseBaggage("respBaggageC_force"));
    Assert.assertNull(context.getResponseBaggage("respBaggageD_force"));
}
Also used : RpcInvokeContext(com.alipay.sofa.rpc.context.RpcInvokeContext) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) MethodConfig(com.alipay.sofa.rpc.config.MethodConfig) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig)

Example 3 with MethodConfig

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

the class GenericTest method testAll.

@Test
public void testAll() throws SofaRpcException, InterruptedException {
    // 发布服务
    ServerConfig serverConfig2 = new ServerConfig().setPort(22222).setDaemon(false);
    List<MethodConfig> methodConfigs = new ArrayList<MethodConfig>();
    MethodConfig config1 = new MethodConfig().setName("helloFuture").setInvokeType("future");
    methodConfigs.add(config1);
    MethodConfig config2 = new MethodConfig().setName("helloCallback").setInvokeType("callback").setOnReturn(new TestCallback());
    methodConfigs.add(config2);
    MethodConfig config21 = new MethodConfig().setName("helloCallbackException").setInvokeType("callback").setOnReturn(new TestCallback());
    methodConfigs.add(config21);
    MethodConfig config3 = new MethodConfig().setName("helloOneway").setInvokeType("oneway");
    methodConfigs.add(config3);
    MethodConfig config4 = new MethodConfig().setName("helloTimeout").setInvokeType("sync");
    methodConfigs.add(config4);
    // C服务的服务端
    ProviderConfig<TestInterface> CProvider = new ProviderConfig<TestInterface>().setInterfaceId(TestInterface.class.getName()).setRef(new TestClass()).setServer(serverConfig2);
    CProvider.export();
    // 引用服务
    ConsumerConfig<GenericService> BConsumer = new ConsumerConfig<GenericService>().setInterfaceId(TestInterface.class.getName()).setGeneric(true).setMethods(methodConfigs).setDirectUrl("bolt://127.0.0.1:22222").setTimeout(3000).setRetries(2);
    GenericService proxy = BConsumer.refer();
    GenericObject genericObject = new GenericObject("com.alipay.sofa.rpc.test.generic.bean.People");
    genericObject.putField("name", "Lilei");
    genericObject.putField("job", new Job("coder"));
    People people = new People();
    people.setName("Lilei");
    people.setJob(new Job("coder"));
    // sync 调用
    assertEquals(proxy.$invoke("hello", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { people }), new TestClass().hello(people));
    People peopleResult = proxy.$genericInvoke("hello", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { genericObject }, People.class);
    assertEquals(peopleResult, new TestClass().hello(people));
    GenericObject result = (GenericObject) proxy.$genericInvoke("hello", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { genericObject });
    isCorrect(result);
    // future 调用
    proxy.$invoke("helloFuture", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { people });
    assertEquals(SofaResponseFuture.getResponse(1000, true), new TestClass().helloFuture(people));
    proxy.$genericInvoke("helloFuture", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { genericObject }, People.class);
    assertEquals(SofaResponseFuture.getResponse(1000, true), new TestClass().helloFuture(people));
    proxy.$genericInvoke("helloFuture", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { genericObject });
    result = (GenericObject) SofaResponseFuture.getResponse(1000, true);
    isCorrect(result);
    // callback调用
    proxy.$invoke("helloCallback", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { people });
    TestCallback.startLatach();
    assertEquals(TestCallback.result, new TestClass().helloCallback(people));
    proxy.$genericInvoke("helloCallback", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { genericObject }, People.class);
    TestCallback.startLatach();
    assertEquals(TestCallback.result, new TestClass().helloCallback(people));
    proxy.$genericInvoke("helloCallback", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { genericObject });
    TestCallback.startLatach();
    isCorrect((GenericObject) TestCallback.result);
    TestCallback.result = null;
    // oneway调用
    proxy.$invoke("helloOneway", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { people });
    proxy.$genericInvoke("helloOneway", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { genericObject }, People.class);
    proxy.$genericInvoke("helloOneway", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { genericObject });
    // callback出现异常
    proxy.$invoke("helloCallbackException", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { people });
    TestCallback.startLatach();
    Assert.assertEquals(((Throwable) TestCallback.result).getMessage(), "Hello~");
    proxy.$genericInvoke("helloCallbackException", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { genericObject }, People.class);
    TestCallback.startLatach();
    Assert.assertEquals(((Throwable) TestCallback.result).getMessage(), "Hello~");
    proxy.$genericInvoke("helloCallbackException", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { genericObject });
    TestCallback.startLatach();
    Assert.assertEquals(((Throwable) TestCallback.result).getMessage(), "Hello~");
    testTimeout(proxy, genericObject, people);
    testComplexBean(proxy);
    testBasicBean(proxy);
}
Also used : GenericService(com.alipay.sofa.rpc.api.GenericService) ArrayList(java.util.ArrayList) People(com.alipay.sofa.rpc.test.generic.bean.People) MethodConfig(com.alipay.sofa.rpc.config.MethodConfig) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) GenericObject(com.alipay.hessian.generic.model.GenericObject) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) GenericObject(com.alipay.hessian.generic.model.GenericObject) Job(com.alipay.sofa.rpc.test.generic.bean.Job) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Example 4 with MethodConfig

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

the class RpcLookoutTest method before.

/**
 * invoke
 */
@Before
public void before() {
    final Registry currentRegistry = Lookout.registry();
    // clear all metrics now
    Iterator<Metric> itar = currentRegistry.iterator();
    while (itar.hasNext()) {
        Metric metric = itar.next();
        Id id = metric.id();
        currentRegistry.removeMetric(id);
    }
    serverConfig = new ServerConfig().setPort(12201).setCoreThreads(30).setMaxThreads(500).setQueues(600).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT);
    providerConfig = new ProviderConfig<LookoutService>().setInterfaceId(LookoutService.class.getName()).setRef(new LookoutServiceImpl()).setServer(serverConfig).setBootstrap("bolt").setRegister(false).setApplication(new ApplicationConfig().setAppName("TestLookOutServer"));
    providerConfig.export();
    MethodConfig methodConfigFuture = new MethodConfig().setName("sayFuture").setInvokeType("future");
    onReturn = new CountSofaResponseCallback();
    MethodConfig methodConfigCallback = new MethodConfig().setName("sayCallback").setInvokeType("callback").setOnReturn(onReturn);
    MethodConfig methodConfigOneway = new MethodConfig().setName("sayOneway").setInvokeType("oneway");
    List<MethodConfig> methodConfigs = new ArrayList<MethodConfig>();
    methodConfigs.add(methodConfigFuture);
    methodConfigs.add(methodConfigCallback);
    methodConfigs.add(methodConfigOneway);
    consumerConfig = new ConsumerConfig<LookoutService>().setInterfaceId(LookoutService.class.getName()).setProtocol("bolt").setBootstrap("bolt").setMethods(methodConfigs).setTimeout(3000).setRegister(false).setDirectUrl("bolt://127.0.0.1:12201?appName=TestLookOutServer").setApplication(new ApplicationConfig().setAppName("TestLookOutClient"));
    lookoutService = consumerConfig.refer();
}
Also used : ArrayList(java.util.ArrayList) DefaultRegistry(com.alipay.lookout.core.DefaultRegistry) NoopRegistry(com.alipay.lookout.api.NoopRegistry) Registry(com.alipay.lookout.api.Registry) MethodConfig(com.alipay.sofa.rpc.config.MethodConfig) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) Metric(com.alipay.lookout.api.Metric) Id(com.alipay.lookout.api.Id) Before(org.junit.Before)

Example 5 with MethodConfig

use of com.alipay.sofa.rpc.config.MethodConfig 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

MethodConfig (com.alipay.sofa.rpc.config.MethodConfig)24 ApplicationConfig (com.alipay.sofa.rpc.config.ApplicationConfig)14 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)12 ArrayList (java.util.ArrayList)11 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)10 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)7 SofaResponseCallback (com.alipay.sofa.rpc.core.invoke.SofaResponseCallback)7 RegistryConfig (com.alipay.sofa.rpc.config.RegistryConfig)5 Map (java.util.Map)5 Test (org.junit.Test)5 SofaBootRpcRuntimeException (com.alipay.sofa.rpc.boot.common.SofaBootRpcRuntimeException)4 RpcBindingMethodInfo (com.alipay.sofa.rpc.boot.runtime.binding.RpcBindingMethodInfo)4 RpcBindingParam (com.alipay.sofa.rpc.boot.runtime.param.RpcBindingParam)4 RpcInvokeContext (com.alipay.sofa.rpc.context.RpcInvokeContext)4 Filter (com.alipay.sofa.rpc.filter.Filter)4 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)3 RequestBase (com.alipay.sofa.rpc.core.request.RequestBase)3 DemoService (com.alipay.sofa.rpc.bootstrap.dubbo.demo.DemoService)2 DemoServiceImpl (com.alipay.sofa.rpc.bootstrap.dubbo.demo.DemoServiceImpl)2 UserThreadPool (com.alipay.sofa.rpc.server.UserThreadPool)2