Fixed checkstyle.

pull/2349/head
Freeman Lau 3 years ago
parent 6f3a5d892a
commit 61c2c31cf7

@ -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;

@ -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);
}
}

@ -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<String, Object> map;
private List<User> 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<String, Object> getMap() {
return map;
}
public void setMap(Map<String, Object> map) {
this.map = map;
}
public List<User> getUsers() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
private String name;
private Integer age;
private Map<String, Object> map;
private List<User> 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<String, Object> getMap() {
return map;
}
public void setMap(Map<String, Object> map) {
this.map = map;
}
public List<User> getUsers() {
return users;
}
public void setUsers(List<User> 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;
}
}
}

@ -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");
}
}

@ -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);
}
}

@ -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);
}

@ -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";
}
}

@ -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);
}

@ -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";
}
}

@ -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<RefreshScopeRefreshedEvent>, 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);
});
}

@ -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.

@ -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

@ -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();
}

@ -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;

@ -40,7 +40,8 @@ import static com.alibaba.cloud.nacos.configdata.NacosConfigDataResource.NacosIt
/**
* Implementation of {@link ConfigDataLoader}.
*
* <p> Load {@link ConfigData} via {@link NacosConfigDataResource}
* <p>
* Load {@link ConfigData} via {@link NacosConfigDataResource}
*
* @author freeman
*/
@ -79,7 +80,7 @@ public class NacosConfigDataLoader implements ConfigDataLoader<NacosConfigDataRe
NacosPropertySourceRepository.collectNacosPropertySource(propertySource);
// TODO Currently based on 2.4.2,
// compatibility needs to be done when upgrading to boot version 2.4.5
// compatibility needs to be done when upgrading to boot version 2.4.5
return new ConfigData(propertySources);
}
catch (Exception e) {
@ -93,8 +94,8 @@ public class NacosConfigDataLoader implements ConfigDataLoader<NacosConfigDataRe
return null;
}
private List<PropertySource<?>> pullConfig(ConfigService configService,
String group, String dataId, String suffix, long timeout)
private List<PropertySource<?>> 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,

@ -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<String, String> queryMap = getQueryMap(uri);
return queryMap.containsKey(GROUP)
? queryMap.get(GROUP)
: properties.getGroup();
return queryMap.containsKey(GROUP) ? queryMap.get(GROUP) : properties.getGroup();
}
private Map<String, String> getQueryMap(URI uri) {

@ -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 {

@ -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<NacosConfigDataResource> resources = testUri(locationUri);
@ -172,11 +195,12 @@ public class NacosConfigDataLocationResolverTest {
assertThat(resource.getProperties().getServerAddr()).isEqualTo("localhost:9999");
}
private List<NacosConfigDataResource> testUri(String locationUri, String... activeProfiles) {
private List<NacosConfigDataResource> 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<NacosConfigDataResource> 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);

Loading…
Cancel
Save