1. Why choose NacOS

Nacos is used as a registry and configuration center in the springcloud architecture. It’s like Eureka and Apollo.

There is an old question about the difference between Nacos and Eureka.

But in the actual application of the building Lord there are a little special praise:

  1. Nacos has a configuration function, which simplifies the complexity of the system compared to eureka+ Apollo.
  2. Nacos uses a database to manage data, making it more comfortable to work with it.
  3. Nacos has the concepts of Namespace and Gourp to isolate services of the same name. This allows you to register with a NACOS service isolation when multiple people are using the backend service.

2.Nacos start fast

This quick start manual is designed to help you quickly download, install and use Nacos on your computer.

1. Version selection

You can find descriptions of the features supported by each release in Nacos’s Release Notes and blog, and the current recommended stable versions are 1.4.2 or 2.0.1.

2. Prepare the environment

Nacos relies on the Java environment to run. If you build and run Nacos from code and need to configure the Maven environment for this, make sure you install it in one of the following versions:

  1. The 64-bit OS supports Linux, Unix, Mac, and Windows. Linux, Unix, and Mac are recommended.
  2. 64-bit JDK 1.8+; Download & Configure.
  3. Maven 3.2 x +; Download & Configure.

3. Download the source code or install the package

You can get Nacos both from source code and distribution packages.

1. Download source code from Github

git clone https://github.com/alibaba/nacos.git
cd nacos/
mvn -Prelease-nacos -Dmaven.test.skip=true clean install -U  
ls -al distribution/target/

// change the $version to your actual path
cd distribution/target/nacos-server-$version/nacos/bin
Copy the code

2. Download the compressed package

You can download the nacos-server-$version.zip package from the latest stable version.

Unzip nacos-server-$version.zip or tar -xvf nacos-server-$version.tar.gz CD nacos/binCopy the code

4. Start the server

1.Linux/Unix/Mac

Startup command (standalone stands for standalone mode, not cluster mode):

sh startup.sh -m standalone
Copy the code

If you are using Ubuntu, or if you run the script, an error message [[symbol cannot be found, try the following:

bash startup.sh -m standalone
Copy the code

2.Windows

Startup command (standalone stands for standalone mode, not cluster mode):

startup.cmd -m standalone
Copy the code

The following configuration is required when IDEA is started using the source stand-alone

-Dnacos.standalone=true
Copy the code

5. Use the database in standalone mode

When configuration is not added, the default is to use memory to store information. We can add database configuration to keep data in the database.

For the time being, only mysql is officially supported. Other databases need to be manually modified.

1. Start the source code

Starting with the source idea requires modifying the following files in the following locations

2. The JAR package has been packed

Nacos \conf\application.properties Modifies the database configuration

6. The startup is successful

If the following pattern appears, the startup is successful. It can be seen that the mode is single-machine mode and the path is Console output

For the official website cluster construction requirements, is required by large enterprises, I think small companies can not meet the conditions, but also can start normally. But conditional or recommended cluster.

3. Springboot registers with NACOS

1. Modify the POM file

<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <! -- SpringCloud Alibaba Nacos Config --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency>Copy the code

It is best to choose a reasonable version in the POM.xml file, otherwise all kinds of errors will be reported.

The version can be selected according to the official document: github.com/alibaba/spr…

At the same time to ensure that the version of the same advice to his father to join the following configuration in engineering, and means of the groupid is org. Springframework. Cloud, com. Alibaba. Cloud, org. Springframework. Depend on the parent project version of the boot

<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <! Maven does not support multiple inheritance and uses import to rely on management configurations --> <scope>import</scope> </dependency> <! --> <dependency> <groupId>com.alibaba. Cloud </groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>${spring-cloud-alibaba.version}</version> <type>pom</type> <scope>import</scope> </dependency> <! --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>Copy the code

2. Modify the bootstrap. XML

Yml must be used instead of application.yml, because bootstrap.yml runs before application.yml, and it may be possible to connect to localhost:8848 even if there is a registered address.

Cloud: nacos: discovery: # service register address server-addr: 192.168.xx.x:8848 # Namespace namespace: b80f0AA4-3af2-a6e3-c6fda24c2bc0 # group: XXX config: 192.168.xx.xx:8848 # Configuration file format file-extension: yML # Namespace namespace: b80f0AA4-3AF2-a6E3-c6fda24c2BC0 # Group group: XXXCopy the code

You can specify namespaces and groups for services. Services in a namespace or group can only obtain services in the same namespace or group. This allows you to configure dev and Pro environments at the same time. Each member of the acquisition group can connect to one NacOS service, rather than starting one as Eureka does.

1. Create a namespace

The namespace in the configuration file must be created, as shown in the figure.

The namespace ID is in the configuration file

2. Create a group

Instead of creating groups in NACOS, write them directly.

3. Modify the startup class

Add the @enableDiscoveryClient annotation to the Spring Boot startup class.

@EnableDiscoveryClient @SpringBootApplication public class GetwayApplication { public static void main(String[] args) { / / remove nacos log System. SetProperty (" nacos. Logging. Default. Config. Enabled ", "false"); SpringApplication.run(GetwayApplication.class, args); }}Copy the code

Note: if you use the logback as log System. SetProperty (” nacos. Logging. Default. Config. Enabled “, “false”); Otherwise, an error will be reported because of the log name

4. Start

Then start the service and access IP :8848/nacos to verify the registration.Also published to the specified namespace and groupid.

4. Configure the center

Configuration center: The configuration in Bootstrap. yml can be modified through nacOS configuration, and large departments can take effect without restarting the service.

Due to the introduction of NACOS, we no longer use Apollo configuration center. The reasons for the change are as follows

  1. Due to the architecture change to SpringCloud Alibaba, NACOS can assume the functions of Eureka + Apollo.
  2. You can reduce the complexity of the system. Convenient operation and maintenance.
  3. The requirement of the configuration center is only dynamic configuration, without more detailed permissions and grayscale publishing and other functional requirements.

1. Springboot integration

Bootstrap.yml is recommended for starting classes

1. Create the NACOS configuration

Be careful to select the appropriate namespace

The configuration is explained as follows:

  1. The Data ID is named in the following format: {spring.application.name}-{spring.profiles.active}.{file type}, that is, the system name +dev/pro.yaml.

“-” and “spring.profiles. Active” are omitted if there is no spring.profiles.

  1. Group: The namespace and group must correspond to nacOS configurations (these two concepts are described above); otherwise, the configuration cannot be obtained.

  2. Configuration format: The above configuration is chosen because bootstrap.yml is used.

  3. Configuration items: Configuration items that need to be changed in NACOS.

2. The configuration is successful

If successful:

Remember that namespace and group must match.

You can also monitor which service is using this configuration.

3. Compile the client interface

@RestController @RefreshScope public class DemoController { @Value("${nacostest.demo}") private String demo; @GetMapping("/testConfig") public String testConfig() { return demo; }}Copy the code

RefreshScope don’t write too little! Otherwise error!

Change configuration When you call the interface several times, you can find that the return value changes.

At the end of the article draw

First of all, I would like to thank the nuggets community for providing me with two nuggets MEDALS.

MEDALS can do what: put on the computer, backpack, the pattern is pretty beautiful. Basically be whole journey free (pack post), more main win probability is high, he not sweet.

This article chooses the comment likes most two old iron to send, if the comment is the same, chooses the comment time earliest. Your comments and likes are appreciated.