top 是 linux 最常用的命令, 包括很多少直观的信息, 有利于我们对系统运行状态的把握.
top 使用 top 系统自带命令,可以直接使用.
top top 详情 a. 如图编号(1)
top - 10:34:07 up 16 min, 1 user, load average: 0.00, 0.01, 0.05 10:34:07 当前时间 up 16 min 系统运行时间, 如 16 分钟 1 user 当前登陆用户数 load average: 0.00, 0.01, 0.05 系统负载. 三个数值分别为 1分钟、5分钟、15分钟前到现在的平均值。 b. 如图编号(2)
Tasks: 206 total, 1 running, 205 sleeping, 0 stopped, 0 zombie Tasks: 206 total 进程总数 1 running 正在运行的进程数 205 sleeping 睡眠进程数 0 stopped 停止进程数 0 zombie 僵尸进程数 c.1. MySQL 并发参数调整 1.1. max_connections 参数 该参数设置mysql连接最大数量. max_connections 默认151个连接.
show variables like 'max_connections' 服务器性能优时可以调节这个参数, 范围: 500~1000
注 当连接过大时, 查看 Connection_errors_max_connections 参数是否大于0 , 表示连接过多, 错误连接
show status like 'Connection_errors_max_connections'; 2. back_log 参数 积压栈的大小.
也就是说当 mysql 连接超过 max_connections 连接数时, 如果back_log大小为0时, mysql将授予连接资源. 如果back_log大于零时,则接受多余的请求, 以等待某一连接释放.而等待的连接数大于back_log数时则也将不授予连接资源.
back_log默认大小: 50 + (max_connections/5), 最大可设置为900
show variables like 'back_log' 3. table_open_cache 该参数用来控制所有SQL语句执行线程可打开表缓存的数量.
最大数量设定: max_connections * N
show variables like 'table_open_cache' 4. thread_cache_size 该参数可控制 mysql缓存客户服务线程的数量, 相当于mysql的线程池, 也备重用.
show variables like 'thread_cache_size' 5.MySQL 运行的状态 重点关注以下参数
show status like 'Queries';
show status like 'Threads_connected';
show status like 'Threads_running';
show status like 'Connection_errors_max_connections'; MySQL 运行线程 show processlist 开启慢查询日志 一、参数查询
slow_query_log 开启慢查询
mysql> show variables like '%slow_query_log%'; +---------------------+--------------------------------------+ | Variable_name | Value | +---------------------+--------------------------------------+ | slow_query_log | ON | | slow_query_log_file | /var/lib/mysql/7709d56792f9-slow.log | +---------------------+--------------------------------------+ 2 rows in set (0.00 sec) set global slow_query_log=1; slow_query_log_file 慢日志存放位置
set global slow_query_log_file='/data/logs/slow-mysql.log'; long_query_time 表示1秒的SQL就记录
mysql> show variables like 'long_query_time'; +-----------------+-----------+ | Variable_name | Value | +-----------------+-----------+ | long_query_time | 10.systemd是Linux电脑操作系统之下的一套中央化系统及设置管理程序(init),包括有守护进程、程序库以及应用软件 在Unix中常以“d”作为系统守护进程(英语:daemon,亦称后台进程)的后缀标识
systemctl 命令的使用 以下以 redis 为例
启动 systemctl start redis 停止 systemctl stop redis 重启 systemctl restart redis 状态 systemctl status redis 刷新服务 systemctl daemon-reload ### 设置开机启动
systemctl enable redis 自定义配置 新建 systemctl 文件 以 redis 服务为例
touch /lib/systemd/system/redis.service
vim /lib/systemd/system/redis.service 配置文件 vim /lib/systemd/system/redis.service
[Unit]
Description=Redis
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
ExecReload=/usr/local/bin/redis-server -s reload
ExecStop=/usr/local/bin/redis-server -s stop
PrivateTmp=true
Type=simple
KillMode=process
Restart=on-failure
RestartSec=3s
[Install]
WantedBy=multi-user.target [Unit] 表示这是基础信息
Description 是描述 After 是在那个服务后面启动,一般是网络服务启动后启动 [Service] 表示这里是服务信息工欲善其事, 必先利其器
添加 Go MOD File->Settings->Go->Go Modules
填写 goproxy: GOPROXY=https://goproxy.cn,direct 结构体添加 tags File->Settings->Editor->Live Templates -> Go
复制一个 tag 新建一个 “gorm” 使用 保存时并格式代码 File->Settings->Tools->File Watchers
选择 go fmtgin 自带验证器 参考文档: https://godoc.org/gopkg.in/go-playground/validator.v8#hdr-Baked_In_Validators_and_Tags
gin 是个优秀的web框架, 集大成于一身. 对于参数的验证可以进行过滤. gin是引用了 go-playground框架, 今天我们来学习一下如何使用验证器.[TOC]
基于es 7.x版本
安装 1. 安装jdk1.8 浏览不同的es版本对java版本的要求: https://www.elastic.co/cn/support/matrix#matrix_jvm
elasticsearch 7以后自带 java jdk, 无需以下安装操作.
JDK1.8下载与安装
centos
yum -y install java-1.8.0-openjdk-devel.x86_64 ubuntu
apt-get -y install java-1.8.0-openjdk-devel.x86_64 安装完后查看java版本
java -version 2. 下载es v7.6.2 https://www.elastic.co/cn/downloads/elasticsearch 3. linux/win环境安装 目录概述
bin 执行文件目录 bin/elasticsearch.bat 双击安装(window) bin/elasticsearch (linux) 加个 -d 参数的话表示后台静默运行 config 配置目录 elasticsearch.yml es配置文件 jvm.options jdk配置文件 -Xms1g 表示使用1G内存 log4j2.properties 日志配置文件 data 数据目录 lib jar包目录 logs日志目录 modules 模块目录 plugins 插件目录 后台启动
./bin/elasticsearch -d -p /var/elasticsearch/es.pid 启动ElaticSearch
window: 双击bin/elasticsearch.bat 文件, 差不多需要1~2分钟, 注意屏幕不动, 敲个回车.前言 平时我们做Web开发, 经常会遇到需要请求网络资源,使用http请求, 如下面代码,注释处如果没有打开话,会导致句柄泄露, 最终报: dial tcp 127.0.0.1:80: socket: too many open files 这是为什么呢? 在linux中万物皆文件, 网络请求也相当于打开一个文件.如果打开文件忘记关闭的话, 没有及时回收资源, linux有文件打开上限,可以使用ulimit -n 查看最大支持文件打开数.
如下代码会导致句柄泄露 cli := &http.Client{}
req, err := http.NewRequest(http.MethodGet, "http://www.google.com", nil)
if err != nil {
return
}
resp, err := cli.Do(req)
if err != nil {
return
}
// 必须关闭, 如果我们没有写关闭resp.Body打开的句柄,就会导致句柄泄露
// defer resp.Body.Close() // data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
fmt.Println(string(data))
return 分析 可以使用并发工具请求你的代码, 如使用Jmeter, 然后使用lsof -p 18001 |wc -l , 18001就你程序的进程ID, 可以查看当前程序打开文件数.Benchmark 新建测试文件 util.go文件
func GetMd5V(s string) string {
h := md5.New()
h.Write([]byte(s))
return hex.EncodeToString(h.Sum(nil))
}
func SumMd5(s string) string {
data := []byte(s)
return fmt.Sprintf("%x", md5.Sum(data))
} 以_test结尾的文件, 如util_test.go
func BenchmarkSumMd5(b *testing.B) {
for i := 0; i < b.N; i++ {
SumMd5("1")
}
}
func BenchmarkGetMd5V(b *testing.B) {
for i := 0; i < b.N; i++ {
GetMd5V("1")
}
} 运行Benchmark函数 go test -bench="." -benchmem 输出结果 BenchmarkSumMd5-8 1719777 847 ns/op 64 B/op 3 allocs/op
BenchmarkGetMd5V-8 1861362 644 ns/op 184 B/op 5 allocs/op
PASS
ok openapi/app/util 4.前言 如果你的应用是一直运行的,比如 web 应用,那么可以使用 net/http/pprof 库,它能够在提供 HTTP 服务进行分析。而非一直运行的程序可以使用 runtime/pprof 库
可以先看下pprof入门
go1.10自带 go tool pprof工具
go version 查看golang版本
安装 引用包_ "net/http/pprof"
内置包的路径: net\http\pprof\pprof.go
const (
PProfPort = 6060 // 端口
)
func PprofServer() {
runtime.SetMutexProfileFraction(1) // 开启对锁调用的跟踪
runtime.SetBlockProfileRate(1) // 开启对阻塞操作的跟踪
go func() {
err := http.ListenAndServe(fmt.Sprintf(":%d", PProfPort), nil)
if err != nil {
zlog.Warn().Err(err).Msg("BootPprof")
}
}()
}
func main() {
PprofServer()
select{}
} 浏览器查看 allocs 查看内存分配详情 block 同步原语阻塞的堆栈跟踪 cmdline 当前程序运行的参数 goroutine 所有当前goroutines的堆栈跟踪 heap 活动对象的内存分配的抽样 mutex 争用互斥锁的持有者的堆栈跟踪 profile CPU配置文件, 还可以使用go tool pprof 查看某时间段的cpu情况, 并生成火焰图 go tool pprof http://localhost:6060/debug/pprof/profile?