preface

The SpringBoot project uses NACOS as the configuration center and service registry, while being compatible with Dubbo’s registry. The SpringBoot version used in this Demo project is 2.3.9.RELEASE.

In the project, the integration method of SpringBoot was not used in the official way provided by NACOS, but the dependency package of Alibaba Spring Cloud was used for integration. The reason is that in the SpringBoot integration mode provided by the official website, the configuration center and the service discovery function are used at the same time. Therefore, some attribute conflicts of the service discovery function do not take effect. The most straightforward is that the configuration center and service discovery function cannot have two different namespaces. The namespace configured in the configuration center is used as the namespace for service discovery by default.

The other thing is that it is very compatible with Spring annotations, and no additional annotations are required. For example, the @nacosValue annotation.

Maven rely on

<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> < version > 2.2.5. RELEASE < / version > < / dependency > < the dependency > < groupId > com. Alibaba. Cloud < / groupId > < artifactId > spring - the cloud - starter - alibaba - nacos - config - server < / artifactId > < version > 2.2.5. RELEASE < / version > < / dependency >  <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> The < version > 2.7.9 < / version > < / dependency >Copy the code

Nacos configuration center

The configuration file

Create the bootstrap.properties file and configure the configuration information of nacOS in the configuration file.

# = = = = = = = = = = = = = = nacos - config = = = = = = = = = = = = = = # spring. Cloud. Nacos. Config. The server - addr = 127.0.0.1:8848 spring.cloud.nacos.config.username=nacos spring.cloud.nacos.config.password=nacos spring.cloud.nacos.config.namespace=config-dev spring.cloud.nacos.config.group=demeterCopy the code

Note: Due to the loading mechanism of Spring Cloud Alibaba nacos config, the configuration information written in the application-{env}.properties configuration file cannot be parsed. Therefore, if you need to dynamically load multiple environments, you can write it in the bootstrap-{env}. Properties file. Configuration at startup timeactive profilesThe + environment will read the configuration correctly

Dynamic configuration
The new configuration

Create a new configuration in the namespace named config-devdemeter-application-admin.

There is no need to talk about data-id in the configuration, this can be viewed in the data, there are many resources on the web to describe it in detail. Data-id is as follows:

  1. demeter-application-admin
  2. demeter-application-admin.properties
  3. demeter-application-admin-dev.properties
Configuring entity Classes

DemeterAdminProperties.class

@Data
@Accessors(chain = true)
@ConfigurationProperties(prefix = "demeter.admin")
public class DemeterAdminProperties implements Serializable {
    private static final long serialVersionUID = 1563821704172888362L;
    private String name;
    private String description;
    private String time;
}
Copy the code

The configuration takes effect. @ EnableConfigurationProperties annotations make DemeterAdminProperties can be instantiated, automatically injected into other components.

@Configuration @ComponentScan(value = { "org.demeter.application.admin", }) @Import(value = {AutoRedisConfiguration.class}) @EnableConfigurationProperties({DemeterAdminProperties.class}) public  class AdminWebConfig { }Copy the code
Access to the results

Controller The results of

Nacos service discovery

The configuration file
# = = = = = = = = = = = = = = nacos - discovery = = = = = = = = = = = = = = # spring. Cloud. Nacos. Discovery. The server - addr = 127.0.0.1:8848 spring.cloud.nacos.discovery.username=nacos spring.cloud.nacos.discovery.password=nacos spring.cloud.nacos.discovery.namespace=discovery-dev spring.cloud.nacos.discovery.group=demeterCopy the code
Make the configuration take effect

use@EnableDiscoveryClientAnnotations enable configuration files to take effect.

The results of