Nacos Spring

This section provides a detailed description of the key features of nacos-spring-context:

  • Annotation-Driven
  • Dependency Injection
  • Externalized Configuration
  • Event-Driven

1. Annotation-Driven

1.1. Enable Nacos

@EnableNacos is a modular-driven annotation that enables all features of Nacos Spring, including Service Discovery and Distributed Configuration. It equals to @EnableNacosDiscovery and@EnableNacosConfig, which can be configured separately and used in different scenarios.

1.2. Configure Change Listener method

Suppose there was a config in Nacos Server whose dataId is "testDataId" and groupId is default group("DEFAULT_GROUP"). Now you would like to change its content by using the ConfigService#publishConfig method:

  1. @NacosInjected
  2. private ConfigService configService;
  3. @Test
  4. public void testPublishConfig() throws NacosException {
  5. configService.publishConfig(DATA_ID, DEFAULT_GROUP, "9527");
  6. }

Then you would like to add a listener, which will be listening for the config changes. You can do this by adding a config change listener method into your Spring Beans:

  1. @NacosConfigListener(dataId = DATA_ID)
  2. public void onMessage(String config) {
  3. assertEquals("mercyblitz", config); // asserts true
  4. }

The code below has the same effect:

  1. configService.addListener(DATA_ID, DEFAULT_GROUP, new AbstractListener() {
  2. @Override
  3. public void receiveConfigInfo(String config) {
  4. assertEquals("9527", config); // asserts true
  5. }
  6. });

Note: @NacosConfigListener supports richer type conversions.

1.2.1. Type Conversion

The type conversion of @NacosConfigListener includes both build-in and customized implementations. By default, build-in type conversion is based on Spring DefaultFormattingConversionService, which means it covers most of the general cases as well as the rich features of the higher Spring framework.

For example, the content "9527" in the preceding example can also be listened by a method with integer or the Integer argument:

  1. @NacosConfigListener(dataId = DATA_ID)
  2. public void onInteger(Integer value) {
  3. assertEquals(Integer.valueOf(9527), value); // asserts true
  4. }
  5. @NacosConfigListener(dataId = DATA_ID)
  6. public void onInt(int value) {
  7. assertEquals(9527, value); // asserts true
  8. }

Of course, nacos-spring-context provides elastic extension for developers. If you define a named nacosConfigConversionService Spring Bean whose type is ConversionService , the DefaultFormattingConversionService will be ignored. In addition, you can customize the implementation of the NacosConfigConverter interface to specify a listener method for type conversion:

  1. public class UserNacosConfigConverter implements NacosConfigConverter<User> {
  2. @Override
  3. public boolean canConvert(Class<User> targetType) {
  4. return true;
  5. }
  6. @Override
  7. public User convert(String source) {
  8. return JSON.parseObject(source, User.class);
  9. }
  10. }

The UserNacosConfigConverter class binds the @NacosConfigListener.converter() attribute:

  1. @NacosInjected
  2. private ConfigService configService;
  3. @Test
  4. public void testPublishUser() throws NacosException {
  5. configService.publishConfig("user", DEFAULT_GROUP, "{\"id\":1,\"name\":\"mercyblitz\"}");
  6. }
  7. @NacosConfigListener(dataId = "user", converter = UserNacosConfigConverter.class)
  8. public void onUser(User user) {
  9. assertEquals(Long.valueOf(1L), user.getId());
  10. assertEquals("mercyblitz", user.getName());
  11. }

1.2.2. Timeout of Execution

As it might cost some time to run customized NacosConfigConverter, you can set max execution time in the @NacosConfigListener.timeout() attribute to prevent it from blocking other listeners:

  1. @Configuration
  2. public class Listeners {
  3. private Integer integerValue;
  4. private Double doubleValue;
  5. @NacosConfigListener(dataId = DATA_ID, timeout = 50)
  6. public void onInteger(Integer value) throws Exception {
  7. Thread.sleep(100); // timeout of execution
  8. this.integerValue = value;
  9. }
  10. @NacosConfigListener(dataId = DATA_ID, timeout = 200)
  11. public void onDouble(Double value) throws Exception {
  12. Thread.sleep(100); // normal execution
  13. this.doubleValue = value;
  14. }
  15. public Integer getIntegerValue() {
  16. return integerValue;
  17. }
  18. public Double getDoubleValue() {
  19. return doubleValue;
  20. }
  21. }

