Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
- [fix: shutdown thread pool before the container closes](https://github.com/Tencent/spring-cloud-tencent/pull/353)
- [docs:update logo in README.](https://github.com/Tencent/spring-cloud-tencent/pull/358)
- [Refator JacksonUtils and JacksonUtilsTest](https://github.com/Tencent/spring-cloud-tencent/pull/365)
- [refactor:optimize project and code.](https://github.com/Tencent/spring-cloud-tencent/pull/368)
- [docs: Fix javadoc <br /> error](https://github.com/Tencent/spring-cloud-tencent/pull/371)
- [UT: add Polaris LoadBalancer unit test](https://github.com/Tencent/spring-cloud-tencent/pull/373)
- [docs:update docs](https://github.com/Tencent/spring-cloud-tencent/pull/378)


4 changes: 4 additions & 0 deletions changes/changes-1.5.3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Change Log
---

- [fix not load application.yml bug & fix guava version conflict bug](https://github.com/Tencent/spring-cloud-tencent/pull/284)
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,10 @@ static class MetadataServletFilterConfig {
@Bean
public FilterRegistrationBean<DecodeTransferMetadataServletFilter> metadataServletFilterRegistrationBean(
DecodeTransferMetadataServletFilter decodeTransferMetadataServletFilter) {
FilterRegistrationBean<DecodeTransferMetadataServletFilter> filterRegistrationBean = new FilterRegistrationBean<>(
decodeTransferMetadataServletFilter);
filterRegistrationBean.setDispatcherTypes(ASYNC, ERROR, FORWARD, INCLUDE,
REQUEST);
filterRegistrationBean
.setOrder(MetadataConstant.OrderConstant.WEB_FILTER_ORDER);
FilterRegistrationBean<DecodeTransferMetadataServletFilter> filterRegistrationBean =
new FilterRegistrationBean<>(decodeTransferMetadataServletFilter);
filterRegistrationBean.setDispatcherTypes(ASYNC, ERROR, FORWARD, INCLUDE, REQUEST);
filterRegistrationBean.setOrder(MetadataConstant.OrderConstant.WEB_FILTER_ORDER);
return filterRegistrationBean;
}

Expand Down Expand Up @@ -158,29 +156,25 @@ public EncodeTransferMedataRestTemplateInterceptor encodeTransferMedataRestTempl
BeanPostProcessor encodeTransferMetadataRestTemplatePostProcessor(
EncodeTransferMedataRestTemplateInterceptor encodeTransferMedataRestTemplateInterceptor) {
// Coping with multiple bean injection scenarios
Map<String, RestTemplate> beans = this.context
.getBeansOfType(RestTemplate.class);
Map<String, RestTemplate> beans = this.context.getBeansOfType(RestTemplate.class);
// If the restTemplate has been created when the
// MetadataRestTemplatePostProcessor Bean
// is initialized, then manually set the interceptor.
if (!CollectionUtils.isEmpty(beans)) {
for (RestTemplate restTemplate : beans.values()) {
List<ClientHttpRequestInterceptor> interceptors = restTemplate
.getInterceptors();
List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
// Avoid setting interceptor repeatedly.
if (!interceptors.contains(encodeTransferMedataRestTemplateInterceptor)) {
interceptors.add(encodeTransferMedataRestTemplateInterceptor);
restTemplate.setInterceptors(interceptors);
}
}
}
return new EncodeTransferMetadataRestTemplatePostProcessor(
encodeTransferMedataRestTemplateInterceptor);
return new EncodeTransferMetadataRestTemplatePostProcessor(encodeTransferMedataRestTemplateInterceptor);
}

@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.context = applicationContext;
}

Expand All @@ -203,8 +197,7 @@ public Object postProcessBeforeInitialization(Object bean, String beanName) {
public Object postProcessAfterInitialization(Object bean, String beanName) {
if (bean instanceof RestTemplate) {
RestTemplate restTemplate = (RestTemplate) bean;
List<ClientHttpRequestInterceptor> interceptors = restTemplate
.getInterceptors();
List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
// Avoid setting interceptor repeatedly.
if (!interceptors.contains(encodeTransferMedataRestTemplateInterceptor)) {
interceptors.add(this.encodeTransferMedataRestTemplateInterceptor);
Expand All @@ -213,9 +206,6 @@ public Object postProcessAfterInitialization(Object bean, String beanName) {
}
return bean;
}

}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,25 @@

/**
* Resolve custom transitive metadata from request.
*
* @author lepdou 2022-05-20
*/
public class CustomTransitiveMetadataResolver {
public final class CustomTransitiveMetadataResolver {

private static final String TRANSITIVE_HEADER_PREFIX = "X-SCT-Metadata-Transitive-";
private static final int TRANSITIVE_HEADER_PREFIX_LENGTH = TRANSITIVE_HEADER_PREFIX.length();
private CustomTransitiveMetadataResolver() {
}

public static Map<String, String> resolve(ServerWebExchange exchange) {
Map<String, String> result = new HashMap<>();

HttpHeaders headers = exchange.getRequest().getHeaders();
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
String key = entry.getKey();

if (StringUtils.isNotBlank(key) &&
StringUtils.startsWithIgnoreCase(key, TRANSITIVE_HEADER_PREFIX)
if (StringUtils.isNotBlank(key)
&& StringUtils.startsWithIgnoreCase(key, TRANSITIVE_HEADER_PREFIX)
&& !CollectionUtils.isEmpty(entry.getValue())) {

String sourceKey = StringUtils.substring(key, TRANSITIVE_HEADER_PREFIX_LENGTH);
result.put(sourceKey, entry.getValue().get(0));
}
Expand All @@ -66,10 +67,9 @@ public static Map<String, String> resolve(HttpServletRequest request) {
while (headers.hasMoreElements()) {
String key = headers.nextElement();

if (StringUtils.isNotBlank(key) &&
StringUtils.startsWithIgnoreCase(key, TRANSITIVE_HEADER_PREFIX)
if (StringUtils.isNotBlank(key)
&& StringUtils.startsWithIgnoreCase(key, TRANSITIVE_HEADER_PREFIX)
&& StringUtils.isNotBlank(request.getHeader(key))) {

String sourceKey = StringUtils.substring(key, TRANSITIVE_HEADER_PREFIX_LENGTH);
result.put(sourceKey, request.getHeader(key));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@
*/
public class DecodeTransferMetadataReactiveFilter implements WebFilter, Ordered {

private static final Logger LOG = LoggerFactory
.getLogger(DecodeTransferMetadataReactiveFilter.class);
private static final Logger LOG = LoggerFactory.getLogger(DecodeTransferMetadataReactiveFilter.class);

@Override
public int getOrder() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@
@Order(MetadataConstant.OrderConstant.WEB_FILTER_ORDER)
public class DecodeTransferMetadataServletFilter extends OncePerRequestFilter {

private static final Logger LOG = LoggerFactory
.getLogger(DecodeTransferMetadataServletFilter.class);
private static final Logger LOG = LoggerFactory.getLogger(DecodeTransferMetadataServletFilter.class);

@Override
protected void doFilterInternal(HttpServletRequest httpServletRequest,
Expand All @@ -75,8 +74,7 @@ protected void doFilterInternal(HttpServletRequest httpServletRequest,

private Map<String, String> getInternalTransitiveMetadata(HttpServletRequest httpServletRequest) {
// Get custom metadata string from http header.
String customMetadataStr = httpServletRequest
.getHeader(MetadataConstant.HeaderName.CUSTOM_METADATA);
String customMetadataStr = httpServletRequest.getHeader(MetadataConstant.HeaderName.CUSTOM_METADATA);
try {
if (StringUtils.hasText(customMetadataStr)) {
customMetadataStr = URLDecoder.decode(customMetadataStr, StandardCharsets.UTF_8.name());
Expand All @@ -90,5 +88,4 @@ private Map<String, String> getInternalTransitiveMetadata(HttpServletRequest htt
// create custom metadata.
return JacksonUtils.deserialize2Map(customMetadataStr);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@
*/
public class EncodeTransferMedataFeignInterceptor implements RequestInterceptor, Ordered {

private static final Logger LOG = LoggerFactory
.getLogger(EncodeTransferMedataFeignInterceptor.class);
private static final Logger LOG = LoggerFactory.getLogger(EncodeTransferMedataFeignInterceptor.class);

@Override
public int getOrder() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@
*
* @author Haotian Zhang
*/
public class EncodeTransferMedataRestTemplateInterceptor
implements ClientHttpRequestInterceptor, Ordered {
public class EncodeTransferMedataRestTemplateInterceptor implements ClientHttpRequestInterceptor, Ordered {

@Override
public int getOrder() {
Expand Down Expand Up @@ -71,5 +70,4 @@ public ClientHttpResponse intercept(HttpRequest httpRequest, byte[] bytes,

return clientHttpRequestExecution.execute(httpRequest, bytes);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@
*/
public class EncodeTransferMedataScgFilter implements GlobalFilter, Ordered {

private static final int METADATA_SCG_FILTER_ORDER = LOAD_BALANCER_CLIENT_FILTER_ORDER
+ 1;
private static final int METADATA_SCG_FILTER_ORDER = LOAD_BALANCER_CLIENT_FILTER_ORDER + 1;

@Override
public int getOrder() {
Expand All @@ -59,8 +58,7 @@ public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
ServerHttpRequest.Builder builder = exchange.getRequest().mutate();

// get metadata of current thread
MetadataContext metadataContext = exchange
.getAttribute(MetadataConstant.HeaderName.METADATA_CONTEXT);
MetadataContext metadataContext = exchange.getAttribute(MetadataConstant.HeaderName.METADATA_CONTEXT);

// add new metadata and cover old
if (metadataContext == null) {
Expand All @@ -80,5 +78,4 @@ public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {

return chain.filter(exchange.mutate().request(builder.build()).build());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,11 @@ public Object run() {
if (!CollectionUtils.isEmpty(customMetadata)) {
String metadataStr = JacksonUtils.serialize2Json(customMetadata);
try {
requestContext.addZuulRequestHeader(
MetadataConstant.HeaderName.CUSTOM_METADATA,
requestContext.addZuulRequestHeader(MetadataConstant.HeaderName.CUSTOM_METADATA,
URLEncoder.encode(metadataStr, StandardCharsets.UTF_8.name()));
}
catch (UnsupportedEncodingException e) {
requestContext.addZuulRequestHeader(
MetadataConstant.HeaderName.CUSTOM_METADATA, metadataStr);
requestContext.addZuulRequestHeader(MetadataConstant.HeaderName.CUSTOM_METADATA, metadataStr);
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ public void test1() {
Assertions.assertThat(context).hasSingleBean(
EncodeTransferMedataRestTemplateInterceptor.class);
Assertions.assertThat(context).hasSingleBean(
MetadataTransferAutoConfiguration.MetadataTransferRestTemplateConfig.EncodeTransferMetadataRestTemplatePostProcessor.class);
MetadataTransferAutoConfiguration
.MetadataTransferRestTemplateConfig
.EncodeTransferMetadataRestTemplatePostProcessor.class);
Assertions.assertThat(context).hasSingleBean(
MetadataTransferAutoConfiguration.MetadataTransferZuulFilterConfig.class);
Assertions.assertThat(context)
Expand All @@ -65,5 +67,4 @@ public void test1() {
Assertions.assertThat(context).hasSingleBean(GlobalFilter.class);
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,4 @@ public void test2() {
.isEqualTo("2");
Assertions.assertThat(metadataLocalProperties.getContent().get("c")).isNull();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,13 @@ public void test1() throws ServletException, IOException {
request.addHeader(MetadataConstant.HeaderName.CUSTOM_METADATA, "{\"c\": \"3\"}");
MockHttpServletResponse response = new MockHttpServletResponse();
metadataServletFilter.doFilter(request, response, filterChain);
Assertions.assertThat(metadataLocalProperties.getContent().get("a"))
.isEqualTo("1");
Assertions.assertThat(metadataLocalProperties.getContent().get("b"))
.isEqualTo("2");
Assertions.assertThat(metadataLocalProperties.getContent().get("a")).isEqualTo("1");
Assertions.assertThat(metadataLocalProperties.getContent().get("b")).isEqualTo("2");
Assertions.assertThat(metadataLocalProperties.getContent().get("c")).isNull();
}

@SpringBootApplication
protected static class TestApplication {

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

Expand Down Expand Up @@ -66,10 +65,8 @@ public class EncodeTransferMedataFeignInterceptorTest {
public void testTransitiveMetadataFromApplicationConfig() {
String metadata = testFeign.test();
Assertions.assertThat(metadata).isEqualTo("2");
Assertions.assertThat(metadataLocalProperties.getContent().get("a"))
.isEqualTo("1");
Assertions.assertThat(metadataLocalProperties.getContent().get("b"))
.isEqualTo("2");
Assertions.assertThat(metadataLocalProperties.getContent().get("a")).isEqualTo("1");
Assertions.assertThat(metadataLocalProperties.getContent().get("b")).isEqualTo("2");
}

@SpringBootApplication
Expand All @@ -78,9 +75,7 @@ public void testTransitiveMetadataFromApplicationConfig() {
protected static class TestApplication {

@RequestMapping("/test")
public String test(
@RequestHeader(MetadataConstant.HeaderName.CUSTOM_METADATA) String customMetadataStr)
throws UnsupportedEncodingException {
public String test() throws UnsupportedEncodingException {
return MetadataContextHolder.get().getContext(MetadataContext.FRAGMENT_TRANSITIVE, "b");
}

Expand All @@ -89,7 +84,6 @@ public interface TestFeign {

@RequestMapping("/test")
String test();

}

@Configuration
Expand All @@ -100,9 +94,6 @@ public void apply(RequestTemplate template) {
template.header(MetadataConstant.HeaderName.CUSTOM_METADATA,
"{\"a\":\"11\",\"b\":\"22\",\"c\":\"33\"}");
}

}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import java.io.UnsupportedEncodingException;

import com.tencent.cloud.common.constant.MetadataConstant;
import com.tencent.cloud.common.metadata.MetadataContext;
import com.tencent.cloud.common.metadata.MetadataContextHolder;
import org.assertj.core.api.Assertions;
Expand All @@ -35,7 +34,6 @@
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
Expand All @@ -50,7 +48,8 @@
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT,
classes = EncodeTransferMedataRestTemplateInterceptorTest.TestApplication.class,
properties = { "spring.config.location = classpath:application-test.yml", "spring.main.web-application-type = reactive" })
properties = {"spring.config.location = classpath:application-test.yml",
"spring.main.web-application-type = reactive"})
public class EncodeTransferMedataRestTemplateInterceptorTest {

@Autowired
Expand All @@ -64,8 +63,7 @@ public void testTransitiveMetadataFromApplicationConfig() {
HttpHeaders httpHeaders = new HttpHeaders();
HttpEntity<String> httpEntity = new HttpEntity<>(httpHeaders);
String metadata = restTemplate
.exchange("http://localhost:" + localServerPort + "/test", HttpMethod.GET,
httpEntity, String.class)
.exchange("http://localhost:" + localServerPort + "/test", HttpMethod.GET, httpEntity, String.class)
.getBody();
Assertions.assertThat(metadata).isEqualTo("2");
}
Expand All @@ -80,12 +78,8 @@ public RestTemplate restTemplate() {
}

@RequestMapping("/test")
public String test(
@RequestHeader(MetadataConstant.HeaderName.CUSTOM_METADATA) String customMetadataStr)
throws UnsupportedEncodingException {
public String test() throws UnsupportedEncodingException {
return MetadataContextHolder.get().getContext(MetadataContext.FRAGMENT_TRANSITIVE, "b");
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT,
classes = EncodeTransferMedataScgFilterTest.TestApplication.class,
properties = {"spring.config.location = classpath:application-test.yml", "spring.main.web-application-type = reactive"})
properties = {"spring.config.location = classpath:application-test.yml",
"spring.main.web-application-type = reactive"})
public class EncodeTransferMedataScgFilterTest {

@Autowired
Expand Down
Loading