# 1. 简介
Nacos是阿里巴巴的一个开源项目,帮助实现服务的动态发现、配置和管理。
# 2. 源码编译
# 2.1 版本说明
不同springboot和springcloud下载的nacos版本不同,以下是阿里针对不同版本的说明:
每个 Spring Cloud Alibaba 版本及其自身所适配的各组件对应版本
Spring Cloud Alibaba Version | Sentinel Version | Nacos Version | RocketMQ Version | Dubbo Version | Seata Version |
---|---|---|---|---|---|
2021.0.1.0 | 1.8.3 | 1.4.2 | 4.9.2 | 2.7.15 | 1.4.2 |
2.2.7.RELEASE | 1.8.1 | 2.0.3 | 4.6.1 | 2.7.13 | 1.3.0 |
2.2.6.RELEASE | 1.8.1 | 1.4.2 | 4.4.0 | 2.7.8 | 1.3.0 |
2021.1 or 2.2.5.RELEASE or 2.1.4.RELEASE or 2.0.4.RELEASE | 1.8.0 | 1.4.1 | 4.4.0 | 2.7.8 | 1.3.0 |
2.2.3.RELEASE or 2.1.3.RELEASE or 2.0.3.RELEASE | 1.8.0 | 1.3.3 | 4.4.0 | 2.7.8 | 1.3.0 |
2.2.1.RELEASE or 2.1.2.RELEASE or 2.0.2.RELEASE | 1.7.1 | 1.2.1 | 4.4.0 | 2.7.6 | 1.2.0 |
2.2.0.RELEASE | 1.7.1 | 1.1.4 | 4.4.0 | 2.7.4.1 | 1.0.0 |
2.1.1.RELEASE or 2.0.1.RELEASE or 1.5.1.RELEASE | 1.7.0 | 1.1.4 | 4.4.0 | 2.7.3 | 0.9.0 |
2.1.0.RELEASE or 2.0.0.RELEASE or 1.5.0.RELEASE | 1.6.3 | 1.1.1 | 4.4.0 | 2.7.3 | 0.7.1 |
下表为按时间顺序发布的 Spring Cloud Alibaba 以及对应的适配 Spring Cloud 和 Spring Boot 版本关系(由于 Spring Cloud 版本命名有调整,所以对应的 Spring Cloud Alibaba 版本号也做了对应变化)
Spring Cloud Alibaba Version | Spring Cloud Version | Spring Boot Version |
---|---|---|
2021.0.1.0 | Spring Cloud 2021.0.1 | 2.6.3 |
2.2.7.RELEASE | Spring Cloud Hoxton.SR12 | 2.3.12.RELEASE |
2021.1 | Spring Cloud 2020.0.1 | 2.4.2 |
2.2.6.RELEASE | Spring Cloud Hoxton.SR9 | 2.3.2.RELEASE |
2.1.4.RELEASE | Spring Cloud Greenwich.SR6 | 2.1.13.RELEASE |
2.2.1.RELEASE | Spring Cloud Hoxton.SR3 | 2.2.5.RELEASE |
2.2.0.RELEASE | Spring Cloud Hoxton.RELEASE | 2.2.X.RELEASE |
2.1.2.RELEASE | Spring Cloud Greenwich | 2.1.X.RELEASE |
2.0.4.RELEASE(停止维护,建议升级) | Spring Cloud Finchley | 2.0.X.RELEASE |
1.5.1.RELEASE(停止维护,建议升级) | Spring Cloud Edgware | 1.5.X.RELEASE |
# 2.2 源码下载
git clone [https://github.com/alibaba/nacos.git](https://github.com/alibaba/nacos.git) -b feature_multiple_datasource_support
目前nacos默认持久化方案为derby、mysql,为适配不同数据库,需要我们手动扩展支持的数据库。Nacos官方曾新建分支(feature_multiple_datasource_support)维护适配其他数据库,基于Spring Data JPA实现,Nacos自2021年6月以后不再对该分支进行维护,但nacos 1.4.2的版本基本可以满足日常需求。
# 2.3 源码编译
mvn -Prelease-nacos -Dmaven.test.skip=true apache-rat:check -Drat.numUnapprovedLicenses=600 clean install -U
# 2.4 数据库初始化
编译成功后,会在distribution/target目录输出 nacos-server-1.4.2-SNAPSHOT.zip,解压后nacos/conf目录下存有mysql、postgre、oracle的数据库建表脚本, 根据实际情况初始化数据库即可。
# 2.5 修改配置
根据数据库配置信息,修改conf目录下的application.properties文件。
mysql
spring.datasource.platform=mysql
nacos.datasource.type=mysql
nacos.datasource.relational.dsList[0].url=jdbc:mysql://localhost:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
nacos.datasource.relational.dsList[0].username=root
nacos.datasource.relational.dsList[0].password=123456
nacos.datasource.relational.dsList[0].driver-class-name=com.mysql.jdbc.Driver
2
3
4
5
6
postgres
spring.datasource.platform=postgresql
nacos.datasource.type=postgresql
nacos.datasource.relational.dsList[0].url=jdbc:postgresql://10.0.0.1:5432/nacos
nacos.datasource.relational.dsList[0].username=postgres
nacos.datasource.relational.dsList[0].password=123456
nacos.datasource.relational.dsList[0].driver-class-name=org.postgresql.Driver
2
3
4
5
6
# 3. 启动服务
将 nacos-server-1.4.2-SNAPSHOT.zip 文件上传至 服务器 /opt 目录下,并解压。
cd /opt
unzip nacos-server-1.4.2-SNAPSHOT.zip
修改配置文件后执行启动脚本。linux下启动单机模式:
sh /opt/nacos/bin/startup.sh -m standalone
# 4. 微服务改造
# 4.1 Nacos添加配置文件
# 4.2 添加maven依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.2.5.RELEASE</version>
</dependency>
2
3
4
5
6
7
8
9
10
11
版本与上述版本说明保持一致
# 4.3 服务配置文件
整合Nacos,需要将之前的application.properties/application.yml,改为bootstrap.properties/bootstrap.yml,添加服务注册和配置地址
spring:
cloud:
nacos:
config:
# 配置地址
server-addr: 192.168.1.11:8488
# 分组
group: USERCENTER
# Data Id
name: auth-security-nacos
# 配置格式
file-extension: yaml
# 命名空间
namespace: 79ffc0ed-970a-46c7-a23f-796f29c10892
discovery:
server-addr: 192.168.1.11:8488
group: USERCENTER
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
启动类中添加@EnableDiscoveryClient
注解
项目若使用Eureka注册中心,需排除对应自动配置类
spring:
autoconfigure:
exclude: org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration
eureka:
client:
enabled: false
healthcheck:
enabled: false
2
3
4
5
6
7
8
# 4.4 配置动态加载
在Nacos配置好后,服务端添加@RefreshScope
注解,实现动态加载配置
# 5. 容器化
修改代码后在Dockerfile所在目录下执行构建镜像命令(nacos-server)
docker build -t nacos-server-hq:1.4.2
# 5.1 参数说明
参数 | 描述 | 备注 |
---|---|---|
MODE | cluster模式/standalone模式 | cluster/standalone default cluster |
NACOS_SERVERS | nacos cluster地址 | eg. ip1,ip2,ip3 |
PREFER_HOST_MODE | 是否支持hostname | hostname/ip default ip |
NACOS_SERVER_PORT | nacos服务器端口 | default 8848 |
NACOS_SERVER_IP | 多网卡下的自定义nacos服务器IP | |
SPRING_DATASOURCE_PLATFORM | standalone 支持数据库类型 | mysql/postgresql 默认mysql |
JVM_XMS | -Xms | default :2g |
JVM_XMX | -Xmx | default :2g |
JVM_XMN | -Xmn | default :1g |
JVM_MS | -XX:MetaspaceSize | default :128m |
JVM_MMS | -XX:MaxMetaspaceSize | default :320m |
NACOS_DEBUG | 开启远程调试 y/n | default :n |
TOMCAT_ACCESSLOG_ENABLED | server.tomcat.accesslog.enabled | default :false |
JPA_SHOW_SQL | 是否显示sql语句 | default :false |
MYSQL_SERVICE_HOST | standalone mysql主机地址 | |
MYSQL_SERVICE_PORT | standalone mysql端口 | default :3306 |
MYSQL_SERVICE_USER | standalone mysql用户名 | |
MYSQL_SERVICE_PASSWORD | standalone mysql密码 | |
MYSQL_SERVICE_DB_PARAM | myql数据库连接参数 | default :characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC |
PG_SERVICE_HOST | standalone postgre主机地址 | |
PG_SERVICE_PORT | standalone postgre端口号 | default :5432 |
PG_SERVICE_DB_NAME | standalone postgre数据库名 | |
PG_SERVICE_DB_PARAM | standalone postgre启动参数 | |
PG_SERVICE_USER | standalone postgre用户名 | |
PG_SERVICE_PASSWORD | standalone postgre密码 | |
PG_SERVICE_DB_PARAM | postgre数据库连接参数 | default :currentSchema=public |
NACOS_ACTIVE | standalone spring.profiles.active的值 |
# 5.2 调用示例
mysql
docker run -d -e MODE=standalone --net=bridge -e SPRING_DATASOURCE_PLATFORM=mysql \
-e MYSQL_SERVICE_HOST=localhost \
-e MYSQL_SERVICE_PORT=13001 \
-e MYSQL_SERVICE_USER=root \
-e MYSQL_SERVICE_PASSWORD=hq@auth \
-e MYSQL_SERVICE_DB_NAME=nacos -e NACOS_ACTIVE=mysql\
-p 8848:8848 --restart=always --name nacos-server \
-v /opt/nacos/logs/:/home/nacos/logs \
--privileged=true nacos-server-hq:1.4.2
2
3
4
5
6
7
8
9
postgre
docker run -d -e MODE=standalone --net=bridge -e SPRING_DATASOURCE_PLATFORM=mysql \
-e PG_SERVICE_HOST=172.18.1.248 \
-e PG_SERVICE_PORT=24359 \
-e PG_SERVICE_USER=postgres \
-e PG_SERVICE_PASSWORD=apegeek@uav \
-e PG_SERVICE_DB_NAME=nacos -e NACOS_ACTIVE=pg\
-p 8848:8848 --restart=always --name nacos-server \
-v /opt/nacos/logs/:/home/nacos/logs \
--privileged=true nacos-server-hq:1.4.2
2
3
4
5
6
7
8
9
# 6. 链接
# 6.1 代码
源码地址:https://github.com/alibaba/nacos (opens new window)
# 6.2 Nacos-Server
数据库类型 | 地址 | 用户名/密码 |
---|---|---|
Postgre | http://172.18.1.248:9875/nacos | nacos/123456 pg/pg_0224 |
MySQL | http://172.18.1.248:9876/nacos | nacos/nacos |
# 6.3 官方文档
https://nacos.io/zh-cn/docs/deployment.html (opens new window)
# 7. 常见异常
# 7.1 start.sh 启动异常
一般为编码、文件格式错误。
sed -i 's/\r$//' startup.sh