编程笔记

lifelong learning & practice makes perfect

vscode不使用maven等工具手动导入jar包

步骤

  1. 下载相应的jar包
  2. 加入Referenced Libraries中
    比如下面日志用了Commons Logging(一个第三方日志库)
阅读全文 »

刚开始使用vscode+java时遇到这个问题,Could not find or load main class x,
将源文件加入”Java source paths”并修改文件名与主类名相同后解决了.
这样就能方便地调试运行单个java文件了

新的问题

  1. 但是由于”Run|Debug”这两个快捷按钮每次会根据类名生成配置并写入launch.json,
    且当主类名相同时调试运行的程序会出错(不是当前文件),还是很不方便,
    这要求运行的主类名与已在”Java source paths”的类不同才能正常调试运行
  2. 每次自动根据类名生成配置也会导致配置文件中的配置越来越多
阅读全文 »

建立ssl/tls连接需要若干步骤,协议结合使用了对称加密和非对称加密,客户机和服务端需要协商所使用的算法并交换密钥信息.
下面以TLS 1.2为例介绍下流程(TlS1.3做了很多优化)

阅读全文 »

This function returns a string result with the concatenated non-NULL values from a group. It returns NULL if there are no non-NULL values

group_concat将非NULL的值拼接返回字符串,当没有非NULL的值时返回NULL.
有长度限制,遇到过一个由于长度限制数据截断导致的bug,sql如下

1
2
3
4
5
-- 这里id,price都是int,4字节
SELECT id,SUBSTRING_INDEX(GROUP_CONCAT(price order by id), ',', -1) AS price
FROM xxx
where xxxx=xxx
GROUP BY id;

这里将price拼接,再通过SUBSTRING_INDEX取最后一个price,由于数据截断,GROUP_CONCAT返回的字符串最后一个是”,”而不是price,导致SUBSTRING_INDEX取到的是””(字符串),在业务逻辑里直接转为int报错了,由此产生bug.
且只有当数据量足够多时才会触发(超过171行时,1024=1714+1702,引擎为InnnoDB,MySQL8.0,字符集为utf8mb4_0900_ai_ci)

1
2
3
4
5
-- 有兴趣的可以复现下,查一个int类型的数据就行
SELECT GROUP_CONCAT(price) FROM (
SELECT `price`
FROM xxx
where `xxx` = "xxx" limit 171 ) as t;
阅读全文 »

下标访问

1
2
3
4
5
func TestStr(t *testing.T) {
str := `中\a`
log.Println(unsafe.Sizeof(rune('中')), unsafe.Sizeof(str[0]), str[0], string(str[0]))
}
// 输出: 2022/09/15 19:55:27 4 1 228 ä

range

1
2
3
4
5
6
7
8
const nihongo = "日本語"
for i, v := range nihongo {
fmt.Printf("%#U starts at byte position %d\n", v, i)
}
// 输出:
// U+65E5 '日' starts at byte position 0
// U+672C '本' starts at byte position 3
// U+8A9E '語' starts at byte position 6
阅读全文 »

中文翻译:<微服务与事件驱动架构>

第一章

要点

  1. 康威定律
  2. 微服务
  3. 事件驱动架构

Concept

  1. 康威定律: 设计系统的组织……受到约束,产生的设计是这些组织的沟通结构的副本

    Organizations which design systems are constrained to produce designs which are copies of the communication structures of these organizations. - Melvin Conway(1967)

    references:

    • 阿里肥侠文章:”微服务架构的理论基础 - 康威定律” https://developer.aliyun.com/article/8611

    • 康威定律详细介绍

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      第一定律
      Communication dictates design
      组织沟通方式会通过系统设计表达出来
      第二定律
      There is never enough time to do something right, but there is always enough time to do it over
      时间再多一件事情也不可能做的完美,但总有时间做完一件事情
      第三定律
      There is a homomorphism from the linear graph of a system to the linear graph of its design organization
      线型系统和线型组织架构间有潜在的异质同态特性
      第四定律
      The structures of large systems tend to disintegrate during development, qualitatively more so than with small ystems
      大的系统组织总是比小系统更倾向于分解
  2. 微服务

    The microservice architectural style is an approach to developing a single application as a suite of small services, each running in its own process and communicating with lightweight mechanisms, often an HTTP resource API. These services are built around business capabilities and independently deployable by fully automated deployment machinery. There is a bare minimum of centralized management of these services , which may be written in different programming languages and use different data storage technologies.
    – James Lewis and Martin Fowler

    Martin Fowler是国际著名的OO专家,敏捷开发方法的创始人之一,现为ThoughtWorks公司的首席科学家.福勒(Martin Fowler),在面向对象分析设计、UML、模式、软件开发方法学、XP、重构等方面,都是世界顶级的专家,现为Thought Works公司的首席科学家。Thought Works是一家从事企业应用开发和集成的公司。早在20世纪80年代,Fowler就是使用对象技术构建多层企业应用的倡导者,他著有几本经典书籍:《企业应用架构模式》、《UML精粹》和《重构》等。—— 百度百科

  3. 微服务问题
    微服务架构需要考虑的问题,包括

  • API Gateway
  • 服务间调用
  • 服务发现
  • 服务容错
  • 服务部署
  • 数据调用

What

How

Display

  1. go tool pprof -http=:9999 cpu.pprof

Benchmark & pprof

  1. gen

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21

    func fib(n int) int {
    if n == 0 || n == 1 {
    return n
    }
    return fib(n-2) + fib(n-1)
    }

    func BenchmarkFib(b *testing.B) {
    for n := 0; n < b.N; n++ {
    fib(30) // run fib(30) b.N times
    }
    }
    只需要在 go test 添加 -cpuprofile 参数即可生成 BenchmarkFib 对应的 CPU profile 文件:
    $ go test -bench="Fib$" -cpuprofile=cpu.pprof .
    goos: linux
    goarch: amd64
    pkg: example
    BenchmarkFib-8 196 6071636 ns/op
    PASS
    ok example 2.046s

References

  1. 极客兔兔https://geektutu.com/post/hpg-pprof.html

正则匹配/regexp 规则

元字符

  1. ^ 匹配输入字符串的开始位置
  2. $ 匹配输入字符串的结束位置

example

vin码

1
2
3
4
5
6
7
8
// 完整匹配: ^[A-HJ-NPR-Z\d]{8}[X\d][A-HJ-NPR-Z\d]{3}\d{5}$
// vin码可能不在字符串最前面,有可能在中间
// 去掉^(匹配字符串开头,在多行模式中匹配每一行的开头)和$
var vinPattern = regexp.MustCompile(`[A-HJ-NPR-Z\d]{8}[X\d][A-HJ-NPR-Z\d]{3}\d{5}`)
in := "车架号:LFV2A21K2J4076260"
vin:= vinPattern.FindAllString(in, -1)
fmt.Println(vin)
// => LFV2A21K2J4076260

references