Tars--------企业级入门实践篇

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Tars--------企业级入门实践篇相关的知识,希望对你有一定的参考价值。

背景

上一篇详细介绍了Tars的背景、优势与架构等信息,本篇继续介绍Tars安装过程。
注:本篇采用CentOS 7(7.6)系统

环境[关闭selinux和清空防火墙规则]

[[email protected] ~]# cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core) 
[[email protected] ~]# uname -r
3.10.0-957.5.1.el7.x86_64
[[email protected] ~]# hostname
lisea
[[email protected] ~]# getenforce 
Disabled
[[email protected] ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
[[email protected] ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.21.0.15  netmask 255.255.240.0  broadcast 172.21.15.255
        ether 52:54:00:09:b7:6e  txqueuelen 1000  (Ethernet)
        RX packets 9648  bytes 9083460 (8.6 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 5327  bytes 524204 (511.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 2  bytes 272 (272.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2  bytes 272 (272.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

依赖准备

[[email protected] ~]# yum install gcc gcc-c++ -y
[[email protected] ~]# yum install bison -y
[[email protected] ~]# yum install flex -y
[[email protected] ~]# yum install cmake -y
[[email protected] ~]# yum install ncurses-devel -y 
[[email protected] ~]# yum install zlib-devel -y
[[email protected] ~]# yum install glibc-devel -y
[[email protected] ~]# yum install numactl -y
[[email protected] ~]# yum install git -y

安装mysql

这里推荐mysql5.6或者5.7版本的二进制包安装方式。
本次采用5.7版本安装,MySQL5.6安装方式查看历史MySQL安装篇幅。
经测试,在安装8.0版本的MySQL,在后续无法成功编译TarsFramework。

  • 切换工作路径至/usr/local/src
    [[email protected] ~]# cd /usr/local/src/
  • 下载MySQL
    [[email protected] src]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.25-linux-glibc2.12-x86_64.tar
  • 解压 MySQL 5.7 二进制包
    [[email protected] src]# tar xf mysql-5.7.25-linux-glibc2.12-x86_64.tar
    [[email protected] src]# tar xf mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz
  • 移动 解压好的MySQL 5.7目录至/usr/local
    [[email protected] src]# mv mysql-5.7.25-linux-glibc2.12-x86_64 /usr/local
  • 创建MySQL软连接
    [[email protected] src]# ln -s /usr/local/mysql-5.7.25-linux-glibc2.12-x86_64 /usr/local/mysql
  • 创建 mysql 应用运行用户
    [[email protected] src]# useradd -r -s /sbin/nologin mysql
  • 创建 MySQL 数据存放目录
    [[email protected] src]# mkdir -vp /data/mysql_data
    mkdir: created directory `/data‘
    mkdir: created directory `/data/mysql_data‘
  • 在 MySQL 二进制包目录中创建 mysql-files 目录 [MySQL 数据导入/导出数据专放目录]
    [[email protected] src]# mkdir -v /usr/local/mysql/mysql-files
    mkdir: created directory `/usr/local/mysql/mysql-files‘
  • 修改 MySQL 二进制包目录的所属用户与所属组
    [[email protected] src]# chown root.mysql -R /usr/local/mysql-5.7.25-linux-glibc2.12-x86_64
  • 修改 MySQL 数据目录与 数据导入/导出专放目录的所属用户与所属组
    [[email protected] src]# chown mysql.mysql /usr/local/mysql/mysql-files /data/mysql_data
  • 重命名不使用系统自带 MySQL 配置文件 /etc/my.cnf [ debian类系统在 /etc/mysql/my.cnf ]
    [[email protected] src]# mv /etc/my.cnf{,.old}
  • 初始化
    [[email protected] src]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql_data
    2019-02-03T13:56:34.415131Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
    2019-02-03T13:56:35.563289Z 0 [Warning] InnoDB: New log files created, LSN=45790
    2019-02-03T13:56:35.835511Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
    2019-02-03T13:56:35.915147Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 853b8efd-27bb-11e9-9c8d-52540009b76e.
    2019-02-03T13:56:35.925540Z 0 [Warning] Gtid table is not ready to be used. Table ‘mysql.gtid_executed‘ cannot be opened.
    2019-02-03T13:56:35.926081Z 1 [Note] A temporary password is generated for [email protected]: wKoMdt7<yNor
  • 开启 SSL 连接[可选]

    [[email protected] src]# /usr/local/mysql/bin/mysql_ssl_rsa_setup -d /data/mysql_data
  • 修改SSL证书文件所属用户和用户组

    [[email protected] src]# chown mysql.mysql -R /data/mysql_data
  • 创建mysql systemctl启动脚本[由于博客markdown语法支持问题,无法展示脚本内容,参考MySQL8.0启动脚本]
    [[email protected] src]# cat /usr/lib/systemd/system/mysqld.service
  • systemd加载启动配置文件
    [[email protected] src]#  systemctl daemon-reload
  • 创建MySQL配置文件my.cnf, 指定数据目录
    [[email protected] src]# cat /etc/my.cnf
    [mysqld]
    user = mysql
    bind_address = 0.0.0.0
    character_set_server=utf8mb4
    skip_name_resolve = 1
    max_connections = 800
    max_connect_errors = 1000
    datadir = /data/mysql_data
  • 启动MySQL服务
    [ro[email protected] src]# systemctl start mysqld
  • 添加MySQL bin目录至path变量,并生效环境变量
    
    [[email protected] src]# cat /etc/profile.d/mysql.sh 
    export MYSQL_HOME=/usr/local/mysql
    export PATH=$MYSQL_HOME/bin:$PATH

[[email protected] src]# source /etc/profile

* 通过mysql命令连接mysql服务,并修改默认密码(本次修改为123), 默认密码为初始化时的随机密码
```bash
[[email protected] src]# mysql -uroot -p‘wKoMdt7<yNor‘
mysql> set password = ‘123‘;
Query OK, 0 rows affected (0.00 sec)

tars安装

  • 下载TarsFramework源码
    [[email protected] src]# git clone https://github.com/TarsCloud/TarsFramework.git
    Cloning into ‘TarsFramework‘...
    remote: Enumerating objects: 20, done.
    remote: Counting objects: 100% (20/20), done.
    remote: Compressing objects: 100% (16/16), done.
    remote: Total 465 (delta 6), reused 14 (delta 4), pack-reused 445
    Receiving objects: 100% (465/465), 553.49 KiB | 192.00 KiB/s, done.
    Resolving deltas: 100% (174/174), done.
  • TarsFramework编译
    [[email protected] src]# cd TarsFramework/build/
    [[email protected] build]# chmod u+x build.sh
    [[email protected] build]# ./build.sh prepare
    [[email protected] build]# ./build.sh all

tars数据库初始化

  • 注意将${your machine ip}改为部署机器的IP
  • 如果exec-sql.sh脚本执行出错,需要脚本里修改数据库用户名root对应的密码
  • 脚本执行后,会创建3个数据库,分别是db_tars、tars_stat、tars_property。
    • db_tars是框架运行依赖的核心数据库,里面包括了服务部署信息、服务模版信息、服务配置信息等等;
    • tars_stat是服务监控数据存储的数据库;
    • tars_property是服务属性监控数据存储的数据库;
  • 添加数据库用户
    mysql> grant all on *.* to ‘tars‘@‘%‘ identified by ‘tars2015‘ with grant option;
    Query OK, 0 rows affected, 1 warning (0.08 sec)
    mysql> grant all on *.* to ‘tars‘@‘localhost‘ identified by ‘tars2015‘ with grant option;
    Query OK, 0 rows affected, 2 warnings (0.00 sec)
    mysql> grant all on *.* to ‘tars‘@‘172.21.0.15‘ identified by ‘tars2015‘ with grant option;         
    Query OK, 0 rows affected, 1 warning (0.00 sec)
  • 修改部署数据库的脚本ip信息[sql脚本在framework/sql目录下,修改部署的ip信息]
    注:172.21.0.15是测试环境地址,修改成你们本地地址即可。
    [[email protected] build]# cd ../sql
    [[email protected] sql]# sed -i "s/192.168.2.131/172.21.0.15/g" `grep 192.168.2.131 -rl ./*`                  
    [[email protected] sql]# sed -i "s/db.tars.com/172.21.0.15/g" `grep db.tars.com -rl ./*`                  
    [[email protected] sql]# sed -i "s/10.120.129.226/172.21.0.15/g" `grep 10.120.129.226 -rl ./*`
  • 修改exec-sql.sh文件数据库root密码
    [[email protected] sql]# cat exec-sql.sh 
    mysql -uroot -p123 -e "create database db_tars"
    mysql -uroot -p123 -e "create database tars_stat"
    mysql -uroot -p123 -e "create database tars_property"
    mysql -uroot -p123 -e "create database db_tars_web"
    mysql -uroot -p123 db_tars < db_tars.sql
  • 创建数据库
    [[email protected] sql]# chmod u+x exec-sql.sh
    [[email protected] sql]# ./exec-sql.sh
    mysql: [Warning] Using a password on the command line interface can be insecure.
    mysql: [Warning] Using a password on the command line interface can be insecure.
    mysql: [Warning] Using a password on the command line interface can be insecure.
    mysql: [Warning] Using a password on the command line interface can be insecure.
    mysql: [Warning] Using a password on the command line interface can be insecure.

tars框架运行环境搭建

框架服务的安装分两种:

  • 一种是核心基础服务(必须的),必须手工部署的,
    • tarsAdminRegistry
    • tarsregistry
    • tarsnode
    • tarsconfig
    • tarspatch
  • 一种是普通基础服务(可选的),可以通过管理平台发布的(和普通服务一样)。
    • tarsstat
    • tarsproperty
    • tarsnotify
    • tarslog
    • tarsquerystat
    • tarsqueryproperty
  • 准备核心基础服务安装包
    会在当前目录生成framework.tgz 包 这个包包含了 tarsAdminRegistry, tarsregistry, tarsnode, tarsconfig, tarspatch 部署相关的文件
    [[email protected] sql]# cd ../build/
    [[email protected] build]# make framework-tar
  • 普通基础服务安装包准备[可选]
    生成的发布包,在管理平台部署发布完成后
    [[email protected] build]# make tarsstat-tar
    [[email protected] build]# make tarsnotify-tar
    [[email protected] build]# make tarsproperty-tar
    [[email protected] build]# make tarslog-tar
    [[email protected] build]# make tarsquerystat-tar
    [[email protected] build]# make tarsqueryproperty-tar

    安装核心基础服务

  • 创建基础服务的工作目录
    [[email protected] build]# mkdir -vp /usr/local/app/tars
    mkdir: created directory ‘/usr/local/app’
    mkdir: created directory ‘/usr/local/app/tars’
  • 将已打好的框架服务包复制到/usr/local/app/tars/,并解压
    [[email protected] build]# cp framework.tgz /usr/local/app/tars/
    [[email protected] build]# cd /usr/local/app/tars
    [[email protected] tars]# tar xf /usr/local/app/tars/framework.tgz
  • 修改各个服务对应conf目录下配置文件,注意将配置文件中的ip地址修改为本机ip地址

    [[email protected] tars]# cd /usr/local/app/tars
    [[email protected] tars]# sed -i "s/192.168.2.131/172.21.0.15/g" `grep 192.168.2.131 -rl ./*`                  
    [[email protected] tars]# sed -i "s/db.tars.com/172.21.0.15/g" `grep db.tars.com -rl ./*`                               
    [[email protected] tars]# sed -i "s/registry.tars.com/172.21.0.15/g" `grep registry.tars.com -rl ./*`                  
    [[email protected] tars]# sed -i "s/web.tars.com/172.21.0.15/g" `grep web.tars.com -rl ./*` 
  • 启动tars框架服务
    [[email protected] tars]# chmod u+x tars_install.sh 
    [[email protected] tars]# ./tars_install.sh
  • 部署管理平台并启动web管理平台(管理平台必须和tarspatch部署在同一台服务器上)部署tarspatch
    看看rsync进程是否起来了,若没有看看rsync使用的配置中的ip是否正确(即把web.tars.com替换成本机ip)
    [[email protected] tars]# tarspatch/util/init.sh
  • tarsnode配置crontab监控[可选]
    * * * * * /usr/local/app/tars/tarsnode/util/monitor.sh

    安装web管理系统

  • 下载tars-web
    [[email protected] tars]# cd /usr/local/app
    [[email protected] app]# git clone https://github.com/TarsCloud/TarsWeb.git
  • 修改配置文件,将配置文件中的ip地址修改为本机ip(172.21.0.15)地址
    [[email protected] app]# cd TarsWeb/
    [[email protected] TarsWeb]# sed -i ‘s/db.tars.com/172.21.0.15/g‘ config/webConf.js                  
    [[email protected] TarsWeb]# sed -i ‘s/registry.tars.com/172.21.0.15/g‘ config/tars.conf
  • 安装npm环境
    [[email protected] TarsWeb]# wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
    [[email protected] TarsWeb]# source ~/.bashrc
    [[email protected] TarsWeb]# nvm install v8.11.3
    [[email protected] TarsWeb]# npm install -g pm2 --registry=https://registry.npm.taobao.org
  • 安装web管理页面依赖, 创建日志目录,启动web
    [[email protected] TarsWeb]# npm install --registry=https://registry.npm.taobao.org
    [root[email protected] TarsWeb]# mkdir -p /data/log/tars
    [[email protected] TarsWeb]# npm run prd

访问站点 浏览器输入 ip:3000

技术图片

以上是关于Tars--------企业级入门实践篇的主要内容,如果未能解决你的问题,请参考以下文章

开源微服务TARS接口测试揭秘

腾讯高性能 RPC 开发框架

腾讯推出高性能 RPC 开发框架

开源项目:腾讯推出高性能 RPC 开发框架

Dubbo注意:腾讯推出高性能 RPC 开发框架

基于开源Tars的动态负载均衡实践