I studied in the company for nearly a month.
Within a month, I started to touch the distributed micro-service architecture from 0, which gave me a lot of harvest. Today, I’m going to go through the core (dry stuff) of the microservices architecture from start to finish.
Below, you will see the core principles of the leading microservices frameworks in the industry, including service discovery, gateway, configuration center, monitoring, and other components, features, and architecture principles. Thanks for reading! ๐
Want to unlock more new poses? Please visit my blog. ๐
Hello, Microservices
What are microservices
The father of Microservices, Martin. Fowler’s overview of microservices is as follows:
Currently, there is no uniform, standard definition of microservices While there is no precise definition of this architectural style. But generally speaking, microservices architecture is an architectural pattern or architectural style that advocates the partitioning of a single application into a set of small services, each running its own independent process, which coordinate and cooperate with each other to provide ultimate value to users. Services communicate with each other using lightweight communication mechanisms (usually RESTful apis based on HTTP). Each service is built around a specific business and can be independently deployed to a production environment, class production environment, and so on. In addition, unified and centralized service management mechanism should be avoided as far as possible. For a specific service, appropriate language and tools should be selected to build it according to the business context, so that there can be a very lightweight centralized management to coordinate these services. Services can be written in different languages, and different data stores can be used.
Based on Martin Fowler’s description, I summarize the following points:
(Word difference, don’t hate)
Small service
A small service has no specific standard or specification, but it must be small in general specification.
Process of independent
Each set of services runs independently, maybe one of my services is running on the Tomcat container and another is running on Jetty. The entire service can be continuously scaled horizontally in a process manner.
communication
The protocols of the past were heavy, like ESB, like SOAP, light communication, which meant that smarter and lighter services called each other than in the past, in terms of smart endpoints and dumb Pipes, these endpoints were decoupled. Completing a business communication call to string these Micro Services is like piping a series of command services through a Linux system.
In the past, we used to think about all kinds of dependencies and the problems of system coupling. Microservices can make developers more focused on business logic development.
The deployment of
It’s not just the business that needs to be independent, it’s also the deployment that needs to be independent. However, it also means that the traditional development process will change to a certain extent, and there will be some operational criticism of development suitability
management
Traditional enterprise-level SOA services tend to be large, difficult to manage, highly coupled, and costly for teams to develop. Microservices allow teams to choose their own technology implementation, and different services can choose different technology stacks to implement their business logic according to their own needs.
The pros and cons of microservices
Why microservices? Because it’s fun?
Isn’t. Here are some of the more complete advantages I found on the Internet:
Advantages Each service is cohesive and small enough that the code is easy to understand so that it can focus on a specific business function or business requirement
Development is simple and efficient, and a service may be dedicated to doing only one thing.
Microservices can be developed independently by small teams of two to five developers.
Microservices are loosely coupled, functional services that are independent in both development and deployment phases.
Microservices can be developed in different languages.
Easy to integrate with third parties, microservices allow an easy and flexible way to integrate automatic deployment through continuous integration tools such as Jenkins,Hudson,bamboo.
Microservices can be easily understood, modified, and maintained by a single developer so that small teams can focus on their own work. No – Value is achieved through cooperation. Microservices allow you to leverage the latest technology.
Microservices are just code for business logic, not mixed with HTML,CSS, or other interface components.
Each microservice has its own storage capacity and can have its own database. You can also have a unified database.
In general, the advantage of microservices is that, in the face of large systems, it can effectively reduce the complexity and make the logic of service architecture clearer.
However, it also brings many problems, such as data consistency in distributed environment, complexity of testing, and complexity of operation and maintenance.
What organizations are suitable to use microservices?
Microservices bring all kinds of advantages, all kinds of disadvantages, so which organization is suitable to use microservices?
Murphy’s Law (designing systems) and Conway’s Law (partitioning systems)
Conway’s Law is a microservice concept that has been around for more than 50 years. The most famous line in Conway’s article is:
Organizations which design systems are constrained to produce designs which are copies of the communication structures of these organizations. – Melvin Conway(1967)
The design of the organization is equivalent to the communication structure within and between the organizations. Take a look at the picture below and think about Apple’s product design and Microsoft’s product design.
Those of you who are interested can look into it
Evolution of architecture
Architecture is constantly evolving, and so is microservices, when it becomes large enough for major technology companies to evolve into a more managed technology architecture.
(Word difference, don’t hate)
Traditional teams are process-oriented. When the product is finished, they want to find planning, and then they want to find development step by step. We do technology for products, and if something goes wrong with the process, it’s very time consuming to go back and find it.
(Word difference, don’t hate)
With microservices architecture, the team organization needs to be transformed into cross-functional teams, where each team has product experts, planning experts, development experts, operations experts, who use APIS to deliver their functions, and the platform uses their functions to deliver products
Microservices technology architecture system
Let me share with you the technical architecture of microservices that most companies use.
(Don’t be afraid of the difference)
Service discovery
Mainstream service discovery falls into three categories
In the first way, after the developer develops the program, he will assign a domain name to the operation and maintenance service, and then he can find the corresponding service through DNS
The disadvantage is that there can be considerable performance problems for load-balancing services because the service does not have load-balancing capabilities.
The second is the current common practice. Refer to the Zuul gateway that I analyzed in my last blog post. Each service is registered to the registry through built-in functionality on the server side, and the service consumer constantly polls the registry to find the corresponding service and invokes the service using built-in load balancing.
The downside is that it’s not great for multilingual environments, and you need to develop service discovery and load balancing capabilities for consumer clients alone. Of course, this approach is typically used on the Spring Cloud.
Third, put the client and load balancer on the same host, not in the same process.
Compared with the first method and the second method, this method improves their disadvantages, but will greatly increase the operation and maintenance cost.
The gateway
What is the gateway to microservices?
We can think about it in real life. Every big company will have its own building area, and this building area, there are many doormen. If there is a foreign personnel into the company, will be the first to greet the guard before entering.
It’s not hard to understand what a gateway means when you put the facts of life into microservices.
What does a gateway do
- Reverse routing: Many times, companies do not want outsiders to see the inside of our company, so they need gateways for reverse routing. Convert external requests into internal concrete service bars
- Security authentication: there will be a lot of malicious access in the network, such as crawlers, such as hacker attacks, gateways maintain security functions.
- Limit current fusing: refer to my blog about learning distributed Zookepper, when many services are overwhelmed by requests, our services will be automatically shut down, resulting in unusable services. Current limiting fuse can effectively avoid such problems
- Log monitoring: All external requests go through the gateway so that we can use the gateway to log information
- Grayscale release, blue-green deployment. It’s a release that has a smooth transition. A/B testing can be carried out on it, that is, some users continue to use product feature A and some users start to use product feature B. If users have no objection to B, the scope can be gradually expanded to migrate all users to B.
Open source gateway Zuul architecture
The core of the Zuul gateway is actually a servlet, and all requests are passed through the Zuul servlet to the zuulFilter Runner, which is then distributed to three filters.
Let’s start with the left half of the architecture diagram, which shows pre-routing filters, routing filters, and post-routing filters implemented using Groovy.
Common requests are processed through pre-routing filters, and common custom Java encapsulation logic is implemented here.
Route filter, the realization is to find the corresponding micro service to call.
When it’s done, the response comes back, it goes through a post-routing filter, and with the post-routing filter we can encapsulate the processing of the log audit.
Arguably the biggest feature of zuul gateway is its three layers of filters.
The right half of the architecture diagram shows the custom filter loading mechanism designed by Zuul Gateway. There will be a producer-consumer model inside the gateway that automatically publishes filter scripts to zuul gateway for reading and loading.
Configuration center
In the past, developers put configuration files in development files, which has a lot of pitfalls. For example, different configuration specifications make it impossible to trace the configuration personnel. Once large-scale configuration changes are required, the changes will take a long time, and the configuration personnel cannot be traced back, thus affecting the whole product, which we cannot afford.
Hence the configuration center
At present, there are baidu configuration center Disconf, Spring Cloud Config, Apollo. Today, I will focus on Apollo, the configuration center with good application quality.
Ctrip open source Apollo
Open source ๐ : github.com/ctripcorp/a…
Apollo has a large configuration center. Local applications have a responsive configuration center client that can periodically synchronize the configuration in the configuration center. If the configuration center is idle, caching is used for configuration.
Communication methods
As for communication methods, there are generally two remote call methods in the market. I have compiled a table:
RPC | REST | |
---|---|---|
coupling | Strong coupling | Loose coupling |
Message protocol | TCP | HTTP |
Communication protocol | binary | Text XML, Json |
performance | high | Below the RPC |
Interface contract IDL | thrift,protobuf,IdL | Swagger |
The client | Strongly typed client, usually automatically generated | General HTTP access, generate strong type client, multi – language support good |
case | Dubbo, Dubbox motan, tars, GRPC and thrift | spring boot,tax-rs,dropwizard |
Developer friendly | On the client side, binary messages are unreadable | To read the message |
Opening to the outside world | You generally need to switch to REST/ text protocols | Direct external development |
Monitoring early warning
Monitoring and early warning is very important for micro services, and a reliable monitoring and early warning system is crucial for the operation of micro services. General monitoring is divided into the following levels:
From the infrastructure to the client, there are layers of monitoring, all-round, multi-angle, every level is important. In general, microservices can be divided into five monitoring points: log monitoring, Metrics monitoring, health check, call chain check, and alarm system
Monitoring architecture
The following diagram is a monitoring architecture diagram for most companies. Each service has an Agent, which collects key information and passes it to some MQ for decoupling. At the same time, pass logs to ELK and Metrics to the InfluxDB time series library. Nagios, for example, can periodically initiate information checking microservices to agents.
Call chain monitors APM
Many companies have call chain monitoring. For example, Alibaba has Hawk-Eye monitoring, yelp Cat, and most of the call chain monitoring (yes, I mean Zipkin) architecture is ๐
As a request enters the Web container, you create a Tracer to connect to SPANS (mimicking latency for potentially distributed work, and this module includes a toolkit for passing tracking context information across a system network, such as via HTTP Headers). Spans has a context that contains the Tracer identifier, put it in the right place on the tree representing distributed actions. Our service call chain dynamically generates the call chain when we put the various spans in the diagram on the back end.
Here are some of the more common call chain monitoring in the market:
1, Pinpoint github address: naver/ Pinpoint: Pinpoint is an open source APM (Application Performance Management) tool for large-scale distributed systems written in Java. Anyone who is interested in performance analysis in the Java field should have a look at this open source project, which is developed by a Korean team. It uses the JavaAgent mechanism to implant bytecode code to achieve the purpose of adding Traceid and capturing performance data. Tools such as NewRelic, Oneapm and others perform performance analysis on the Java platform in a similar way.
SkyWalking github is a system for tracking, alerting and analyzing business performance of JAVA distributed application clusters. It has more than 400 stars on Github. Relative pinpoint function or slightly weaker, plug-in is not so rich, but also difficult to get.
Zipkin ยท A Distributed tracing system github Zipkin is a distributed tracing system developed by Twitter and based on Dapper’s system.
Zipkin’s Java application uses a component called Brave to collect performance analysis data within the application. Brave’s Github address: github.com/openzipkin/… This component implements a series of Java interceptors to track the invocation of HTTP /servlet requests and database access. You can then collect performance data for Java applications by adding these interceptors to configuration files such as Spring.
This is the public dianping open source, the implementation of the function is still quite rich, there are also some companies in the use of the. But the way he does it is by hard-coding “buried points” — intrusive ones — into the code. This has both advantages and disadvantages, the advantage is that you can add buried points in their own needs, more targeted; The downside is that you have to change existing systems, which many development teams don’t want to do.
5. The combination of Xhprof/Xhgui is a non-invasive tool that provides APM capabilities for PHP applications. Xhprof Github address: Github preinheimer/ Xhprof XHGUI is a GUI for the XHProf PHP extension, using a database backend, And pretty graphs to make it easy to use and interpret. Xhgui github A graphical interface for XHProf data built on MongoDB A graphical interface for XHProf data built on MongoDB I am not familiar with PHP.
Fusing, isolation, current limiting and degradation
Faced with large bursts of traffic, large companies typically employ a series of circuit breakers (the system automatically shuts down services to minimize problems), isolation (isolating services from services, To prevent a service hung up the other services cannot access), current limiting (per unit time allow a certain number of users access), down (when the micro service architecture as a whole the maximum load is beyond the preset threshold or the coming flow is expected to more than the preset threshold, in order to ensure that important or basic services can run normally, We can delay or suspend the use of services or tasks that are not important or urgent.
Here’s how Hystrix works (sorry if you can’t find the architecture diagram) :
For each microservice invocation, Hystrix’s Command approach is used (the one in the upper left corner of the image above), and then command is used synchronously, either responsively or asynchronously to determine whether the circuit is fusing (follow the image from left to right).
If the circuit is disconnected, downgrade fallback.
If the line is closed, but the thread resource is gone and the queue is full, then traffic limiting is implemented (see step 5).
If the execution succeeds, the run() method is used to get response, but if this process fails, the fallback is continued.
At the same time, there is a suffix “health” at the top of the diagram, which is a component that calculates whether the whole link is healthy or not, and it records every step.
Container and service choreography engine
From physical machines to virtual machines and from virtual machines to containers; From physical cluster to Open Stack, open Stack to Kubernetes; Technology is constantly changing, and so are our perceptions.
Let’s start with the container, which is first and foremost a relatively independent running environment, somewhat similar to, but not quite as thorough as, a virtual machine. A VM packs virtual hardware, kernel (that is, operating system), and user space into a new VM. The VM can run on physical devices using the VM Hypervisor. Virtual machines rely on hypervisors, which are often installed on top of “bare-metal” system hardware, leading to hypervisors being considered an operating system in some ways. Once the hypervisor is installed, virtual machine instances can be allocated from the system’s available computing resources, and each virtual machine gets a unique operating system and payload (applications). In short, a virtual machine needs to first virtual a physical environment, then build a complete operating system, then build a layer of Runtime, and then run applications. For container environments, there is no need to install the host operating system and the container layer (such as LXC or libContainer) is installed directly on top of the host operating system (usually a Linux variant). After the container layer is installed, container instances can be allocated from the system’s available computing resources, and enterprise applications can be deployed in the container. However, each containerized application shares the same operating system (a single host operating system). A container can be viewed as a virtual machine that holds a set of specific applications. It makes direct use of the kernel of the host machine, has fewer layers of abstraction than a virtual machine, is more lightweight, and starts up very quickly.
Containers are more resource efficient than virtual machines because they do not require a separate operating system for each application — instances are smaller and faster to create and migrate. This means that a single operating system can host more containers than a virtual machine. Cloud providers are keen on container technology because more container instances can be deployed on the same hardware. In addition, containers are easy to migrate, but can only be migrated to other servers with compatible operating system kernels, which limits migration options. Because containers are not packaged with the same kernel or virtual hardware as virtual machines, each container has its own isolated user space, allowing multiple containers to run on the same host system. We can see that all operating system-level architectures can be shared across containers, and the only things that need to be built independently are binaries and libraries. Because of this, the container is extremely lightweight.
Our most common container is daocker, which can be found at ๐https://www.docker.com/
The container arrangement
In the past, virtual machines could manage virtualization through the cloud platform Open Stack. How to manage containers in the container era? That’s where the container choreography engine comes in.
Apache mesos
Mesos is based on the master and Slave architecture. The framework decides how resources are used, the master manages the machine, and the slave periodically reports the condition of the machine to the master, who then sends the information to the framework. Master is highly available because zK also has leader. Below is the architecture diagram ๐
kubernetes
Kubernetes is a very popular open source container choreography engine recently, please refer to kubernetes Chinese documentation
Kubernetes design concept and function is actually a similar to Linux layered architecture, first talk about each Kubernetes node internal, Kubelet global management pod, and each POD carries one or more containers, Kube-proxy is responsible for network proxy and load balancing.
Outside the Kubernetes node, it is the corresponding control and management server, which is responsible for unified management of scheduling, distribution and operation of each node.
Service grid
. To be updated
Materials and literature
Martin Fowler’s description of microservices
The theoretical basis of microservices architecture – Conway’s Law
Call chain selection of Zipkin, Pinpoint, SkyWalking, CAT
The end of the
That’s the end of the show. Want to know more about the new poses?
Please visit my personal blog
This post is originally published on my blog, and may be published on CSDN, SegmentFault, Nuggets, Jane, open Source China. If there are similarities, fate, brother. Hurry up to add a good friend, we two want a number, buy a lottery ticket, earn him a few million ๐