Home > Backend Development > Golang > What are microservices in golang

What are microservices in golang

青灯夜游
Release: 2023-01-04 09:11:36
Original
4406 people have browsed it

In golang, microservices (or microservice architecture) is a software architecture style (technology) that advocates dividing a single application into a set of small services, and the services coordinate and cooperate with each other. Provide ultimate value to users. Each service runs in its own independent process, and services communicate with each other using a lightweight communication mechanism (usually a RESTful API based on HTTP); each service is built around a specific business and can be deployed independently to production environment, production-like environment, etc.

What are microservices in golang

The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.

What are microservices

Microservices (Microservices Architecture) is a software architecture style, service-oriented architecture (SOA) architecture A variation of the style that advocates dividing a single application into a set of small services that coordinate and work with each other to provide ultimate value to users. Each service runs in its own independent process, and services communicate with each other using a lightweight communication mechanism (usually a RESTful API based on HTTP). Each service is built around specific businesses and can be independently deployed to production environments, production-like environments, etc. In addition, a unified and centralized service management mechanism should be avoided as much as possible. For a specific service, appropriate languages ​​and tools should be selected to build it based on the context.

Microservices (or microservices architecture) is a cloud-native architectural approach in which a single application is composed of many smaller components or services that are loosely coupled and independently deployable. These services typically

  • have their own stacks, including databases and data models;

  • interact with each other through a REST API, a combination of event streams and message brokers Communications;

  • They are organized by business capabilities, and the lines that separate services are often called bounded contexts.

While much of the discussion about microservices revolves around architectural definitions and characteristics, their value can be understood more generally through fairly simple business and organizational benefits:

  • Makes it easier to update code.

  • Teams can use different stacks for different components.

  • Components can be scaled independently of each other, reducing the waste and cost associated with having to scale the entire application because a single feature may face excessive load.

Why do we use microservices?

This architecture helps us describe the entire application in parts, small modules, making it easier to understand, develop, and test; it helps us treat each service as independent Services that clearly indicate their purpose. Furthermore, it helps to maintain the consistency of the project's architecture (there is little difference between the originally designed architecture and the actual developed architecture). It can also deploy and expand services by establishing different independent teams, so that each team can develop in parallel. Refactoring code is easier in this architecture. It also supports continuous delivery and deployment processes (CI/CD).

Why use go to build microservices?

Before delving into this issue. First, let me talk about the advantages of Golang. Although Golang is a new language, it has many advantages compared to other languages. Programs written in Golang are more robust. They are able to withstand the heavy load that programs build using running services. Golang is more suitable for multi-processor systems and web applications. Additionally, it easily integrates with GitHub to manage decentralized code packages. The usefulness of microservice architecture is mostly reflected when the program needs to be scalable. If there is a language that is fully standards compliant, it is Golang. The reason is that it inherits from the C-family of programming languages, and components written in Golang are easier to combine with components written in other languages ​​​​of the same family.

Although Go comes from the C-family, it is more efficient than C/C. It has a simpler syntax, somewhat like Python. Its syntax is stable and has not changed much since its first public release, which means it is backwards compatible. This gives golang an upper hand compared to other languages. In addition, Golang's performance is much higher than python and java. The icing on the cake is that it's as simple as C/C++ yet easy to read and understand, making it an excellent choice for developing microservice applications.

Microservice architecture framework in Golang

Next, we discuss the framework that can be used for microservice architecture. There are the following frameworks:

Go Micro

Go Micro is by far the most popular RPC framework I have encountered. It is a pluggable RPC framework. Go Micro provides us with the following functions:

  • Service discovery: The program automatically registers to the service discovery system

  • Load balancing: It provides customers End-to-end load balancing, which helps balance requests between service instances

  • Synchronous communication: Provides Request/Response transport layer

  • Asynchronous communication: Has built-in publishing and subscription functions

  • Message encoding: You can use the Content-Type in the header to encode and decode

  • RPC client/server: utilize the above functions and provide the interfaces needed to build microservices

Go Micro architecture consists of three layers. The first level of abstraction is the service layer. The second layer is the client-server model layer. The server is made up of blocks for writing services, while the client provides us with an interface whose sole purpose is to make requests to services written in the server model.

The third layer has the following types of plug-ins:

  • Broker: Provides an interface for message broker (message broker) in asynchronous communication

  • Codec: Used to encrypt or decrypt messages

  • Registry: Provides service search function

  • Selector: Built the payload on register Balance

  • Transport: Transport is a communication interface for synchronous request/response between services

It also provides a service called Sidecar. Function. Sidecar enables you to integrate services written in languages ​​other than Go. It also provides us with gRPC encoding/decoding, service registration and HTTP request handling

GO Kit

Go Kit is a programming toolkit for building microservices . Unlike Go Micro, it is a library that can be imported as a binary package. Go Kit rules are simple. As follows:

  • No global variables

  • Declarative composition

  • Explicit dependencies

  • Interface as Contracts (Interface Contract)

  • Domain Driven Design (DDD)

Go Kit provides the following Code package:

  • Authentication Authentication: BasicAuth and JWT

  • Transport protocol: HTTP, gRPC, etc.

  • Logging Log: Structured logging interface in the service

  • Metrics Metrics: CloudWatch, Statsd, Graphite, etc.

  • Tracing distributed tracing : Zipkin and Opentracing

  • Service discovery Service discovery: Consul, Etcd, Eureka, etc.

  • Circuitbreaker Current limiting circuit breaker: Hystrix implementation in Go language

Go Kit service architecture is as follows

Gizmo

Gizmo is a microservice toolkit from The New York Times. It provides packages that bring the server daemon and pubsub daemon together. It exposes the following packages:

  • Server: Provides two server implementations: SimpleServer (HTTP) and RPCServer (gRPC)

  • Server/kit : Experimental code package based on Go Kit

  • Config configuration: Contains configuration functions from JSON files, JSON blobs in Consul k/v, or environment variables

  • Pubsub: Provides a common interface for publishing and consuming data from the queue

  • Pubsub/pubsubtest: Contains test implementations of publisher and subscriber interfaces

  • Web: External functions for parsing types from request queries and payloads

The Pubsub package provides interfaces for handling the following queues:

  • pubsub/aws: for Amazon SNS/SQS

  • pubsub/gcp: for Google Pubsub

  • pubsub/ kafka: for Kafka topics

  • pubsub/http: user HTTP push

So, in my opinion, Gizmo is somewhere between Go Micro and Go Kit between. It's not a complete black box like the Go Micro. At the same time, it is not as primitive as Go Kit. It provides higher level building components such as configuration and pubsub packages

Kite

Kite is a framework for developing microservices in Go. It exposes RPC client and server side code packages. The created service will automatically be registered with the service discovery system Kontrol. Kontrol is written in Kite and is itself a Kite service. This means that Kite microservices work well in their own environment. If you need to connect your Kite microservices to another service discovery system, customization is required. This is one of the important reasons why I chose Kite from the list and decided not to introduce this framework

[Related recommendations: Go video tutorial, Programming teaching]

The above is the detailed content of What are microservices in golang. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template