SpringBoot Development Case Nacos Configuration Management Center

Keywords: Programming Spring Database MySQL Java

Preface

In the development process, we usually configure some parameters to achieve certain functions, such as whether to open a service, alarm mail configuration and so on. Generally, it is implemented in the form of hard code, configuration file or database.

So the question arises, how can we achieve it more elegantly? Welcome to the world of Nacos!

Nacos configuration management

Nacos is Alibaba's open source project, fully known as Naming Configuration Service, focusing on service discovery and configuration management.

Nacos is dedicated to helping you discover, configure and manage micro services. Nacos provides a set of easy-to-use features to help you quickly implement dynamic service discovery, service configuration, service metadata, and traffic management.

Nacos ecological map

As shown in the Nacos panorama, Nacos seamlessly supports some mainstream open source ecosystems, such as

  • Spring Cloud
  • Apache Dubbo and Dubbo Mesh TODO
  • Kubernetes and CNCF TODO.

Nacos simplifies service discovery, configuration management, service governance and management solutions, making it easier to discover, manage, share and compose micro services.

Nacos Spring Book Quick Start

Take Spring-Boot2.x as an example:

pom.xml introduces dependencies:

<dependency>
      <groupId>com.alibaba.boot</groupId>
      <artifactId>nacos-config-spring-boot-starter</artifactId>
      <version>0.2.1</version>
</dependency>

Startup class:

package com.itstyle.nacos;

import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Startup class
 * Creator Java Notes https://blog.52itstyle.vip
 * Established on July 14, 2019
 * dataId You can customize it according to your own project
 * autoRefreshed It's a Boolean value, and Nacos pushes the latest configuration to all machines in the application, simple and efficient.
 */
@SpringBootApplication
@NacosPropertySource(dataId = "itstyle.blog", autoRefreshed = true)
public class Application  {
    private static final Logger logger = LoggerFactory.getLogger(Application.class);

    public static void main(String[] args){
        SpringApplication.run(Application.class, args);
        logger.info("start-up");
    }

Use cases:

package com.itstyle.nacos;

import com.alibaba.nacos.api.config.annotation.NacosValue;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Creator Java Notes https://blog.52itstyle.vip
 */
@Controller
@RequestMapping(value = "config")
public class NacosConfigController {

    @NacosValue(value = "${useLocalCache:false}", autoRefreshed = true)
    private boolean useLocalCache;

    @RequestMapping(value = "/get", method = RequestMethod.GET)
    @ResponseBody
    public boolean get() {
        return useLocalCache;
    }
}

Configuration file introduction:

# Safety mechanism, recommend to use intranet and configure firewall
nacos.config.server-addr=127.0.0.1:8848

Refer to Server Installation Configuration:

https://nacos.io/zh-cn/docs/quick-start.html

Home page:

The dataId must be consistent with the system configuration, configuring the content as key-value pairs.

Instance database

Nacos Server uses embedded databases by default, and production environments recommend that mysql databases be modified to store configuration information.

Add configurations in the configuration file application.properties:

spring.datasource.platform=mysql
db.num=1
db.url.0=jdbc:mysql://127.0.0.1:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
db.user=root
db.password=root

Create a database. Under the Nacos Server conf folder, find the nacos-mysql.sql file and import the created database.

Nacos default account password is: nacos, password modification needs to use the introduction:

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
</dependency>

Then encrypt with code:

package com.itstyle.nacos;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * Creator Java Notes https://blog.52itstyle.vip
 */
public class PasswordEncoderUtil {
    public static void main(String[] args) {
        System.out.println(new BCryptPasswordEncoder().encode("nacos"));
    }
}

Summary

In general, Nacos is quite convenient, and the configuration center is just a small function of it.

Posted by skeener on Sun, 21 Jul 2019 23:27:46 -0700