1.linux下安装nacos集群
需要准备的安装包: jdk 、mysql、nginx、nacos
百度 云盘下载地址
链接:
提取码:tgui
2.安装步骤
2.1 配置jdk环境变量
第一步:解压jdk包
tar -zxvf jdk-8u221-linux-x64.tar.gz
第二步:移动解压后的目录到/usr/local
mv jdk-8u221-linux-x64 /usr/local
第三步:配置环境jdk环境
[ root @iZsi4mao063eysZ /]# vim /etc/profile在末尾增加如下两行 JAVA _HOME=/usr/local/bin/jdk1.8/jdk1.8.0_261 PATH=/usr/local/bin/jdk1.8/jdk1.8.0_261/bin:$PATH export JAVA_HOME PATHesc shift+: wq保存退出
source命令通常用于重新执行刚修改的初始化文件
[root@iZsi4mao063eysZ /]# source /etc/profile
测试输入java -version 可看到对应的版本号
2.2 linux安装5.7mysql 建议安装在/usr/local/mysql
默认安装目录: /usr/local/mysql
数据文件目录: /usr/local/mysql/data
MySQL占用端口:3306
默认 socket 文件存放路径:/tmp/mysql.sock 用于客户端与服务端通信得套接字文件
第一步:上传 软件包 到 linux 操作系统
第二步:对mysql压缩包进行解压操作
[root@iZsi4mao063eysZ /]tar -zxvf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
第三步:移动mysql文件夹到/usr/local目录下并更名为mysql
#mv mysql-5.7.26-linux-glibc2.12-x86_64/* /usr/local/mysql
第四步:创建一个mysql用户并更改/usr/local/mysql目录的权限(用户和组)
#useradd -r -s /sbin/nologin mysql
#没有指定用户组 会自定创建同名的用户组 id mysql可以查看到
#更改权限 chown -R mysql.mysql /usr/local/mysql
#查看ll /usr/local/mysql
查看/etc/my.cnf是否存在,不存在则创建加入以下配置
[mysqld]character_set_server=utf8mb4basedir=/usr/local/mysqldatadir=/usr/local/mysql/data/socket=/tmp/mysql.sock
第五步:初始化数据库 需要进入/usr/local/mysql/bin
#./mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
第七步:移动support-files目录下的mysql.server脚本到/etc/init.d目录一份 可直接使用service命令
#cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
赋予执行权限:chmod +x /etc/init.d/mysql
第八步:启动mysql脚本
#service mysql start
第九步:设置密码并测试 mysql数据库
#bin/mysql -uroot -p 输入密码测试是否成功set password=password("要自定义的密码");flush privileges;刷新让密码生效远程连接出现1130 修改帐号不允许从 远程登陆 ,只能在localhost。更改 "mysql" 数据库里的 "user" 表里的 "host" 项,从"localhost"改称"%"select Host, User from user;update user set Host='%' where User='root';flush privileges;
3.linux安装nginx
第一步:解压
tar -zxvf nginx.tar.gz
第二步: 移动解压后的文件到指定/usr/local
mv nginx /usr/local
第三步:执行编译文件 进入 nginx 目录
./configure --prefix=/usr/local/nginx如果出现警告或报错提示:./configure: error: the HTTP rewrite ...... --with-pcre=<path> option.安装perl库yum install -y pcre pcre-devel提示:./configure: error: the HTTP gzip ...... --with-zlib=<path> option.安装zlib库yum install -y zlib zlib-devel 在执行 ./configure --prefix=/usr/local/nginx
第四步:安装
make install
第五步:启动 进入 cd /usr/local/nginx/sbin目录下启动
./nginx -c /usr/local/nginx/conf/nginx.conf
4.nacos集群配置
4.1解压nacos
tar -zxvf nacos-server-2.0.2.tar.gz
4.2移动文件到usr/local文件夹下
mv nacos-server-2.0.2 /usr/local
4.3持久化数据库创建
进入nacos的conf目录找到nacos-mysql.sql文件,复制所有内容在mysql执行
4.4在conf的application.properties文件中添加如下配置
spring.datasource.platform=mysqldb.num=1db.url.0= jdbc :mysql:// 127.0.0.1 :3306/nacos_config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTCdb.user=rootdb.password=123456
4.5修改conf下的cluster.conf文件
[root@localhost conf]# hostname -i127.0.0.1 192.168.137.130
下面的ip地址通过hostname -i查询获得,如上
192.168.137.130:3333192.168.137.130:4444192.168.137.130:5555
4.6进入bin目录修改startup.sh文件
原文件
while getopts ":m:f:s:c:" optdo case $opt in m) MODE=$OPTARG;; f) FUNCTION _MODE=$OPTARG;; s) SERVER=$OPTARG;; c) MEMBER_LIST=$OPTARG;; ?) echo "Unknown parameter" exit 1;; esacdoneecho "$JAVA ${JAVA_OPT}" > ${BASE_DIR}/logs/start.out 2>&1 &nohup $JAVA${JAVA_OPT} nacos.nacos >> ${BASE_DIR}/logs/start.out 2>&1 &
修改后的文件
while getopts ":m:f:s:c:p:" optdo case $opt in m) MODE=$OPTARG;; f) FUNCTION_MODE=$OPTARG;; s) SERVER=$OPTARG;; c) MEMBER_LIST=$OPTARG;; p) PORT=$OPTARG;; ?) echo "Unknown parameter" exit 1;; esacdoneecho "$JAVA ${JAVA_OPT}" > ${BASE_DIR}/logs/start.out 2>&1 &nohup $JAVA -Dserver.port=${PORT} ${JAVA_OPT} nacos.nacos >> ${BASE_DIR}/logs/start.out 2>&1 &
4.7启动nacos集群,进入/usr/local/nacos/bin目录下
./startup.sh -p 3333./startup.sh -p 4444./startup.sh -p 5555
5.配置nginx文件
5.1找到conf下的nginx.conf文件
原文件
server { listen 1111; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME / script s$fastcgi_script_name; # include fastcgi_params; #} # deny access to . htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} }
修改后的文件
upstream cluster{ server 127.0.0.1:3333; server 127.0.0.1:4444; server 127.0.0.1:5555; } server { listen 1111; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { #root html; #index index.html index.htm; proxy_pass } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} }
5.2启动nginx,进入nginx/sbin目录执行下面命令
./nginx -c /usr/local/nginx/conf/nginx.conf
最后访问测试:192.168.137.130:1111/nacos/#/login成功进入nacos界面
发布一条配置信息
数据库查看,出现该条信息则表示成功
6.微服务测试,修改之前就创建的cloudalibaba-provider-payment9002的yml配置文件后,启动
server: port: 9002spring: application: name: nacos-payment-provider cloud: nacos: discovery:# server-addr: localhost:8848 server-addr: 192.168.137.130:1111management: endpoints: web: exposure: include: '*'
查看到如下,表示注册成功