“This article has participated in the call for good writing activities, click to view: the back end, the big front end double track submission, 20,000 yuan prize pool waiting for you to challenge!”

Accumulate over a long period, constant dripping wears away a stone 😄

preface

After using Nacos for a period of time, the frequent release of the service, but do not know the latest release date of the service, resulting in uncertainty about whether it is the latest version. And the leaders are like, how can you do that? Well, if the leadership is in demand, then it’s up to us minions to do it.

Final rendering

First post a final effect drawing!

The development process

We can see that there is default data in the metadata before development begins. The default value is:preserved.register.source=SPRING_CLOUD

So where is the default configuration configured? This configuration is located inNacosDiscoveryPropertiestheinitMethods!According to the graph above, we can know thatmetadataThe data structure of is Map.

private Map<String, String> metadata = new HashMap();
Copy the code

Let’s take a look firstNacosDiscoveryPropertiesClass. We know from the class name that this isNacos RegistryConfiguration class. (Digression: Then we can assume that there should be another oneNacosConfigPropertiesClass. As aNacos configuration centerConfiguration class.To see this@ConfigurationProperties("spring.cloud.nacos.discovery") , thinking this is not simple? Play!!

spring:
  application:
    name: naocs-service
  profiles:
    active: dev
  cloud:
    nacos:
      config:
        group: ${spring.profiles.active}
        file-extension: yaml
        server-addr: 127.0. 01.: 8848
        prefix: ${spring.application.name}
      discovery:
        server-addr: 127.0. 01.: 8848
        group: ${spring.profiles.active}
        metadata:
          version: 1.0. 0
          desc: Development's minions
Copy the code

indiscoveryAdd under nodemetadataInterface. Then start the project and goNacostheService detailsList to view.It was a little exciting to see the results. How do I get the current time in the configuration file? There are ways! We seeNacosDiscoveryPropertiesThere’s a little button next to it. Click the green button.

You should then see this code:

@Bean
@ConditionalOnMissingBean
public NacosDiscoveryProperties nacosProperties(a) {
    return new NacosDiscoveryProperties();
}
Copy the code

The @bean annotation doesn’t explain that.

ConditionalOnMissingBean The @conditionalonmissingBean annotation says that the current Bean is instantiated when the labeled Bean does not exist. That’s representative I only need to provide the NacosDiscoveryProperties Bean object, SpringBoot NacosDiscoveryPropertiesBean object will use what we provide.

Add the following code to the startup class, of course you can create your own configuration class, as long as it can be scanned.

@Bean
public NacosDiscoveryProperties nacosProperties(a) {
    NacosDiscoveryProperties nacosDiscoveryProperties = new NacosDiscoveryProperties();
    Map<String, String> metadata = nacosDiscoveryProperties.getMetadata();
    metadata.put("startup.time".new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                                 .format(new Date()));
    return nacosDiscoveryProperties;
}
Copy the code

Then start the project and view the metadata for the service.

Happy to see the results! I am happy when the leader is happy.

  • If you have any questions or errors in this article, please feel free to comment. If you find this article helpful, please like it and follow it.