-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Supports Provider-side asynchronous #1511
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
wunameya
wants to merge
2
commits into
sofastack:master
Choose a base branch
from
wunameya:suppose_service_async
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
example/src/test/java/com/alipay/sofa/rpc/serviceasync/HelloService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* 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 com.alipay.sofa.rpc.serviceasync; | ||
|
||
public interface HelloService { | ||
String sayHello(String string); | ||
} |
35 changes: 35 additions & 0 deletions
35
example/src/test/java/com/alipay/sofa/rpc/serviceasync/HelloServiceImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* 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 com.alipay.sofa.rpc.serviceasync; | ||
|
||
import com.alipay.sofa.rpc.message.bolt.BoltAsyncContext; | ||
|
||
public class HelloServiceImpl implements HelloService { | ||
@Override | ||
public String sayHello(String name) { | ||
BoltAsyncContext boltAsyncContext = new BoltAsyncContext(); | ||
new Thread(() -> { | ||
// 如果需要在新线程中使用调用上下文,需要调用signalContextSwitch方法 | ||
boltAsyncContext.signalContextSwitch(); | ||
|
||
boltAsyncContext.write("Hello " + name); | ||
|
||
boltAsyncContext.resetContext(); | ||
}).start(); | ||
return null; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
example/src/test/java/com/alipay/sofa/rpc/serviceasync/start/Client.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* 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 com.alipay.sofa.rpc.serviceasync.start; | ||
|
||
import com.alipay.sofa.rpc.config.ConsumerConfig; | ||
import com.alipay.sofa.rpc.log.Logger; | ||
import com.alipay.sofa.rpc.log.LoggerFactory; | ||
import com.alipay.sofa.rpc.serviceasync.HelloService; | ||
|
||
public class Client { | ||
private final static Logger LOGGER = LoggerFactory.getLogger(Client.class); | ||
|
||
public static void main(String[] args) { | ||
|
||
ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>() | ||
.setInterfaceId(HelloService.class.getName()) // 指定接口 | ||
.setProtocol("bolt") // 指定协议 | ||
.setDirectUrl("bolt://127.0.0.1:12200") // 指定直连地址 | ||
.setConnectTimeout(10 * 1000); | ||
|
||
HelloService helloService = consumerConfig.refer(); | ||
|
||
while (true) { | ||
try { | ||
LOGGER.info(helloService.sayHello("world")); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
try { | ||
Thread.sleep(2000); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
example/src/test/java/com/alipay/sofa/rpc/serviceasync/start/Service.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 com.alipay.sofa.rpc.serviceasync.start; | ||
|
||
import com.alipay.sofa.rpc.config.ProviderConfig; | ||
import com.alipay.sofa.rpc.config.ServerConfig; | ||
import com.alipay.sofa.rpc.serviceasync.HelloService; | ||
import com.alipay.sofa.rpc.serviceasync.HelloServiceImpl; | ||
|
||
public class Service { | ||
public static void main(String[] args) { | ||
ServerConfig serverConfig = new ServerConfig() | ||
.setProtocol("bolt") // 设置一个协议,默认bolt | ||
.setPort(12200) // 设置一个端口,默认12200 | ||
.setDaemon(false); // 非守护线程 | ||
|
||
ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>() | ||
.setInterfaceId(HelloService.class.getName()) // 指定接口 | ||
.setRef(new HelloServiceImpl()) // 指定实现 | ||
.setServer(serverConfig); // 指定服务端 | ||
|
||
providerConfig.export(); // 发布服务 | ||
} | ||
} |
116 changes: 116 additions & 0 deletions
116
remoting/remoting-bolt/src/main/java/com/alipay/sofa/rpc/message/bolt/BoltAsyncContext.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
* 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 com.alipay.sofa.rpc.message.bolt; | ||
|
||
import com.alipay.remoting.AsyncContext; | ||
import com.alipay.sofa.rpc.common.RemotingConstants; | ||
import com.alipay.sofa.rpc.common.RpcConstants; | ||
import com.alipay.sofa.rpc.context.RpcInternalContext; | ||
import com.alipay.sofa.rpc.context.RpcInvokeContext; | ||
import com.alipay.sofa.rpc.core.exception.SofaRpcException; | ||
import com.alipay.sofa.rpc.core.request.SofaRequest; | ||
import com.alipay.sofa.rpc.core.response.SofaResponse; | ||
import com.alipay.sofa.rpc.event.EventBus; | ||
import com.alipay.sofa.rpc.event.ServerEndHandleEvent; | ||
import com.alipay.sofa.rpc.event.ServerSendEvent; | ||
|
||
public class BoltAsyncContext { | ||
private volatile boolean sent = false; | ||
|
||
private final AsyncContext asyncContext; | ||
private final SofaRequest sofaRequest; | ||
|
||
private final RpcInternalContext internalContext; | ||
private final RpcInvokeContext invokeCtx; | ||
private final ClassLoader restoreClassLoader; | ||
private volatile ClassLoader stagedClassLoader; | ||
|
||
public BoltAsyncContext() { | ||
internalContext = RpcInternalContext.getContext(); | ||
asyncContext = (AsyncContext) internalContext.getAttachment(RpcConstants.HIDDEN_KEY_ASYNC_CONTEXT); | ||
sofaRequest = (SofaRequest) internalContext.getAttachment(RpcConstants.HIDDEN_KEY_ASYNC_REQUEST); | ||
invokeCtx = RpcInvokeContext.getContext(); | ||
restoreClassLoader = Thread.currentThread().getContextClassLoader(); | ||
invokeCtx.put(RemotingConstants.INVOKE_CTX_IS_ASYNC_CHAIN, true); | ||
} | ||
|
||
public synchronized void signalContextSwitch() { | ||
RpcInvokeContext.setContext(invokeCtx); | ||
RpcInternalContext.setContext(internalContext); | ||
if (restoreClassLoader != null) { | ||
Thread.currentThread().setContextClassLoader(restoreClassLoader); | ||
stagedClassLoader = Thread.currentThread().getContextClassLoader(); | ||
} | ||
wunameya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
public synchronized void resetContext() { | ||
RpcInvokeContext.removeContext(); | ||
RpcInternalContext.removeContext(); | ||
// 修复 ClassLoader 恢复逻辑 | ||
if (stagedClassLoader != null) { | ||
Thread.currentThread().setContextClassLoader(stagedClassLoader); | ||
} | ||
} | ||
|
||
private synchronized void checkState() { | ||
if (sent) { | ||
throw new IllegalStateException("Current async context has already sent response"); | ||
} | ||
sent = true; | ||
} | ||
|
||
public void write(Object result) { | ||
checkState(); | ||
try { | ||
SofaResponse response = new SofaResponse(); | ||
response.setAppResponse(result); | ||
sendResponse(response, null); | ||
} catch (Exception e) { | ||
throw new RuntimeException("Failed to send async response", e); | ||
} | ||
} | ||
|
||
public void writeException(Throwable throwable) { | ||
checkState(); | ||
try { | ||
SofaResponse response = new SofaResponse(); | ||
if (throwable instanceof SofaRpcException) { | ||
SofaRpcException sofaRpcException = (SofaRpcException) throwable; | ||
response.setErrorMsg(sofaRpcException.getMessage()); | ||
sendResponse(response, sofaRpcException); | ||
} else { | ||
response.setAppResponse(throwable); | ||
sendResponse(response, null); | ||
} | ||
} catch (Exception e) { | ||
throw new RuntimeException("Failed to send async exception response", e); | ||
} | ||
} | ||
|
||
private void sendResponse(SofaResponse response, SofaRpcException sofaRpcException) { | ||
try { | ||
asyncContext.sendResponse(response); | ||
} finally { | ||
if (EventBus.isEnable(ServerSendEvent.class)) { | ||
EventBus.post(new ServerSendEvent(sofaRequest, response, sofaRpcException)); | ||
} | ||
if (EventBus.isEnable(ServerEndHandleEvent.class)) { | ||
EventBus.post(new ServerEndHandleEvent()); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.