nacos-group / nacos-spring-project Goto Github PK
View Code? Open in Web Editor NEWNacos ECO Project for Spring Framework
Home Page: https://nacos.io/
License: Apache License 2.0
Nacos ECO Project for Spring Framework
Home Page: https://nacos.io/
License: Apache License 2.0
@NacosService
Programming Model is like below :
public class ConfigServiceTest {
@NacosService(serverAddr = "${serverAddr}")
private ConfigService configService;
@Test
public void testConfigService() throws NacosException {
String dataId = "testDataId";
String group = "testGroupId";
Properties properties = new Properties();
configService.publishConfig(dataId, group, "Hello,World");
// Actively get the configuration.
String content = configService.getConfig(dataId, group, 5000);
System.out.println(content);
}
}
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.Properties;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {CacheableEventPublishingNacosServiceFactory.class})
public class CacheableNacosServiceFactoryTest {
@Autowired
private NacosServiceFactory nacosServiceFactory;
private Properties properties = new Properties();
private Properties properties2 = new Properties();
@Before
public void init() {
nacosServiceFactory = new CacheableEventPublishingNacosServiceFactory();
properties.setProperty(PropertyKeyConst.NAMESPACE, "nc");
properties.setProperty(PropertyKeyConst.SERVER_ADDR, "127.0.0.1");
properties.setProperty(PropertyKeyConst.ACCESS_KEY, "a");
properties.setProperty(PropertyKeyConst.SECRET_KEY, "s");
properties2.setProperty(PropertyKeyConst.CLUSTER_NAME, "nc");
properties2.setProperty(PropertyKeyConst.SERVER_ADDR, "127.0.0.1");
properties2.setProperty(PropertyKeyConst.ACCESS_KEY, "a");
properties2.setProperty(PropertyKeyConst.SECRET_KEY, "s");
}
@Test
public void testCreateConfigService() throws NacosException {
ConfigService configService = nacosServiceFactory.createConfigService(properties);
ConfigService configService2 = nacosServiceFactory.createConfigService(properties2);
// throws java.lang.AssertionError
Assert.assertTrue(configService != configService2);
}
@Test
public void testCreateNamingService() throws NacosException {
NamingService namingService = nacosServiceFactory.createNamingService(properties);
NamingService namingService2 = nacosServiceFactory.createNamingService(properties2);
// throws java.lang.AssertionError
Assert.assertTrue(namingService != namingService2);
}
}
@NacosConfigurationProperties
is better to understand from Spring Boot's developers, since its name is like @ConfigurationProperties
for binding.
<!-- Compiler settings properties -->
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
java.lang.IllegalStateException: Could not register object [java.util.concurrent.ThreadPoolExecutor@e383572[Running, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0]] under bean name 'nacosConfigListenerExecutor': there is already object [java.util.concurrent.ThreadPoolExecutor@5ddf0d24[Running, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0]] bound
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.registerSingleton(DefaultSingletonBeanRegistry.java:120) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.registerSingleton(DefaultListableBeanFactory.java:932) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at com.alibaba.nacos.spring.util.NacosBeanUtils.registerSingleton(NacosBeanUtils.java:115) ~[nacos-spring-context-0.1.0-SNAPSHOT.jar:na]
at com.alibaba.nacos.spring.util.NacosBeanUtils.registerNacosConfigListenerExecutor(NacosBeanUtils.java:269) ~[nacos-spring-context-0.1.0-SNAPSHOT.jar:na]
at com.alibaba.nacos.spring.util.NacosBeanUtils.registerNacosConfigBeans(NacosBeanUtils.java:327) ~[nacos-spring-context-0.1.0-SNAPSHOT.jar:na]
at com.alibaba.nacos.spring.context.annotation.NacosConfigBeanDefinitionRegistrar.registerBeanDefinitions(NacosConfigBeanDefinitionRegistrar.java:53) ~[nacos-spring-context-0.1.0-SNAPSHOT.jar:na]
at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.lambda$loadBeanDefinitionsFromRegistrars$1(ConfigurationClassBeanDefinitionReader.java:358) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at java.util.LinkedHashMap.forEach(LinkedHashMap.java:684) ~[na:1.8.0_141]
at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsFromRegistrars(ConfigurationClassBeanDefinitionReader.java:357) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForConfigurationClass(ConfigurationClassBeanDefinitionReader.java:145) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitions(ConfigurationClassBeanDefinitionReader.java:117) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:328) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:233) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:273) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:93) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:694) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:532) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:395) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
at com.alibaba.boot.nacos.sample.ConfigApplication.main(ConfigApplication.java:65) [classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_141]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_141]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_141]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_141]
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) [idea_rt.jar:na]
public static void main(String[] args) {
new ClassPathXmlApplicationContext("classpath:/test.xml");
}
test.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:nacos="http://nacos.io/schema/nacos"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://nacos.io/schema/nacos
http://nacos.io/schema/nacos.xsd">
<nacos:annotation-driven/>
<nacos:global-properties server-addr="127.0.0.1" />
<nacos:property-source data-id="user" auto-refreshed="true" />
</beans>
The following exception is thrown at runtime
Caused by: java.lang.ClassCastException: org.springframework.beans.factory.support.DefaultListableBeanFactory cannot be cast to org.springframework.context.ApplicationEventPublisher
at com.alibaba.nacos.spring.context.annotation.NacosPropertySourceBeanDefinitionParser.addPropertySourceAttribute(NacosPropertySourceBeanDefinitionParser.java:94)
at com.alibaba.nacos.spring.context.annotation.NacosPropertySourceBeanDefinitionParser.parse(NacosPropertySourceBeanDefinitionParser.java:76)
at org.springframework.beans.factory.xml.NamespaceHandlerSupport.parse(NamespaceHandlerSupport.java:73)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1432)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1422)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:187)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:147)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:101)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:495)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:391)
@EnableNacosConfiguration
@EnableNacosNaming / @EnableNacosDiscoveryClient
@NacosListener
custom method with annotation @NacosConfigListener may block Nacos notify thread, support specific an other to execute custom method, or set a max time for method invocation.
When the dataId
and group
parameters are the same, the content returned by getConfig
is inconsistent with the content published by publishConfig
.
EnableNacosConfig: nacos.config.*
EnableNacosDiscovery: nacos.discovery.*
Invoke getServerStatus
method of ConfigService and NamingService in HealthIndicator
As title
com.alibaba.nacos.spring.config.NacosNamespaceHandlerTest
com.alibaba.nacos.spring.context.config.xml.NacosPropertySourceBeanDefinitionParserTest
By default, once loaded, the configured ApplicationContext is reused for each test. Thus the setup cost is incurred only once per test suite, and subsequent test execution is much faster. In this context, the term test suite means all tests run in the same JVM — for example, all tests run from an Ant, Maven, or Gradle build for a given project or module. In the unlikely case that a test corrupts the application context and requires reloading — for example, by modifying a bean definition or the state of an application object — the TestContext framework can be configured to reload the configuration and rebuild the application context before executing the next test.
properties like server-addr, endpoint, namespace that define in @NacosProperties.
Before
@Target(ElementType.ANNOTATION_TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface NacosProperties {
String endpoint() default "${nacos.endpoint:}";
String namespace() default "${nacos.namespace:}";
String accessKey() default "${nacos.accessKey:}";
String secretKey() default "${nacos.secretKey:}";
String serverAddr() default "${nacos.serverAddr:}";
String contextPath() default "${nacos.contextPath:}";
String clusterName() default "${nacos.clusterName:}";
String encode() default "${nacos.encode:" + ENCODE + "}";
}
After
@Target(ElementType.ANNOTATION_TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface NacosProperties {
String endpoint() default "${nacos.endpoint:}";
String namespace() default "${nacos.namespace:}";
String accessKey() default "${nacos.access-key:}";
String secretKey() default "${nacos.secret-key:}";
String serverAddr() default "${nacos.server-addr:}";
String contextPath() default "${nacos.context-path:}";
String clusterName() default "${nacos.cluster-name:}";
String encode() default "${nacos.encode:" + ENCODE + "}";
}
// CacheableEventPublishingNacosServiceFactory.createNamingService
Properties copy = new Properties();
copy.putAll(properties);
// Java 8 @see org.springframework.context.annotation.PropertySource
@Repeatable(xxx.class)
NacosUtils.identify
@NacosConfigurationProperties
supports binding config to Bean.
@NacosConfigurationProperties
supports auto-refreshed Bean If config is changed.
AnnotationInjectedBeanPostProcessor
is come from
<dependency>
<groupId>com.alibaba.spring</groupId>
<artifactId>spring-context-support</artifactId>
<version>1.0.1</version>
</dependency>
NamingServiceInjectedBeanPostProcessor
will re-use this one.
// @see org.springframework.boot.env.PropertySourceLoader
public interface PropertySourceLoader {
String[] getFileExtensions();
List<PropertySource<?>> load(String name, Resource resource) throws IOException;
}
The annotations contains NacosConfigurationProperties,NacosConfigListener,NacosPropertySource.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.