Skip to content
Permalink
Browse files
embedded call in async provider (#419)
  • Loading branch information
chickenlj committed Jan 27, 2022
1 parent f37e0bb commit 70f556eac935720b529cdf5418a0932ef66773fe
Showing 8 changed files with 226 additions and 4 deletions.
@@ -22,3 +22,70 @@ props:
zookeeper_port: 2181
dubbo_port: 20880


# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


services:
zookeeper:
image: zookeeper:latest

async-provider:
type: app
basedir: dubbo-samples-async-provider
mainClass: org.apache.dubbo.samples.async.AsyncProvider
systemProps:
- zookeeper.address=zookeeper
waitPortsBeforeRun:
- zookeeper:2181
checkPorts:
- 20880
checkLog: "dubbo service started"
depends_on:
- zookeeper

async-provider-embedded:
type: app
basedir: dubbo-samples-async-provider
mainClass: org.apache.dubbo.samples.async.EmbeddedAsyncProvider
systemProps:
- zookeeper.address=zookeeper
waitPortsBeforeRun:
- zookeeper:2181
checkPorts:
- 20881
checkLog: "dubbo service started"
depends_on:
- zookeeper

async-consumer-test:
type: test
basedir: servicediscovery-consumer-old
tests:
- "**/*IT.class"
systemProps:
- zookeeper.address=zookeeper
waitPortsBeforeRun:
- zookeeper:2181
- provider-embedded:20880
- async-provider-embedded:20881
depends_on:
- zookeeper
- async-provider
- async-provider-embedded


@@ -0,0 +1,38 @@
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.apache.dubbo.samples.async;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.concurrent.CountDownLatch;

public class EmbeddedAsyncProvider {

public static void main(String[] args) throws Exception {
new EmbeddedZooKeeper(2181, false).start();

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("META-INF/spring/embedded-async-provider.xml");
context.start();

System.out.println("dubbo service started");
new CountDownLatch(1).await();
}

}
@@ -0,0 +1,29 @@
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.apache.dubbo.samples.async.api;

/**
* Dubbo service called by AsyncServiceImpl
*/
public interface EmbeddedAsyncService {

String sayHello(String name);

}
@@ -22,13 +22,16 @@
import org.apache.dubbo.rpc.AsyncContext;
import org.apache.dubbo.rpc.RpcContext;
import org.apache.dubbo.samples.async.api.AsyncService;
import org.apache.dubbo.samples.async.api.EmbeddedAsyncService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AsyncServiceImpl implements AsyncService {
private static Logger logger = LoggerFactory.getLogger(AsyncServiceImpl.class);

private EmbeddedAsyncService embeddedService;

@Override
public String sayHello(String name) {
AsyncContext asyncContext = RpcContext.startAsync();
@@ -38,17 +41,27 @@ public String sayHello(String name) {
asyncContext.signalContextSwitch();
logger.info("Attachment from consumer: " + RpcContext.getContext().getAttachment("consumer-key1"));
logger.info("async start");
String embeddedCallResult = null;
try {
embeddedCallResult = embeddedService.sayHello("embedded call");
logger.info(" embedded call result is " + embeddedCallResult);
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
asyncContext.write("Hello " + name + ", response from provider.");
asyncContext.write("Hello " + name + ", " + embeddedCallResult + " response from provider.");
logger.info("async end");
}).start();

logger.info("sayHello end");
return "hello, " + name;
}

public EmbeddedAsyncService getEmbeddedService() {
return embeddedService;
}

public void setEmbeddedService(EmbeddedAsyncService embeddedService) {
this.embeddedService = embeddedService;
}
}
@@ -0,0 +1,32 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.samples.async.impl;

import org.apache.dubbo.samples.async.api.EmbeddedAsyncService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class EmbeddedAsyncServiceImpl implements EmbeddedAsyncService {
private static Logger logger = LoggerFactory.getLogger(AsyncServiceImpl.class);

@Override
public String sayHello(String name) {
logger.info("Received embedded call from async service impl.");
return "embedded service";
}
}
@@ -33,8 +33,13 @@

<dubbo:provider token="true"/>

<bean id="asyncService" class="org.apache.dubbo.samples.async.impl.AsyncServiceImpl"/>

<bean id="asyncService" class="org.apache.dubbo.samples.async.impl.AsyncServiceImpl">
<property name="embeddedService" ref="embeddedAsyncService"/>
</bean>
<dubbo:service interface="org.apache.dubbo.samples.async.api.AsyncService" ref="asyncService"/>

<dubbo:reference interface="org.apache.dubbo.samples.async.api.EmbeddedAsyncService" id="embeddedAsyncService"
check="false"/>


</beans>
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~
~ Licensed to the Apache Software Foundation (ASF) under one or more
~ contributor license agreements. See the NOTICE file distributed with
~ this work for additional information regarding copyright ownership.
~ The ASF licenses this file to You under the Apache License, Version 2.0
~ (the "License"); you may not use this file except in compliance with
~ the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
~
-->

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder/>

<dubbo:application name="embedded-async-provider"/>

<dubbo:registry address="zookeeper://${zookeeper.address:127.0.0.1}:2181"/>

<dubbo:protocol name="dubbo" port="20881"/>

<bean id="embeddedAsyncService" class="org.apache.dubbo.samples.async.impl.EmbeddedAsyncServiceImpl"/>

<dubbo:service interface="org.apache.dubbo.samples.async.api.EmbeddedAsyncService" ref="embeddedAsyncService"/>

</beans>
@@ -34,6 +34,6 @@ public class AsyncServiceIT {

@Test
public void test() throws Exception {
Assert.assertEquals("Hello dubbo, response from provider.", asyncService.sayHello("dubbo"));
Assert.assertEquals("Hello dubbo, embedded service response from provider.", asyncService.sayHello("dubbo"));
}
}

0 comments on commit 70f556e

Please sign in to comment.