当前位置:网站首页>Nacos practice record

Nacos practice record

2022-06-25 03:30:00 Hurry to Friday

The dependent versions are as follows :

<dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
            <version>2021.0.1.0</version>
</dependency>

     <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2021.0.1.0</version>
            <exclusions>
                <exclusion>
                    <groupId>springframework.cloud</groupId>
                    <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

Configuration Management

The configuration file

Use nacos Configuration Management , Need to create a new one bootstrap.properties(yaml) The configuration file , If @Value Still can't get the value , It may be that the project did not recognize bootstrap.properties file , Plus dependence :

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
            <version>3.1.0</version>
        </dependency>

Value method

@Value(“${minutes:61}”) The value after the colon can be used as the default value , That is, it will be used when the configuration value cannot be obtained . I use @NacosValue You can't get the value all the time , But with @Value Can , I don't know if it's a version problem .

When configuring parameters , Do not set parameter properties to static , And then in set Method plus @Value annotation , Although it is possible to get the value , But there will be problems when the configuration value is refreshed . Just adopt @Value Annotations on attributes , By using get Method to obtain the configuration value .

stay bootstrap.properties in , If not specified spring.application.name, That's the correspondence ${prefix}( The default is spring.application.name Value ); I thought I was applicaiton.properties Specified in spring.application.name, And I did recognize it when I started it , That's in bootstrap.properties It should be possible not to write in , But the configuration value can be obtained after startup , But when the configuration value is refreshed , The background will not receive the configuration , So I can't help writing .

The phenomenon is as follows , I have modified the values of two parameters in the console , But I didn't pull it :

 Insert picture description here

If the namespace is used public Can be used without writing , Or write public, But if you are using a new namespace , The random string generated after creation must be used in the configuration file , Otherwise I can't recognize .

Configure dynamic refresh

Add... To the top of the two printing methods respectively @PostConstruct @PreDestrtoy annotation .

@PostConstruct Is executed after the constructor ;
@PreDestrtoy It is in Bean Before destruction

 Insert picture description here

At the start of the project , You can see the execution init Method , This is the time day The value of is day2217,week The value of is week2217;

Then change the value on the console ; At this time, information will be printed out in the background :

 Insert picture description here

You can see the call destory Method , But the value printed out at this time is still the same as before , You can also know which configuration values have been modified ;

Then call the method to access the configuration value on the page , The background will continue to print information :

 Insert picture description here

There was a call init Method , At this time, the printed value is the modified value .

In the use of @RefreshScope After the annotation listens to the configuration changes , The object will be destroyed first , Then initialize the object when you want to use the configuration attribute .

That is, when the configuration changes, it will call the methods of the object's life cycle .

Add a piece of code to the project :

    @Bean
    public ApplicationRunner runner() {
    
        return args -> {
    
            String dataId = "one-provider.properties";
            String group = "testGroup1";
            manager.getConfigService().addListener(dataId, group, new AbstractListener() {
    
                @Override
                public void receiveConfigInfo(String s) {
    
                    log.info(" The monitored content is :{}", s);
                }
            });
        };
    }

And then after the restart , Modify the configuration value again , Background printing is as follows :

 Insert picture description here

The custom listener listens to a string after the configuration changes , This string contains my dataId and group All configurations configured in . After obtaining this configuration, you can convert the contents of this string into objects ,@RefreshScope The function of should be similar to this process .

stay nacos Can be used directly in spring.cloud.nacos.config.name To specify the dataId, That is to say bootstrap.properties Write in spring.application.name and spring.cloud.nacos.config.file-extension, as well as spring.profiles.active. Direct use spring.cloud.nacos.config.name Just go .

Because in different namespace in ,dataId It can be the same , So it can be used namespace To distinguish between different environments , development environment , Test environment , Production environment .

Service discovery

spring.cloud.n acos.discover y.service Configuration is used to configure the service name , The default value is spring.application.name The value of the configuration ,

spring.cloud.nacos.discovery.weight It is used to configure the weight , The greater the number , The greater the weight .

原网站

版权声明
本文为[Hurry to Friday]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206242358484653.html