简单部署单机模式Nacos

参考 https://nacos.io/docs/latest/quickstart/quick-start/

安装依赖

apt install openjdk-11-jdk
# 或者
dnf install java-11-openjdk-devel

下载安装包

https://github.com/alibaba/nacos/releases
找到最新的稳定版本


下载好后,直接解压

wget https://github.com/alibaba/nacos/releases/download/2.5.1/nacos-server-2.5.1.tar.gz
tar xf nacos-server-2.5.1.tar.gz
cd nacos/
tree
.
├── LICENSE
├── NOTICE
├── bin
│   ├── shutdown.cmd
│   ├── shutdown.sh
│   ├── startup.cmd
│   └── startup.sh
├── conf
│   ├── 1.4.0-ipv6_support-update.sql
│   ├── announcement_en-US.conf
│   ├── announcement_zh-CN.conf
│   ├── application.properties
│   ├── application.properties.example
│   ├── cluster.conf.example
│   ├── console-guide.conf
│   ├── derby-schema.sql
│   ├── mysql-schema.sql
│   └── nacos-logback.xml
└── target
    └── nacos-server.jar

直接使用bin目录下的脚本来启动和关闭
默认可以直接启动

注意:单机模式需要加上参数 -m standalone

$ bash  bin/startup.sh -m standalone
/usr/lib/jvm/java-11-openjdk-amd64/bin/java    -Xms512m -Xmx512m -Xmn256m -Dnacos.standalone=true -Dnacos.member.list= -Xlog:gc*:file=/root/nacos/logs/nacos_gc.log:time,tags:filecount=10,filesize=100m -Dloader.path=/root/nacos/plugins,/root/nacos/plugins/health,/root/nacos/plugins/cmdb,/root/nacos/plugins/selector -Dnacos.home=/root/nacos -jar /root/nacos/target/nacos-server.jar  --spring.config.additional-location=file:/root/nacos/conf/ --logging.config=/root/nacos/conf/nacos-logback.xml --server.max-http-header-size=524288
nacos is starting with standalone
nacos is starting. you can check the /root/nacos/logs/start.out

$ ps -ef | grep nacos
root      240122       1 99 16:00 pts/2    00:00:39 /usr/lib/jvm/java-11-openjdk-amd64/bin/java -Xms512m -Xmx512m -Xmn256m -Dnacos.standalone=true -Dnacos.member.list= -Xlog:gc*:file=/root/nacos/logs/nacos_gc.log:time,tags:filecount=10,filesize=100m -Dloader.path=/root/nacos/plugins,/root/nacos/plugins/health,/root/nacos/plugins/cmdb,/root/nacos/plugins/selector -Dnacos.home=/root/nacos -jar /root/nacos/target/nacos-server.jar --spring.config.additional-location=file:/root/nacos/conf/ --logging.config=/root/nacos/conf/nacos-logback.xml --server.max-http-header-size=524288 nacos.nacos
root      240613   27464  0 16:01 pts/2    00:00:00 grep --color=auto nacos


$ tail -100 logs/start.out 
/usr/lib/jvm/java-11-openjdk-amd64/bin/java    -Xms512m -Xmx512m -Xmn256m -Dnacos.standalone=true -Dnacos.member.list= -Xlog:gc*:file=/root/nacos/logs/nacos_gc.log:time,tags:filecount=10,filesize=100m -Dloader.path=/root/nacos/plugins,/root/nacos/plugins/health,/root/nacos/plugins/cmdb,/root/nacos/plugins/selector -Dnacos.home=/root/nacos -jar /root/nacos/target/nacos-server.jar  --spring.config.additional-location=file:/root/nacos/conf/ --logging.config=/root/nacos/conf/nacos-logback.xml --server.max-http-header-size=524288

         ,--.
       ,--.'|
   ,--,:  : |                                           Nacos 2.5.1
