feat:optimize code style.

pull/123/head
SkyeBeFreeman 3 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)
- [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: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 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)
- [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.
if (null != interceptors && !interceptors
.contains(encodeTransferMedataRestTemplateInterceptor)) {
interceptors.add(this.encodeTransferMedataRestTemplateInterceptor);
interceptors
.add(this.encodeTransferMedataRestTemplateInterceptor);
restTemplate.setInterceptors(interceptors);
}
}

@ -38,13 +38,12 @@ import org.springframework.web.server.WebFilterChain;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.MOCK;
/**
*
* @author Haotian Zhang
*/
@RunWith(SpringRunner.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 DecodeTransferMetadataReactiveFilterTest {
@Autowired

@ -40,13 +40,12 @@ import org.springframework.test.context.junit4.SpringRunner;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
/**
*
* @author Haotian Zhang
*/
@RunWith(SpringRunner.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 DecodeTransferMetadataServletFilterTest {
@Autowired

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

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

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

@ -91,8 +91,10 @@ public class QuotaCheckReactiveFilter implements WebFilter, Ordered {
if (!CollectionUtils.isEmpty(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();
response.setStatusCode(HttpStatus.TOO_MANY_REQUESTS);
response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
DataBuffer dataBuffer = response.bufferFactory().allocateBuffer().write(
RateLimitConstant.QUOTA_LIMITED_INFO.getBytes(StandardCharsets.UTF_8));
DataBuffer dataBuffer = response.bufferFactory().allocateBuffer()
.write(RateLimitConstant.QUOTA_LIMITED_INFO
.getBytes(StandardCharsets.UTF_8));
return response.writeWith(Mono.just(dataBuffer));
}
}

@ -57,9 +57,11 @@ public class QuotaCheckServletFilter extends OncePerRequestFilter {
.getLogger(QuotaCheckServletFilter.class);
private final LimitAPI limitAPI;
private final PolarisRateLimiterLabelServletResolver labelResolver;
public QuotaCheckServletFilter(LimitAPI limitAPI, PolarisRateLimiterLabelServletResolver labelResolver) {
public QuotaCheckServletFilter(LimitAPI limitAPI,
PolarisRateLimiterLabelServletResolver labelResolver) {
this.limitAPI = limitAPI;
this.labelResolver = labelResolver;
}
@ -87,8 +89,10 @@ public class QuotaCheckServletFilter extends OncePerRequestFilter {
if (!CollectionUtils.isEmpty(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.
*
* @param exchange the http request
* @return resolved labels
*/

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

@ -55,17 +55,20 @@ public class MetadataContext {
private final Map<String, String> systemMetadata;
static {
String namespace = ApplicationContextAwareUtils.getProperties("spring.cloud.polaris.namespace");
String namespace = ApplicationContextAwareUtils
.getProperties("spring.cloud.polaris.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;
String serviceName = ApplicationContextAwareUtils.getProperties("spring.cloud.polaris.service");
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));
"spring.cloud.polaris.discovery.service", ApplicationContextAwareUtils
.getProperties("spring.application.name", null));
}
LOCAL_SERVICE = serviceName;
}

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

@ -30,18 +30,19 @@ import org.springframework.stereotype.Component;
/**
* resolver custom label from request.
*
*@author lepdou 2022-03-31
* @author lepdou 2022-03-31
*/
@Component
public class CustomLabelResolver implements PolarisRateLimiterLabelServletResolver {
@Override
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<>();
labels.put("user", "zhangsan");
return labels;
}
}

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

@ -49,4 +49,5 @@ public class RouterCalleeController {
LOG.info("Discovery Service Callee [{}] 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.
*
* @author lepdou 2022-04-06
*/
@SpringBootApplication
@ -31,4 +32,5 @@ public class RouterCalleeApplication2 {
public static void main(String[] args) {
SpringApplication.run(RouterCalleeApplication2.class, args);
}
}

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

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

Loading…
Cancel
Save