Nacos series (1) docker install stand-alone nacos service

Article Directory

I. Overview

In 2018, Alibaba will open source Nacos. Nacos is the name after the open source. It is based on the core code integration of Alibaba's internal Configserver (registration center), Diamond (persistent configuration center), and Vipserver (a registry similar to Configserver that provides routing information) , The goal is to create the world's most powerful service registration and configuration center.

This article simply deploys a nacos service for an intuitive understanding.

2. Deployment steps

Since it is installed using docker, this article will not introduce the use of docker, you can refer to: docker use

2.1 Download the demo file

Nacos provides a demo configuration file on github, download it first:

#从github上clone下来
git clone https://github.com/nacos-group/nacos-docker.git
#进入文件目录
cd nacos-docker

There are files as follows: the

Insert picture description here


key is the env and example files.

2.2 Install and start

Start command:

#注意最后的-d 表示后台启动,不加-d启动后可能无法访问控制台
docker-compose -f example/standalone-mysql-5.7.yaml up -d

Note:
Since nacos itself does not have a storage function, it needs to be used with a database. Here we use mysql5.7. By default, derby, mysql5.7, and mysql8 are supported, and the corresponding yaml configuration file is in the example path.

Example file description:

  • standalone-mysql-5.7.yaml: standalone mode, using mysql5.7 database
  • standalone-mysql-8.yaml: standalone mode, using mysql8 database
  • standalone-derby.yaml: standalone mode, using the embedded Derby database
  • cluster-hostname.yaml: cluster mode, using mysql5.7 database
  • cluster-embedded.yaml: cluster mode, using embedded derby database

Note before starting, MySQL uses port 3306 by default, then when your server has installed mysql and occupies 3306, it will fail to start. At this time, you can modify the nacos configuration to use your locally installed mysql,
or you can modify it directly The mapped port of mysql, and then start,
modify the mysql ports in the standalone-mysql-5.7.yaml file as follows:

version: "2"
services:
  nacos:
    image: nacos/nacos-server:${NACOS_VERSION}
    container_name: nacos-standalone-mysql
    env_file:
      - ../env/nacos-standlone-mysql.env
    volumes:
      - ./standalone-logs/:/home/nacos/logs
      - ./init.d/custom.properties:/home/nacos/init.d/custom.properties
    ports:
      - "8848:8848"
      - "9848:9848"
      - "9555:9555"
    depends_on:
      - mysql
    restart: on-failure
  mysql:
    container_name: mysql
    image: nacos/nacos-mysql:5.7
    env_file:
      - ../env/mysql.env
    volumes:
      - ./mysql:/var/lib/mysql
    # 将mysql的端口改为3307,避免冲突
    ports:
      - "3307:3306"
#  prometheus:
#    container_name: prometheus
#    image: prom/prometheus:latest
#    volumes:
#      - ./prometheus/prometheus-standalone.yaml:/etc/prometheus/prometheus.yml
#    ports:
#      - "9090:9090"
#    depends_on:
#      - nacos
#    restart: on-failure
#  grafana:
#    container_name: grafana
#    image: grafana/grafana:latest
#    ports:
#      - 3000:3000
#    restart: on-failure

View container

docker ps
Insert picture description here

2.3 Nacos console

llink: http://127.0.0.1:8848/nacos/default
username/password: nacos/nacos

The interface after login is as follows:

Insert picture description here


because it is the registration center + configuration center, the main menu is configuration management + service management.

By default, they are all empty. We can use the following commands to simply register and add configuration.

2.4 Service registration and discovery

registered:

# 随便注册一个服务,参数:服务名、ip、port
curl -X POST 'http://127.0.0.1:8848/nacos/v1/ns/instance?serviceName=nacos.naming.serviceName&ip=20.18.7.10&port=8080'

Then go to the service list to check and find that the registration is successful:

Insert picture description here

Find:

curl -X GET 'http://127.0.0.1:8848/nacos/v1/ns/instance/list?serviceName=nacos.naming.serviceName'

get:

{"name":"[email protected]@nacos.naming.serviceName","groupName":"DEFAULT_GROUP","clusters":"","cacheMillis":10000,"hosts":[{"instanceId":"20.18.7.11#8080#DEFAULT#[email protected]@nacos.naming.serviceName","ip":"20.18.7.11","port":8080,"weight":1.0,"healthy":false,"enabled":true,"ephemeral":true,"clusterName":"DEFAULT","serviceName":"[email protected]@nacos.naming.serviceName","metadata":{},"instanceHeartBeatInterval":5000,"instanceIdGenerator":"simple","instanceHeartBeatTimeOut":15000,"ipDeleteTimeout":30000},{"instanceId":"20.18.7.10#8080#DEFAULT#[email protected]@nacos.naming.serviceName","ip":"20.18.7.10","port":8080,"weight":1.0,"healthy":true,"enabled":true,"ephemeral":true,"clusterName":"DEFAULT","serviceName":"[email protected]@nacos.naming.serviceName","metadata":{},"instanceHeartBeatInterval":5000,"instanceIdGenerator":"simple","instanceHeartBeatTimeOut":15000,"ipDeleteTimeout":30000}],"lastRefTime":1622882336296,"checksum":"","allIPs":false,"reachProtectionThreshold":false,"valid":true}

2.5 Configuration release and acquisition

Add configuration:
add a configuration at will:
mainly configure data id, group, and content:

Insert picture description here


but to view this data in the mysql database:

Insert picture description here


you can also call the interface:

# 参数:dataId、group、content
curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos.cfg.dataId&group=test&content=helloWorld"

This article will stop here first.

Reference:
https://baijiahao.baidu.com/s?id=1655764444148144522&wfr=spider&for=pc
https://nacos.io/zh-cn/docs/quick-start-docker.html