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.
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!

Goimpactsdevelopmentpositivelythroughspeed,efficiency,andsimplicity.1)Speed:Gocompilesquicklyandrunsefficiently,idealforlargeprojects.2)Efficiency:Itscomprehensivestandardlibraryreducesexternaldependencies,enhancingdevelopmentefficiency.3)Simplicity:

C is more suitable for scenarios where direct control of hardware resources and high performance optimization is required, while Golang is more suitable for scenarios where rapid development and high concurrency processing are required. 1.C's advantage lies in its close to hardware characteristics and high optimization capabilities, which are suitable for high-performance needs such as game development. 2.Golang's advantage lies in its concise syntax and natural concurrency support, which is suitable for high concurrency service development.

Golang excels in practical applications and is known for its simplicity, efficiency and concurrency. 1) Concurrent programming is implemented through Goroutines and Channels, 2) Flexible code is written using interfaces and polymorphisms, 3) Simplify network programming with net/http packages, 4) Build efficient concurrent crawlers, 5) Debugging and optimizing through tools and best practices.

The core features of Go include garbage collection, static linking and concurrency support. 1. The concurrency model of Go language realizes efficient concurrent programming through goroutine and channel. 2. Interfaces and polymorphisms are implemented through interface methods, so that different types can be processed in a unified manner. 3. The basic usage demonstrates the efficiency of function definition and call. 4. In advanced usage, slices provide powerful functions of dynamic resizing. 5. Common errors such as race conditions can be detected and resolved through getest-race. 6. Performance optimization Reuse objects through sync.Pool to reduce garbage collection pressure.

Go language performs well in building efficient and scalable systems. Its advantages include: 1. High performance: compiled into machine code, fast running speed; 2. Concurrent programming: simplify multitasking through goroutines and channels; 3. Simplicity: concise syntax, reducing learning and maintenance costs; 4. Cross-platform: supports cross-platform compilation, easy deployment.

Confused about the sorting of SQL query results. In the process of learning SQL, you often encounter some confusing problems. Recently, the author is reading "MICK-SQL Basics"...

The relationship between technology stack convergence and technology selection In software development, the selection and management of technology stacks are a very critical issue. Recently, some readers have proposed...

Golang ...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment