Nacos Config Integrated Spring Cloud Usage Instructions

Keywords: Programming Spring Maven Java

Nacos config provides the solution of configuration center, and its function is very powerful and applicable. It provides the mode of single machine and cluster.

  • Ways of System Integration
  1. maven package dependency
	<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
			<version>0.2.1.RELEASE</version>
	</dependency>

2.bootstrap.properties configuration file

# nacos configuration center address
spring.cloud.nacos.config.server-addr=10.136.15.122:8848
# Default application name, default data-id in configuration center is name+file-extension
spring.application.name=example
Use name as prefix without configuration
#spring.cloud.nacos.config.prefix
# Applying file format to support properties,yml
spring.cloud.nacos.config.file-extension=properties

3.java application code

package com.nacos;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/config")
@RefreshScope
public class ConfigController {

    @Value("${useLocalCache:false}")
    private boolean useLocalCache;
    
    /**
     * http://localhost:8080/config/get
     */
    @RequestMapping("/get")
    public boolean get() {
        return useLocalCache;
    }
}
package com.nacos;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class NacosConfigApplication {

    public static void main(String[] args) {
        SpringApplication.run(NacosConfigApplication.class, args);
    }
}

4. Start the application, nacos configuration center configuration parameters, access / config/get result is true, the result configuration is successful.

Posted by digitalmartyr on Wed, 02 Oct 2019 01:21:29 -0700