-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize the Appactive module (#3107)
- Loading branch information
Showing
17 changed files
with
210 additions
and
78 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
|
@@ -20,16 +20,16 @@ | |
* @author raozihao, mageekchiu | ||
* @author <a href="mailto:[email protected]">Steve</a> | ||
*/ | ||
public class ServiceMeta implements Comparable<ServiceMeta> { | ||
public class ServiceMetaEntity implements Comparable<ServiceMetaEntity> { | ||
|
||
private String uriPrefix; | ||
|
||
private String ra; | ||
|
||
public ServiceMeta() { | ||
public ServiceMetaEntity() { | ||
} | ||
|
||
public ServiceMeta(String uriPrefix, String ra) { | ||
public ServiceMetaEntity(String uriPrefix, String ra) { | ||
this.uriPrefix = uriPrefix; | ||
this.ra = ra; | ||
} | ||
|
@@ -56,7 +56,7 @@ public String toString() { | |
} | ||
|
||
@Override | ||
public int compareTo(ServiceMeta o) { | ||
public int compareTo(ServiceMetaEntity o) { | ||
int pre = this.uriPrefix.compareTo(o.getUriPrefix()); | ||
return pre == 0 ? this.ra.compareTo(o.getRa()) : pre; | ||
} | ||
|
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
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 |
---|---|---|
|
@@ -20,14 +20,29 @@ | |
* @author raozihao, mageekchiu | ||
* @author <a href="mailto:[email protected]">Steve</a> | ||
*/ | ||
public final class Constants { | ||
public final class AppactiveConstants { | ||
|
||
/** | ||
* Router Id header key. | ||
*/ | ||
public static final String ROUTER_ID_HEADER_KEY = "appactive-router-id"; | ||
|
||
private Constants() { | ||
/** | ||
* Unit type. | ||
*/ | ||
public static final String UT = "ut"; | ||
|
||
/** | ||
* Meta. | ||
*/ | ||
public static final String SVC_META = "svc_meta"; | ||
|
||
/** | ||
* Version. | ||
*/ | ||
public static final String SVC_META_V = "svc_meta_v"; | ||
|
||
private AppactiveConstants() { | ||
} | ||
|
||
} |
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
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
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
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 |
---|---|---|
|
@@ -31,13 +31,13 @@ | |
* @author raozihao, mageekchiu | ||
* @author <a href="mailto:[email protected]">Steve</a> | ||
*/ | ||
public class ResponseInterceptor implements Decoder { | ||
public class FeignResponseDecoderInterceptor implements Decoder { | ||
|
||
private static final Logger logger = LogUtil.getLogger(); | ||
|
||
final Decoder delegate; | ||
|
||
public ResponseInterceptor(Decoder delegate) { | ||
public FeignResponseDecoderInterceptor(Decoder delegate) { | ||
Objects.requireNonNull(delegate, "Decoder must not be null. "); | ||
this.delegate = delegate; | ||
} | ||
|
@@ -46,7 +46,7 @@ public ResponseInterceptor(Decoder delegate) { | |
public Object decode(Response response, Type type) | ||
throws IOException, FeignException { | ||
Object object = delegate.decode(response, type); | ||
logger.info("ResponseInterceptor uri {} for request {} got cleared by {}", | ||
logger.info("FeignResponseDecoderInterceptor uri {} for request {} got cleared by {}", | ||
UriContext.getUriPath(), response.request().url(), delegate.getClass()); | ||
UriContext.clearContext(); | ||
return object; | ||
|
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 |
---|---|---|
|
@@ -19,7 +19,7 @@ | |
import javax.servlet.http.HttpServletRequest; | ||
|
||
import com.alibaba.cloud.appactive.common.UriContext; | ||
import com.alibaba.cloud.appactive.constant.Constants; | ||
import com.alibaba.cloud.appactive.constant.AppactiveConstants; | ||
import feign.RequestInterceptor; | ||
import feign.RequestTemplate; | ||
import io.appactive.java.api.base.AppContextClient; | ||
|
@@ -31,7 +31,7 @@ | |
* @author raozihao, mageekchiu | ||
* @author <a href="mailto:[email protected]">Steve</a> | ||
*/ | ||
public class RouterIdTransmissionRequestInterceptor implements RequestInterceptor { | ||
public class FeignRouterIdTransmissionRequestInterceptor implements RequestInterceptor { | ||
|
||
@Override | ||
public void apply(RequestTemplate requestTemplate) { | ||
|
@@ -44,7 +44,7 @@ public void apply(RequestTemplate requestTemplate) { | |
if (request == null) { | ||
return; | ||
} | ||
requestTemplate.header(Constants.ROUTER_ID_HEADER_KEY, | ||
requestTemplate.header(AppactiveConstants.ROUTER_ID_HEADER_KEY, | ||
AppContextClient.getRouteId()); | ||
// store uri for routing filter | ||
UriContext.setUriPath(requestTemplate.url()); | ||
|
70 changes: 70 additions & 0 deletions
70
...n/java/com/alibaba/cloud/appactive/consumer/ReactiveRequestStrategyBeanPostProcessor.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,70 @@ | ||
/* | ||
* Copyright 2013-2018 the original author or authors. | ||
* | ||
* Licensed 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 | ||
* | ||
* https://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.alibaba.cloud.appactive.consumer; | ||
|
||
import javax.validation.constraints.NotNull; | ||
|
||
import com.alibaba.cloud.appactive.constant.AppactiveConstants; | ||
import io.appactive.java.api.base.AppContextClient; | ||
|
||
import org.springframework.beans.BeansException; | ||
import org.springframework.beans.factory.config.BeanPostProcessor; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.web.reactive.function.client.ClientRequest; | ||
import org.springframework.web.reactive.function.client.WebClient; | ||
|
||
/** | ||
* @author yuluo | ||
*/ | ||
public class ReactiveRequestStrategyBeanPostProcessor implements BeanPostProcessor { | ||
|
||
final ApplicationContext applicationContext; | ||
|
||
public ReactiveRequestStrategyBeanPostProcessor( | ||
ApplicationContext applicationContext) { | ||
this.applicationContext = applicationContext; | ||
} | ||
|
||
@Override | ||
public Object postProcessBeforeInitialization(Object bean, String beanName) | ||
throws BeansException { | ||
return bean; | ||
} | ||
|
||
@Override | ||
public Object postProcessAfterInitialization(@NotNull Object bean, | ||
@NotNull String beanName) { | ||
if (bean instanceof WebClient || bean instanceof WebClient.Builder) { | ||
assert bean instanceof WebClient; | ||
WebClient webClient = (WebClient) bean; | ||
|
||
// add filter | ||
webClient.mutate().filter((request, next) -> { | ||
ClientRequest clientRequest = ClientRequest.from(request) | ||
.headers(headers -> headers.set( | ||
AppactiveConstants.ROUTER_ID_HEADER_KEY, | ||
AppContextClient.getRouteId())) | ||
.build(); | ||
return next.exchange(clientRequest); | ||
}).build(); | ||
} | ||
|
||
return bean; | ||
|
||
} | ||
|
||
} |
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 |
---|---|---|
|
@@ -19,7 +19,7 @@ | |
import java.io.IOException; | ||
|
||
import com.alibaba.cloud.appactive.common.UriContext; | ||
import com.alibaba.cloud.appactive.constant.Constants; | ||
import com.alibaba.cloud.appactive.constant.AppactiveConstants; | ||
import io.appactive.java.api.base.AppContextClient; | ||
import io.appactive.support.log.LogUtil; | ||
import org.slf4j.Logger; | ||
|
@@ -33,21 +33,21 @@ | |
* @author raozihao, mageekchiu | ||
* @author <a href="mailto:[email protected]">Steve</a> | ||
*/ | ||
public class ReqResInterceptor implements ClientHttpRequestInterceptor { | ||
public class RestTemplateInterceptor implements ClientHttpRequestInterceptor { | ||
|
||
private static final Logger logger = LogUtil.getLogger(); | ||
|
||
@Override | ||
public ClientHttpResponse intercept(HttpRequest request, byte[] body, | ||
ClientHttpRequestExecution execution) throws IOException { | ||
|
||
request.getHeaders().add(Constants.ROUTER_ID_HEADER_KEY, | ||
request.getHeaders().add(AppactiveConstants.ROUTER_ID_HEADER_KEY, | ||
AppContextClient.getRouteId()); | ||
UriContext.setUriPath(request.getURI().getPath()); | ||
|
||
ClientHttpResponse response = execution.execute(request, body); | ||
|
||
logger.info("ReqResInterceptor uri {} for request {} got cleared", | ||
logger.info("RestTemplateInterceptor uri {} for request {} got cleared", | ||
UriContext.getUriPath(), request.getURI()); | ||
UriContext.clearContext(); | ||
return response; | ||
|
Oops, something went wrong.