Improve code style & Upgrade spring boot version with 2.6.9 . (#322)

* Improve code style & Upgrade spring boot version with 2.6.9 .

* Update CHANGELOG.md
pull/332/head
VOPEN.XYZ 2 years ago committed by GitHub
parent 8020e35760
commit cf906626d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -9,7 +9,8 @@
- [Feature: Add config module unit test](https://github.com/Tencent/spring-cloud-tencent/pull/301) - [Feature: Add config module unit test](https://github.com/Tencent/spring-cloud-tencent/pull/301)
- [Feature:add restTemplate Report Polaris](https://github.com/Tencent/spring-cloud-tencent/pull/304) - [Feature:add restTemplate Report Polaris](https://github.com/Tencent/spring-cloud-tencent/pull/304)
- [Update GitHub Actions workflow](https://github.com/Tencent/spring-cloud-tencent/pull/305) - [Update GitHub Actions workflow](https://github.com/Tencent/spring-cloud-tencent/pull/305)
- [fix: 将blocking call改为non-blocking call](https://github.com/Tencent/spring-cloud-tencent/pull/309) - [fix: use non-blocking call instead of blocking call](https://github.com/Tencent/spring-cloud-tencent/pull/309)
- [fix:solve the chaos code problem on rejectTips](https://github.com/Tencent/spring-cloud-tencent/pull/283) - [fix:solve the chaos code problem on rejectTips](https://github.com/Tencent/spring-cloud-tencent/pull/283)
- [Fix config file format misspell](https://github.com/Tencent/spring-cloud-tencent/pull/321) - [Fix config file format misspell](https://github.com/Tencent/spring-cloud-tencent/pull/321)
- [Feature: Add instance metadata spi for registration](https://github.com/Tencent/spring-cloud-tencent/pull/324) - [Feature: Add instance metadata spi for registration](https://github.com/Tencent/spring-cloud-tencent/pull/324)
- [Improve: Improve code style & Upgrade spring boot version with 2.6.9](https://github.com/Tencent/spring-cloud-tencent/pull/322)

@ -91,6 +91,9 @@
<!-- Spring Cloud --> <!-- Spring Cloud -->
<spring.cloud.version>2021.0.3</spring.cloud.version> <spring.cloud.version>2021.0.3</spring.cloud.version>
<!-- Spring Boot -->
<spring.boot.version>2.6.9</spring.boot.version>
<!-- Spring Framework --> <!-- Spring Framework -->
<spring.framework.version>5.3.21</spring.framework.version> <spring.framework.version>5.3.21</spring.framework.version>
@ -127,6 +130,14 @@
<scope>import</scope> <scope>import</scope>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Spring Framework Dependencies --> <!-- Spring Framework Dependencies -->
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>

@ -32,8 +32,8 @@ import org.springframework.util.CollectionUtils;
import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.ServerWebExchange;
/** /**
* resolve custom transitive metadata from request. * Resolve custom transitive metadata from request.
*@author lepdou 2022-05-20 * @author lepdou 2022-05-20
*/ */
public class CustomTransitiveMetadataResolver { public class CustomTransitiveMetadataResolver {

@ -90,6 +90,12 @@
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>test</scope>
</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>

@ -32,9 +32,9 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
/** /**
* @author : wh * Auto configuration PolarisRestTemplateAutoConfiguration .
* @date : 2022/6/21 21:34 *
* @description: Auto configuration PolarisRestTemplateAutoConfiguration * @author wh 2022/6/21
*/ */
@ConditionalOnProperty(value = "spring.cloud.polaris.circuitbreaker.enabled", @ConditionalOnProperty(value = "spring.cloud.polaris.circuitbreaker.enabled",
havingValue = "true", matchIfMissing = true) havingValue = "true", matchIfMissing = true)
@ -44,13 +44,16 @@ public class PolarisRestTemplateAutoConfiguration {
@Bean @Bean
@ConditionalOnBean(RestTemplate.class) @ConditionalOnBean(RestTemplate.class)
public PolarisRestTemplateResponseErrorHandler polarisRestTemplateResponseErrorHandler(ConsumerAPI consumerAPI, @Autowired(required = false) PolarisResponseErrorHandler polarisResponseErrorHandler) { public PolarisRestTemplateResponseErrorHandler polarisRestTemplateResponseErrorHandler(
ConsumerAPI consumerAPI, @Autowired(required = false) PolarisResponseErrorHandler polarisResponseErrorHandler) {
return new PolarisRestTemplateResponseErrorHandler(consumerAPI, polarisResponseErrorHandler); return new PolarisRestTemplateResponseErrorHandler(consumerAPI, polarisResponseErrorHandler);
} }
@Bean @Bean
@ConditionalOnBean(RestTemplate.class) @ConditionalOnBean(RestTemplate.class)
public PolarisRestTemplateModifier polarisRestTemplateBeanPostProcessor(PolarisRestTemplateResponseErrorHandler restTemplateResponseErrorHandler) { public PolarisRestTemplateModifier polarisRestTemplateBeanPostProcessor(
PolarisRestTemplateResponseErrorHandler restTemplateResponseErrorHandler) {
return new PolarisRestTemplateModifier(restTemplateResponseErrorHandler); return new PolarisRestTemplateModifier(restTemplateResponseErrorHandler);
} }
} }

@ -24,7 +24,6 @@ import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.cloud.client.loadbalancer.LoadBalancerProperties;
import org.springframework.cloud.loadbalancer.blocking.client.BlockingLoadBalancerClient; import org.springframework.cloud.loadbalancer.blocking.client.BlockingLoadBalancerClient;
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory; import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
import org.springframework.cloud.openfeign.loadbalancer.FeignBlockingLoadBalancerClient; import org.springframework.cloud.openfeign.loadbalancer.FeignBlockingLoadBalancerClient;
@ -64,7 +63,6 @@ public class PolarisFeignBeanPostProcessor implements BeanPostProcessor, BeanFac
if (delegate != null) { if (delegate != null) {
return new PolarisFeignBlockingLoadBalancerClient(createPolarisFeignClient(delegate), return new PolarisFeignBlockingLoadBalancerClient(createPolarisFeignClient(delegate),
factory.getBean(BlockingLoadBalancerClient.class), factory.getBean(BlockingLoadBalancerClient.class),
factory.getBean(LoadBalancerProperties.class),
factory.getBean(LoadBalancerClientFactory.class)); factory.getBean(LoadBalancerClientFactory.class));
} }
} }

@ -20,7 +20,6 @@ package com.tencent.cloud.polaris.circuitbreaker.feign;
import feign.Client; import feign.Client;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerProperties;
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory; import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
import org.springframework.cloud.openfeign.loadbalancer.FeignBlockingLoadBalancerClient; import org.springframework.cloud.openfeign.loadbalancer.FeignBlockingLoadBalancerClient;
@ -32,8 +31,8 @@ import org.springframework.cloud.openfeign.loadbalancer.FeignBlockingLoadBalance
public class PolarisFeignBlockingLoadBalancerClient extends FeignBlockingLoadBalancerClient { public class PolarisFeignBlockingLoadBalancerClient extends FeignBlockingLoadBalancerClient {
public PolarisFeignBlockingLoadBalancerClient(Client delegate, LoadBalancerClient loadBalancerClient, public PolarisFeignBlockingLoadBalancerClient(Client delegate, LoadBalancerClient loadBalancerClient,
LoadBalancerProperties properties, LoadBalancerClientFactory loadBalancerClientFactory) { LoadBalancerClientFactory loadBalancerClientFactory) {
super(delegate, loadBalancerClient, properties, loadBalancerClientFactory); super(delegate, loadBalancerClient, loadBalancerClientFactory);
} }
} }

@ -20,9 +20,9 @@ package com.tencent.cloud.polaris.circuitbreaker.resttemplate;
import org.springframework.web.client.ResponseErrorHandler; import org.springframework.web.client.ResponseErrorHandler;
/** /**
* @author : wh * Polaris Response Error Handler Definition Of {@link ResponseErrorHandler}.
* @date : 2022/6/21 19:12 *
* @description: errorHandler {@link ResponseErrorHandler} * @author wh 2022/6/21
*/ */
public interface PolarisResponseErrorHandler extends ResponseErrorHandler { public interface PolarisResponseErrorHandler extends ResponseErrorHandler {

@ -28,10 +28,10 @@ import org.springframework.util.ObjectUtils;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
/** /**
* @author : wh * Auto configuration RestTemplate, Find the RestTemplate bean annotated with {@link LoadBalanced},
* @date : 2022/6/21 21:20 * then replace {@link org.springframework.web.client.ResponseErrorHandler} with {@link PolarisRestTemplateResponseErrorHandler} .
* @description: auto configuration RestTemplate Find the RestTemplate bean annotated with {@link LoadBalanced} and replace {@link org.springframework.web.client.ResponseErrorHandler} *
* with {@link PolarisRestTemplateResponseErrorHandler} * @author wh 2022/6/21
*/ */
public class PolarisRestTemplateModifier implements ApplicationContextAware, SmartInitializingSingleton { public class PolarisRestTemplateModifier implements ApplicationContextAware, SmartInitializingSingleton {
@ -62,4 +62,5 @@ public class PolarisRestTemplateModifier implements ApplicationContextAware, Sma
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext; this.applicationContext = applicationContext;
} }
} }

@ -29,42 +29,42 @@ import com.tencent.polaris.api.core.ConsumerAPI;
import com.tencent.polaris.api.pojo.RetStatus; import com.tencent.polaris.api.pojo.RetStatus;
import com.tencent.polaris.api.pojo.ServiceKey; import com.tencent.polaris.api.pojo.ServiceKey;
import com.tencent.polaris.api.rpc.ServiceCallResult; import com.tencent.polaris.api.rpc.ServiceCallResult;
import com.tencent.polaris.api.utils.StringUtils; import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import org.springframework.http.client.ClientHttpResponse; import org.springframework.http.client.ClientHttpResponse;
import org.springframework.lang.NonNull;
import org.springframework.web.client.ResponseErrorHandler; import org.springframework.web.client.ResponseErrorHandler;
/** /**
* @author : wh * Extend ResponseErrorHandler to get request information.
* @date : 2022/6/21 17:25 *
* @description: Extend ResponseErrorHandler to get request information * @author wh 2022/6/21
*/ */
public class PolarisRestTemplateResponseErrorHandler implements ResponseErrorHandler { public class PolarisRestTemplateResponseErrorHandler implements ResponseErrorHandler {
private static final Logger LOG = LoggerFactory.getLogger(PolarisRestTemplateResponseErrorHandler.class); private static final Logger LOG = LoggerFactory.getLogger(PolarisRestTemplateResponseErrorHandler.class);
private static final String FileName = "connection"; private static final String FILE_NAME = "connection";
private final ConsumerAPI consumerAPI; private final ConsumerAPI consumerAPI;
private final PolarisResponseErrorHandler polarisResponseErrorHandler; private final PolarisResponseErrorHandler polarisResponseErrorHandler;
public PolarisRestTemplateResponseErrorHandler(ConsumerAPI consumerAPI, PolarisResponseErrorHandler polarisResponseErrorHandler) { public PolarisRestTemplateResponseErrorHandler(ConsumerAPI consumerAPI, PolarisResponseErrorHandler polarisResponseErrorHandler) {
this.consumerAPI = consumerAPI; this.consumerAPI = consumerAPI;
this.polarisResponseErrorHandler = polarisResponseErrorHandler; this.polarisResponseErrorHandler = polarisResponseErrorHandler;
} }
@Override @Override
public boolean hasError(ClientHttpResponse response) { public boolean hasError(@NonNull ClientHttpResponse response) {
return true; return true;
} }
@Override @Override
public void handleError(ClientHttpResponse response) throws IOException { public void handleError(@NonNull ClientHttpResponse response) throws IOException {
if (Objects.nonNull(polarisResponseErrorHandler)) { if (Objects.nonNull(polarisResponseErrorHandler)) {
if (polarisResponseErrorHandler.hasError(response)) { if (polarisResponseErrorHandler.hasError(response)) {
polarisResponseErrorHandler.handleError(response); polarisResponseErrorHandler.handleError(response);
@ -72,12 +72,22 @@ public class PolarisRestTemplateResponseErrorHandler implements ResponseErrorHan
} }
} }
public void handleError(URI url, HttpMethod method, ClientHttpResponse response) throws IOException { @Override
ServiceCallResult resultRequest = null; public void handleError(@NonNull URI url, @NonNull HttpMethod method, @NonNull ClientHttpResponse response) throws IOException {
ServiceCallResult resultRequest = createServiceCallResult(url);
try { try {
resultRequest = builderServiceCallResult(url, response); HttpURLConnection connection = (HttpURLConnection) ReflectionUtils.getFieldValue(response, FILE_NAME);
if (connection != null) {
URL realURL = connection.getURL();
resultRequest.setHost(realURL.getHost());
resultRequest.setPort(realURL.getPort());
}
if (response.getStatusCode().value() > 500) {
resultRequest.setRetStatus(RetStatus.RetFail);
}
} }
catch (IOException e) { catch (Exception e) {
LOG.error("Will report response of {} url {}", response, url, e); LOG.error("Will report response of {} url {}", response, url, e);
throw e; throw e;
} }
@ -86,7 +96,7 @@ public class PolarisRestTemplateResponseErrorHandler implements ResponseErrorHan
} }
} }
private ServiceCallResult builderServiceCallResult(URI uri, ClientHttpResponse response) throws IOException { private ServiceCallResult createServiceCallResult(URI uri) {
ServiceCallResult resultRequest = new ServiceCallResult(); ServiceCallResult resultRequest = new ServiceCallResult();
String serviceName = uri.getHost(); String serviceName = uri.getHost();
resultRequest.setService(serviceName); resultRequest.setService(serviceName);
@ -98,13 +108,6 @@ public class PolarisRestTemplateResponseErrorHandler implements ResponseErrorHan
if (StringUtils.isNotBlank(sourceNamespace) && StringUtils.isNotBlank(sourceService)) { if (StringUtils.isNotBlank(sourceNamespace) && StringUtils.isNotBlank(sourceService)) {
resultRequest.setCallerService(new ServiceKey(sourceNamespace, sourceService)); resultRequest.setCallerService(new ServiceKey(sourceNamespace, sourceService));
} }
HttpURLConnection connection = (HttpURLConnection) ReflectionUtils.getFieldValue(response, FileName);
URL url = connection.getURL();
resultRequest.setHost(url.getHost());
resultRequest.setPort(url.getPort());
if (response.getStatusCode().value() > 500) {
resultRequest.setRetStatus(RetStatus.RetFail);
}
return resultRequest; return resultRequest;
} }

@ -3,4 +3,3 @@ org.springframework.cloud.bootstrap.BootstrapConfiguration=\
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.tencent.cloud.polaris.circuitbreaker.config.PolarisFeignClientAutoConfiguration,\ com.tencent.cloud.polaris.circuitbreaker.config.PolarisFeignClientAutoConfiguration,\
com.tencent.cloud.polaris.circuitbreaker.config.PolarisRestTemplateAutoConfiguration com.tencent.cloud.polaris.circuitbreaker.config.PolarisRestTemplateAutoConfiguration

@ -0,0 +1,72 @@
/*
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* 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.polaris.circuitbreaker;
import com.tencent.cloud.polaris.circuitbreaker.config.PolarisRestTemplateAutoConfiguration;
import com.tencent.cloud.polaris.circuitbreaker.resttemplate.PolarisRestTemplateModifier;
import com.tencent.cloud.polaris.circuitbreaker.resttemplate.PolarisRestTemplateResponseErrorHandler;
import com.tencent.cloud.polaris.context.PolarisContextAutoConfiguration;
import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Test For {@link PolarisRestTemplateAutoConfiguration} .
*
* @author <a href="mailto:iskp.me@gmail.com">Palmer Xu</a> 2022-06-28
*/
public class PolarisRestTemplateAutoConfigurationTest {
private final WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
.withConfiguration(
AutoConfigurations.of(
PolarisRestTemplateAutoConfigurationTester.class,
PolarisContextAutoConfiguration.class,
PolarisRestTemplateAutoConfiguration.class))
.withPropertyValues("spring.cloud.polaris.circuitbreaker.enabled=true");
@Test
public void testInitialization() {
this.contextRunner
.run(context -> {
assertThat(context).hasSingleBean(PolarisRestTemplateModifier.class);
assertThat(context).hasSingleBean(PolarisRestTemplateResponseErrorHandler.class);
});
}
@Configuration
@EnableAutoConfiguration
@AutoConfigureBefore(PolarisRestTemplateAutoConfiguration.class)
static class PolarisRestTemplateAutoConfigurationTester {
@Bean
RestTemplate restTemplate() {
return new RestTemplate();
}
}
}

@ -30,7 +30,7 @@ public class PolarisFeignBlockingLoadBalancerClientTest {
@Test @Test
public void testConstructor() { public void testConstructor() {
try { try {
new PolarisFeignBlockingLoadBalancerClient(null, null, null, null); new PolarisFeignBlockingLoadBalancerClient(null, null, null);
} }
catch (Exception e) { catch (Exception e) {
Assertions.fail("Exception encountered.", e); Assertions.fail("Exception encountered.", e);

@ -15,14 +15,13 @@
* specific language governing permissions and limitations under the License. * specific language governing permissions and limitations under the License.
*/ */
package com.tencent.cloud.polaris.circuitbreaker; package com.tencent.cloud.polaris.circuitbreaker.resttemplate;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URI; import java.net.URI;
import java.net.URL; import java.net.URL;
import com.tencent.cloud.polaris.circuitbreaker.resttemplate.PolarisRestTemplateResponseErrorHandler;
import com.tencent.polaris.api.core.ConsumerAPI; import com.tencent.polaris.api.core.ConsumerAPI;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -36,9 +35,9 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
/** /**
* @author : wh * Test For {@link PolarisRestTemplateResponseErrorHandler}.
* @date : 2022/6/22 09:00 *
* @description: Test for {@link PolarisRestTemplateResponseErrorHandler}. * @author wh 2022/6/22
*/ */
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest(classes = PolarisRestTemplateResponseErrorHandlerTest.TestApplication.class, @SpringBootTest(classes = PolarisRestTemplateResponseErrorHandlerTest.TestApplication.class,

@ -15,7 +15,7 @@
* specific language governing permissions and limitations under the License. * specific language governing permissions and limitations under the License.
*/ */
package com.tencent.cloud.polaris.circuitbreaker; package com.tencent.cloud.polaris.circuitbreaker.resttemplate;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -29,9 +29,9 @@ import org.springframework.util.StringUtils;
/** /**
* @author : wh * Mock Test for {@link AbstractClientHttpResponse}.
* @date : 2022/6/22 09:00 *
* @description: mock {@link org.springframework.http.client.SimpleClientHttpResponse} * @author wh 2022/6/22
*/ */
public class SimpleClientHttpResponseTest extends AbstractClientHttpResponse { public class SimpleClientHttpResponseTest extends AbstractClientHttpResponse {
Loading…
Cancel
Save