The integerValue of Listeners Bean is always null and will not be changed. Therefore, those asserts will be true:

  1. @Autowired
  2. private Listeners listeners;
  3. @Test
  4. public void testPublishConfig() throws NacosException {
  5. configService.publishConfig(DATA_ID, DEFAULT_GROUP, "9527");
  6. assertNull(listeners.getIntegerValue()); // asserts true
  7. assertEquals(Double.valueOf(9527), listeners.getDoubleValue()); // asserts true
  8. }

1.3. Global and Special Nacos Properties

The globalProperties is a required attribute in any @EnableNacos, @EnableNacosDiscovery or @EnableNacosConfig, and its type is @NacosProperties. globalProperties initializes "Global Nacos Properties" that will be used by other annotationsand components, e,g @NacosInjected. In other words, Global Nacos Properties" defines the global and default properties. It is set with the lowest priority and can be overridden if needed. The precedence of overiding rules is shown in the following table:

Precedence OrderNacos AnnotationRequired
1*.properties()N
2@EnableNacosConfig.globalProperties() or @EnableNacosDiscovery.globalProperties()Y
3@EnableNacos.globalProperties()Y

*.properties() defines special Nacos properties which come from one of the following:

  • @NacosInjected.properties()
  • @NacosConfigListener.properties()
  • @NacosPropertySource.properties()
  • @NacosConfigurationProperties.properties()
Special Nacos properties are also configured by @NacosProperties. However, they are optional and are used to override Global Nacos Properties in special scenarios. If not defined, the Nacos Properties willtry to retrieve properities from @EnableNacosConfig.globalProperties() or @EnableNacosDiscovery.globalProperties(), or@EnableNacos.globalProperties(). ### 1.4. @NacosProperties @NacosProperties is a uniform annotation for global and special Nacos properties. It serves as a mediator between Java Properties and NacosFactory class. NacosFactory is responsible for creating ConfigService or NamingService instances. The attributes of @NacosProperties completely support placeholders whose source is all kinds of PropertySource in Spring Environment abstraction, typically Java System Properties and OS environment variables. The prefix of all placeholders are nacos.. The mapping between the attributes of @NacosProperties and Nacos properties are shown below:
AttributePropertyPlaceholderDescriptionRequired
endpoint()endpoint${nacos.endpoint:}N
namespace()namespace${nacos.namespace:}N
accessKey()access-key${nacos.access-key:}N
secretKey()secret-key${nacos.secret-key:}N
serverAddr()server-addr${nacos.server-addr:}Y
contextPath()context-path${nacos.context-path:}N
clusterName()cluster-name${nacos.cluster-name:}N
encode()encode${nacos.encode:UTF-8}N
Note that there are some differences in the placeholders of globalProperties() between @EnableNacosDiscovery and @EnableNacosConfig:
Attribute@EnableNacosDiscovery's Placeholder@EnableNacosConfig's Placeholder
endpoint()${nacos.discovery.endpoint:${nacos.endpoint:}}${nacos.config.endpoint:${nacos.endpoint:}}
namespace()${nacos.discovery.namespace:${nacos.namespace:}}${nacos.config.namespace:${nacos.namespace:}}
accessKey()${nacos.discovery.access-key:${nacos.access-key:}}${nacos.config.access-key:${nacos.access-key:}}
secretKey()${nacos.discovery.secret-key:${nacos.secret-key:}}${nacos.config.secret-key:${nacos.secret-key:}}
serverAddr()${nacos.discovery.server-addr:${nacos.server-addr:}}${nacos.config.server-addr:${nacos.server-addr:}}
contextPath()${nacos.discovery.context-path:${nacos.context-path:}}${nacos.config.context-path:${nacos.context-path:}}
clusterName()${nacos.discovery.cluster-name:${nacos.cluster-name:}}${nacos.config.cluster-name:${nacos.cluster-name:}}
encode()${nacos.discovery.encode:${nacos.encode:UTF-8}}${nacos.config.encode:${nacos.encode:UTF-8}}
These placeholders of @EnableNacosDiscovery and @EnableNacosConfig are designed to isolate different Nacos servers, and are unnecessary in most scenarios. By default, general placeholders will be reused. ## 2. Dependency Injection @NacosInjected is a core annotation which is used to inject ConfigService or NamingService instance in your Spring Beans and make these instances cacheable. This means the instances will be the same if their @NacosProperties are equal, regargless of whether the properties come from global or special Nacos properties:
  1. @NacosInjected
  2. private ConfigService configService;
  3. @NacosInjected(properties = @NacosProperties(encode = "UTF-8"))
  4. private ConfigService configService2;
  5. @NacosInjected(properties = @NacosProperties(encode = "GBK"))
  6. private ConfigService configService3;
  7. @NacosInjected
  8. private NamingService namingService;
  9. @NacosInjected(properties = @NacosProperties(encode = "UTF-8"))
  10. private NamingService namingService2;
  11. @NacosInjected(properties = @NacosProperties(encode = "GBK"))
  12. private NamingService namingService3;
  13. @Test
  14. public void testInjection() {
  15. Assert.assertEquals(configService, configService2);
  16. Assert.assertNotEquals(configService2, configService3);
  17. Assert.assertEquals(namingService, namingService2);
  18. Assert.assertNotEquals(namingService2, namingService3);
  19. }
