-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support rest server for lookout. (#505)
- Loading branch information
1 parent
e161d3c
commit 9a174e1
Showing
6 changed files
with
489 additions
and
2 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
...nsion-impl/remoting-resteasy/src/main/java/com/alipay/sofa/rpc/lookout/RestConstants.java
This file contains 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,27 @@ | ||
/* | ||
* 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.lookout; | ||
|
||
/** | ||
* @author bystander | ||
* @version $Id: RestConstants.java, v 0.1 2019年02月15日 13:17 bystander Exp $ | ||
*/ | ||
public class RestConstants { | ||
|
||
public static final String REST_SERVICE_KEY = "rest_service"; | ||
public static final String REST_METHODNAME_KEY = "rest_methodname"; | ||
} |
63 changes: 63 additions & 0 deletions
63
...-impl/remoting-resteasy/src/main/java/com/alipay/sofa/rpc/lookout/RestLookoutAdapter.java
This file contains 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,63 @@ | ||
/* | ||
* 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.lookout; | ||
|
||
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.RpcRuntimeContext; | ||
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.ServerSendEvent; | ||
import com.alipay.sofa.rpc.event.rest.RestServerSendEvent; | ||
|
||
import static com.alipay.sofa.rpc.common.RpcConstants.INTERNAL_KEY_PREFIX; | ||
|
||
/** | ||
* @author bystander | ||
* @version $Id: RestLookoutAdapter.java, v 0.1 2019年01月28日 16:44 bystander Exp $ | ||
*/ | ||
public class RestLookoutAdapter { | ||
|
||
public static void sendRestServerSendEvent(RestServerSendEvent restServerSendEvent) { | ||
//this is special for rest | ||
if (EventBus.isEnable(ServerSendEvent.class)) { | ||
SofaRequest request = new SofaRequest(); | ||
|
||
String appName = (String) RpcRuntimeContext.get(RpcRuntimeContext.KEY_APPNAME); | ||
request.setTargetAppName(appName); | ||
request.addRequestProp(RemotingConstants.HEAD_APP_NAME, restServerSendEvent.getRequest().getHttpHeaders() | ||
.getHeaderString(RemotingConstants.HEAD_APP_NAME)); | ||
RpcInternalContext context = RpcInternalContext.getContext(); | ||
request.setTargetServiceUniqueName((String) context.getAttachment(INTERNAL_KEY_PREFIX + | ||
RestConstants.REST_SERVICE_KEY)); | ||
|
||
request.setMethodName((String) context.getAttachment(INTERNAL_KEY_PREFIX + | ||
RestConstants.REST_METHODNAME_KEY)); | ||
request.addRequestProp(RemotingConstants.HEAD_PROTOCOL, RpcConstants.PROTOCOL_TYPE_REST); | ||
request.setInvokeType(RpcConstants.INVOKER_TYPE_SYNC); | ||
SofaResponse response = new SofaResponse(); | ||
|
||
if (restServerSendEvent.getThrowable() != null) { | ||
response.setErrorMsg(restServerSendEvent.getThrowable().getMessage()); | ||
} | ||
final ServerSendEvent event = new ServerSendEvent(request, response, restServerSendEvent.getThrowable()); | ||
EventBus.post(event); | ||
} | ||
} | ||
} |
This file contains 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
71 changes: 71 additions & 0 deletions
71
...ntracing-resteasy/src/main/java/com/alipay/sofa/rpc/server/rest/LookoutRequestFilter.java
This file contains 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,71 @@ | ||
/* | ||
* 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.server.rest; | ||
|
||
import com.alipay.sofa.rpc.common.RemotingConstants; | ||
import com.alipay.sofa.rpc.context.RpcInternalContext; | ||
import com.alipay.sofa.rpc.log.Logger; | ||
import com.alipay.sofa.rpc.log.LoggerFactory; | ||
import com.alipay.sofa.rpc.lookout.RestConstants; | ||
import org.jboss.resteasy.core.interception.PostMatchContainerRequestContext; | ||
|
||
import javax.annotation.Priority; | ||
import javax.ws.rs.container.ContainerRequestContext; | ||
import javax.ws.rs.container.ContainerRequestFilter; | ||
import javax.ws.rs.ext.Provider; | ||
import java.io.IOException; | ||
|
||
import static com.alipay.sofa.rpc.common.RpcConstants.INTERNAL_KEY_PREFIX; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">liangen</a> | ||
*/ | ||
@Provider | ||
@Priority(100) | ||
public class LookoutRequestFilter implements ContainerRequestFilter { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(LookoutRequestFilter.class); | ||
|
||
@Override | ||
public void filter(ContainerRequestContext requestContext) throws IOException { | ||
|
||
try { | ||
SofaResourceMethodInvoker resourceMethodInvoker = (SofaResourceMethodInvoker) | ||
((PostMatchContainerRequestContext) requestContext) | ||
.getResourceMethod(); | ||
|
||
SofaResourceFactory factory = resourceMethodInvoker.getResource(); | ||
String serviceName = factory.getServiceName(); | ||
String appName = factory.getAppName(); | ||
|
||
if (serviceName == null) { | ||
serviceName = resourceMethodInvoker.getResourceClass().getName(); | ||
} | ||
|
||
String methodName = resourceMethodInvoker.getMethod().getName(); | ||
|
||
RpcInternalContext context = RpcInternalContext.getContext(); | ||
context.setAttachment(INTERNAL_KEY_PREFIX + RestConstants.REST_SERVICE_KEY, serviceName); | ||
context.setAttachment(INTERNAL_KEY_PREFIX + RestConstants.REST_METHODNAME_KEY, methodName); | ||
|
||
context.setAttachment(RemotingConstants.HEAD_APP_NAME, appName); | ||
} catch (Exception e) { | ||
logger.error("the process of rest tracer server request occur error ", e); | ||
} | ||
|
||
} | ||
} |
This file contains 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.