diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-config-2.4.x-example/src/main/java/com/alibaba/cloud/imports/examples/Application.java b/spring-cloud-alibaba-examples/nacos-example/nacos-config-2.4.x-example/src/main/java/com/alibaba/cloud/imports/examples/Application.java index 7fd605331..a3e47854f 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-config-2.4.x-example/src/main/java/com/alibaba/cloud/imports/examples/Application.java +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-config-2.4.x-example/src/main/java/com/alibaba/cloud/imports/examples/Application.java @@ -17,6 +17,7 @@ package com.alibaba.cloud.imports.examples; import com.alibaba.cloud.imports.examples.model.UserConfig; + import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.EnableConfigurationProperties; diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-config-2.4.x-example/src/main/java/com/alibaba/cloud/imports/examples/controller/UserController.java b/spring-cloud-alibaba-examples/nacos-example/nacos-config-2.4.x-example/src/main/java/com/alibaba/cloud/imports/examples/controller/UserController.java index 88275cd15..adfce66b1 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-config-2.4.x-example/src/main/java/com/alibaba/cloud/imports/examples/controller/UserController.java +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-config-2.4.x-example/src/main/java/com/alibaba/cloud/imports/examples/controller/UserController.java @@ -1,8 +1,25 @@ +/* + * Copyright 2013-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * 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.alibaba.cloud.imports.examples.controller; import com.alibaba.cloud.imports.examples.model.UserConfig; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @@ -15,12 +32,12 @@ import org.springframework.web.bind.annotation.RestController; @RestController public class UserController { - @Autowired - private UserConfig userConfig; + @Autowired + private UserConfig userConfig; - @GetMapping - public String get() throws JsonProcessingException { - return new ObjectMapper().writeValueAsString(userConfig); - } + @GetMapping + public String get() throws JsonProcessingException { + return new ObjectMapper().writeValueAsString(userConfig); + } } diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-config-2.4.x-example/src/main/java/com/alibaba/cloud/imports/examples/model/UserConfig.java b/spring-cloud-alibaba-examples/nacos-example/nacos-config-2.4.x-example/src/main/java/com/alibaba/cloud/imports/examples/model/UserConfig.java index 12df483d5..b6b8df93c 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-config-2.4.x-example/src/main/java/com/alibaba/cloud/imports/examples/model/UserConfig.java +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-config-2.4.x-example/src/main/java/com/alibaba/cloud/imports/examples/model/UserConfig.java @@ -1,10 +1,26 @@ -package com.alibaba.cloud.imports.examples.model; +/* + * Copyright 2013-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + */ -import org.springframework.boot.context.properties.ConfigurationProperties; +package com.alibaba.cloud.imports.examples.model; import java.util.List; import java.util.Map; +import org.springframework.boot.context.properties.ConfigurationProperties; + /** * * @@ -12,64 +28,62 @@ import java.util.Map; */ @ConfigurationProperties(prefix = "configdata.user") public class UserConfig { - private String name; - private Integer age; - private Map map; - private List users; - - public static class User { - private String name; - private Integer age; - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public Integer getAge() { - return age; - } - - public void setAge(Integer age) { - this.age = age; - } - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public Integer getAge() { - return age; - } - - public void setAge(Integer age) { - this.age = age; - } - - public Map getMap() { - return map; - } - - public void setMap(Map map) { - this.map = map; - } - - public List getUsers() { - return users; - } - - public void setUsers(List users) { - this.users = users; - } - - + private String name; + private Integer age; + private Map map; + private List users; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Integer getAge() { + return age; + } + + public void setAge(Integer age) { + this.age = age; + } + + public Map getMap() { + return map; + } + + public void setMap(Map map) { + this.map = map; + } + + public List getUsers() { + return users; + } + + public void setUsers(List users) { + this.users = users; + } + + public static class User { + private String name; + private Integer age; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Integer getAge() { + return age; + } + + public void setAge(Integer age) { + this.age = age; + } + } } diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-circuitbreaker-example/src/main/java/com/alibaba/cloud/examples/controller/ApiController.java b/spring-cloud-alibaba-examples/sentinel-example/sentinel-circuitbreaker-example/src/main/java/com/alibaba/cloud/examples/controller/ApiController.java index 0e78ee1a7..31ec17a2a 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-circuitbreaker-example/src/main/java/com/alibaba/cloud/examples/controller/ApiController.java +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-circuitbreaker-example/src/main/java/com/alibaba/cloud/examples/controller/ApiController.java @@ -1,3 +1,19 @@ +/* + * Copyright 2013-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * 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.alibaba.cloud.examples.controller; import org.springframework.web.bind.annotation.GetMapping; @@ -12,28 +28,28 @@ import org.springframework.web.bind.annotation.RestController; @RestController public class ApiController { - @GetMapping("/default/{ok}") - public String defaultConfig(@PathVariable boolean ok) { - if (ok) { - return "ok"; - } - throw new RuntimeException("fail"); - } + @GetMapping("/default/{ok}") + public String defaultConfig(@PathVariable boolean ok) { + if (ok) { + return "ok"; + } + throw new RuntimeException("fail"); + } - @GetMapping("/feign/{ok}") - public String feignConfig(@PathVariable boolean ok) { - if (ok) { - return "ok"; - } - throw new RuntimeException("fail"); - } + @GetMapping("/feign/{ok}") + public String feignConfig(@PathVariable boolean ok) { + if (ok) { + return "ok"; + } + throw new RuntimeException("fail"); + } - @GetMapping("/feignMethod/{ok}") - public String feignMethodConfig(@PathVariable boolean ok) { - if (ok) { - return "ok"; - } - throw new RuntimeException("fail"); - } + @GetMapping("/feignMethod/{ok}") + public String feignMethodConfig(@PathVariable boolean ok) { + if (ok) { + return "ok"; + } + throw new RuntimeException("fail"); + } } diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-circuitbreaker-example/src/main/java/com/alibaba/cloud/examples/controller/TestController.java b/spring-cloud-alibaba-examples/sentinel-example/sentinel-circuitbreaker-example/src/main/java/com/alibaba/cloud/examples/controller/TestController.java index e6f4a3588..281c1c2c8 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-circuitbreaker-example/src/main/java/com/alibaba/cloud/examples/controller/TestController.java +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-circuitbreaker-example/src/main/java/com/alibaba/cloud/examples/controller/TestController.java @@ -1,7 +1,24 @@ +/* + * Copyright 2013-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * 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.alibaba.cloud.examples.controller; import com.alibaba.cloud.examples.feign.OrderClient; import com.alibaba.cloud.examples.feign.UserClient; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @@ -15,25 +32,24 @@ import org.springframework.web.bind.annotation.RestController; @RestController public class TestController { - @Autowired - private UserClient userClient; - @Autowired - private OrderClient orderClient; - - - @GetMapping("/test/default/{ok}") - public String testDefault(@PathVariable boolean ok) { - return orderClient.defaultConfig(ok); - } - - @GetMapping("/test/feign/{ok}") - public String testFeign(@PathVariable boolean ok) { - return userClient.feign(ok); - } - - @GetMapping("/test/feignMethod/{ok}") - public String testFeignMethod(@PathVariable boolean ok) { - return userClient.feignMethod(ok); - } + @Autowired + private UserClient userClient; + @Autowired + private OrderClient orderClient; + + @GetMapping("/test/default/{ok}") + public String testDefault(@PathVariable boolean ok) { + return orderClient.defaultConfig(ok); + } + + @GetMapping("/test/feign/{ok}") + public String testFeign(@PathVariable boolean ok) { + return userClient.feign(ok); + } + + @GetMapping("/test/feignMethod/{ok}") + public String testFeignMethod(@PathVariable boolean ok) { + return userClient.feignMethod(ok); + } } diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-circuitbreaker-example/src/main/java/com/alibaba/cloud/examples/feign/OrderClient.java b/spring-cloud-alibaba-examples/sentinel-example/sentinel-circuitbreaker-example/src/main/java/com/alibaba/cloud/examples/feign/OrderClient.java index c715b12ea..0448e4f07 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-circuitbreaker-example/src/main/java/com/alibaba/cloud/examples/feign/OrderClient.java +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-circuitbreaker-example/src/main/java/com/alibaba/cloud/examples/feign/OrderClient.java @@ -1,3 +1,19 @@ +/* + * Copyright 2013-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * 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.alibaba.cloud.examples.feign; import org.springframework.cloud.openfeign.FeignClient; @@ -12,7 +28,7 @@ import org.springframework.web.bind.annotation.PathVariable; @FeignClient(value = "order", url = "http://localhost:${server.port}", fallback = OrderClientFallBack.class) public interface OrderClient { - @GetMapping("/default/{ok}") - String defaultConfig(@PathVariable boolean ok); + @GetMapping("/default/{ok}") + String defaultConfig(@PathVariable boolean ok); } diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-circuitbreaker-example/src/main/java/com/alibaba/cloud/examples/feign/OrderClientFallBack.java b/spring-cloud-alibaba-examples/sentinel-example/sentinel-circuitbreaker-example/src/main/java/com/alibaba/cloud/examples/feign/OrderClientFallBack.java index bb142ba2a..9ef6e797a 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-circuitbreaker-example/src/main/java/com/alibaba/cloud/examples/feign/OrderClientFallBack.java +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-circuitbreaker-example/src/main/java/com/alibaba/cloud/examples/feign/OrderClientFallBack.java @@ -1,3 +1,19 @@ +/* + * Copyright 2013-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * 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.alibaba.cloud.examples.feign; import org.springframework.stereotype.Component; @@ -9,8 +25,8 @@ import org.springframework.stereotype.Component; */ @Component public class OrderClientFallBack implements OrderClient { - @Override - public String defaultConfig(boolean ok) { - return "order fallback"; - } + @Override + public String defaultConfig(boolean ok) { + return "order fallback"; + } } diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-circuitbreaker-example/src/main/java/com/alibaba/cloud/examples/feign/UserClient.java b/spring-cloud-alibaba-examples/sentinel-example/sentinel-circuitbreaker-example/src/main/java/com/alibaba/cloud/examples/feign/UserClient.java index 301846de0..ea5bd8b66 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-circuitbreaker-example/src/main/java/com/alibaba/cloud/examples/feign/UserClient.java +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-circuitbreaker-example/src/main/java/com/alibaba/cloud/examples/feign/UserClient.java @@ -1,3 +1,19 @@ +/* + * Copyright 2013-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * 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.alibaba.cloud.examples.feign; import org.springframework.cloud.openfeign.FeignClient; @@ -12,10 +28,10 @@ import org.springframework.web.bind.annotation.PathVariable; @FeignClient(value = "user", url = "http://localhost:${server.port}", fallback = UserClientFallBack.class) public interface UserClient { - @GetMapping("/feignMethod/{ok}") - String feignMethod(@PathVariable boolean ok); + @GetMapping("/feignMethod/{ok}") + String feignMethod(@PathVariable boolean ok); - @GetMapping("/feign/{ok}") - String feign(@PathVariable boolean ok); + @GetMapping("/feign/{ok}") + String feign(@PathVariable boolean ok); } diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-circuitbreaker-example/src/main/java/com/alibaba/cloud/examples/feign/UserClientFallBack.java b/spring-cloud-alibaba-examples/sentinel-example/sentinel-circuitbreaker-example/src/main/java/com/alibaba/cloud/examples/feign/UserClientFallBack.java index 933d9c281..454240cff 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-circuitbreaker-example/src/main/java/com/alibaba/cloud/examples/feign/UserClientFallBack.java +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-circuitbreaker-example/src/main/java/com/alibaba/cloud/examples/feign/UserClientFallBack.java @@ -1,3 +1,19 @@ +/* + * Copyright 2013-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * 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.alibaba.cloud.examples.feign; import org.springframework.stereotype.Component; @@ -9,13 +25,13 @@ import org.springframework.stereotype.Component; */ @Component public class UserClientFallBack implements UserClient { - @Override - public String feignMethod(boolean ok) { - return "user fallback"; - } + @Override + public String feignMethod(boolean ok) { + return "user fallback"; + } - @Override - public String feign(boolean ok) { - return "user fallback"; - } + @Override + public String feign(boolean ok) { + return "user fallback"; + } } diff --git a/spring-cloud-alibaba-starters/spring-cloud-circuitbreaker-sentinel/src/main/java/com/alibaba/cloud/circuitbreaker/sentinel/feign/CircuitBreakerRuleChangeListener.java b/spring-cloud-alibaba-starters/spring-cloud-circuitbreaker-sentinel/src/main/java/com/alibaba/cloud/circuitbreaker/sentinel/feign/CircuitBreakerRuleChangeListener.java index 7b41d2257..049d6b6fe 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-circuitbreaker-sentinel/src/main/java/com/alibaba/cloud/circuitbreaker/sentinel/feign/CircuitBreakerRuleChangeListener.java +++ b/spring-cloud-alibaba-starters/spring-cloud-circuitbreaker-sentinel/src/main/java/com/alibaba/cloud/circuitbreaker/sentinel/feign/CircuitBreakerRuleChangeListener.java @@ -1,7 +1,30 @@ +/* + * Copyright 2013-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * 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.alibaba.cloud.circuitbreaker.sentinel.feign; import java.lang.reflect.Method; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.Set; import com.alibaba.cloud.circuitbreaker.sentinel.SentinelConfigBuilder; import com.alibaba.csp.sentinel.EntryType; @@ -27,11 +50,13 @@ import org.springframework.core.annotation.AnnotationUtils; */ public class CircuitBreakerRuleChangeListener implements ApplicationContextAware, ApplicationListener, SmartInitializingSingleton { - private static final Logger LOGGER = LoggerFactory.getLogger(CircuitBreakerRuleChangeListener.class); + private static final Logger LOGGER = LoggerFactory + .getLogger(CircuitBreakerRuleChangeListener.class); private SentinelFeignClientProperties properties; /** - * properties backup, prevent rules from being updated every time the container is refreshed + * properties backup, prevent rules from being updated every time the container is + * refreshed. */ private SentinelFeignClientProperties propertiesBackup; private AbstractCircuitBreakerFactory circuitBreakerFactory; @@ -58,7 +83,8 @@ public class CircuitBreakerRuleChangeListener implements ApplicationContextAware } @Override - public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + public void setApplicationContext(ApplicationContext applicationContext) + throws BeansException { this.applicationContext = applicationContext; } @@ -73,13 +99,16 @@ public class CircuitBreakerRuleChangeListener implements ApplicationContextAware // as it will cause the bean to be initialized prematurely, // and we don't want to change the initialization order of the beans if (circuitBreakerFactory == null) { - String[] names = applicationContext.getBeanNamesForType(AbstractCircuitBreakerFactory.class); + String[] names = applicationContext + .getBeanNamesForType(AbstractCircuitBreakerFactory.class); if (names.length >= 1) { - this.circuitBreakerFactory = applicationContext.getBean(names[0], AbstractCircuitBreakerFactory.class); + this.circuitBreakerFactory = applicationContext.getBean(names[0], + AbstractCircuitBreakerFactory.class); } } if (properties == null) { - this.properties = applicationContext.getBean(SentinelFeignClientProperties.class); + this.properties = applicationContext + .getBean(SentinelFeignClientProperties.class); } } @@ -105,7 +134,9 @@ public class CircuitBreakerRuleChangeListener implements ApplicationContextAware // first, clear all manually configured feign clients and methods. propertiesBackup.getRules().keySet().stream() .filter(key -> !Objects.equals(key, propertiesBackup.getDefaultRule())) - .forEach(resource -> Optional.ofNullable(DegradeRuleManager.getRulesOfResource(resource)).ifPresent(Set::clear)); + .forEach(resource -> Optional + .ofNullable(DegradeRuleManager.getRulesOfResource(resource)) + .ifPresent(Set::clear)); // Find all feign clients, clear the corresponding rules // NOTE: feign client name cannot be the same as the general resource name !!! @@ -116,7 +147,8 @@ public class CircuitBreakerRuleChangeListener implements ApplicationContextAware .map(beanName -> { try { return Class.forName(beanName); - } catch (ClassNotFoundException ignore) { + } + catch (ClassNotFoundException ignore) { // definitely not a feign client, just ignore return null; } @@ -128,7 +160,9 @@ public class CircuitBreakerRuleChangeListener implements ApplicationContextAware return; } String feignClientName = AnnotationUtils.getValue(anno).toString(); - Optional.ofNullable(DegradeRuleManager.getRulesOfResource(feignClientName)).ifPresent(Set::clear); + Optional.ofNullable( + DegradeRuleManager.getRulesOfResource(feignClientName)) + .ifPresent(Set::clear); }); } diff --git a/spring-cloud-alibaba-starters/spring-cloud-circuitbreaker-sentinel/src/main/java/com/alibaba/cloud/circuitbreaker/sentinel/feign/FeignClientCircuitNameResolver.java b/spring-cloud-alibaba-starters/spring-cloud-circuitbreaker-sentinel/src/main/java/com/alibaba/cloud/circuitbreaker/sentinel/feign/FeignClientCircuitNameResolver.java index 4d15dfbaf..368c42cc4 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-circuitbreaker-sentinel/src/main/java/com/alibaba/cloud/circuitbreaker/sentinel/feign/FeignClientCircuitNameResolver.java +++ b/spring-cloud-alibaba-starters/spring-cloud-circuitbreaker-sentinel/src/main/java/com/alibaba/cloud/circuitbreaker/sentinel/feign/FeignClientCircuitNameResolver.java @@ -1,3 +1,19 @@ +/* + * Copyright 2013-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * 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.alibaba.cloud.circuitbreaker.sentinel.feign; import java.lang.reflect.Method; @@ -9,7 +25,7 @@ import feign.Target; import org.springframework.cloud.client.circuitbreaker.AbstractCircuitBreakerFactory; import org.springframework.cloud.openfeign.CircuitBreakerNameResolver; -import static com.alibaba.cloud.circuitbreaker.sentinel.feign.CircuitBreakerRuleChangeListener.*; +import static com.alibaba.cloud.circuitbreaker.sentinel.feign.CircuitBreakerRuleChangeListener.getConfigurations; /** * Feign client circuit breaker name resolver. diff --git a/spring-cloud-alibaba-starters/spring-cloud-circuitbreaker-sentinel/src/main/java/com/alibaba/cloud/circuitbreaker/sentinel/feign/SentinelFeignClientAutoConfiguration.java b/spring-cloud-alibaba-starters/spring-cloud-circuitbreaker-sentinel/src/main/java/com/alibaba/cloud/circuitbreaker/sentinel/feign/SentinelFeignClientAutoConfiguration.java index 90264b24d..f2e2d5e74 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-circuitbreaker-sentinel/src/main/java/com/alibaba/cloud/circuitbreaker/sentinel/feign/SentinelFeignClientAutoConfiguration.java +++ b/spring-cloud-alibaba-starters/spring-cloud-circuitbreaker-sentinel/src/main/java/com/alibaba/cloud/circuitbreaker/sentinel/feign/SentinelFeignClientAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2019 the original author or authors. + * Copyright 2013-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -32,7 +32,6 @@ import org.springframework.cloud.client.circuitbreaker.AbstractCircuitBreakerFac import org.springframework.cloud.client.circuitbreaker.Customizer; import org.springframework.cloud.openfeign.CircuitBreakerNameResolver; import org.springframework.cloud.openfeign.FeignClientFactoryBean; -import org.springframework.cloud.util.ConditionalOnBootstrapEnabled; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -46,14 +45,12 @@ import static com.alibaba.cloud.circuitbreaker.sentinel.feign.CircuitBreakerRule */ @Configuration(proxyBeanMethods = false) @ConditionalOnClass({ Feign.class, FeignClientFactoryBean.class }) -@ConditionalOnProperty(name = "spring.cloud.circuitbreaker.sentinel.enabled", - havingValue = "true", matchIfMissing = true) +@ConditionalOnProperty(name = "spring.cloud.circuitbreaker.sentinel.enabled", havingValue = "true", matchIfMissing = true) @EnableConfigurationProperties(SentinelFeignClientProperties.class) public class SentinelFeignClientAutoConfiguration { @Configuration(proxyBeanMethods = false) - @ConditionalOnProperty(name = "feign.sentinel.refresh-rules", - havingValue = "true", matchIfMissing = true) + @ConditionalOnProperty(name = "feign.sentinel.refresh-rules", havingValue = "true", matchIfMissing = true) public static class CircuitBreakerListenerConfiguration { @Bean diff --git a/spring-cloud-alibaba-starters/spring-cloud-circuitbreaker-sentinel/src/main/java/com/alibaba/cloud/circuitbreaker/sentinel/feign/SentinelFeignClientProperties.java b/spring-cloud-alibaba-starters/spring-cloud-circuitbreaker-sentinel/src/main/java/com/alibaba/cloud/circuitbreaker/sentinel/feign/SentinelFeignClientProperties.java index cb93eb7db..532501303 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-circuitbreaker-sentinel/src/main/java/com/alibaba/cloud/circuitbreaker/sentinel/feign/SentinelFeignClientProperties.java +++ b/spring-cloud-alibaba-starters/spring-cloud-circuitbreaker-sentinel/src/main/java/com/alibaba/cloud/circuitbreaker/sentinel/feign/SentinelFeignClientProperties.java @@ -1,14 +1,31 @@ +/* + * Copyright 2013-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * 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.alibaba.cloud.circuitbreaker.sentinel.feign; + import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule; - import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; + import org.springframework.boot.context.properties.ConfigurationProperties; /** @@ -79,7 +96,8 @@ public class SentinelFeignClientProperties { ObjectMapper objectMapper = new ObjectMapper(); String json = objectMapper.writeValueAsString(this); return objectMapper.readValue(json, this.getClass()); - } catch (JsonProcessingException ignored) { + } + catch (JsonProcessingException ignored) { } return new SentinelFeignClientProperties(); } diff --git a/spring-cloud-alibaba-starters/spring-cloud-circuitbreaker-sentinel/src/test/java/com/alibaba/cloud/circuitbreaker/sentinel/feign/FeignClientCircuitBreakerRuleIntegrationTest.java b/spring-cloud-alibaba-starters/spring-cloud-circuitbreaker-sentinel/src/test/java/com/alibaba/cloud/circuitbreaker/sentinel/feign/FeignClientCircuitBreakerRuleIntegrationTest.java index 202426393..8f7da1220 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-circuitbreaker-sentinel/src/test/java/com/alibaba/cloud/circuitbreaker/sentinel/feign/FeignClientCircuitBreakerRuleIntegrationTest.java +++ b/spring-cloud-alibaba-starters/spring-cloud-circuitbreaker-sentinel/src/test/java/com/alibaba/cloud/circuitbreaker/sentinel/feign/FeignClientCircuitBreakerRuleIntegrationTest.java @@ -29,7 +29,7 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; -import static com.alibaba.cloud.circuitbreaker.sentinel.feign.FeignClientCircuitBreakerRuleIntegrationTest.*; +import static com.alibaba.cloud.circuitbreaker.sentinel.feign.FeignClientCircuitBreakerRuleIntegrationTest.Application; import static org.assertj.core.api.Assertions.assertThat; import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.DEFINED_PORT; diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/configdata/NacosConfigDataLoader.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/configdata/NacosConfigDataLoader.java index f3271210a..db8a105ba 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/configdata/NacosConfigDataLoader.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/configdata/NacosConfigDataLoader.java @@ -40,7 +40,8 @@ import static com.alibaba.cloud.nacos.configdata.NacosConfigDataResource.NacosIt /** * Implementation of {@link ConfigDataLoader}. * - *

Load {@link ConfigData} via {@link NacosConfigDataResource} + *

+ * Load {@link ConfigData} via {@link NacosConfigDataResource} * * @author freeman */ @@ -79,7 +80,7 @@ public class NacosConfigDataLoader implements ConfigDataLoader> pullConfig(ConfigService configService, - String group, String dataId, String suffix, long timeout) + private List> pullConfig(ConfigService configService, String group, + String dataId, String suffix, long timeout) throws NacosException, IOException { String config = configService.getConfig(dataId, group, timeout); return NacosDataParserHandler.getInstance().parseNacosData(dataId, config, diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/configdata/NacosConfigDataLocationResolver.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/configdata/NacosConfigDataLocationResolver.java index f2c1bef00..15ef3a402 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/configdata/NacosConfigDataLocationResolver.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/configdata/NacosConfigDataLocationResolver.java @@ -31,7 +31,13 @@ import org.apache.commons.logging.Log; import org.springframework.boot.BootstrapRegistry.InstanceSupplier; import org.springframework.boot.ConfigurableBootstrapContext; -import org.springframework.boot.context.config.*; +import org.springframework.boot.context.config.ConfigDataLocation; +import org.springframework.boot.context.config.ConfigDataLocationNotFoundException; +import org.springframework.boot.context.config.ConfigDataLocationResolver; +import org.springframework.boot.context.config.ConfigDataLocationResolverContext; +import org.springframework.boot.context.config.ConfigDataResource; +import org.springframework.boot.context.config.ConfigDataResourceNotFoundException; +import org.springframework.boot.context.config.Profiles; import org.springframework.boot.context.properties.bind.BindHandler; import org.springframework.boot.context.properties.bind.Bindable; import org.springframework.boot.context.properties.bind.Binder; @@ -40,7 +46,8 @@ import org.springframework.core.Ordered; import static com.alibaba.cloud.nacos.configdata.NacosConfigDataResource.NacosItemConfig; /** - * Implementation of {@link ConfigDataLocationResolver}, load Nacos {@link ConfigDataResource}. + * Implementation of {@link ConfigDataLocationResolver}, load Nacos + * {@link ConfigDataResource}. * * @author freeman */ @@ -55,9 +62,9 @@ public class NacosConfigDataLocationResolver // support params - public static final String GROUP = "group"; + private static final String GROUP = "group"; - public static final String REFRESH_ENABLED = "refreshEnabled"; + private static final String REFRESH_ENABLED = "refreshEnabled"; public NacosConfigDataLocationResolver(Log log) { this.log = log; @@ -177,7 +184,7 @@ public class NacosConfigDataLocationResolver } private void registerConfigManager(NacosConfigProperties properties, - ConfigurableBootstrapContext bootstrapContext) { + ConfigurableBootstrapContext bootstrapContext) { if (!bootstrapContext.isRegistered(NacosConfigManager.class)) { bootstrapContext.register(NacosConfigManager.class, InstanceSupplier.of(new NacosConfigManager(properties))); @@ -191,7 +198,8 @@ public class NacosConfigDataLocationResolver URI uri; try { uri = new URI(uris); - } catch (URISyntaxException e) { + } + catch (URISyntaxException e) { throw new IllegalArgumentException("illegal URI: " + uris); } return uri; @@ -199,9 +207,7 @@ public class NacosConfigDataLocationResolver private String groupFor(URI uri, NacosConfigProperties properties) { Map queryMap = getQueryMap(uri); - return queryMap.containsKey(GROUP) - ? queryMap.get(GROUP) - : properties.getGroup(); + return queryMap.containsKey(GROUP) ? queryMap.get(GROUP) : properties.getGroup(); } private Map getQueryMap(URI uri) { diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/test/java/com/alibaba/cloud/nacos/NacosConfigAutoConfigurationTest.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/test/java/com/alibaba/cloud/nacos/NacosConfigAutoConfigurationTest.java index e6e652161..59309e3d5 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/test/java/com/alibaba/cloud/nacos/NacosConfigAutoConfigurationTest.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/test/java/com/alibaba/cloud/nacos/NacosConfigAutoConfigurationTest.java @@ -1,3 +1,19 @@ +/* + * Copyright 2013-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * 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.alibaba.cloud.nacos; import org.junit.jupiter.api.Test; @@ -9,7 +25,7 @@ import static org.assertj.core.api.Assertions.assertThat; /** * NacosConfigAutoConfiguration Tester. - * + * * @author freeman */ public class NacosConfigAutoConfigurationTest { diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/test/java/com/alibaba/cloud/nacos/configdata/NacosConfigDataLocationResolverTest.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/test/java/com/alibaba/cloud/nacos/configdata/NacosConfigDataLocationResolverTest.java index 43749ba48..b149c3a2e 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/test/java/com/alibaba/cloud/nacos/configdata/NacosConfigDataLocationResolverTest.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/test/java/com/alibaba/cloud/nacos/configdata/NacosConfigDataLocationResolverTest.java @@ -1,3 +1,19 @@ +/* + * Copyright 2013-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * 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.alibaba.cloud.nacos.configdata; import java.util.Arrays; @@ -20,24 +36,29 @@ import org.springframework.mock.env.MockEnvironment; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; /** * NacosConfigDataLocationResolver Tester. - * + * * @author freeman */ public class NacosConfigDataLocationResolverTest { private NacosConfigDataLocationResolver resolver; - private ConfigDataLocationResolverContext context = mock(ConfigDataLocationResolverContext.class); + private ConfigDataLocationResolverContext context = mock( + ConfigDataLocationResolverContext.class); private MockEnvironment environment; private Binder environmentBinder; - private ConfigurableBootstrapContext bootstrapContext = mock(ConfigurableBootstrapContext.class); + private ConfigurableBootstrapContext bootstrapContext = mock( + ConfigurableBootstrapContext.class); @BeforeEach void setup() { @@ -95,7 +116,8 @@ public class NacosConfigDataLocationResolverTest { @Test void testDataIdMustBeSpecified() { String locationUri = "nacos:"; - assertThatThrownBy(() -> testUri(locationUri)).hasMessage("dataId must be specified"); + assertThatThrownBy(() -> testUri(locationUri)) + .hasMessage("dataId must be specified"); } @Test @@ -161,7 +183,8 @@ public class NacosConfigDataLocationResolverTest { environment.setProperty("spring.cloud.nacos.password", "root"); environment.setProperty("spring.cloud.nacos.config.password", "not_root"); environment.setProperty("spring.cloud.nacos.server-addr", "localhost:8888"); - environment.setProperty("spring.cloud.nacos.config.server-addr", "localhost:9999"); + environment.setProperty("spring.cloud.nacos.config.server-addr", + "localhost:9999"); String locationUri = "nacos:test.yml"; List resources = testUri(locationUri); @@ -172,11 +195,12 @@ public class NacosConfigDataLocationResolverTest { assertThat(resource.getProperties().getServerAddr()).isEqualTo("localhost:9999"); } - private List testUri(String locationUri, String... activeProfiles) { + private List testUri(String locationUri, + String... activeProfiles) { Profiles profiles = mock(Profiles.class); when(profiles.getActive()).thenReturn(Arrays.asList(activeProfiles)); - return this.resolver.resolveProfileSpecific( - context, ConfigDataLocation.of(locationUri), profiles); + return this.resolver.resolveProfileSpecific(context, + ConfigDataLocation.of(locationUri), profiles); } @Test @@ -186,8 +210,7 @@ public class NacosConfigDataLocationResolverTest { when(bootstrapContext.get(eq(NacosConfigProperties.class))) .thenReturn(new NacosConfigProperties()); List resources = this.resolver.resolveProfileSpecific( - context, ConfigDataLocation.of("nacos:test.yml"), - mock(Profiles.class)); + context, ConfigDataLocation.of("nacos:test.yml"), mock(Profiles.class)); assertThat(resources).hasSize(1); verify(bootstrapContext, times(0)).get(eq(NacosConfigProperties.class)); NacosConfigDataResource resource = resources.get(0);