The property configService uses @EnableNacos#globalProperties() or @EnableNacosConfig#globalProperties(), and because the default value of the encode attribute is "UTF-8", therefore the configService instance and the configService2 instance which is annotated by @NacosProperties(encode = "UTF-8") are the same. The same is true for namingService and namingService2. More importantly, unlike the ConfigService instances created by the NacosFactory.createConfigService() method, the ConfigService instances created by the @NacosInjected annotation support Nacos Spring events. For instance, there will be an NacosConfigPublishedEvent after an enhanced ConfigService invokes the publishConfig() method. Refer to the Event/Listener Driven section for more details.

3. Externalized Configuration

Externalized configuration is a concept introduced by Spring Boot, which allows applications to receive external property sources to control runtime behavior. Nacos Server runs an isolation process outside the application to maintain the application configurations. nacos-spring-context provides properties features including object binding, dynamic configuration(auto-refreshed) and so on, and dependence on Spring Boot or Spring Cloud framework is required.

Here is a simple comparison between nacos-spring-context and Spring stack:

Spring StackNacos SpringHighlight
@Value@NacosValueauto-refreshed
@ConfigurationProperties@NacosConfigurationPropertiesauto-refreshed,@NacosProperty,@NacosIgnore
@PropertySource@NacosPropertySourceauto-refreshed, precedence order control
@PropertySources@NacosPropertySources

4. Event/Listener Driven

Nacos Event/Listener Driven is based on the standard Spring Event/Listener mechanism. The ApplicationEvent of Spring is an abstract super class for all Nacos Spring events:

Nacos Spring EventTrigger
NacosConfigPublishedEventAfter ConfigService.publishConfig()
NacosConfigReceivedEventAfterListener.receiveConfigInfo()
NacosConfigRemovedEventAfter configService.removeConfig()
NacosConfigTimeoutEventConfigService.getConfig() on timeout
NacosConfigListenerRegisteredEventAfter ConfigService.addListner() or ConfigService.removeListener()
NacosConfigurationPropertiesBeanBoundEventAfter @NacosConfigurationProperties binding
NacosConfigMetadataEventAfter Nacos Config operations

Related Projects