SpringBoot Development Case Nacos Configuration Management Center

Foreword

During development, we would usually configure some parameters to perform certain functions, such as whether to open a service, warning mail configuration, and so on. Typically achieved in the form of hard-coded configuration file or database.

So the question is, how to be more elegant implementation? Welcome to the world of Nacos!

Nacos Configuration Management

Nacos Alibaba open-source projects, the full name Naming Configuration Service, focused on service discovery and configuration management.

Nacos committed to helping you discover, configure, and micro-management services. Nacos provides a set of simple-to-use set of features that help you quickly realize dynamic service discovery, service configuration, service metadata and traffic management.

Nacos ecological map

As shown in FIG. Nacos panorama, Nacos seamless support some mainstream OSS Eco, e.g.

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

Use Nacos simplify service discovery, configuration management, service governance and management solutions that enable service discovery micro, manage, share, combination easier.

Nacos Spring Boot Quick Start

Here that Spring-Boot2.x example:

pom.xml introduced dependence:

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

Start categories:

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;

/**
 * 启动类
 * 创建者 爪哇笔记 https://blog.52itstyle.vip
 * 创建时间 2019年7月14日
 * dataId 可以根据自己的项目自定义
 * autoRefreshed 是一个布尔值, Nacos 就会把最新的配置推送到该应用的所有机器上,简单而高效。
 */
@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("启动");
    }

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;

/**
 * 创建者 爪哇笔记 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;
    }
}

Profile introduction:

# 安全机制,建议走内网、配置防火墙
nacos.config.server-addr=127.0.0.1:8848

Please refer to the server installation configuration:

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

Homepage:

dataId must be consistent with the system configuration, the configuration of the content key way.

Examples of database

Nacos Server is used by default embedded database, modify the production environment is recommended to use mysql database to store configuration information.

application.properties add configuration profiles:

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 in Nacos Server conf folder, find nacos-mysql.sql file, into the database can be created.

Nacos default account password is: nacos, modify the password to be required to introduce:

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

Then encryption using code:

package com.itstyle.nacos;

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

/**
 * 创建者 爪哇笔记 https://blog.52itstyle.vip
 */
public class PasswordEncoderUtil {
    public static void main(String[] args) {
        System.out.println(new BCryptPasswordEncoder().encode("nacos"));
    }
}

summary

Overall, Nacos is very convenient, distribution center is only one of its small function only.

Case presentation

http://47.104.197.9:8848/nacos/

参考

https://nacos.io/en-us/

Guess you like

Origin www.cnblogs.com/smallSevens/p/11223830.html