Spring Cloud Alibaba uses the NACOS registry

background

In the first nacOS introduction, we mentioned that NACOS is both a registry and a configuration center. In the last article, we introduced the use of the NacOS registry by Spring Cloud Alibaba. In this article, we discuss the use of the configuration center.

Nacos configuration center

The previous section talked about configuration files. This time we just need to add the maven NacOS configuration center dependency in the previous code. This is to configure the poM version control file into the Spring Cloud Alibaba tutorial. Again using Maven’s own JAR dependencies. Discovery-server and Cloud-Discovery-client-common modules automatically introduce nacOS configuration center dependencies

<! -- nacos config --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency>Copy the code

Modify the corresponding YML file added

Yaml configuration file on the server

Spring: cloud: config: server-addr: 47.99.209.72:8848 File-extension: YAMLCopy the code

Finally, the results are presented separately

server: port: 9012 spring: profiles: active: dev application: name: cloud-discovery-server cloud: nacos: config: Yaml discovery: server-addr: 47.99.209.72:8848Copy the code

Modifying an HTTP Interface

package com.xian.cloud.controller; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; /** * @Author: xlr * @Date: Created in 2:57 PM 2019/10/27 */ @restController @requestMapping ("server") @slf4j # Provides distributed configuration dynamic RefreshScope public class DiscoverCotroller { @Value( "${nacos.yaml.age}" ) private String age; /** * HTTP interface * @param name * @return */ @getMapping ("/hello") public String hello(@requestParam String name) { log.info("invoked name = " + name+ " age = " + age); return "hello " + name + " age = " + age; }}Copy the code

Then create a profile in the configuration center by clicking log in to the NacOS Configuration Center

Create cloud-discovery-server-dev.yaml configuration nacos.yaml. Age = 30

Start the service access curl http://localhost:9012/server/hello? name=tom

Log print

Cloud-discovery-server-dev. yaml config file age is 20

Background logs are displayed

[.99.209.72_8848] trationDelegate$BeanPostProcessorChecker: Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration?EnhancerBySpringCGLIB?87d25f89 ] is not eligible for getting processed by all BeanPostProcessors (for example: Uneligible for auto-proxying) 2019-10-27 19:53:09.091 INFO 44618 -- [.99.209.72_8848] c.a.c.n.c.NacosPropertySourceBuilder : Loading nacos data, dataId: 'cloud-discovery-server-dev.yaml', group: 'DEFAULT_GROUP 19:53:09 2019-10-27. 44618-092 the INFO [. 99.209.72 _8848] B.C.P ropertySourceBootstrapConfiguration: Located property source: CompositePropertySource {name='NACOS', propertySources=[NacosPropertySource {name='cloud-discovery-server-dev.yaml'}, NacosPropertySource {name='cloud-discovery-server.yaml'}]} 2019-10-27 19:53:09.093 INFO 44618 -- [.99.209.72_8848] o.s.boot.SpringApplication : The following profiles are active: Dev 19:53:09 2019-10-27. 44618-103 the INFO [. 99.209.72 _8848] O.S.B oot. SpringApplication: Started Application in 0.277 seconds (JVM running for 883.2) 2019-10-27 19:53:09.119 INFO 44618 -- [.99.209.72_8848] o.s.c.e.event.RefreshEventListener : Refresh keys changed: [nacos.yaml.age]Copy the code

Request again without restarting the service. age Has sent changes

This is the configuration center of NACOS.

Then we walk through the matching rules for the NACOS configuration

dataID

In Nacos Config Starter, the dataId (cloud-discovery-server-dev.yaml above) is concatenated in the following format

${prefix} – ${spring.profiles.active}. ${file-extension}prefix defaults to the value of spring.application.name, But can be by spring configuration items. Cloud. Nacos. Config. The prefix to configuration.

Spring.profiles. active is the profile corresponding to the current environment. For details, refer to the Spring Boot documentation

${prefix}.${file-extension} = ${prefix}.${file-extension}

File – the extension for the configuration of content data format, can be configured a spring. The cloud. Nacos. Config. The file – the extension to the configuration. Currently, only properties is supported.

group

Group by default DEFAULT_GROUP, by spring. Cloud. Nacos. Config. The group configuration.

Automatic injection

Nacos Config Starter realizes org. Springframework. Cloud. The bootstrap. Config. PropertySourceLocator interface, and set the priority is the highest.

In the startup stage of Spring Cloud application, it will take the initiative to obtain the corresponding data from Nacos Server, convert the obtained data into PropertySource and inject it into the PropertySources property of Environment. So using the @value annotation you can also get the Nacos Server configuration directly.

Dynamic refresh

The Nacos Config Starter adds listening by default for all Nacos configuration items that successfully get data. When listening to the server configuration change real-time trigger org. Springframework. Cloud. Context. Refresh. ContextRefresher refresh method.

If you need to refresh beans dynamically, refer to the Spring and Spring Cloud specifications. It is recommended to add an @refreshScope or @ConfigurationProperties annotation to your class,

That’s it for the NACOS Configuration Center.

Supplementary namespace

Both the nacOS registry and the NACOS configuration center have a namespace attribute. This property is specific to our NACOS console namespace.

Specifically, the concept of namespaces because configuration centers and registries are generic concepts. So put it in the configuration center here.

We create a namespace for the LMS on the console

The namespace ID e071C3AB-B280-4AE7-A081-044FFF5613ad will be added to the configuration file. If the namespace attribute is not modified, the default namespace is public

server: port: 9013 spring: profiles: active: dev application: name: cloud-discovery-server cloud: nacos: config: Server-addr: 47.99.209.72:8848 File-Extension: YAMl Namespace: e071C3AB-b280-4AE7-A081-044FFF5613AD Discovery: Server - addr: 47.99.209.72:8848 namespace: e071c3ab - fff5613ad ae7 - a081 b280-4-044Copy the code

Restart and you will find that the service is registered in the LMS namespace.

reflection

DataID and group are used together. ${prefix} – ${spring.profiles.active}. ${file-extension}. The change of the active parameter can help us achieve the isolation of the environment. Group changes. And can help us to achieve the project group, version of the distinction. So the configuration doesn’t mess with the two parameters to achieve the dynamic differentiation we want. In fact, this part of the design meets the needs and support of most of our scenarios

Add the concept of namespaces. We have a variety of circumstances and options. However, so many, when our cluster environment is large enough, but will make many students can not find the clue. This requires us to make an appointment in advance. Use of rules

How can you like to share this public number.

Copyright notice: This article is originally published BY the blogger. It follows the COPYRIGHT agreement CC 4.0 BY-SA. Please attach the link of the original source and this statement. Please attach the qr code of the public account