Golang Micro-Service Learning notes (i)

Source: Internet
Author: User
Tags etcd

1. Preface

Background

Work from PHP to Golang, recently in using go to develop micro-service backstage, in the study, if the understanding is wrong, welcome to Exchange ~ ~ Now just use go micro-frame to build a background micro-service, service found using ETCD, protocol using PB, data read using Redis.

Technology stack

    • Golang (Development language)
    • Go-micro (RPC framework)
    • ETCD (Distributed key-value database)
    • Docker (Container)
    • Mysql,redis (Database)


Micro-Service

What is the concept of microservices, in general traditional development, the entire application is built in a code base, it may be based on the characteristics of MVC into model, controller and view layer to develop, or according to functions, such as login check and other business logic separate, But it's actually inside a code base. This increases with the development time, because of the high degree of coupling, the following problems are faced:

    • Writing and maintenance work can become complicated, especially as the logic of the project becomes more and more complex, and the logic is likely to be more coupled and more difficult to develop.
    • Extension is difficult, if you need to extend a module, you may face the entire code base and the module is too deep coupling, have to extend the entire code base.

Micro-service is actually the entire application according to the various functions to split into each code base, each function as a service, independent deployment operation, clear responsibilities. In fact, to solve the above mentioned problems, the division of services can be better maintained and expanded.

Golang

Advantages of Golang

    • Can be directly compiled into machine code, do not rely on other libraries, deployment is compiled to execute files directly uploaded to the server to be done ...
    • Statically typed language, but with dynamic language characteristics
    • Excellent support for concurrency and parallelism. The designer achieves this by goroutine the concept of this lightweight thread, and then communicates between the various goroutine through the channel. They achieve the growth of segment stacks and the automation of multiplexing technologies based on Goroutine on-line. This is really a very attractive place to create a process is very simple, the cost of only 4kb of stack memory to create. It is important to note that the specific run is concurrency, run parallel to use the GOMAXPROCS variable
    • Support cross-compilation, you can compile Linux applications under Windows
    • Built-in powerful tools, debugging and Code specification tools
    • The rich standard library, especially the network library, the standard library is very rich, basically encapsulates the route to be able to use directly.
    • Supports GC. However, the GC has some points to pay attention to, so the development of the time to pay attention to this aspect, otherwise the magnitude to a certain extent, may cause performance problems due to GC.

Because the individual is just transferred from PHP Golang Development, during the learning process. Personal impressions are:

    • In fast small application development or PHP is relatively fast. But static language advantages can reduce a lot of hidden problems, but also have dynamic language effects, this is very convenient for language.
    • Without object-oriented, implemented through the concept of Interfaces (interface), it's a little difficult to understand at first.

    • Language level support concurrency, convenient for background programming
    • Easy to deploy, unlike PHP's various build nginx servers and PHP extensions.
    • Performance goes without saying, close to C + + and Java.
    • According to colleagues, the idea of design is not necessarily bad, but it is designed for engineering.
      Others are still in the process of understanding ....

Rpc

Remote Procedure Calls (Procedure call, abbreviated RPC) are a computer communication protocol. The protocol allows programs running on one computer to invoke subroutines from another computer, and programmers do not need to program this interaction for additional purposes. If the software involved is object-oriented, the remote procedure call can also be called a remote call or a remote method call.

The popular point is that applications like a server invoke the functions/methods provided by the B server application. There are several issues in which communication establishes a TCP connection. Addressing is addressed through service discovery, where the parameters of the method are passed to the B server via the underlying network protocol such as TCP, which is binary based and requires serialization and deserialization.

Protocol buffers

According to Google, this is Google's platform-independent, language-independent, extensible serialized data format, reference XML, but smaller, faster and simpler. As long as the data structure is defined, the corresponding language version can be produced in each language. The internal data is in pure binary format.

ETCD

The goal of ETCD is to build a highly available distributed key value (Key-value database). ETCD internal use of the raft this consistency algorithm, interested can go to understand the next. The main application scenario is Service discovery, and the necessary support is the following:

    • A strongly consistent, highly available service store directory. (based on Raft)
    • A mechanism for registering services and health services. (timed heartbeat to determine if the service is available)
    • A mechanism for finding and connecting services.

2. Architecture

Overall application

In fact, the entire background application is composed of the access layer, the various service modules and the Service Discovery module.

    • Front-End HTTP request request to access layer
    • Access Layer (Client): Responsible for the current limit and the specific Logic service distribution Request Service module, through the registry to obtain the list of available servers for each service, invoke each service through the PROTOCBUF protocol with the various modules call (RPC)
    • Registration Center (Registry): Listen to the service directory (register with key watcher), when listening to the new service, add the server to the list of available servers, some server shutdown, the agent detects a key timeout, You can decide whether or not to remove the server according to your own policy; Before the client connects to the server, the service IP address is assigned by the registry according to the policy (this is not clear and subsequent determination is updated)
    • Service (server): When each service is deployed on the Server, it registers the service with ETCD and periodically sends a heartbeat to ETCD.


      Micro-service framework. png

Above, there are omissions and follow-up, the next one should be to build the overall environment and protobuf of the writing and generation, if only local testing, ETCD only built a single example is very simple, after the installation of the direct start, the default listening to the local 2379 port. If it is a cluster, you may need to configure something. In addition PROTOBUF generated the Go language version of the time do not know why it is more than the definition of data structure more than three parameters, check the data seems to be Protoc-gen-micro update content (not big sure), but if the comments out of the words will not affect the use of ...

Reference articles

    • What is Micro Service
    • MicroServices in Golang
    • The To Go
    • Go RPC Development Guide
    • Getting Started with Etcd
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.