编程笔记

lifelong learning & practice makes perfect

条件格式

  1. 求交/差集
    使用条件格式=>突出显示单元规则=>重复值,可以将两列数据相同的值高亮显示:
    高亮部分为交集,其余为差集

    阅读全文 »

redis

连接

  1. 通过select选库

    1
    2
    3
    4
    5
    r := redis.NewRedis(redisAddr, "node", password)
    err = r.Pipelined(func(p redis.Pipeliner) error {
    p.Select(1)
    return nil
    })

问题

  1. https://programnotes.cn/golang-1000-question/
  2. code,repo in github <git@github.com:yiGmMk/golang-1000-question.git>
  3. 代码允许环境:
    • go version go1.15.3 linux/amd64

const

  1. [001]iota,有常量如下,second数值是?

    1
    2
    3
    4
    5
    6
    const (
    first = 1
    second =iota
    third
    fourth
    )

string

  1. [002]strings.ReplaceAll,代码如下,replace的值是?

    1
    2
    3
    4
    origin := "   特斯拉Model X    "
    replace := strings.ReplaceAll(origin, " ", "")

    fmt.Printf("origin:%s\nreplace:%s\n", origin, replace)

    答: “ 特斯拉Model X “,即origin

解答

  1. https://programnotes.cn/golang-1000-question-answer/
  2. code,repo in github <git@github.com:yiGmMk/golang-1000-question.git>
  3. 代码允许环境:
    • go version go1.15.3 linux/amd64

const

  1. [001]iota,有常量如下,second数值是?

    1
    2
    3
    4
    5
    6
    const (
    first = 1
    second =iota
    third
    fourth
    )

string

  1. [002]strings.ReplaceAll,代码如下,replace的值是?

    1
    2
    3
    4
    origin := "   特斯拉Model X    "
    replace := strings.ReplaceAll(origin, " ", "")

    fmt.Printf("origin:%s\nreplace:%s\n", origin, replace)

广告屏蔽类

  1. AdGuard 广告拦截器

  2. uBlock Origin
    屏蔽效果好,支持使用选择器手动添加,屏蔽页面上指定的各种广告

代码查看

  1. Octotree - GitHub code tree

影音娱乐

  1. Listen 1
    支持通过url导入各平台的歌单

文档

  1. 超级复制

  2. Redirector 自动跳转,看一些有镜像站的文档,网页很方便

    • edge插件地址https://microsoftedge.microsoft.com/addons/detail/redirector/jdhdjbcalnfbmfdpfggcogaegfcjdcfp?hl=zh-CN

    • golang官方文档自动跳转设置(跳转到golang.google.cn)

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      {
      "createdBy": "Redirector v3.5.4",
      "createdAt": "2021-12-15T10:57:39.405Z",
      "redirects": [
      {
      "description": "重定向golang官方文档",
      "exampleUrl": "https://golang.org/",
      "exampleResult": "https://golang.google.cn/",
      "error": null,
      "includePattern": "*golang.org*",
      "excludePattern": "",
      "patternDesc": "",
      "redirectUrl": "$1golang.google.cn$2",
      "patternType": "W",
      "processMatches": "noProcessing",
      "disabled": false,
      "grouped": false,
      "appliesTo": [
      "main_frame"
      ]
      }
      ]
      }
  3. 彩云小译 翻译网页支持双语阅读

    • 收费
    • 对比阅读

下载

  1. IDM Integration Module
  • 多线程,不限速,下载超快
  • 收费

代码

  1. Go Search Extension
    快速搜索go package。

golang.org

  1. effective go: https://golang.google.cn/doc/effective_go.html

面试

  1. go面试题:https://github.com/lifei6671/interview-go
  2. go https://github.com/shomali11/go-interview

其他

  1. 国光: https://www.sqlsec.com/
  2. Go Questions: https://golang.design/go-questions/

golang blog

  1. 曹春晖: https://xargin.com/
  2. 兔兔(7天从零实现系列): https://geektutu.com/post/gee.html
  3. go by example:https://golangbyexample.com/

golang book

golang 社区

  1. GOCN: https://gocn.vip/

使用

  1. 状态
    auto,on和off

  2. 启用
    export GO111MODULE=on

  3. 固定版本号
    在go.mod里使用replace将任意版本的package替换为需要的版本

    1
    2
    3
    4
    5
    6
    7
    8
    module programnotes.cn/test
    go 1.13
    require (
    gorm.io/driver/mysql v1.1.3
    gorm.io/gorm v1.22.2
    )
    replace gorm.io/gorm => gorm.io/gorm v1.21.15
    replace gorm.io/driver/mysql => gorm.io/driver/mysql v1.0.5

    有些没节操的package不遵循规范,小版本改动不兼容导致线上bug,google的grpc就被吐槽过,这里将依赖设置成测试过的稳定版本可以减少依赖的package不兼容变更带来的问题.
    go get -u 解决依赖时可能改动项目中其他的package的版本,固定版本号也可以减少意外的版本变更,例如:这里升级gorm的版本也会改动mysql的版本.

