A registry is an essential component when using SpringCloud for a distributed microservices architecture. Currently, Eureka, Consul, and Zookeeper are available. Today, let’s talk about Alibaba’s Nacos.

Download and Install

  • Download from github.com/alibaba/nac…

  • Installation:

    • Windows download and decompress (.zip), directly click bin/start.bat.
    • After the Linux download is decompressed (.tar.gz), run the bin/start.sh script.
  • Open the console:

    Nacos provides a visual operating platform that, once installed, can be accessed by typing http://localhost:8848 into a browser. The default username and password are Nacos (VERSION 1.2.0 I used). Password authentication is turned off by default, this option will be said later)

Nacos profile

Nacos is a good thing by Alibaba open source, directly posted on its official website, it is no longer so cumbersome, after all, it is developed by Chinese, there is a Chinese document, it is written very comprehensive. Nacos. IO/useful – cn/docs /…

Nacos as the registry

Unlike Eureka, it doesn’t require you to create a new Web project. Like Zookeeper and Consul, you just download, install, and register our microservices.

Create two microservices, one client (caller) and one server (provider),

  • Introduction of depend on
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
Copy the code
  • Introduce the following code in their configuration files:
spring:
  cloud:
    nacos:
      discovery:
      # Nacos address
        server-addr: localhost:8848
Copy the code
  • Add to the main startup class (no matter which registry you have) :
@EnableDiscoveryClient
Copy the code
  • Of course, the ribbon load balancing needs to be added on the client side, but there is no need to add dependencies. Nacos has already been added
@Configuration
public class AppConfig {
    
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(a){
        return newRestTemplate(); }}Copy the code
  • Ok, open up the console of Nacos, and you can see the two microservices.

Nacos acts as a distributed configuration center

Previously we used SpringCloudConfig to pull configuration files from github and other repositories, but with Nacos we can directly configure from Nacos, isn’t it convenient? Create a new project, nacos-config-server-8002

Pom.xml introduces bottom dependencies

<! -- Distributed Configuration Center -->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
Copy the code

Add the following configuration information to application.yml

spring:
  profiles:
    active: dev
Copy the code

Add a bootstrap.yml

server:
  port: 8002
spring:
  application:
    name: nacos-config-server-8002
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
      config:
        server-addr: localhost:8848
The configuration file type is yaml and properties
        file-extension: yaml
Copy the code
  1. What does configuration in application.yml mean?
  1. Why bootstrap.yml?

In short, this configuration is the same as application.yml, but it is loaded before appliation.yml

Interface in Controller

@RestController
@RefreshScope // This is a native annotation of SpingCloud that enables dynamic configuration refreshes
public class InfoController {
    /* This is an example of: It means that, if not in the configuration file, use directly behind the string, I write is nothing in the local project, we have not configured configInfo, so if this time call http://localhost:8002/info return is nothing * /
    @Value("${configInfo}")
    private String configInfo;

    /** ** for configuration center demo **@return* /
    @GetMapping("info")
    public String getConfig(a) {
        returnconfigInfo; }}Copy the code

Several spatial concepts in Nacos:

  • NameSpaces

    Equivalent to a package name in our project, multiple namespaces can be created in Nacos. Micro service registration, can be configured spring. Cloud. Nacos. Discovery. The namespace, of course, the configuration file can also configure the namespace to specify the corresponding namespace, if no configuration is to use the default public space.

    If you have several projects sharing a Nacos cluster, namespce can be used to differentiate projects.

    At the bottom of the Nacos console there is a namespace that you can create. Once you have created a new namespace, click on the services list and configuration list again and your command space selection will appear above it.

  • Group = Group

    Equivalent to class names in Java, there can be multiple classes in a package. However, this is only relative to configuration files, there is no such thing as service registration. It has a default group called DEFAULT_GROUP, which is present when a new configuration file is created (see figure below)

  • DataID

    This is the equivalent of a method in a class, and again, there can be multiple method names in a class.

Our configuration file was added in the image above, and here is a snapshot of the official Nacos documentation for DataID:

Try creating a new configuration file

Create a new configuration file in the configuration list. If you have more than one namespace, select the one connected to your project

As long as the point at the bottom right corner, will be automatically configured, visit http://localhost:8002/info again, you will see has changed, this is a real-time changes.

These are very simple operations, as long as you do it once, will be used

Configuration files of Nacos are persisted

Nacos uses the embedded database Derby. For embedded database, refer to the embedded database H2 in Spring. Although the database is different, the principle and operation method are the same. However, we want to use our mysql to store nacOS data, ok?

  • Import data into your local Mysql library

    In Nacos installation package under the conf directory has a Nacos – mysql. SQL file and put it on your mysql tools perform again (it the SQL statement zhongjian library, first built a library named nacos_config)

  • Modify the application.properties file in conf

The default user name and password are nacos nacos.core.auth.enabled=true ### Spring. The datasource. Platform = mysql db. Num = 1 db. Url. 0 = JDBC: mysql: / / 127.0.0.1:3306 / nacos_config? characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true db.user=root db.password=123Copy the code

All of these are from Nacos

This way, when you start Nacos again, all your previous configuration files are gone because you have configured the Mysql library. So, every time you make a change, you’re going to go into the Mysql library, which is pretty simple, and you can kind of get the idea.

Follow “Small Fish and Java” on wechat and reply to “SpingCloud” for more learning materials of SpringCloud