refactor transfer metadata

pull/113/head
lepdou 3 years ago
parent 435b46382d
commit 8e7966dfa4

@ -1,7 +1,8 @@
# Change Log # Change Log
--- ---
- [Bugfix: fix router bug and add router example](https://github.com/Tencent/spring-cloud-tencent/pull/109) - [Bugfix: fix router bug and add router example](https://github.com/Tencent/spring-cloud-tencent/pull/89)
- [feat:add custom label resolver spi for rate limit](https://github.com/Tencent/spring-cloud-tencent/pull/107) - [feat:add custom label resolver spi for rate limit](https://github.com/Tencent/spring-cloud-tencent/pull/105)
- [feat:fix discovery weight param not set to register request bug](https://github.com/Tencent/spring-cloud-tencent/pull/104) - [feat:fix discovery weight param not set to register request bug](https://github.com/Tencent/spring-cloud-tencent/pull/102)
- [Bugfix: fix causing cpu 100% when set ScheduledThreadPoolExecutor corePoolSize=0](https://github.com/Tencent/spring-cloud-tencent/pull/101) - [Bugfix: fix causing cpu 100% when set ScheduledThreadPoolExecutor corePoolSize=0](https://github.com/Tencent/spring-cloud-tencent/pull/98)
- [Refactor: refactor transfer metadata](https://github.com/Tencent/spring-cloud-tencent/pull/113)

@ -33,6 +33,12 @@
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<optional>true</optional>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>

@ -13,6 +13,7 @@
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * 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 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License. * specific language governing permissions and limitations under the License.
*
*/ */
package com.tencent.cloud.metadata.config; package com.tencent.cloud.metadata.config;
@ -20,13 +21,18 @@ package com.tencent.cloud.metadata.config;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import com.tencent.cloud.metadata.core.filter.gateway.Metadata2HeaderScgFilter; import com.tencent.cloud.common.constant.MetadataConstant;
import com.tencent.cloud.metadata.core.interceptor.Metadata2HeaderFeignInterceptor; import com.tencent.cloud.metadata.core.DecodeTransferMetadataReactiveFilter;
import com.tencent.cloud.metadata.core.interceptor.Metadata2HeaderRestTemplateInterceptor; 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 org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 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.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationContextAware;
@ -36,6 +42,11 @@ import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import org.springframework.web.client.RestTemplate; 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. * Metadata transfer auto configuration.
* *
@ -44,6 +55,46 @@ import org.springframework.web.client.RestTemplate;
@Configuration @Configuration
public class MetadataTransferAutoConfiguration { public class MetadataTransferAutoConfiguration {
/**
* Create when web application type is SERVLET.
*/
@Configuration
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
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);
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 SCG. * Create when gateway application is SCG.
*/ */
@ -52,8 +103,8 @@ public class MetadataTransferAutoConfiguration {
static class MetadataTransferScgFilterConfig { static class MetadataTransferScgFilterConfig {
@Bean @Bean
public GlobalFilter metadata2HeaderScgFilter() { public GlobalFilter encodeTransferMedataScgFilter() {
return new Metadata2HeaderScgFilter(); return new EncodeTransferMedataScgFilter();
} }
} }
@ -66,8 +117,8 @@ public class MetadataTransferAutoConfiguration {
static class MetadataTransferFeignInterceptorConfig { static class MetadataTransferFeignInterceptorConfig {
@Bean @Bean
public Metadata2HeaderFeignInterceptor metadata2HeaderFeignInterceptor() { public EncodeTransferMedataFeignInterceptor encodeTransferMedataFeignInterceptor() {
return new Metadata2HeaderFeignInterceptor(); return new EncodeTransferMedataFeignInterceptor();
} }
} }
@ -82,13 +133,13 @@ public class MetadataTransferAutoConfiguration {
private ApplicationContext context; private ApplicationContext context;
@Bean @Bean
public Metadata2HeaderRestTemplateInterceptor metadata2HeaderRestTemplateInterceptor() { public EncodeTransferMedataRestTemplateInterceptor encodeTransferMedataRestTemplateInterceptor() {
return new Metadata2HeaderRestTemplateInterceptor(); return new EncodeTransferMedataRestTemplateInterceptor();
} }
@Bean @Bean
BeanPostProcessor metadata2HeaderRestTemplatePostProcessor( BeanPostProcessor encodeTransferMetadataRestTemplatePostProcessor(
Metadata2HeaderRestTemplateInterceptor metadata2HeaderRestTemplateInterceptor) { EncodeTransferMedataRestTemplateInterceptor encodeTransferMedataRestTemplateInterceptor) {
// Coping with multiple bean injection scenarios // 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 // If the restTemplate has been created when the
@ -98,13 +149,15 @@ public class MetadataTransferAutoConfiguration {
for (RestTemplate restTemplate : beans.values()) { for (RestTemplate restTemplate : beans.values()) {
List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors(); List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
// Avoid setting interceptor repeatedly. // Avoid setting interceptor repeatedly.
if (null != interceptors && !interceptors.contains(metadata2HeaderRestTemplateInterceptor)) { if (null != interceptors && !interceptors
interceptors.add(metadata2HeaderRestTemplateInterceptor); .contains(encodeTransferMedataRestTemplateInterceptor)) {
interceptors.add(encodeTransferMedataRestTemplateInterceptor);
restTemplate.setInterceptors(interceptors); restTemplate.setInterceptors(interceptors);
} }
} }
} }
return new Metadata2HeaderRestTemplatePostProcessor(metadata2HeaderRestTemplateInterceptor); return new EncodeTransferMetadataRestTemplatePostProcessor(
encodeTransferMedataRestTemplateInterceptor);
} }
@Override @Override
@ -112,13 +165,14 @@ public class MetadataTransferAutoConfiguration {
this.context = applicationContext; this.context = applicationContext;
} }
public static class Metadata2HeaderRestTemplatePostProcessor implements BeanPostProcessor { public static class EncodeTransferMetadataRestTemplatePostProcessor
implements BeanPostProcessor {
private Metadata2HeaderRestTemplateInterceptor metadata2HeaderRestTemplateInterceptor; private EncodeTransferMedataRestTemplateInterceptor encodeTransferMedataRestTemplateInterceptor;
Metadata2HeaderRestTemplatePostProcessor( EncodeTransferMetadataRestTemplatePostProcessor(
Metadata2HeaderRestTemplateInterceptor metadata2HeaderRestTemplateInterceptor) { EncodeTransferMedataRestTemplateInterceptor encodeTransferMedataRestTemplateInterceptor) {
this.metadata2HeaderRestTemplateInterceptor = metadata2HeaderRestTemplateInterceptor; this.encodeTransferMedataRestTemplateInterceptor = encodeTransferMedataRestTemplateInterceptor;
} }
@Override @Override
@ -132,8 +186,9 @@ public class MetadataTransferAutoConfiguration {
RestTemplate restTemplate = (RestTemplate) bean; RestTemplate restTemplate = (RestTemplate) bean;
List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors(); List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
// Avoid setting interceptor repeatedly. // Avoid setting interceptor repeatedly.
if (null != interceptors && !interceptors.contains(metadata2HeaderRestTemplateInterceptor)) { if (null != interceptors && !interceptors
interceptors.add(this.metadata2HeaderRestTemplateInterceptor); .contains(encodeTransferMedataRestTemplateInterceptor)) {
interceptors.add(this.encodeTransferMedataRestTemplateInterceptor);
restTemplate.setInterceptors(interceptors); restTemplate.setInterceptors(interceptors);
} }
} }

@ -13,9 +13,10 @@
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * 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 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License. * 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.io.UnsupportedEncodingException;
import java.net.URLDecoder; import java.net.URLDecoder;
@ -42,9 +43,10 @@ import org.springframework.web.server.WebFilterChain;
* *
* @author Haotian Zhang * @author Haotian Zhang
*/ */
public class MetadataReactiveFilter implements WebFilter, Ordered { public class DecodeTransferMetadataReactiveFilter implements WebFilter, Ordered {
private static final Logger LOG = LoggerFactory.getLogger(MetadataReactiveFilter.class); private static final Logger LOG = LoggerFactory
.getLogger(DecodeTransferMetadataReactiveFilter.class);
@Override @Override
public int getOrder() { public int getOrder() {

@ -13,9 +13,10 @@
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * 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 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License. * 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.IOException;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
@ -44,9 +45,10 @@ import org.springframework.web.filter.OncePerRequestFilter;
* @author Haotian Zhang * @author Haotian Zhang
*/ */
@Order(MetadataConstant.OrderConstant.WEB_FILTER_ORDER) @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); private static final Logger LOG = LoggerFactory
.getLogger(DecodeTransferMetadataServletFilter.class);
@Override @Override
protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,

@ -13,9 +13,10 @@
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * 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 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License. * 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.io.UnsupportedEncodingException;
import java.net.URLEncoder; import java.net.URLEncoder;
@ -41,9 +42,10 @@ import static com.tencent.cloud.common.constant.MetadataConstant.HeaderName.CUST
* *
* @author Haotian Zhang * @author Haotian Zhang
*/ */
public class Metadata2HeaderFeignInterceptor implements RequestInterceptor, Ordered { public class EncodeTransferMedataFeignInterceptor implements RequestInterceptor, Ordered {
private static final Logger LOG = LoggerFactory.getLogger(Metadata2HeaderFeignInterceptor.class); private static final Logger LOG = LoggerFactory
.getLogger(EncodeTransferMedataFeignInterceptor.class);
@Override @Override
public int getOrder() { public int getOrder() {

@ -13,9 +13,10 @@
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * 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 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License. * 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.IOException;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
@ -41,7 +42,8 @@ import org.springframework.util.CollectionUtils;
* *
* @author Haotian Zhang * @author Haotian Zhang
*/ */
public class Metadata2HeaderRestTemplateInterceptor implements ClientHttpRequestInterceptor, Ordered { public class EncodeTransferMedataRestTemplateInterceptor
implements ClientHttpRequestInterceptor, Ordered {
@Override @Override
public int getOrder() { public int getOrder() {

@ -13,9 +13,10 @@
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * 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 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License. * 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.io.UnsupportedEncodingException;
import java.net.URLEncoder; import java.net.URLEncoder;
@ -41,7 +42,7 @@ import static org.springframework.cloud.gateway.filter.ReactiveLoadBalancerClien
* *
* @author Haotian Zhang * @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; private static final int METADATA_SCG_FILTER_ORDER = LOAD_BALANCER_CLIENT_FILTER_ORDER + 1;

@ -13,12 +13,14 @@
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * 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 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License. * 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.constant.MetadataConstant;
import com.tencent.cloud.common.metadata.config.MetadataLocalProperties; import com.tencent.cloud.common.metadata.config.MetadataLocalProperties;
import com.tencent.cloud.metadata.core.DecodeTransferMetadataReactiveFilter;
import org.assertj.core.api.Assertions; import org.assertj.core.api.Assertions;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -36,23 +38,23 @@ import org.springframework.web.server.WebFilterChain;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.MOCK; import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.MOCK;
/** /**
* Test for {@link MetadataReactiveFilter}
* *
* @author Haotian Zhang * @author Haotian Zhang
*/ */
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = MOCK, classes = MetadataServletFilterTest.TestApplication.class, @SpringBootTest(webEnvironment = MOCK,
classes = DecodeTransferMetadataServletFilterTest.TestApplication.class,
properties = {"spring.config.location = classpath:application-test.yml"}) properties = {"spring.config.location = classpath:application-test.yml"})
public class MetadataReactiveFilterTest { public class DecodeTransferMetadataReactiveFilterTest {
@Autowired @Autowired
private MetadataLocalProperties metadataLocalProperties; private MetadataLocalProperties metadataLocalProperties;
private MetadataReactiveFilter metadataReactiveFilter; private DecodeTransferMetadataReactiveFilter metadataReactiveFilter;
@Before @Before
public void setUp() { public void setUp() {
this.metadataReactiveFilter = new MetadataReactiveFilter(); this.metadataReactiveFilter = new DecodeTransferMetadataReactiveFilter();
} }
@Test @Test

@ -13,9 +13,10 @@
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * 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 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License. * 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; import java.io.IOException;
@ -24,6 +25,7 @@ import javax.servlet.ServletException;
import com.tencent.cloud.common.constant.MetadataConstant; import com.tencent.cloud.common.constant.MetadataConstant;
import com.tencent.cloud.common.metadata.config.MetadataLocalProperties; import com.tencent.cloud.common.metadata.config.MetadataLocalProperties;
import com.tencent.cloud.metadata.core.DecodeTransferMetadataServletFilter;
import org.assertj.core.api.Assertions; import org.assertj.core.api.Assertions;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; 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; import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
/** /**
* Test for {@link MetadataServletFilter}
* *
* @author Haotian Zhang * @author Haotian Zhang
*/ */
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT, classes = MetadataServletFilterTest.TestApplication.class, @SpringBootTest(webEnvironment = RANDOM_PORT,
classes = DecodeTransferMetadataServletFilterTest.TestApplication.class,
properties = {"spring.config.location = classpath:application-test.yml"}) properties = {"spring.config.location = classpath:application-test.yml"})
public class MetadataServletFilterTest { public class DecodeTransferMetadataServletFilterTest {
@Autowired @Autowired
private MetadataLocalProperties metadataLocalProperties; private MetadataLocalProperties metadataLocalProperties;
@Autowired @Autowired
private MetadataServletFilter metadataServletFilter; private DecodeTransferMetadataServletFilter metadataServletFilter;
@Test @Test
public void test1() throws ServletException, IOException { public void test1() throws ServletException, IOException {

@ -13,12 +13,13 @@
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * 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 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License. * specific language governing permissions and limitations under the License.
*
*/ */
package com.tencent.cloud.metadata.config; package com.tencent.cloud.metadata.config;
import com.tencent.cloud.metadata.core.interceptor.Metadata2HeaderFeignInterceptor; import com.tencent.cloud.metadata.core.EncodeTransferMedataFeignInterceptor;
import com.tencent.cloud.metadata.core.interceptor.Metadata2HeaderRestTemplateInterceptor; import com.tencent.cloud.metadata.core.EncodeTransferMedataRestTemplateInterceptor;
import org.assertj.core.api.Assertions; import org.assertj.core.api.Assertions;
import org.junit.Test; import org.junit.Test;
@ -44,14 +45,16 @@ public class MetadataTransferAutoConfigurationTest {
.run(context -> { .run(context -> {
Assertions.assertThat(context).hasSingleBean( Assertions.assertThat(context).hasSingleBean(
MetadataTransferAutoConfiguration.MetadataTransferFeignInterceptorConfig.class); MetadataTransferAutoConfiguration.MetadataTransferFeignInterceptorConfig.class);
Assertions.assertThat(context).hasSingleBean(Metadata2HeaderFeignInterceptor.class);
Assertions.assertThat(context) Assertions.assertThat(context)
.hasSingleBean(MetadataTransferAutoConfiguration.MetadataTransferRestTemplateConfig.class); .hasSingleBean(EncodeTransferMedataFeignInterceptor.class);
Assertions.assertThat(context).hasSingleBean(Metadata2HeaderRestTemplateInterceptor.class);
Assertions.assertThat(context).hasSingleBean( Assertions.assertThat(context).hasSingleBean(
MetadataTransferAutoConfiguration.MetadataTransferRestTemplateConfig.Metadata2HeaderRestTemplatePostProcessor.class); MetadataTransferAutoConfiguration.MetadataTransferRestTemplateConfig.class);
Assertions.assertThat(context) Assertions.assertThat(context)
.hasSingleBean(MetadataTransferAutoConfiguration.MetadataTransferScgFilterConfig.class); .hasSingleBean(EncodeTransferMedataRestTemplateInterceptor.class);
Assertions.assertThat(context).hasSingleBean(
MetadataTransferAutoConfiguration.MetadataTransferRestTemplateConfig.EncodeTransferMetadataRestTemplatePostProcessor.class);
Assertions.assertThat(context).hasSingleBean(
MetadataTransferAutoConfiguration.MetadataTransferScgFilterConfig.class);
Assertions.assertThat(context).hasSingleBean(GlobalFilter.class); Assertions.assertThat(context).hasSingleBean(GlobalFilter.class);
}); });
} }

@ -13,6 +13,7 @@
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * 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 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License. * specific language governing permissions and limitations under the License.
*
*/ */
package com.tencent.cloud.metadata.core.intercepter; package com.tencent.cloud.metadata.core.intercepter;
@ -25,7 +26,7 @@ import com.tencent.cloud.common.constant.MetadataConstant;
import com.tencent.cloud.common.metadata.MetadataContextHolder; import com.tencent.cloud.common.metadata.MetadataContextHolder;
import com.tencent.cloud.common.metadata.config.MetadataLocalProperties; import com.tencent.cloud.common.metadata.config.MetadataLocalProperties;
import com.tencent.cloud.common.util.JacksonUtils; 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.RequestInterceptor;
import feign.RequestTemplate; import feign.RequestTemplate;
import org.assertj.core.api.Assertions; import org.assertj.core.api.Assertions;
@ -51,14 +52,16 @@ import org.springframework.web.bind.annotation.RestController;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.DEFINED_PORT; import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.DEFINED_PORT;
/** /**
* Test for {@link Metadata2HeaderFeignInterceptor} * Test for {@link EncodeTransferMedataFeignInterceptor}
* *
* @author Haotian Zhang * @author Haotian Zhang
*/ */
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = DEFINED_PORT, classes = Metadata2HeaderFeignInterceptorTest.TestApplication.class, @SpringBootTest(webEnvironment = DEFINED_PORT,
properties = { "server.port=8081", "spring.config.location = classpath:application-test.yml" }) classes = EncodeTransferMedataFeignInterceptorTest.TestApplication.class,
public class Metadata2HeaderFeignInterceptorTest { properties = { "server.port=8081",
"spring.config.location = classpath:application-test.yml" })
public class EncodeTransferMedataFeignInterceptorTest {
@Autowired @Autowired
private MetadataLocalProperties metadataLocalProperties; private MetadataLocalProperties metadataLocalProperties;

@ -13,6 +13,7 @@
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * 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 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License. * specific language governing permissions and limitations under the License.
*
*/ */
package com.tencent.cloud.metadata.core.intercepter; 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.MetadataContextHolder;
import com.tencent.cloud.common.metadata.config.MetadataLocalProperties; import com.tencent.cloud.common.metadata.config.MetadataLocalProperties;
import com.tencent.cloud.common.util.JacksonUtils; 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.assertj.core.api.Assertions;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -46,15 +47,15 @@ import org.springframework.web.client.RestTemplate;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
/** /**
* Test for {@link Metadata2HeaderRestTemplateInterceptor} * Test for {@link EncodeTransferMedataRestTemplateInterceptor}
* *
* @author Haotian Zhang * @author Haotian Zhang
*/ */
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT, @SpringBootTest(webEnvironment = RANDOM_PORT,
classes = Metadata2HeaderRestTemplateInterceptorTest.TestApplication.class, classes = EncodeTransferMedataRestTemplateInterceptorTest.TestApplication.class,
properties = { "spring.config.location = classpath:application-test.yml" }) properties = { "spring.config.location = classpath:application-test.yml" })
public class Metadata2HeaderRestTemplateInterceptorTest { public class EncodeTransferMedataRestTemplateInterceptorTest {
@Autowired @Autowired
private MetadataLocalProperties metadataLocalProperties; private MetadataLocalProperties metadataLocalProperties;

@ -49,7 +49,7 @@ public class PolarisDiscoveryProperties {
/** /**
* Service name to registry. * 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; private String service;
/** /**

@ -13,6 +13,7 @@
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * 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 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License. * specific language governing permissions and limitations under the License.
*
*/ */
package com.tencent.cloud.common.metadata; 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.ApplicationContextAwareUtils;
import com.tencent.cloud.common.util.JacksonUtils; import com.tencent.cloud.common.util.JacksonUtils;
import org.springframework.util.StringUtils;
/** /**
* Metadata Context. * Metadata Context.
* *
@ -34,15 +37,12 @@ public class MetadataContext {
/** /**
* Namespace of local instance. * Namespace of local instance.
*/ */
public static final String LOCAL_NAMESPACE = ApplicationContextAwareUtils public static String LOCAL_NAMESPACE;
.getProperties("spring.cloud.polaris.discovery.namespace", "default");
/** /**
* Service name of local instance. * Service name of local instance.
*/ */
public static final String LOCAL_SERVICE = ApplicationContextAwareUtils.getProperties( public static String LOCAL_SERVICE;
"spring.cloud.polaris.discovery.service",
ApplicationContextAwareUtils.getProperties("spring.application.name", null));
/** /**
* Transitive custom metadata content. * Transitive custom metadata content.
@ -54,6 +54,22 @@ public class MetadataContext {
*/ */
private final Map<String, String> systemMetadata; private final Map<String, String> 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() { public MetadataContext() {
this.transitiveCustomMetadata = new ConcurrentHashMap<>(); this.transitiveCustomMetadata = new ConcurrentHashMap<>();
this.systemMetadata = new ConcurrentHashMap<>(); this.systemMetadata = new ConcurrentHashMap<>();

@ -13,29 +13,19 @@
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * 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 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License. * specific language governing permissions and limitations under the License.
*
*/ */
package com.tencent.cloud.common.metadata.config; package com.tencent.cloud.common.metadata.config;
import com.tencent.cloud.common.constant.MetadataConstant;
import com.tencent.cloud.common.metadata.filter.gateway.MetadataFirstScgFilter; import com.tencent.cloud.common.metadata.filter.gateway.MetadataFirstScgFilter;
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 com.tencent.cloud.common.metadata.interceptor.feign.MetadataFirstFeignInterceptor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 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.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; 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. * Metadata auto configuration.
* *
@ -53,44 +43,6 @@ public class MetadataAutoConfiguration {
return new MetadataLocalProperties(); return new MetadataLocalProperties();
} }
/**
* Create when web application type is SERVLET.
*/
@Configuration
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
static class MetadataServletFilterConfig {
@Bean
public FilterRegistrationBean<MetadataServletFilter> metadataServletFilterRegistrationBean(
MetadataServletFilter metadataServletFilter) {
FilterRegistrationBean<MetadataServletFilter> 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. * Create when Feign exists.
*/ */

@ -13,13 +13,12 @@
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * 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 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License. * specific language governing permissions and limitations under the License.
*
*/ */
package com.tencent.cloud.common.metadata.config; package com.tencent.cloud.common.metadata.config;
import com.tencent.cloud.common.metadata.filter.gateway.MetadataFirstScgFilter; import com.tencent.cloud.common.metadata.filter.gateway.MetadataFirstScgFilter;
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 com.tencent.cloud.common.metadata.interceptor.feign.MetadataFirstFeignInterceptor;
import org.assertj.core.api.Assertions; import org.assertj.core.api.Assertions;
import org.junit.Test; import org.junit.Test;
@ -49,13 +48,10 @@ public class MetadataAutoConfigurationTest {
public void test1() { public void test1() {
this.applicationContextRunner.withConfiguration(AutoConfigurations.of(MetadataAutoConfiguration.class)) this.applicationContextRunner.withConfiguration(AutoConfigurations.of(MetadataAutoConfiguration.class))
.run(context -> { .run(context -> {
Assertions.assertThat(context).hasSingleBean(MetadataLocalProperties.class);
Assertions.assertThat(context)
.doesNotHaveBean(MetadataAutoConfiguration.MetadataServletFilterConfig.class);
Assertions.assertThat(context).doesNotHaveBean(MetadataServletFilter.class);
Assertions.assertThat(context) Assertions.assertThat(context)
.doesNotHaveBean(MetadataAutoConfiguration.MetadataReactiveFilterConfig.class); .hasSingleBean(MetadataLocalProperties.class);
Assertions.assertThat(context).doesNotHaveBean(MetadataReactiveFilter.class); Assertions.assertThat(context).hasSingleBean(
MetadataAutoConfiguration.MetadataFeignInterceptorConfig.class);
Assertions.assertThat(context) Assertions.assertThat(context)
.hasSingleBean(MetadataAutoConfiguration.MetadataFeignInterceptorConfig.class); .hasSingleBean(MetadataAutoConfiguration.MetadataFeignInterceptorConfig.class);
Assertions.assertThat(context).hasSingleBean(MetadataFirstFeignInterceptor.class); Assertions.assertThat(context).hasSingleBean(MetadataFirstFeignInterceptor.class);
@ -72,13 +68,10 @@ public class MetadataAutoConfigurationTest {
public void test2() { public void test2() {
this.webApplicationContextRunner.withConfiguration(AutoConfigurations.of(MetadataAutoConfiguration.class)) this.webApplicationContextRunner.withConfiguration(AutoConfigurations.of(MetadataAutoConfiguration.class))
.run(context -> { .run(context -> {
Assertions.assertThat(context).hasSingleBean(MetadataLocalProperties.class);
Assertions.assertThat(context)
.hasSingleBean(MetadataAutoConfiguration.MetadataServletFilterConfig.class);
Assertions.assertThat(context).hasSingleBean(MetadataServletFilter.class);
Assertions.assertThat(context) Assertions.assertThat(context)
.doesNotHaveBean(MetadataAutoConfiguration.MetadataReactiveFilterConfig.class); .hasSingleBean(MetadataLocalProperties.class);
Assertions.assertThat(context).doesNotHaveBean(MetadataReactiveFilter.class); Assertions.assertThat(context).hasSingleBean(
MetadataAutoConfiguration.MetadataFeignInterceptorConfig.class);
Assertions.assertThat(context) Assertions.assertThat(context)
.hasSingleBean(MetadataAutoConfiguration.MetadataFeignInterceptorConfig.class); .hasSingleBean(MetadataAutoConfiguration.MetadataFeignInterceptorConfig.class);
Assertions.assertThat(context).hasSingleBean(MetadataFirstFeignInterceptor.class); Assertions.assertThat(context).hasSingleBean(MetadataFirstFeignInterceptor.class);
@ -97,14 +90,9 @@ public class MetadataAutoConfigurationTest {
.withConfiguration(AutoConfigurations.of(MetadataAutoConfiguration.class)).run(context -> { .withConfiguration(AutoConfigurations.of(MetadataAutoConfiguration.class)).run(context -> {
Assertions.assertThat(context).hasSingleBean(MetadataLocalProperties.class); Assertions.assertThat(context).hasSingleBean(MetadataLocalProperties.class);
Assertions.assertThat(context) Assertions.assertThat(context)
.doesNotHaveBean(MetadataAutoConfiguration.MetadataServletFilterConfig.class); .hasSingleBean(MetadataLocalProperties.class);
Assertions.assertThat(context).doesNotHaveBean(MetadataServletFilter.class); Assertions.assertThat(context).hasSingleBean(
Assertions.assertThat(context) MetadataAutoConfiguration.MetadataFeignInterceptorConfig.class);
.hasSingleBean(MetadataAutoConfiguration.MetadataReactiveFilterConfig.class);
Assertions.assertThat(context).hasSingleBean(MetadataReactiveFilter.class);
Assertions.assertThat(context)
.hasSingleBean(MetadataAutoConfiguration.MetadataFeignInterceptorConfig.class);
Assertions.assertThat(context).hasSingleBean(MetadataFirstFeignInterceptor.class);
Assertions.assertThat(context) Assertions.assertThat(context)
.hasSingleBean(MetadataAutoConfiguration.MetadataScgFilterConfig.class); .hasSingleBean(MetadataAutoConfiguration.MetadataScgFilterConfig.class);
Assertions.assertThat(context).hasSingleBean(MetadataFirstScgFilter.class); Assertions.assertThat(context).hasSingleBean(MetadataFirstScgFilter.class);

@ -57,6 +57,11 @@ public class PolarisContextProperties {
*/ */
private String namespace = "default"; private String namespace = "default";
/**
* polaris service name.
*/
private String service;
@Autowired @Autowired
private Environment environment; private Environment environment;
@ -111,4 +116,11 @@ public class PolarisContextProperties {
this.namespace = namespace; this.namespace = namespace;
} }
String getService() {
return service;
}
void setService(String service) {
this.service = service;
}
} }

@ -20,6 +20,13 @@
"default": "default", "default": "default",
"sourceType": "com.tencent.cloud.polaris.context.PolarisContextProperties" "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", "name": "spring.cloud.polaris.local-ip-address",
"type": "java.lang.String", "type": "java.lang.String",

Loading…
Cancel
Save