工欲善其事, 必先利其器
添加 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?安装 参考 https://blog.csdn.net/pengjiangchun/article/details/105419683
使用 切换成中文 新建"线程组" 配置"线程组" 设置Cookie Http请求 Http断言 填写请求的URl 填写报告 参考 https://zhuanlan.zhihu.com/p/142897766 https://blog.csdn.net/pengjiangchun/article/details/105419683select 用于chan通道专用的控制结构
ch := make(chan bool)
select {
case c <- ch:
fmt.Println("hello world")
default:
return
} 使用误区 39行, return 会一直阻塞? 希望大神解释下? 答: return相当于返回,不再继续,将永远阻塞,直到主程序退出.一般用于结束这个goroutine才加上return,否则不要轻易使用. 如果return 换成break或continue就不会阻塞 package main
import (
"fmt"
"github.com/gin-gonic/gin"
"log"
"math/rand"
"sync"
)
type Cache struct {
ch chan int
}
var (
_cache *Cache
_once sync.Once
)
func NewCache() *Cache {
_once.Do(func() {
_cache = &Cache{
ch: make(chan int, 10),
}
_cache.monitor()
})
return _cache
}
func (c *Cache) Push(x int) {
c.运算符 算术运算符 八种运算符号 +, -, *, /, %, =, ==, !=
采用此表达式: `expr $a + $b` 计算加法, 其它类似
数字判断相等: [ $a == $b ] 数字判断不相等: [ $a != $b ] 乘号比较特殊: val=`expr $a \* $b` 需要加\才能运算
不使用expr, 可以使用$((表达式))代替
例: a=10
b=20
val=`expr $a + $b`
echo "a + b : $val" 关系运算符 关系运算符只支持数字, 不支持字符串,除非字符串是数字
六种运算符号
-eq 判断左边等于右边
-ne 判断左边不相等右边
-gt 判断左边大于右边
-lt 判断左边小于右边
-ge 判断左边大于等于右边
-le 判断左边小于等于右边
例: a=10
b=20
if [ $a -eq $b ]; then
echo "a与b相等"
else
echo "a与b不相等"
fi 布尔运算符 共三种符号: !介绍 supervisor是python写的一个进程守护工具,非常实用,时时监听你的进程是否正常, 发现异常自动启动, 生产环境必备软件.
安装 CentOS Yum安装 #!/bin/bash
yum install -y epel-release # 依赖
yum install -y supervisor # 安装
systemctl enable supervisord # 开机自启动
systemctl start supervisord # 启动supervisord服务
systemctl status supervisord # 查看supervisord服务状态
ps -ef|grep supervisord # 查看是否存在supervisord进程 离线安装 参考: https://segmentfault.com/a/1190000011696023 基础 supervisord 启动工具 echo_supervisord_conf 生成配置工具 supervisorctl 管理进程工具 /etc/supervisor.conf 默认配置工具路径 /etc/supervisord.d/ 配置工具目录 supervisorctl 命令使用 status 查看状态 reload 重启所有服务 update 更新 stop [进程名] start [进程名] restart [进程名] supervisor.conf配置 vim /etc/supervisor.