diff --git a/CHANGELOG.md b/CHANGELOG.md index ff2392502..fc7796085 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,3 +7,4 @@ - [Bugfix: fix causing cpu 100% when set ScheduledThreadPoolExecutor corePoolSize=0](https://github.com/Tencent/spring-cloud-tencent/pull/99) - [Bugfix: fix circuitbreaker http code greater than 400 as fail response bug](https://github.com/Tencent/spring-cloud-tencent/pull/117) - [Feat: optimize router dependency](https://github.com/Tencent/spring-cloud-tencent/pull/114) +- [Refactor: refactor transfer metadata](https://github.com/Tencent/spring-cloud-tencent/pull/112) diff --git a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/config/MetadataTransferAutoConfiguration.java b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/config/MetadataTransferAutoConfiguration.java index eb30f3318..51740c1cf 100644 --- a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/config/MetadataTransferAutoConfiguration.java +++ b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/config/MetadataTransferAutoConfiguration.java @@ -13,6 +13,7 @@ * 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.tencent.cloud.metadata.config; @@ -21,14 +22,19 @@ import java.util.List; import java.util.Map; import com.netflix.zuul.ZuulFilter; -import com.tencent.cloud.metadata.core.filter.gateway.Metadata2HeaderScgFilter; -import com.tencent.cloud.metadata.core.filter.gateway.Metadata2HeaderZuulFilter; -import com.tencent.cloud.metadata.core.interceptor.Metadata2HeaderFeignInterceptor; -import com.tencent.cloud.metadata.core.interceptor.Metadata2HeaderRestTemplateInterceptor; +import com.tencent.cloud.common.constant.MetadataConstant; +import com.tencent.cloud.metadata.core.DecodeTransferMetadataReactiveFilter; +import com.tencent.cloud.metadata.core.DecodeTransferMetadataServletFilter; +import com.tencent.cloud.metadata.core.EncodeTransferMedataFeignInterceptor; +import com.tencent.cloud.metadata.core.EncodeTransferMedataRestTemplateInterceptor; +import com.tencent.cloud.metadata.core.EncodeTransferMedataScgFilter; +import com.tencent.cloud.metadata.core.EncodeTransferMetadataZuulFilter; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; +import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.cloud.gateway.filter.GlobalFilter; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; @@ -38,6 +44,12 @@ import org.springframework.http.client.ClientHttpRequestInterceptor; import org.springframework.util.CollectionUtils; import org.springframework.web.client.RestTemplate; +import static javax.servlet.DispatcherType.ASYNC; +import static javax.servlet.DispatcherType.ERROR; +import static javax.servlet.DispatcherType.FORWARD; +import static javax.servlet.DispatcherType.INCLUDE; +import static javax.servlet.DispatcherType.REQUEST; + /** * Metadata transfer auto configuration. * @@ -46,6 +58,46 @@ import org.springframework.web.client.RestTemplate; @Configuration public class MetadataTransferAutoConfiguration { + /** + * Create when web application type is SERVLET. + */ + @Configuration + @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) + static class MetadataServletFilterConfig { + + @Bean + public FilterRegistrationBean metadataServletFilterRegistrationBean( + DecodeTransferMetadataServletFilter decodeTransferMetadataServletFilter) { + FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean<>( + decodeTransferMetadataServletFilter); + filterRegistrationBean.setDispatcherTypes(ASYNC, ERROR, FORWARD, INCLUDE, + REQUEST); + filterRegistrationBean + .setOrder(MetadataConstant.OrderConstant.WEB_FILTER_ORDER); + return filterRegistrationBean; + } + + @Bean + public DecodeTransferMetadataServletFilter metadataServletFilter() { + return new DecodeTransferMetadataServletFilter(); + } + + } + + /** + * Create when web application type is REACTIVE. + */ + @Configuration + @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE) + static class MetadataReactiveFilterConfig { + + @Bean + public DecodeTransferMetadataReactiveFilter metadataReactiveFilter() { + return new DecodeTransferMetadataReactiveFilter(); + } + + } + /** * Create when gateway application is Zuul. */ @@ -54,8 +106,8 @@ public class MetadataTransferAutoConfiguration { static class MetadataTransferZuulFilterConfig { @Bean - public ZuulFilter metadata2HeaderZuulFilter() { - return new Metadata2HeaderZuulFilter(); + public ZuulFilter encodeTransferMetadataZuulFilter() { + return new EncodeTransferMetadataZuulFilter(); } } @@ -68,8 +120,8 @@ public class MetadataTransferAutoConfiguration { static class MetadataTransferScgFilterConfig { @Bean - public GlobalFilter metadata2HeaderScgFilter() { - return new Metadata2HeaderScgFilter(); + public GlobalFilter encodeTransferMedataScgFilter() { + return new EncodeTransferMedataScgFilter(); } } @@ -82,8 +134,8 @@ public class MetadataTransferAutoConfiguration { static class MetadataTransferFeignInterceptorConfig { @Bean - public Metadata2HeaderFeignInterceptor metadata2HeaderFeignInterceptor() { - return new Metadata2HeaderFeignInterceptor(); + public EncodeTransferMedataFeignInterceptor encodeTransferMedataFeignInterceptor() { + return new EncodeTransferMedataFeignInterceptor(); } } @@ -98,13 +150,13 @@ public class MetadataTransferAutoConfiguration { private ApplicationContext context; @Bean - public Metadata2HeaderRestTemplateInterceptor metadata2HeaderRestTemplateInterceptor() { - return new Metadata2HeaderRestTemplateInterceptor(); + public EncodeTransferMedataRestTemplateInterceptor encodeTransferMedataRestTemplateInterceptor() { + return new EncodeTransferMedataRestTemplateInterceptor(); } @Bean - BeanPostProcessor metadata2HeaderRestTemplatePostProcessor( - Metadata2HeaderRestTemplateInterceptor metadata2HeaderRestTemplateInterceptor) { + BeanPostProcessor encodeTransferMetadataRestTemplatePostProcessor( + EncodeTransferMedataRestTemplateInterceptor encodeTransferMedataRestTemplateInterceptor) { // Coping with multiple bean injection scenarios Map beans = this.context .getBeansOfType(RestTemplate.class); @@ -117,14 +169,14 @@ public class MetadataTransferAutoConfiguration { .getInterceptors(); // Avoid setting interceptor repeatedly. if (null != interceptors && !interceptors - .contains(metadata2HeaderRestTemplateInterceptor)) { - interceptors.add(metadata2HeaderRestTemplateInterceptor); + .contains(encodeTransferMedataRestTemplateInterceptor)) { + interceptors.add(encodeTransferMedataRestTemplateInterceptor); restTemplate.setInterceptors(interceptors); } } } - return new Metadata2HeaderRestTemplatePostProcessor( - metadata2HeaderRestTemplateInterceptor); + return new EncodeTransferMetadataRestTemplatePostProcessor( + encodeTransferMedataRestTemplateInterceptor); } @Override @@ -133,14 +185,14 @@ public class MetadataTransferAutoConfiguration { this.context = applicationContext; } - public static class Metadata2HeaderRestTemplatePostProcessor + public static class EncodeTransferMetadataRestTemplatePostProcessor implements BeanPostProcessor { - private Metadata2HeaderRestTemplateInterceptor metadata2HeaderRestTemplateInterceptor; + private EncodeTransferMedataRestTemplateInterceptor encodeTransferMedataRestTemplateInterceptor; - Metadata2HeaderRestTemplatePostProcessor( - Metadata2HeaderRestTemplateInterceptor metadata2HeaderRestTemplateInterceptor) { - this.metadata2HeaderRestTemplateInterceptor = metadata2HeaderRestTemplateInterceptor; + EncodeTransferMetadataRestTemplatePostProcessor( + EncodeTransferMedataRestTemplateInterceptor encodeTransferMedataRestTemplateInterceptor) { + this.encodeTransferMedataRestTemplateInterceptor = encodeTransferMedataRestTemplateInterceptor; } @Override @@ -156,8 +208,8 @@ public class MetadataTransferAutoConfiguration { .getInterceptors(); // Avoid setting interceptor repeatedly. if (null != interceptors && !interceptors - .contains(metadata2HeaderRestTemplateInterceptor)) { - interceptors.add(this.metadata2HeaderRestTemplateInterceptor); + .contains(encodeTransferMedataRestTemplateInterceptor)) { + interceptors.add(this.encodeTransferMedataRestTemplateInterceptor); restTemplate.setInterceptors(interceptors); } } diff --git a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/filter/web/MetadataReactiveFilter.java b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/DecodeTransferMetadataReactiveFilter.java similarity index 94% rename from spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/filter/web/MetadataReactiveFilter.java rename to spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/DecodeTransferMetadataReactiveFilter.java index adbf4a780..aac449fac 100644 --- a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/filter/web/MetadataReactiveFilter.java +++ b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/DecodeTransferMetadataReactiveFilter.java @@ -13,9 +13,10 @@ * 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.tencent.cloud.common.metadata.filter.web; +package com.tencent.cloud.metadata.core; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; @@ -42,10 +43,10 @@ import org.springframework.web.server.WebFilterChain; * * @author Haotian Zhang */ -public class MetadataReactiveFilter implements WebFilter, Ordered { +public class DecodeTransferMetadataReactiveFilter implements WebFilter, Ordered { private static final Logger LOG = LoggerFactory - .getLogger(MetadataReactiveFilter.class); + .getLogger(DecodeTransferMetadataReactiveFilter.class); @Override public int getOrder() { diff --git a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/filter/web/MetadataServletFilter.java b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/DecodeTransferMetadataServletFilter.java similarity index 93% rename from spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/filter/web/MetadataServletFilter.java rename to spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/DecodeTransferMetadataServletFilter.java index 018313ec4..da1261f65 100644 --- a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/filter/web/MetadataServletFilter.java +++ b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/DecodeTransferMetadataServletFilter.java @@ -13,9 +13,10 @@ * 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.tencent.cloud.common.metadata.filter.web; +package com.tencent.cloud.metadata.core; import java.io.IOException; import java.io.UnsupportedEncodingException; @@ -44,10 +45,10 @@ import org.springframework.web.filter.OncePerRequestFilter; * @author Haotian Zhang */ @Order(MetadataConstant.OrderConstant.WEB_FILTER_ORDER) -public class MetadataServletFilter extends OncePerRequestFilter { +public class DecodeTransferMetadataServletFilter extends OncePerRequestFilter { private static final Logger LOG = LoggerFactory - .getLogger(MetadataServletFilter.class); + .getLogger(DecodeTransferMetadataServletFilter.class); @Override protected void doFilterInternal(HttpServletRequest httpServletRequest, diff --git a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/interceptor/Metadata2HeaderFeignInterceptor.java b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/EncodeTransferMedataFeignInterceptor.java similarity index 93% rename from spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/interceptor/Metadata2HeaderFeignInterceptor.java rename to spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/EncodeTransferMedataFeignInterceptor.java index 65b901d5b..c89309ade 100644 --- a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/interceptor/Metadata2HeaderFeignInterceptor.java +++ b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/EncodeTransferMedataFeignInterceptor.java @@ -13,9 +13,10 @@ * 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.tencent.cloud.metadata.core.interceptor; +package com.tencent.cloud.metadata.core; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; @@ -42,10 +43,10 @@ import static com.tencent.cloud.common.constant.MetadataConstant.HeaderName.CUST * * @author Haotian Zhang */ -public class Metadata2HeaderFeignInterceptor implements RequestInterceptor, Ordered { +public class EncodeTransferMedataFeignInterceptor implements RequestInterceptor, Ordered { private static final Logger LOG = LoggerFactory - .getLogger(Metadata2HeaderFeignInterceptor.class); + .getLogger(EncodeTransferMedataFeignInterceptor.class); @Override public int getOrder() { diff --git a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/interceptor/Metadata2HeaderRestTemplateInterceptor.java b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/EncodeTransferMedataRestTemplateInterceptor.java similarity index 96% rename from spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/interceptor/Metadata2HeaderRestTemplateInterceptor.java rename to spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/EncodeTransferMedataRestTemplateInterceptor.java index 2ce9d5875..47709ba6c 100644 --- a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/interceptor/Metadata2HeaderRestTemplateInterceptor.java +++ b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/EncodeTransferMedataRestTemplateInterceptor.java @@ -13,9 +13,10 @@ * 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.tencent.cloud.metadata.core.interceptor; +package com.tencent.cloud.metadata.core; import java.io.IOException; import java.io.UnsupportedEncodingException; @@ -41,7 +42,7 @@ import org.springframework.util.StringUtils; * * @author Haotian Zhang */ -public class Metadata2HeaderRestTemplateInterceptor +public class EncodeTransferMedataRestTemplateInterceptor implements ClientHttpRequestInterceptor, Ordered { @Override diff --git a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/filter/gateway/Metadata2HeaderScgFilter.java b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/EncodeTransferMedataScgFilter.java similarity index 95% rename from spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/filter/gateway/Metadata2HeaderScgFilter.java rename to spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/EncodeTransferMedataScgFilter.java index 10e5cfab5..dacde7089 100644 --- a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/filter/gateway/Metadata2HeaderScgFilter.java +++ b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/EncodeTransferMedataScgFilter.java @@ -13,9 +13,10 @@ * 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.tencent.cloud.metadata.core.filter.gateway; +package com.tencent.cloud.metadata.core; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; @@ -41,7 +42,7 @@ import static org.springframework.cloud.gateway.filter.LoadBalancerClientFilter. * * @author Haotian Zhang */ -public class Metadata2HeaderScgFilter implements GlobalFilter, Ordered { +public class EncodeTransferMedataScgFilter implements GlobalFilter, Ordered { private static final int METADATA_SCG_FILTER_ORDER = LOAD_BALANCER_CLIENT_FILTER_ORDER + 1; diff --git a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/filter/gateway/Metadata2HeaderZuulFilter.java b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/EncodeTransferMetadataZuulFilter.java similarity index 95% rename from spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/filter/gateway/Metadata2HeaderZuulFilter.java rename to spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/EncodeTransferMetadataZuulFilter.java index a2e2a334e..3fac6f6d7 100644 --- a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/filter/gateway/Metadata2HeaderZuulFilter.java +++ b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/EncodeTransferMetadataZuulFilter.java @@ -13,9 +13,10 @@ * 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.tencent.cloud.metadata.core.filter.gateway; +package com.tencent.cloud.metadata.core; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; @@ -38,7 +39,7 @@ import static org.springframework.cloud.netflix.zuul.filters.support.FilterConst * * @author Haotian Zhang */ -public class Metadata2HeaderZuulFilter extends ZuulFilter { +public class EncodeTransferMetadataZuulFilter extends ZuulFilter { @Override public String filterType() { diff --git a/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/metadata/filter/web/MetadataReactiveFilterTest.java b/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/DecodeTransferMetadataReactiveFilterTest.java similarity index 83% rename from spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/metadata/filter/web/MetadataReactiveFilterTest.java rename to spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/DecodeTransferMetadataReactiveFilterTest.java index d825db920..418716e87 100644 --- a/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/metadata/filter/web/MetadataReactiveFilterTest.java +++ b/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/DecodeTransferMetadataReactiveFilterTest.java @@ -13,12 +13,14 @@ * 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.tencent.cloud.common.metadata.filter.web; +package com.tencent.cloud.metadata; import com.tencent.cloud.common.constant.MetadataConstant; import com.tencent.cloud.common.metadata.config.MetadataLocalProperties; +import com.tencent.cloud.metadata.core.DecodeTransferMetadataReactiveFilter; import org.assertj.core.api.Assertions; import org.junit.Before; import org.junit.Test; @@ -36,23 +38,23 @@ import org.springframework.web.server.WebFilterChain; import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.MOCK; /** - * Test for {@link MetadataReactiveFilter} * * @author Haotian Zhang */ @RunWith(SpringRunner.class) -@SpringBootTest(webEnvironment = MOCK, classes = MetadataServletFilterTest.TestApplication.class, properties = { - "spring.config.location = classpath:application-test.yml" }) -public class MetadataReactiveFilterTest { +@SpringBootTest(webEnvironment = MOCK, + classes = DecodeTransferMetadataServletFilterTest.TestApplication.class, + properties = {"spring.config.location = classpath:application-test.yml"}) +public class DecodeTransferMetadataReactiveFilterTest { @Autowired private MetadataLocalProperties metadataLocalProperties; - private MetadataReactiveFilter metadataReactiveFilter; + private DecodeTransferMetadataReactiveFilter metadataReactiveFilter; @Before public void setUp() { - this.metadataReactiveFilter = new MetadataReactiveFilter(); + this.metadataReactiveFilter = new DecodeTransferMetadataReactiveFilter(); } @Test diff --git a/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/metadata/filter/web/MetadataServletFilterTest.java b/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/DecodeTransferMetadataServletFilterTest.java similarity index 85% rename from spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/metadata/filter/web/MetadataServletFilterTest.java rename to spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/DecodeTransferMetadataServletFilterTest.java index 96b22cc38..42b5598eb 100644 --- a/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/metadata/filter/web/MetadataServletFilterTest.java +++ b/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/DecodeTransferMetadataServletFilterTest.java @@ -13,9 +13,10 @@ * 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.tencent.cloud.common.metadata.filter.web; +package com.tencent.cloud.metadata; import java.io.IOException; @@ -24,6 +25,7 @@ import javax.servlet.ServletException; import com.tencent.cloud.common.constant.MetadataConstant; import com.tencent.cloud.common.metadata.config.MetadataLocalProperties; +import com.tencent.cloud.metadata.core.DecodeTransferMetadataServletFilter; import org.assertj.core.api.Assertions; import org.junit.Test; import org.junit.runner.RunWith; @@ -38,20 +40,20 @@ import org.springframework.test.context.junit4.SpringRunner; import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; /** - * Test for {@link MetadataServletFilter} * * @author Haotian Zhang */ @RunWith(SpringRunner.class) -@SpringBootTest(webEnvironment = RANDOM_PORT, classes = MetadataServletFilterTest.TestApplication.class, properties = { - "spring.config.location = classpath:application-test.yml" }) -public class MetadataServletFilterTest { +@SpringBootTest(webEnvironment = RANDOM_PORT, + classes = DecodeTransferMetadataServletFilterTest.TestApplication.class, + properties = {"spring.config.location = classpath:application-test.yml"}) +public class DecodeTransferMetadataServletFilterTest { @Autowired private MetadataLocalProperties metadataLocalProperties; @Autowired - private MetadataServletFilter metadataServletFilter; + private DecodeTransferMetadataServletFilter metadataServletFilter; @Test public void test1() throws ServletException, IOException { diff --git a/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/config/MetadataTransferAutoConfigurationTest.java b/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/config/MetadataTransferAutoConfigurationTest.java index 5b1c7aaeb..a60eb5130 100644 --- a/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/config/MetadataTransferAutoConfigurationTest.java +++ b/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/config/MetadataTransferAutoConfigurationTest.java @@ -13,13 +13,14 @@ * 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.tencent.cloud.metadata.config; -import com.tencent.cloud.metadata.core.filter.gateway.Metadata2HeaderZuulFilter; -import com.tencent.cloud.metadata.core.interceptor.Metadata2HeaderFeignInterceptor; -import com.tencent.cloud.metadata.core.interceptor.Metadata2HeaderRestTemplateInterceptor; +import com.tencent.cloud.metadata.core.EncodeTransferMedataFeignInterceptor; +import com.tencent.cloud.metadata.core.EncodeTransferMedataRestTemplateInterceptor; +import com.tencent.cloud.metadata.core.EncodeTransferMetadataZuulFilter; import org.assertj.core.api.Assertions; import org.junit.Test; @@ -48,17 +49,17 @@ public class MetadataTransferAutoConfigurationTest { Assertions.assertThat(context).hasSingleBean( MetadataTransferAutoConfiguration.MetadataTransferFeignInterceptorConfig.class); Assertions.assertThat(context) - .hasSingleBean(Metadata2HeaderFeignInterceptor.class); + .hasSingleBean(EncodeTransferMedataFeignInterceptor.class); Assertions.assertThat(context).hasSingleBean( MetadataTransferAutoConfiguration.MetadataTransferRestTemplateConfig.class); Assertions.assertThat(context) - .hasSingleBean(Metadata2HeaderRestTemplateInterceptor.class); + .hasSingleBean(EncodeTransferMedataRestTemplateInterceptor.class); Assertions.assertThat(context).hasSingleBean( - MetadataTransferAutoConfiguration.MetadataTransferRestTemplateConfig.Metadata2HeaderRestTemplatePostProcessor.class); + MetadataTransferAutoConfiguration.MetadataTransferRestTemplateConfig.EncodeTransferMetadataRestTemplatePostProcessor.class); Assertions.assertThat(context).hasSingleBean( MetadataTransferAutoConfiguration.MetadataTransferZuulFilterConfig.class); Assertions.assertThat(context) - .hasSingleBean(Metadata2HeaderZuulFilter.class); + .hasSingleBean(EncodeTransferMetadataZuulFilter.class); Assertions.assertThat(context).hasSingleBean( MetadataTransferAutoConfiguration.MetadataTransferScgFilterConfig.class); Assertions.assertThat(context).hasSingleBean(GlobalFilter.class); diff --git a/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/core/intercepter/Metadata2HeaderFeignInterceptorTest.java b/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/core/intercepter/EncodeTransferMedataFeignInterceptorTest.java similarity index 90% rename from spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/core/intercepter/Metadata2HeaderFeignInterceptorTest.java rename to spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/core/intercepter/EncodeTransferMedataFeignInterceptorTest.java index 13cdb988e..13ffead51 100644 --- a/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/core/intercepter/Metadata2HeaderFeignInterceptorTest.java +++ b/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/core/intercepter/EncodeTransferMedataFeignInterceptorTest.java @@ -13,6 +13,7 @@ * 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.tencent.cloud.metadata.core.intercepter; @@ -24,7 +25,7 @@ import com.tencent.cloud.common.constant.MetadataConstant; import com.tencent.cloud.common.metadata.MetadataContextHolder; import com.tencent.cloud.common.metadata.config.MetadataLocalProperties; import com.tencent.cloud.common.util.JacksonUtils; -import com.tencent.cloud.metadata.core.interceptor.Metadata2HeaderFeignInterceptor; +import com.tencent.cloud.metadata.core.EncodeTransferMedataFeignInterceptor; import feign.RequestInterceptor; import feign.RequestTemplate; import org.assertj.core.api.Assertions; @@ -45,14 +46,16 @@ import org.springframework.web.bind.annotation.RestController; import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.DEFINED_PORT; /** - * Test for {@link Metadata2HeaderFeignInterceptor} + * Test for {@link EncodeTransferMedataFeignInterceptor} * * @author Haotian Zhang */ @RunWith(SpringRunner.class) -@SpringBootTest(webEnvironment = DEFINED_PORT, classes = Metadata2HeaderFeignInterceptorTest.TestApplication.class, properties = { - "server.port=8081", "spring.config.location = classpath:application-test.yml" }) -public class Metadata2HeaderFeignInterceptorTest { +@SpringBootTest(webEnvironment = DEFINED_PORT, + classes = EncodeTransferMedataFeignInterceptorTest.TestApplication.class, + properties = { "server.port=8081", + "spring.config.location = classpath:application-test.yml" }) +public class EncodeTransferMedataFeignInterceptorTest { @Autowired private MetadataLocalProperties metadataLocalProperties; diff --git a/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/core/intercepter/Metadata2HeaderRestTemplateInterceptorTest.java b/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/core/intercepter/EncodeTransferMedataRestTemplateInterceptorTest.java similarity index 90% rename from spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/core/intercepter/Metadata2HeaderRestTemplateInterceptorTest.java rename to spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/core/intercepter/EncodeTransferMedataRestTemplateInterceptorTest.java index b0dab532f..6a7f98b25 100644 --- a/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/core/intercepter/Metadata2HeaderRestTemplateInterceptorTest.java +++ b/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/core/intercepter/EncodeTransferMedataRestTemplateInterceptorTest.java @@ -13,6 +13,7 @@ * 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.tencent.cloud.metadata.core.intercepter; @@ -24,7 +25,7 @@ import com.tencent.cloud.common.constant.MetadataConstant; import com.tencent.cloud.common.metadata.MetadataContextHolder; import com.tencent.cloud.common.metadata.config.MetadataLocalProperties; import com.tencent.cloud.common.util.JacksonUtils; -import com.tencent.cloud.metadata.core.interceptor.Metadata2HeaderRestTemplateInterceptor; +import com.tencent.cloud.metadata.core.EncodeTransferMedataRestTemplateInterceptor; import org.assertj.core.api.Assertions; import org.junit.Test; import org.junit.runner.RunWith; @@ -46,14 +47,15 @@ import org.springframework.web.client.RestTemplate; import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; /** - * Test for {@link Metadata2HeaderRestTemplateInterceptor} + * Test for {@link EncodeTransferMedataRestTemplateInterceptor} * * @author Haotian Zhang */ @RunWith(SpringRunner.class) -@SpringBootTest(webEnvironment = RANDOM_PORT, classes = Metadata2HeaderRestTemplateInterceptorTest.TestApplication.class, properties = { - "spring.config.location = classpath:application-test.yml" }) -public class Metadata2HeaderRestTemplateInterceptorTest { +@SpringBootTest(webEnvironment = RANDOM_PORT, + classes = EncodeTransferMedataRestTemplateInterceptorTest.TestApplication.class, + properties = { "spring.config.location = classpath:application-test.yml" }) +public class EncodeTransferMedataRestTemplateInterceptorTest { @Autowired private MetadataLocalProperties metadataLocalProperties; diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/PolarisDiscoveryProperties.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/PolarisDiscoveryProperties.java index 351ab62f3..c6647f156 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/PolarisDiscoveryProperties.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/PolarisDiscoveryProperties.java @@ -49,7 +49,7 @@ public class PolarisDiscoveryProperties { /** * Service name to registry. */ - @Value("${spring.cloud.polaris.discovery.service:${spring.application.name:}}") + @Value("${spring.cloud.polaris.discovery.service:${spring.cloud.polaris.service:${spring.application.name:}}}") private String service; /** diff --git a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/MetadataContext.java b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/MetadataContext.java index cf4e59b84..573574a78 100644 --- a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/MetadataContext.java +++ b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/MetadataContext.java @@ -13,6 +13,7 @@ * 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.tencent.cloud.common.metadata; @@ -24,6 +25,8 @@ import java.util.concurrent.ConcurrentHashMap; import com.tencent.cloud.common.util.ApplicationContextAwareUtils; import com.tencent.cloud.common.util.JacksonUtils; +import org.springframework.util.StringUtils; + /** * Metadata Context. * @@ -34,15 +37,12 @@ public class MetadataContext { /** * Namespace of local instance. */ - public static final String LOCAL_NAMESPACE = ApplicationContextAwareUtils - .getProperties("spring.cloud.polaris.discovery.namespace", "default"); + public static String LOCAL_NAMESPACE; /** * Service name of local instance. */ - public static final String LOCAL_SERVICE = ApplicationContextAwareUtils.getProperties( - "spring.cloud.polaris.discovery.service", - ApplicationContextAwareUtils.getProperties("spring.application.name", null)); + public static String LOCAL_SERVICE; /** * Transitive custom metadata content. @@ -54,6 +54,22 @@ public class MetadataContext { */ private final Map systemMetadata; + static { + String namespace = ApplicationContextAwareUtils.getProperties("spring.cloud.polaris.namespace"); + if (StringUtils.isEmpty(namespace)) { + namespace = ApplicationContextAwareUtils.getProperties("spring.cloud.polaris.discovery.namespace", "default"); + } + LOCAL_NAMESPACE = namespace; + + String serviceName = ApplicationContextAwareUtils.getProperties("spring.cloud.polaris.service"); + if (StringUtils.isEmpty(serviceName)) { + serviceName = ApplicationContextAwareUtils.getProperties( + "spring.cloud.polaris.discovery.service", + ApplicationContextAwareUtils.getProperties("spring.application.name", null)); + } + LOCAL_SERVICE = serviceName; + } + public MetadataContext() { this.transitiveCustomMetadata = new ConcurrentHashMap<>(); this.systemMetadata = new ConcurrentHashMap<>(); diff --git a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/config/MetadataAutoConfiguration.java b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/config/MetadataAutoConfiguration.java index 77ac8712d..462f00a49 100644 --- a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/config/MetadataAutoConfiguration.java +++ b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/config/MetadataAutoConfiguration.java @@ -13,32 +13,22 @@ * 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.tencent.cloud.common.metadata.config; import com.netflix.zuul.ZuulFilter; -import com.tencent.cloud.common.constant.MetadataConstant; import com.tencent.cloud.common.metadata.aop.MetadataFeignAspect; import com.tencent.cloud.common.metadata.filter.gateway.MetadataFirstScgFilter; import com.tencent.cloud.common.metadata.filter.gateway.MetadataFirstZuulFilter; -import com.tencent.cloud.common.metadata.filter.web.MetadataReactiveFilter; -import com.tencent.cloud.common.metadata.filter.web.MetadataServletFilter; import com.tencent.cloud.common.metadata.interceptor.feign.MetadataFirstFeignInterceptor; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; -import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; -import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.cloud.gateway.filter.GlobalFilter; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import static javax.servlet.DispatcherType.ASYNC; -import static javax.servlet.DispatcherType.ERROR; -import static javax.servlet.DispatcherType.FORWARD; -import static javax.servlet.DispatcherType.INCLUDE; -import static javax.servlet.DispatcherType.REQUEST; - /** * Metadata auto configuration. * @@ -56,46 +46,6 @@ public class MetadataAutoConfiguration { return new MetadataLocalProperties(); } - /** - * Create when web application type is SERVLET. - */ - @Configuration - @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) - static class MetadataServletFilterConfig { - - @Bean - public FilterRegistrationBean metadataServletFilterRegistrationBean( - MetadataServletFilter metadataServletFilter) { - FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean<>( - metadataServletFilter); - filterRegistrationBean.setDispatcherTypes(ASYNC, ERROR, FORWARD, INCLUDE, - REQUEST); - filterRegistrationBean - .setOrder(MetadataConstant.OrderConstant.WEB_FILTER_ORDER); - return filterRegistrationBean; - } - - @Bean - public MetadataServletFilter metadataServletFilter() { - return new MetadataServletFilter(); - } - - } - - /** - * Create when web application type is REACTIVE. - */ - @Configuration - @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE) - static class MetadataReactiveFilterConfig { - - @Bean - public MetadataReactiveFilter metadataReactiveFilter() { - return new MetadataReactiveFilter(); - } - - } - /** * Create when Feign exists. */ diff --git a/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/metadata/config/MetadataAutoConfigurationTest.java b/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/metadata/config/MetadataAutoConfigurationTest.java index 8b871c174..bcf42ee14 100644 --- a/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/metadata/config/MetadataAutoConfigurationTest.java +++ b/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/metadata/config/MetadataAutoConfigurationTest.java @@ -13,14 +13,13 @@ * 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.tencent.cloud.common.metadata.config; import com.tencent.cloud.common.metadata.filter.gateway.MetadataFirstScgFilter; import com.tencent.cloud.common.metadata.filter.gateway.MetadataFirstZuulFilter; -import com.tencent.cloud.common.metadata.filter.web.MetadataReactiveFilter; -import com.tencent.cloud.common.metadata.filter.web.MetadataServletFilter; import com.tencent.cloud.common.metadata.interceptor.feign.MetadataFirstFeignInterceptor; import org.assertj.core.api.Assertions; import org.junit.Test; @@ -53,14 +52,6 @@ public class MetadataAutoConfigurationTest { .run(context -> { Assertions.assertThat(context) .hasSingleBean(MetadataLocalProperties.class); - Assertions.assertThat(context).doesNotHaveBean( - MetadataAutoConfiguration.MetadataServletFilterConfig.class); - Assertions.assertThat(context) - .doesNotHaveBean(MetadataServletFilter.class); - Assertions.assertThat(context).doesNotHaveBean( - MetadataAutoConfiguration.MetadataReactiveFilterConfig.class); - Assertions.assertThat(context) - .doesNotHaveBean(MetadataReactiveFilter.class); Assertions.assertThat(context).hasSingleBean( MetadataAutoConfiguration.MetadataFeignInterceptorConfig.class); Assertions.assertThat(context) @@ -86,14 +77,6 @@ public class MetadataAutoConfigurationTest { .run(context -> { Assertions.assertThat(context) .hasSingleBean(MetadataLocalProperties.class); - Assertions.assertThat(context).hasSingleBean( - MetadataAutoConfiguration.MetadataServletFilterConfig.class); - Assertions.assertThat(context) - .hasSingleBean(MetadataServletFilter.class); - Assertions.assertThat(context).doesNotHaveBean( - MetadataAutoConfiguration.MetadataReactiveFilterConfig.class); - Assertions.assertThat(context) - .doesNotHaveBean(MetadataReactiveFilter.class); Assertions.assertThat(context).hasSingleBean( MetadataAutoConfiguration.MetadataFeignInterceptorConfig.class); Assertions.assertThat(context) @@ -119,14 +102,6 @@ public class MetadataAutoConfigurationTest { .run(context -> { Assertions.assertThat(context) .hasSingleBean(MetadataLocalProperties.class); - Assertions.assertThat(context).doesNotHaveBean( - MetadataAutoConfiguration.MetadataServletFilterConfig.class); - Assertions.assertThat(context) - .doesNotHaveBean(MetadataServletFilter.class); - Assertions.assertThat(context).hasSingleBean( - MetadataAutoConfiguration.MetadataReactiveFilterConfig.class); - Assertions.assertThat(context) - .hasSingleBean(MetadataReactiveFilter.class); Assertions.assertThat(context).hasSingleBean( MetadataAutoConfiguration.MetadataFeignInterceptorConfig.class); Assertions.assertThat(context) diff --git a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/PolarisContextProperties.java b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/PolarisContextProperties.java index 4554fc31c..47602434a 100644 --- a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/PolarisContextProperties.java +++ b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/PolarisContextProperties.java @@ -59,6 +59,11 @@ public class PolarisContextProperties { */ private String namespace = "default"; + /** + * polaris service name. + */ + private String service; + @Autowired private Environment environment; @@ -114,4 +119,11 @@ public class PolarisContextProperties { this.namespace = namespace; } + String getService() { + return service; + } + + void setService(String service) { + this.service = service; + } } diff --git a/spring-cloud-tencent-polaris-context/src/main/resources/META-INF/spring-configuration-metadata.json b/spring-cloud-tencent-polaris-context/src/main/resources/META-INF/spring-configuration-metadata.json index 75fbdd4f4..e6e2ae7fa 100644 --- a/spring-cloud-tencent-polaris-context/src/main/resources/META-INF/spring-configuration-metadata.json +++ b/spring-cloud-tencent-polaris-context/src/main/resources/META-INF/spring-configuration-metadata.json @@ -20,6 +20,13 @@ "default": "default", "sourceType": "com.tencent.cloud.polaris.context.PolarisContextProperties" }, + { + "name": "spring.cloud.polaris.service", + "type": "java.lang.String", + "description": "polaris service name", + "default": "${spring.application.name}", + "sourceType": "com.tencent.cloud.polaris.context.PolarisContextProperties" + }, { "name": "spring.cloud.polaris.local-ip-address", "type": "java.lang.String",