Go spike server optimization

Structure adjustment

Old architecture
new structure

Preliminaries

cookie,session

  1. The difference between the cookie and session

  2. cookie and session link

  3. chrome Ethereal View // TODO

  4. Under ordinary scene login session cookie and used in conjunction with flow

Symmetric encryption and asymmetric encryption

Appendix 2

  • Symmetric encryption
  • Asymmetric encryption, such as: AES, RSA
  • The use of AES // reference aes.NewCipher (key) function annotation
    16,24,32 bit string, so the corresponding AES-128, AES-192, AES-256 encryption method

base64 // Appendix 3

Q: base64 beginning of the function
A:

  1. In fact, BASE64 encoding original intention was to use the e-mail can not be directly specified non-ASCII characters to meet.
  2. But there are other important significance: all binary files, and therefore can be converted into printable text encoding, using text editing software, and is a simple encryption to the data stream is.

Q: Why use base64 encoding, which demand scenario?
A: There are two scenarios can not transmit binary

  1. For historical reasons e-mail
  2. Plain text protocol, can not transmit binary, can use the base64 encoding. Because a plain-text transmission protocol, partly as a control character processing may occur in binary.

Server optimization points

Chapter 10 achieve cookie validation (single-node)

  • Improved point
    break out session (implementation verification alternative session cookie)
  • Ideas
    removed session, users deposit the processed key information in a cookie, the processing method:
    step1 public key cryptography: the uid using the private key encryption. // 16-bit private key encryption result 128.
    . step2 base64 encoding: // because the cookie http can not be stored in binary data can only be stored ascll characters.
  • The relevant code
1. user_controller.go 
登录接口的后端实现。本来登录成功后往只cookie写入了uid字段,为了防止uid被篡改,往cookie中写入了一个验证uid是否被篡改的sign字段。
2. auth.go
验证用户是否登录的中间件。验证uid是否存在。代码不变。

Performance Optimization of Chapter 11 Distributed Authentication server to achieve

  • Distributed architecture
    from the principle deployed in a process of a machine, instead deployed to multiple machines in different processes.
    After the split configuration: the privilege verifier (horizontal expansion) + number of control (level expansion) Web server + + + mysql message queue

  • Permission Validation: use interceptors to achieve
  1. Interceptor (transfer function) // go decorator pattern to achieve Appendix 4 //
    Function: verify user information in the cookie is authentic (not tampered with).
    Ideas: by uid and sign decryption results validate the cookie matching to verify whether tampering.
    Code: filter.go (mian package) + validate.go 单独部署. 验证代码从代码中提取处理单独部署
    Extended: OAuth Protocol
  • Q: Why are we here need consistency hash algorithm
    A: The main application scenarios consistent hash algorithm is a distributed storage, distributed caching, load balancing. We belong here distributed cache this type of application, note that we did not use redis, but with the memory cache, each machine to store different user information cache.
    Data storage requirements: each machine (process) and stored uid request time. For example uid = 1,2,3 cache machine 1, uid = 4,5,6 buffer 2 in the machine, then the machine will request uid = 1 2 to get the data (within the network) on the cache machine 1

  • Consistency Hash algorithm // Appendix 5
    Q: consistency Hash Algorithm major role
    A: consistent hashing is to solve the problem of scalable nodes.
  1. Ordinary hash algorithm, when the node additions and deletions will produce an avalanche cache (cache at the same time a large number of simultaneous failure).
  2. Consistency hash algorithm the case of such a cache miss, reduced to the area between the node and the adjacent old node additions and deletions.
  • Hash algorithm code to achieve consistency / / TODO
    key points:
  1. IP = Local IP, the unit is read cache. Otherwise IP! = Local IP, then the machine acts as a proxy to another machine to get data. (Network transmission)
  2. The key structure
//创建结构体保存一致性hash信息
type Consistent struct {
    //hash环,key为哈希值,值存放节点的信息 // TODO k: 请求key或节点的hash值  v: 真实节点或虚拟节点信息(如果有2个真实节点,每个节点20个虚拟节点,那么一共有20个v需要存储,环上其他位置并不需要存放数据)
    circle map[uint32]string
    //已经排序的节点hash切片 // TODO 排好序的虚拟节点hash值,40个元素,对应2个v。
    sortedHashes units
    //虚拟节点个数,用来增加hash的平衡性
    VirtualNode int
    //map 读写锁
    sync.RWMutex
}
  • Validate.go consistency in application of the hash algorithm
//设置集群地址,最好内网IP
var hostArray = []string{"192.168.0.1", "192.168.0.2"}
var localHost = "192.168.0.1"

// 创建Consistent结构体
hashConsistent = common.NewConsistent()
// 添加服务器节点
for _, v := range hostArray {
    hashConsistent.Add(v)
}

// 作为缓存的结构体
type AccessControl struct {
    //用来存放用户想要存放的信息
    sourcesArray map[int]interface{}
    sync.RWMutex
}
  • Use the process using the consistency hash algorithm
  1. Ali cloud responsible for SLB to load balance servers on different cloud ECS
    is a random selection of ECS to process the request.

Chapter 12 server performance optimization solution oversold & introduction message queue

  • WRK
    WRK and output parameters

  • Modification fronted/web/controllers/product_controller.GetOrder方法
    of the original method of data access great pressure, every time you create an order would call several times to access the database.
    new method:

  • rabbitmq consumption mode configuration
  1. Message by the flow control: Each time a message is sent only to the consumer. Configuration items:channel.Qos
  2. Acknowledged Mode: channel.Consume (autoAck: false)
  • Related rabbimq code implementation
    product_controller.go: single interface to the next message GetOrder production () (for the message structure has two fields: the productId the userId +)
    consumer.go: a message consumer, the flow control (1 take a message), manual acknowledgment When you delete a message (consumer success, calling msg.Ack (false) makes rabbitmq delete the message // TODO consumption should be how to deal with failure)

  • Problems in the current code
    without a transaction (quantity minus 1+ generate orders)

reference

  1. Symmetric encryption, asymmetric encryption, the RSA (Summary)
  2. Examples of symmetric encryption and asymmetric encryption binding: https
  3. Why use Base64 encoding principle and its realization
  4. Allows you wrap the decorator nature existing functionality and add your own custom functions at the beginning or end of
    expansion: the decorator absolutely the right way to deal with the protection of the REST API is not, I recommend you use OAuth2 JWT or to achieve this goal! OAuth2.0 protocol
    extensions: see the original Go web middleware framework is implemented as follows
  5. Interview Essentials: What is the consistency Hash algorithm?
    Extended: Redis and Mysql double write consistency program resolve

Guess you like

Origin www.cnblogs.com/yudidi/p/12518529.html