说明
nacos简单介绍:
nacos是分布式注册中心、分布式配置中心。
没有注册中心,我要调用其他服务,要怎么办?通过http调用,是不是需要知道对方的ip 端口?
但是如果对方是集群的呢?有多台机器,你咋办?你得把所有ip 端口都拿下来,然后选一台调用;
那如果对方扩容了,增加了一台机器或者释放了几台机器。调用方是不是要改代码?
有没有一种办法,通过一个中间件屏蔽掉这些,只对外提供一个服务名就行了;
注册中心客户端就保存了所有注册上去的服务实例,再配合负载均衡算法,路由到具体的一台服务实例,调用对方提供的接口就行了。
也不是每次都去注册中心找,客户端会有一份缓存,不然每次还要去注册中心找岂不是很慢,但是有关缓存又要保持心跳,健康检查。
为什么要分布式配置?还是一样的道理,我一个服务多台实例,如果没有一个分布式的配置中心。难道每台机器我都要写一份配置吗?
官网地址
https://nacos.io/zh-cn/docs/what-is-nacos.html
集群部署参考
https://nacos.io/zh-cn/docs/cluster-mode-quick-start.html
服务器规划
naocs1: 10.0.7.201
naocs2: 10.0.7.202
naocs3: 10.0.7.203
mysql+nginx: 10.0.7.205
部署mysql
docker-compose.yml
mysql5.6以上版本
version: '3'
services:
mysql:
image: mysql:5.7
container_name: mysql
restart: always
volumes:
- /data/mysql:/var/lib/mysql
- /etc/mysql/my.cnf:/etc/mysql/my.cnf
environment:
MYSQL_ROOT_PASSWORD: 123456cat /da
network_mode: host
command:
- --character-set-server=utf8mb4
- --collation-server=utf8mb4_unicode_ci
- --interactive_timeout=120
- --wait_timeout=120
security_opt:
- seccomp:unconfined
配置文件
[mysqld]
log-error=/var/lib/mysql/mysql-error.log
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
secure-file-priv= NULL
skip-name-resolve
wait_timeout=28800
interactive_timeout=28800
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
max_allowed_packet=500M
default_authentication_plugin=mysql_native_password
max_connections=500
建库
mysql -uroot -h'10.0.7.205' -p'wqrjljvouwerjol' -e "create database nacos"
部署nacos
以下三台naocs服务器部署配置一样
部署jdk
cd /usr/local/src/
tar xf jdk-8u261-linux-x64.tar.gz
mv jdk1.8.0_261/ /usr/local/jdk
cat >> /etc/profile <<eof
export JAVA_HOME=/usr/local/jdk
export PATH=.:$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/bin/lib/dt.jar:$JAVA_HOME/bin/lib/tools.jar
eof
source /etc/profile
java -version
下载解析nacos
cd /usr/local/src
wget -c https://github.com/alibaba/nacos/releases/download/1.3.0/nacos-server-1.3.0.tar.gz
tar xf nacos-server-1.3.0.tar.gz
mv nacos /usr/local
修改cluster.conf
vim /usr/local/nacos/conf/cluster.conf
10.0.7.201:8848
10.0.7.202:8848
10.0.7.203:8848
修改application.properties.example
cd /usr/local/nacos/conf
cp application.properties.example application.properties
vim application.properties.example
...
nacos.inetutils.ip-address=10.0.7.201 # 改为本机IP(不一样的地方)
...
#*************** Config Module Related Configurations ***************#
### If user MySQL as datasource:
spring.datasource.platform=mysql
### Count of DB:
db.num=1
### Connect URL of DB:
db.url.0=jdbc:mysql://10.0.7.205:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
db.user=root
db.password=wqrjljvouwerjol
...
执行sql文件
cd /usr/local/nacos/conf
[root@nacos1 conf]# mysql -uroot -h'10.0.7.205' -p'wqrjljvouwerjol'
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 8.0.27 MySQL Community Server - GPL
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> use nacos
Database changed
MySQL [nacos]> source nacos-mysql.sql;
Query OK, 0 rows affected, 3 warnings (1.71 sec)
Query OK, 0 rows affected, 3 warnings (0.84 sec)
Query OK, 0 rows affected, 3 warnings (1.01 sec)
Query OK, 0 rows affected, 3 warnings (0.88 sec)
Query OK, 0 rows affected, 4 warnings (1.09 sec)
Query OK, 0 rows affected, 9 warnings (1.10 sec)
Query OK, 0 rows affected, 4 warnings (1.33 sec)
Query OK, 0 rows affected, 9 warnings (0.89 sec)
Query OK, 0 rows affected, 5 warnings (1.02 sec)
Query OK, 0 rows affected (0.66 sec)
Query OK, 0 rows affected (0.62 sec)
Query OK, 0 rows affected (0.74 sec)
Query OK, 1 row affected (0.11 sec)
Query OK, 1 row affected (0.11 sec)
MySQL [nacos]>
改脚本启动nacos
-Xms2g -Xmx2g -Xmn1g改为以下
vim /usr/local/nacos/bin/startup.sh
94 JAVA_OPT="${JAVA_OPT} -server -Xms1g -Xmx1g -Xmn512m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m"
sh sh /usr/local/nacos/bin/startup.sh
部署nginx
cat > /etc/yum.repos.d/nginx.repo <<eof
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
eof
yum-config-manager --enable nginx-mainline
yum -y install nginx
cat > /etc/nginx/conf.d/nacos.conf <<eof
upstream nacos {
server 10.0.7.201:8848;
server 10.0.7.202:8848;
server 10.0.7.203:888;
}
server {
listen 80;
server_name 10.0.7.205;
location / {
proxy_pass http://nacos;
}
}
eof
nginx
访问nacos
http://10.0.7.205/nacos/#/login