feat:optimize code style.

pull/123/head
SkyeBeFreeman 2 years ago
parent 9a626dccc6
commit 4038c23fe5

@ -2,10 +2,10 @@
--- ---
- [Bugfix: fix router bug and add router example](https://github.com/Tencent/spring-cloud-tencent/pull/89) - [Bugfix: fix router bug and add router example](https://github.com/Tencent/spring-cloud-tencent/pull/89)
- [feat:add switch of polaris, discovery and register.](https://github.com/Tencent/spring-cloud-tencent/pull/122)
- [feat:add custom label resolver spi for rate limit](https://github.com/Tencent/spring-cloud-tencent/pull/105) - [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/102) - [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/98) - [Bugfix: fix causing cpu 100% when set ScheduledThreadPoolExecutor corePoolSize=0](https://github.com/Tencent/spring-cloud-tencent/pull/98)
- [Bugfix: fix circuitbreaker http code greater than 400 as fail response bug](https://github.com/Tencent/spring-cloud-tencent/pull/116) - [Bugfix: fix circuitbreaker http code greater than 400 as fail response bug](https://github.com/Tencent/spring-cloud-tencent/pull/116)
- [Feat: optimize router dependency](https://github.com/Tencent/spring-cloud-tencent/pull/110) - [Feat: optimize router dependency](https://github.com/Tencent/spring-cloud-tencent/pull/110)
- [Refactor: refactor transfer metadata](https://github.com/Tencent/spring-cloud-tencent/pull/111) - [Refactor: refactor transfer metadata](https://github.com/Tencent/spring-cloud-tencent/pull/111)
- [feat:add switch of polaris, discovery and register.](https://github.com/Tencent/spring-cloud-tencent/pull/122)

@ -209,7 +209,8 @@ public class MetadataTransferAutoConfiguration {
// Avoid setting interceptor repeatedly. // Avoid setting interceptor repeatedly.
if (null != interceptors && !interceptors if (null != interceptors && !interceptors
.contains(encodeTransferMedataRestTemplateInterceptor)) { .contains(encodeTransferMedataRestTemplateInterceptor)) {
interceptors.add(this.encodeTransferMedataRestTemplateInterceptor); interceptors
.add(this.encodeTransferMedataRestTemplateInterceptor);
restTemplate.setInterceptors(interceptors); restTemplate.setInterceptors(interceptors);
} }
} }

@ -38,13 +38,12 @@ 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;
/** /**
*
* @author Haotian Zhang * @author Haotian Zhang
*/ */
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = MOCK, @SpringBootTest(webEnvironment = MOCK,
classes = DecodeTransferMetadataServletFilterTest.TestApplication.class, classes = DecodeTransferMetadataServletFilterTest.TestApplication.class,
properties = {"spring.config.location = classpath:application-test.yml"}) properties = { "spring.config.location = classpath:application-test.yml" })
public class DecodeTransferMetadataReactiveFilterTest { public class DecodeTransferMetadataReactiveFilterTest {
@Autowired @Autowired

@ -40,13 +40,12 @@ 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;
/** /**
*
* @author Haotian Zhang * @author Haotian Zhang
*/ */
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT, @SpringBootTest(webEnvironment = RANDOM_PORT,
classes = DecodeTransferMetadataServletFilterTest.TestApplication.class, classes = DecodeTransferMetadataServletFilterTest.TestApplication.class,
properties = {"spring.config.location = classpath:application-test.yml"}) properties = { "spring.config.location = classpath:application-test.yml" })
public class DecodeTransferMetadataServletFilterTest { public class DecodeTransferMetadataServletFilterTest {
@Autowired @Autowired

@ -52,8 +52,8 @@ public class MetadataTransferAutoConfigurationTest {
.hasSingleBean(EncodeTransferMedataFeignInterceptor.class); .hasSingleBean(EncodeTransferMedataFeignInterceptor.class);
Assertions.assertThat(context).hasSingleBean( Assertions.assertThat(context).hasSingleBean(
MetadataTransferAutoConfiguration.MetadataTransferRestTemplateConfig.class); MetadataTransferAutoConfiguration.MetadataTransferRestTemplateConfig.class);
Assertions.assertThat(context) Assertions.assertThat(context).hasSingleBean(
.hasSingleBean(EncodeTransferMedataRestTemplateInterceptor.class); EncodeTransferMedataRestTemplateInterceptor.class);
Assertions.assertThat(context).hasSingleBean( Assertions.assertThat(context).hasSingleBean(
MetadataTransferAutoConfiguration.MetadataTransferRestTemplateConfig.EncodeTransferMetadataRestTemplatePostProcessor.class); MetadataTransferAutoConfiguration.MetadataTransferRestTemplateConfig.EncodeTransferMetadataRestTemplatePostProcessor.class);
Assertions.assertThat(context).hasSingleBean( Assertions.assertThat(context).hasSingleBean(

@ -73,9 +73,10 @@ public class PolarisServiceRegistry implements ServiceRegistry<Registration> {
this.metadataLocalProperties = metadataLocalProperties; this.metadataLocalProperties = metadataLocalProperties;
if (polarisDiscoveryProperties.isHeartbeatEnabled()) { if (polarisDiscoveryProperties.isHeartbeatEnabled()) {
this.heartbeatExecutor = Executors this.heartbeatExecutor = Executors.newSingleThreadScheduledExecutor(
.newSingleThreadScheduledExecutor(new NamedThreadFactory("spring-cloud-heartbeat")); new NamedThreadFactory("spring-cloud-heartbeat"));
} else { }
else {
this.heartbeatExecutor = null; this.heartbeatExecutor = null;
} }
} }

@ -56,7 +56,8 @@ public class PolarisServerList extends AbstractServerList<Server> {
} }
private List<Server> getServers() { private List<Server> getServers() {
InstancesResponse allInstances = polarisDiscoveryHandler.getFilteredInstances(serviceId); InstancesResponse allInstances = polarisDiscoveryHandler
.getFilteredInstances(serviceId);
ServiceInstances serviceInstances = allInstances.toServiceInstances(); ServiceInstances serviceInstances = allInstances.toServiceInstances();
List<Server> polarisServers = new ArrayList<>(); List<Server> polarisServers = new ArrayList<>();
for (Instance instance : serviceInstances.getInstances()) { for (Instance instance : serviceInstances.getInstances()) {

@ -91,8 +91,10 @@ public class QuotaCheckReactiveFilter implements WebFilter, Ordered {
if (!CollectionUtils.isEmpty(customLabels)) { if (!CollectionUtils.isEmpty(customLabels)) {
labels.putAll(customLabels); labels.putAll(customLabels);
} }
} catch (Throwable e) { }
LOG.error("resolve custom label failed. resolver = {}", labelResolver.getClass().getName(), e); catch (Throwable e) {
LOG.error("resolve custom label failed. resolver = {}",
labelResolver.getClass().getName(), e);
} }
} }
@ -104,8 +106,9 @@ public class QuotaCheckReactiveFilter implements WebFilter, Ordered {
ServerHttpResponse response = exchange.getResponse(); ServerHttpResponse response = exchange.getResponse();
response.setStatusCode(HttpStatus.TOO_MANY_REQUESTS); response.setStatusCode(HttpStatus.TOO_MANY_REQUESTS);
response.getHeaders().setContentType(MediaType.APPLICATION_JSON); response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
DataBuffer dataBuffer = response.bufferFactory().allocateBuffer().write( DataBuffer dataBuffer = response.bufferFactory().allocateBuffer()
RateLimitConstant.QUOTA_LIMITED_INFO.getBytes(StandardCharsets.UTF_8)); .write(RateLimitConstant.QUOTA_LIMITED_INFO
.getBytes(StandardCharsets.UTF_8));
return response.writeWith(Mono.just(dataBuffer)); return response.writeWith(Mono.just(dataBuffer));
} }
} }

@ -57,9 +57,11 @@ public class QuotaCheckServletFilter extends OncePerRequestFilter {
.getLogger(QuotaCheckServletFilter.class); .getLogger(QuotaCheckServletFilter.class);
private final LimitAPI limitAPI; private final LimitAPI limitAPI;
private final PolarisRateLimiterLabelServletResolver labelResolver; private final PolarisRateLimiterLabelServletResolver labelResolver;
public QuotaCheckServletFilter(LimitAPI limitAPI, PolarisRateLimiterLabelServletResolver labelResolver) { public QuotaCheckServletFilter(LimitAPI limitAPI,
PolarisRateLimiterLabelServletResolver labelResolver) {
this.limitAPI = limitAPI; this.limitAPI = limitAPI;
this.labelResolver = labelResolver; this.labelResolver = labelResolver;
} }
@ -87,8 +89,10 @@ public class QuotaCheckServletFilter extends OncePerRequestFilter {
if (!CollectionUtils.isEmpty(customLabels)) { if (!CollectionUtils.isEmpty(customLabels)) {
labels.putAll(customLabels); labels.putAll(customLabels);
} }
} catch (Throwable e) { }
LOG.error("resolve custom label failed. resolver = {}", labelResolver.getClass().getName(), e); catch (Throwable e) {
LOG.error("resolve custom label failed. resolver = {}",
labelResolver.getClass().getName(), e);
} }
} }

@ -31,7 +31,6 @@ public interface PolarisRateLimiterLabelReactiveResolver {
/** /**
* Resolve custom label from request. * Resolve custom label from request.
*
* @param exchange the http request * @param exchange the http request
* @return resolved labels * @return resolved labels
*/ */

@ -31,7 +31,6 @@ public interface PolarisRateLimiterLabelServletResolver {
/** /**
* Resolve custom label from request. * Resolve custom label from request.
*
* @param request the http request * @param request the http request
* @return resolved labels * @return resolved labels
*/ */

@ -55,17 +55,20 @@ public class MetadataContext {
private final Map<String, String> systemMetadata; private final Map<String, String> systemMetadata;
static { static {
String namespace = ApplicationContextAwareUtils.getProperties("spring.cloud.polaris.namespace"); String namespace = ApplicationContextAwareUtils
.getProperties("spring.cloud.polaris.namespace");
if (StringUtils.isEmpty(namespace)) { if (StringUtils.isEmpty(namespace)) {
namespace = ApplicationContextAwareUtils.getProperties("spring.cloud.polaris.discovery.namespace", "default"); namespace = ApplicationContextAwareUtils
.getProperties("spring.cloud.polaris.discovery.namespace", "default");
} }
LOCAL_NAMESPACE = namespace; LOCAL_NAMESPACE = namespace;
String serviceName = ApplicationContextAwareUtils.getProperties("spring.cloud.polaris.service"); String serviceName = ApplicationContextAwareUtils
.getProperties("spring.cloud.polaris.service");
if (StringUtils.isEmpty(serviceName)) { if (StringUtils.isEmpty(serviceName)) {
serviceName = ApplicationContextAwareUtils.getProperties( serviceName = ApplicationContextAwareUtils.getProperties(
"spring.cloud.polaris.discovery.service", "spring.cloud.polaris.discovery.service", ApplicationContextAwareUtils
ApplicationContextAwareUtils.getProperties("spring.application.name", null)); .getProperties("spring.application.name", null));
} }
LOCAL_SERVICE = serviceName; LOCAL_SERVICE = serviceName;
} }

@ -16,8 +16,8 @@ spring:
# host: 127.0.0.1 # host: 127.0.0.1
# enabled: true # enabled: true
# discovery: # discovery:
# enabled: false # enabled: true
# register: false # register: true
# instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port} # instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
# service-name: ${spring.application.name} # service-name: ${spring.application.name}
# ip-address: localhost # ip-address: localhost

@ -30,18 +30,19 @@ import org.springframework.stereotype.Component;
/** /**
* resolver custom label from request. * resolver custom label from request.
* *
*@author lepdou 2022-03-31 * @author lepdou 2022-03-31
*/ */
@Component @Component
public class CustomLabelResolver implements PolarisRateLimiterLabelServletResolver { public class CustomLabelResolver implements PolarisRateLimiterLabelServletResolver {
@Override @Override
public Map<String, String> resolve(HttpServletRequest request) { public Map<String, String> resolve(HttpServletRequest request) {
//rate limit by some request params. such as query params, headers .. // rate limit by some request params. such as query params, headers ..
Map<String, String> labels = new HashMap<>(); Map<String, String> labels = new HashMap<>();
labels.put("user", "zhangsan"); labels.put("user", "zhangsan");
return labels; return labels;
} }
} }

@ -23,7 +23,8 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
/** /**
* Router callee application. * Router callee application.
*@author lepdou 2022-04-06 *
* @author lepdou 2022-04-06
*/ */
@SpringBootApplication @SpringBootApplication
public class RouterCalleeApplication1 { public class RouterCalleeApplication1 {
@ -31,4 +32,5 @@ public class RouterCalleeApplication1 {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(RouterCalleeApplication1.class, args); SpringApplication.run(RouterCalleeApplication1.class, args);
} }
} }

@ -49,4 +49,5 @@ public class RouterCalleeController {
LOG.info("Discovery Service Callee [{}] is called.", port); LOG.info("Discovery Service Callee [{}] is called.", port);
return String.format("Discovery Service Callee [%s] is called.", port); return String.format("Discovery Service Callee [%s] is called.", port);
} }
} }

@ -23,6 +23,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
/** /**
* Router callee application. * Router callee application.
*
* @author lepdou 2022-04-06 * @author lepdou 2022-04-06
*/ */
@SpringBootApplication @SpringBootApplication
@ -31,4 +32,5 @@ public class RouterCalleeApplication2 {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(RouterCalleeApplication2.class, args); SpringApplication.run(RouterCalleeApplication2.class, args);
} }
} }

@ -29,7 +29,7 @@ import org.springframework.web.client.RestTemplate;
/** /**
* Router caller application. * Router caller application.
* *
*@author lepdou 2022-04-06 * @author lepdou 2022-04-06
*/ */
@SpringBootApplication @SpringBootApplication
@EnableDiscoveryClient @EnableDiscoveryClient
@ -45,4 +45,5 @@ public class RouterCallerApplication {
public RestTemplate restTemplate() { public RestTemplate restTemplate() {
return new RestTemplate(); return new RestTemplate();
} }
} }

@ -137,4 +137,5 @@ public class PolarisContextProperties {
void setService(String service) { void setService(String service) {
this.service = service; this.service = service;
} }
} }

Loading…
Cancel
Save