定义

对流量进行按需分发的服务,通过将流量分发到不同的后端服务来扩展应用系统的

服务吞吐能力,并且可以消除系统中的单点故障,提升应用系统的可用性。
主要为HTTP/HTTPS/TCP/UDP/QUICk几种类型的请求设置转发规则。

负载均衡算法

  1. 轮询(Round Robin):第一个请求选择列表中的第一个服务器,按顺序移动列表直到结尾,循环。
  2. 最小连接(Least Connections):优先选择连接数最少的服务器
  3. 请求来源(Source):根据请求源的 IP 的散列(hash)来选择要转发的服务器。

    7层负载均衡与4层负载均衡

    4层

    4层 LB 主要是通过报文中的目的地址和端口,再加上负载均衡设备设置的服务器
    选择方式,决定最终选择的内部服务器。负载能力实现主要基于数据包的传输层信息
    ( ip + port )进行负载转发。
  • 基于域名或URL转发

    7层

    7层 LB 也称为“内容交换”,主要通过报文中真正有意义的应用层内容(证书,
    cookies,http头部信息),会在负载均衡设备上进行证书校验,三次握手等操作,再
    加上负载均衡设备设置的服务器选择方式,决定最终的内服务器。负载能力实现主要基
    于应数据包应用层信息( domain+port+url 等)进行负载转发。
  • 基于内容的路由
  • HTTP标头改写、重定向、重写、限速等

参考

wrk

  1. usage
    HTTP benchmarking tool.
    More information: https://github.com/wg/wrk.
  2. param
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    Options:
    -c, --connections <N> Connections to keep open //连接数
    -d, --duration <T> Duration of test //测试时间
    -t, --threads <N> Number of threads to use //线程数

    -s, --script <S> Load Lua script file //lua脚本
    -H, --header <H> Add header to request //添加头信息
    --latency Print latency statistics
    --timeout <T> Socket/request timeout
    -v, --version Print version details //版本信息
    Numeric arguments may include a SI unit (1k, 1M, 1G)
    Time arguments may include a time unit (2s, 2m, 2h)

examples

  1. 测试go-zero demo中的check接口
    1
    wrk -t10 -c1000 -d30s --latency "http://localhost:8888/check?book=go-zero"
  2. 结果
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    Running 30s test @ http://localhost:8888/check? book=go-zero
    10 threads and 1000 connections
    Thread Stats Avg Stdev Max +/- Stdev
    Latency 128.84ms 60.10ms 593.00ms 77.53%
    Req/Sec 794.67 304.69 1.95k 69.54%
    Latency Distribution
    50% 115.23ms
    75% 153.24ms
    90% 203.13ms
    99% 339.48ms
    233390 requests in 30.10s, 29.60MB read
    Requests/sec: 7754.33
    Transfer/sec: 0.98MB

ubuntu下使用

  1. 安装
    1
    2
    3
    4
    5
    6
    7
    8
    git clone https://github.com/wg/wrk.git

    // 构建可执行文件
    cd wrk
    make

    // 软连接
    ln -s 绝对路径/wrk /usr/local/bin(任意在PATH中的路径都可以)

https://hub.docker.com/_/mysql?tab=description&page=1&ordering=last_updated

  1. 拉取镜像
    docker pull mysql

  2. 运行

    1
    docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag

    where some-mysql is the name you want to assign to your container, my-secret-pw is the password to be set for the MySQL
    root user and tag is the tag specifying the MySQL version you want. See the list above for relevant tags.

    1
    docker run --name docker_mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=your_password -d mysql
  3. 修改验证密码,修改后才能用root@密码登陆

    1
    ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'your_password';
  4. 创建新用户

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    mysql8版本执行 grant all privileges on *.*  to  'root'@'%'  identified by 'root'  with grant option;
    会报语法错误 ERROR 1064 (42000)

    应先创建新用户
    create user 'admin'@'%' identified by 'password';

    执行授权

    GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%';
    (MariaDB: GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%' identified by 'password';)

    刷新
    flush privileges;

    授权远程

    ALTER USER 'admin'@'%' IDENTIFIED WITH mysql_native_password BY 'password';
    (MariaDB: ALTER USER admin@'%' IDENTIFIED VIA mysql_native_password;)

    刷新
    flush privileges;

    references: https://blog.csdn.net/java_cai_niao_han/article/details/110442608