Download nacOS

1.1 download

Address: github.com/alibaba/nac… Here are the download links for various versions of NacOS. I used 1.4.0 as the demo version this time, and I recommend using the stable version.

1.2 unzip

CD/usr/local wget tar XZVF - https://github.com/alibaba/nacos/releases/download/1.4.0/nacos-server-1.4.0.tar.gz Nacos - server - 1.4.0. Tar. GzCopy the code

You can then see the nacOS directory structure in the /usr/local/nacos/ folder

2. Configuration and startup of NACOS

2.1 Creating a persistent database in the configuration center

Create a SQL table for a new database: github.com/alibaba/nac…

2.2 Modifying the Configuration File

Modify the configuration file conf/application. The properties

Modifying contextPath allows you to customize the path of the nacOS management platform

Modify port to customize the boot port of nacOS

End of each line new

Spring. The datasource. Platform = mysql db. Num = 1 db. Url. 0 = JDBC: mysql: / / IP: port/library name? CharacterEncoding = UTf8 &connectTimeout=1000&socketTimeout=3000&autoReconnect=true db.user= user name db.password= passwordCopy the code

This completes the basic configuration of nacOS

2.3 NacOS Single-machine mode Startup

Run the start_up.sh script in the bin directory to start the service

sh startup.sh -m standalone
Copy the code

After the startup is successful, run the

 ps -ef | grep nacos
Copy the code

Grep Process to nacosAccess IP :port/contextPath If the login page is displayed, the system is successfully started. The default account and password arenacos

2.4 NACOS Startup in cluster mode

2.4.1 Modifying a Configuration File

  • Configure conf/cluster.conf to write all nodes in the cluster to the file
If the cluster is deployed on a single host, enter the IP address of the network adapter that can be connected to the network. If the cluster is deployed on different hosts, enter the public IP address 192.168.43.123:8847 192.168.43.123:8848 192.168.43.123:8849Copy the code
  • Make three copies of Nacos

  • Change the application. Properties port to the port of cluster cluster.conf
Vim nacos - 8847 / conf/application. The properties of # modified port 8847 vim nacos - 8849 / conf/application. The properties # modified port 8849Copy the code

2.4.2 Starting the NACOS Cluster

Run bin/start_up.sh on each node

After successful startup, nacOS of all three nodes can be accessed successfullyEnter NACOS, open the node list, and you can see that there are three nodes

2.4.3 Configuring the Nginx Proxy

In order to make the center of the registry and configuration access pressure are split into three nodes, we configure nginx do forward requests to download and start the nginx, refer to: www.cnblogs.com/zhangliuher… Modify the configuration file conf/nginx.conf

Upstream serverList {server 127.0.0.1:8847; Server 127.0.0.1:8848; Server 127.0.0.1:8849; } server { listen 4399; server_name localhost; location / { proxy_pass http://serverList/; }}Copy the code

Start/reload nginx and access port 4399 / nacOS. If you can see the management platform, the nginx configuration is successful

3. Spring integration with NACOS

3.1 Introducing Dependencies (Refer to the dependency version:Github.com/alibaba/spr…)

<! -- nacos config --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> <! -- SpringCloud alibaba nacos --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>com.alibaba.nacos</groupId> <artifactId>nacos-client</artifactId> </dependency> <! -- spring boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>Copy the code

3.2 Creating the configuration file bootstrap.yml

Spring: application: name: order-service # cloud: nacos: config: # Serveraddr: 172.16.0.210:4399 serveraddr: 172.16.0.210:4399 serveraddr: 172.16.0.210:4399 nacOS Nginx namespace: Order-service-group: discovery: namespace: namespace server-addr: 172.16.0.210:4399 # nacOS Service registry address (nginx) Server: port: 10003 #Copy the code

3.3 Start Annotations

// Add @springBootApplication @enableDiscoveryClient to the Spring startup classCopy the code

Notice that @SpringBootApplication and @EnableDiscoveryClient are added here, don’t be lazy and switch to @SpringCloudApplication!! Although the @SpringCloudApplication annotation contains @SpringBootApplication and @EnableDiscoveryClient, it also contains the @Enablecircuitbreaker annotation. @enablecircuitbreaker is a circuit breaker. If you use @SpringCloudApplication, maven will need to import hystrix jars and configuration packages.

3.4 start the spring

3.4.1 Defining configuration files in NACOS

All configuration information — database, message queue, and so on — can be defined in the NACOS configuration center.

    1. Click on the New button
    1. data-id: #{spring.application.name}-#{spring.profiles.active}.yaml
    1. Group: group name (default DEFAULT_GROUP)
    1. Write the configuration information into the configuration content input box and select YAML as the configuration format

3.4.2 start

If spring starts normally and the service instance can be seen in the registry, the spring starts successfully



Reference:Nacos. IO/useful – cn/docs /…