,`--.'`|  ' :                       ,---.               Running in stand alone mode, All function modules
|   :  :  | |                      '   ,'\   .--.--.    Port: 8848
:   |   \ | :  ,--.--.     ,---.  /   /   | /  /    '   Pid: 240122
|   : '  '; | /       \   /     \.   ; ,. :|  :  /`./   Console: http://10.0.2.33:8848/nacos/index.html
'   ' ;.    ;.--.  .-. | /    / ''   | |: :|  :  ;_
|   | | \   | \__\/: . ..    ' / '   | .; : \  \    `.      https://nacos.io
'   : |  ; .' ," .--.; |'   ; :__|   :    |  `----.   \
|   | '`--'  /  /  ,.  |'   | '.'|\   \  /  /  /`--'  /
'   : |     ;  :   .'   \   :    : `----'  '--'.     /
;   |.'     |  ,     .-./\   \  /            `--'---'
'---'        `--`---'     `----'

2025-04-10 16:00:43,883 INFO Tomcat initialized with port(s): 8848 (http)

2025-04-10 16:00:44,328 INFO Root WebApplicationContext: initialization completed in 4055 ms

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.alipay.sofa.jraft.util.internal.UnsafeUtil (jar:file:/root/nacos/target/nacos-server.jar!/BOOT-INF/lib/jraft-core-1.3.14.jar!/) to field java.nio.Buffer.address
WARNING: Please consider reporting this to the maintainers of com.alipay.sofa.jraft.util.internal.UnsafeUtil
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
2025-04-10 16:00:49,973 INFO Will secure any request with [org.springframework.security.web.session.DisableEncodeUrlFilter@46911148, org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@2db1b657, org.springframework.security.web.context.SecurityContextPersistenceFilter@1fbf088b, org.springframework.security.web.header.HeaderWriterFilter@58b5a2f3, org.springframework.security.web.csrf.CsrfFilter@5e99b9c, org.springframework.security.web.authentication.logout.LogoutFilter@3aaa3c39, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@d70f722, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@1e1eeedd, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@7e351d7, org.springframework.security.web.session.SessionManagementFilter@58fbd02e, org.springframework.security.web.access.ExceptionTranslationFilter@4d9754a8]
2025-04-10 16:00:50,405 INFO Adding welcome page: class path resource [static/index.html]
2025-04-10 16:00:50,848 INFO Exposing 1 endpoint(s) beneath base path '/actuator'
2025-04-10 16:00:50,871 WARN You are asking Spring Security to ignore Ant [pattern='/**']. This is not recommended -- please use permitAll via HttpSecurity#authorizeHttpRequests instead.
2025-04-10 16:00:50,872 INFO Will not secure Ant [pattern='/**']
2025-04-10 16:00:50,872 WARN You are asking Spring Security to ignore Mvc [pattern='/prometheus']. This is not recommended -- please use permitAll via HttpSecurity#authorizeHttpRequests instead.
2025-04-10 16:00:50,872 INFO Will not secure Mvc [pattern='/prometheus']
2025-04-10 16:00:50,872 WARN You are asking Spring Security to ignore Mvc [pattern='/prometheus/namespaceId/{namespaceId}']. This is not recommended -- please use permitAll via HttpSecurity#authorizeHttpRequests instead.
2025-04-10 16:00:50,872 INFO Will not secure Mvc [pattern='/prometheus/namespaceId/{namespaceId}']
2025-04-10 16:00:50,872 WARN You are asking Spring Security to ignore Mvc [pattern='/prometheus/namespaceId/{namespaceId}/service/{service}']. This is not recommended -- please use permitAll via HttpSecurity#authorizeHttpRequests instead.
2025-04-10 16:00:50,872 INFO Will not secure Mvc [pattern='/prometheus/namespaceId/{namespaceId}/service/{service}']
2025-04-10 16:00:50,948 INFO Tomcat started on port(s): 8848 (http) with context path '/nacos'
2025-04-10 16:00:50,967 INFO No TaskScheduler/ScheduledExecutorService bean found for scheduled processing
2025-04-10 16:00:51,030 INFO Nacos started successfully in stand alone mode. use embedded storage

查看日志,出现了 Nacos started successfully in stand alone mode. use embedded storage 表示启动成功
然后直接访问 <IP>:8848/nacos

直接就进来了,因为默认是不开启认证的

开启认证

关闭服务

$ bash bin/shutdown.sh 
The nacosServer(240122) is running...
Send shutdown request to nacosServer(240122) OK

