zoukankan      html  css  js  c++  java
  • 在Linux实现nginx+Nacos(集群)+mysql

    Nacos在Linux上的集群配置

    之前用的老师一个Nacos配置中心,但在生产环境中是不可能一台的,肯定是集群。因为如果不是集群,那么如果一个炸了,注册到Nacos的服务集群将全部炸。所以我们必须要搞集群。

    有没有这样的疑惑,之前我们写的配置文件,都是什么存的?其实, 默认Nacos使用嵌入式数据库(derby数据库),实现数据存储。但是如果启动多个默认配置下的Nacos节点,每个nacos都有自己独立呃嵌入式数据库,存放的数据不一致。为了解决这个问题,Nacos采用了集中式存储的方式来支持集群化部署,目前只支持MySQL存储。

    以下步骤:window下配置 -> Linux生产级别使用

    window下配置

    即不使用Nacos,而使用Mysql数据库代替。需要有数据库,第二将数据库配置进去。

    1)创建数据库:在nacos -> conf -> nacos-mysql.sql 执行生成。

    2)nacos配置mysql数据库:在nacos -> conf -> application.properties 追加:

    spring.datasource.platform=mysql
    db.num=1
    db.url.0=jdbc:mysql://127.0.0.1:3306/nacos_config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
    db.user=root
    db.password=3333

    3)启动( bash startup.sh -m standalone )nacos,访问:http://127.0.0.1:8848/nacos/index.html 登录(nacos、nacos)后,发现原来的数据全部消失了。

    加入一条配置文件后,查看数据库中config_info表,发现数据录入了我们配置的mysql数据库中了。

     

    image-20210514003830209

    集群配置环境:我们在Linux上配置好Nacos后,需要在Nacos上配置Mysql,然后再进行集群配置,也就是都是修改Nacos的配置文件。

    以下步骤:在Linux上搭建Nacos -> 将Mysql配置进Nacos -> Nacos的集群环境的配置

    1)在Linux上搭建Nacos

    选择下载第一个:

    • 下载好后,移动到目标目录进行解压。

    2)将Mysql配置到 Nacos:

    • 创建数据库:nacos_config 执行根目录下conf > nacos-mysql.sql

    • 开始配置:将以下配置修改成你的,然后配置进Nacos根目录下的conf > application.properties“追加” :

      #mysql
      spring.datasource.platform=mysql
      db.num=1
      db.url.0=jdbc:mysql://127.0.0.1:3306/nacos_config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
      db.user=root
      db.password=3333

    3)Nacos的集群配置

    1. 说明:其它就只需要配置cluster.conf与startup.sh

    2. cluster.conf:

      • 获取自身ip地址

        vim cluster.conf

    3. 编辑启动文件 startup.sh

      • 加入以下内容:

        b

      4.启动:

      bash ./startup.sh  -p 3333 
      bash ./startup.sh -p 4444
      bash ./startup.sh -p 5555
      #查看启动个数,在根目录下
      ps -ef|grep nacos|grep -v grep|wc -l

    4)nginx的配置到集群系统

    nginx的安装:在Linux上:

    1. 下载:http://nginx.org/en/download.html

    2. 安装环境:

      Ubuntu:

      apt-get install gcc
      apt-get install libpcre3 libpcre3-dev
      apt-get install zlib1g zlib1g-dev
      # Ubuntu14.04的仓库中没有发现openssl-dev,由下面openssl和libssl-dev替代
      #apt-get install openssl openssl-dev
      sudo apt-get install openssl
      sudo apt-get install libssl-dev

      CentOS:

      yum -y install gcc pcre-devel zlib-devel openssl openssl-devel
    3. 安装

      需要说明一下,我们一共需要执行三个命令,执行位置是根目录, 我们安装分别是:

      解压后,将目录修改为别的目录,然后再创建一个目录nginx-1.20.0作为我们安装的目录。

      #指定安装目录
      ./configure --prefix=/usr/local/nginx/nginx-1.20.0
      #安装
      make
      make install
    4. 启动

      进行安装目录,即/usr/local/nginx/nginx-1.20.0下的sbin/下,执行:

      ./nginx

      #nginx的关闭:
      ./nginx -s stop
      ./nginx -s quit

      #测试命令:

      ./nginx -t

      显示以下,则代表成功:

      END

    5. 配置nginx  ,  nginx.conf修改为
      #user  nobody;
      worker_processes  1;
      
      #error_log  logs/error.log;
      #error_log  logs/error.log  notice;
      #error_log  logs/error.log  info;
      
      #pid        logs/nginx.pid;
      
      
      events {
          worker_connections  1024;
      }
      
      
      http {
          include       mime.types;
          default_type  application/octet-stream;
      
          #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
          #                  '$status $body_bytes_sent "$http_referer" '
          #                  '"$http_user_agent" "$http_x_forwarded_for"';
      
          #access_log  logs/access.log  main;
      
          sendfile        on;
          #tcp_nopush     on;
      
          #keepalive_timeout  0;
          keepalive_timeout  65;
      
          #gzip  on;
          upstream cluster{  #第三处配置
              server 127.0.0.1:3333;
          server 127.0.0.1:4444;
          server 127.0.0.1:5555;
          }
          server {
              listen       8888; #第一处配置
              server_name  localhost;
      
              #charset koi8-r;
      
              #access_log  logs/host.access.log  main;
      
              location / {
                  #root   html;
                  #index  index.html index.htm;
                  proxy_pass http://cluster; #第二处配置
              }
      
              #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   http://127.0.0.1;
              #}
      
              # 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;
              #}
          }
      
      
          # another virtual host using mix of IP-, name-, and port-based configuration
          #
          #server {
          #    listen       8000;
          #    listen       somename:8080;
          #    server_name  somename  alias  another.alias;
      
          #    location / {
          #        root   html;
          #        index  index.html index.htm;
          #    }
          #}
      
      
          # HTTPS server
          #
          #server {
          #    listen       443 ssl;
          #    server_name  localhost;
      
          #    ssl_certificate      cert.pem;
          #    ssl_certificate_key  cert.key;
      
          #    ssl_session_cache    shared:SSL:1m;
          #    ssl_session_timeout  5m;
      
          #    ssl_ciphers  HIGH:!aNULL:!MD5;
          #    ssl_prefer_server_ciphers  on;
      
          #    location / {
          #        root   html;
          #        index  index.html index.htm;
          #    }
          #}
      
      }

    5)测试

    如果上面已经按步骤启动Nacos,nginx.则可以直接进行以下测试:

    访问nginx:http://127.0.0.1:8888/nacos/#/login

    发现能可以进行nacos界面,登录进去,指向的是mysql.即实现了:

    img



  • 相关阅读:
    解决Ubuntu 18.04中文输入法的问题,安装搜狗拼音
    POJ 1151 Atlantis 矩形面积求交/线段树扫描线
    [CTF]思维导向图
    Ubuntu Linux 学习篇 配置DHCP服务器
    Ubuntu Linux 学习篇 配置DNS服务器
    Ubuntu Linux DNS服务器 BIND9配置文件命令介绍
    Ubuntu Linux 学习篇 配置DHCP服务器
    Ubuntu Linux 学习篇 配置DNS服务器
    Ubuntu Linux DNS服务器 BIND9配置文件命令介绍
    随记
  • 原文地址:https://www.cnblogs.com/zjazn/p/14781996.html
Copyright © 2011-2022 走看看