修改配置文件

vim conf/application.properties
#修改下面这几个选项

### If turn on auth system:
nacos.core.auth.enabled=true

### Since 1.4.1, worked when nacos.core.auth.enabled=true and nacos.core.auth.enable.userAgentAuthWhite=false.
### The two properties is the white list for auth and used by identity the request from other server.
nacos.core.auth.server.identity.key=abc
nacos.core.auth.server.identity.value=111

### worked when nacos.core.auth.system.type=nacos
### The token expiration in seconds:
nacos.core.auth.plugin.nacos.token.cache.enable=true
nacos.core.auth.plugin.nacos.token.expire.seconds=18000
### The default token (Base64 String):
nacos.core.auth.plugin.nacos.token.secret.key=MTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExCg==

然后再重新启动

$ bash  bin/startup.sh -m standalone
/usr/lib/jvm/java-11-openjdk-amd64/bin/java    -Xms512m -Xmx512m -Xmn256m -Dnacos.standalone=true -Dnacos.member.list= -Xlog:gc*:file=/root/nacos/logs/nacos_gc.log:time,tags:filecount=10,filesize=100m -Dloader.path=/root/nacos/plugins,/root/nacos/plugins/health,/root/nacos/plugins/cmdb,/root/nacos/plugins/selector -Dnacos.home=/root/nacos -jar /root/nacos/target/nacos-server.jar  --spring.config.additional-location=file:/root/nacos/conf/ --logging.config=/root/nacos/conf/nacos-logback.xml --server.max-http-header-size=524288
nacos is starting with standalone
nacos is starting. you can check the /root/nacos/logs/start.out


用户名是nacos,然后密码随便填,你写的什么密码,那密码就会被设置成什么

配置外部数据库

这里使用docker运行测试数据库

services:
  mysql:
    image: mysql:8
    container_name: mysql
    ports:
      - 3306:3306
    restart: always
    user: '0'
    volumes:
      - ./db:/var/lib/mysql
      - /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime
    environment:
      MYSQL_ROOT_PASSWORD: 111111
      #允许空密码
      MYSQL_ALLOW_EMPTY_PASSWORD: no
      #新建数据库
      MYSQL_DATABASE: nacos
      #新建用户
      MYSQL_USER: admin
      #设置用户密码
      MYSQL_PASSWORD: 111111

这里会创建一个nacos数据库
导入表

mysql -h10.1.0.108 -uroot -p111111
mysql> GRANT ALL PRIVILEGES ON nacos.* TO 'admin'@'%';
Query OK, 0 rows affected (0.01 sec)

mysql> exit
Bye

mysql -h10.1.0.108 -uadmin -p111111
mysql> use nacos
Database changed
mysql> show tables;
Empty set (0.00 sec)
# 执行脚本
mysql> source /root/nacos/conf/mysql-schema.sql
Query OK, 0 rows affected, 3 warnings (0.03 sec)

Query OK, 0 rows affected, 1 warning (0.02 sec)

Query OK, 0 rows affected, 4 warnings (0.01 sec)

Query OK, 0 rows affected, 9 warnings (0.01 sec)

Query OK, 0 rows affected, 4 warnings (0.02 sec)

Query OK, 0 rows affected, 9 warnings (0.01 sec)

Query OK, 0 rows affected, 5 warnings (0.01 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

mysql> show tables;
+----------------------+
| Tables_in_nacos      |
+----------------------+
| config_info          |
| config_info_gray     |
| config_tags_relation |
| group_capacity       |
| his_config_info      |
| permissions          |
| roles                |
| tenant_capacity      |
| tenant_info          |
| users                |
+----------------------+
10 rows in set (0.01 sec)

配置nacos

vim conf/application.properties
#修改下面这几个选项
spring.sql.init.platform=mysql

### Count of DB:
db.num=1

### Connect URL of DB:
db.url.0=jdbc:mysql://10.1.0.108:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=Asia/Shanghai
db.user.0=admin
db.password.0=111111

再次启动

bash  bin/startup.sh -m standalone
posted @ 2025-04-15 12:01  厚礼蝎  阅读(131)  评论(0)    收藏  举报