Original article was published on Deep Learning on Medium
Micro service interview questions
Technology is open source and knowledge is shared. This blog is a little summary and record of your learning. If you are interested in Java and algorithms, you can follow my dynamics and we will study together
1. What is a micro service?
Micro service architecture is an architectural pattern or an architectural style. It advocates the division of a single application into a set of small services. Each service runs in its own process. The services coordinate and cooperate with each other. To provide users with ultimate value. Services use a lightweight communication mechanism to communicate with each other (usually based on HTTP RESTful API).
Each service is built around a specific business and can be independently deployed to a production environment, production-like environment, etc. In addition, a unified and centralized service management mechanism should be avoided as far as possible. For a specific service, it should be selected according to the business context, select the appropriate language, tools to build it, there can be a very lightweight centralized management To coordinate these services, you can use different languages to write services, or you can use different data stores.
From the technical dimension
The core of micro services is to split the traditional one-stop application into services one by one according to the business and thoroughly decouple them. Each micro service provides a service with a single business function, and one service does one thing from a technical perspective. It is a small and independent process, similar to the concept of a process, which can be started or destroyed independently and has its own independent database.
2. How do micro services communicate?
① Remote Procedure Invocation
Access other services directly through remote procedure calls.
Examples: REST, gRPC, Apache, Thrift
Simple and common. Because there is no middle ware agent, the system is simpler
Only support request/response mode, not support others, such as notification, request/asynchronous response, publish/subscribe, publish/async response
Reduced usability, because both the client and server must be available during the request
Use asynchronous messages to communicate between services. Messages are exchanged between services through message pipes to communicate.
Examples: Apache Kafka, RabbitMQ
Decoupling the client and the server, looser coupling improves usability, because the message middle ware caches the message until the consumer can consume it
Support many communication mechanisms such as notification, request/asynchronous response, publish/subscribe, publish/async response
Message middle ware has additional complexity
3. What are the differences between springcloud and dubbo?
SpringCloud and Dubbo can implement the RPC remote call framework and can implement service management.
SpringCloud is a set of comparative website microservice framework. It integrates common distributed solutions and encountered problems. Registration center Eureka, load balancer Ribbon, client call tools Rest and Feign, distributed configuration center Config, service protection Hystrix, gateway Zuul Gateway, service link Zipkin, message bus, etc.
The internal implementation function of Dubbo is not as powerful as Spring Cloud (family bucket), but only implements service governance. It lacks a distributed configuration center, gateway, link, bus, etc. If you need to use these components, you need to integrate other frameworks.
Spring Cloud and Dubbo function comparison
Function name DubboSpring Cloud
Service registration center ZooKeeperSpring Cloud Netflix Eureka, ZooKeeper
Service call method RPCREST API
Service Gateway without Spring Cloud Netflix Zuul
Circuit breaker is not perfect Spring Cloud Netflix Hystrix
Distributed configuration without Spring Cloud Config
Service tracking without Spring Cloud Sleuth
Message bus without Spring Cloud Bus
Data stream without Spring Cloud Stream
Batch task without Spring Cloud Task
4. Please talk about the understanding of SpringBoot and SpringCloud
① SpringBoot focuses on the rapid and convenient development of individual microservices.
② SpringCloud is a global micro service coordination and management framework that integrates and manages individual micro services developed by SpringBoot.
Provide integrated services between various micro services, configuration management, service discovery, circuit breaker, routing, micro agent, event bus, global lock, decision campaign, distributed session, etc.
③ SpringBoot can leave SpringCloud to use development projects independently, but SpringCloud cannot do without SpringBoot, which belongs to the dependency relationship.
④ SpringBoot focuses on the rapid and convenient development of individual microservices, and SpringCloud focuses on the overall service governance framework.
Spring Boot can use the development project independently from Spring Cloud, but Spring Cloud is inseparable from Spring Boot and belongs to the dependency relationship.
5. Problems faced by distributed systems
An application in a complex distributed architecture has dozens of dependencies, and each dependency will inevitably fail at some point.
When calling between multiple micro services, suppose micro service A calls micro service B and micro service C, and micro service B and micro service C call other micro services. This is called “fan-out”. If the response time of a micro service call on the fan-out link is too long or unavailable, the call to micro service A will consume more and more system resources, which will cause the system to crash, the so-called “avalanche effect”.
For high-traffic applications, a single back-end dependency may cause all resources on all servers to saturate within a few seconds. Worse than failure, these applications may also cause increased latency between services, backup queues, threads and other system resources, resulting in more cascading failures throughout the system. These all indicate the need to isolate and manage failures and delays so that the failure of a single dependency cannot cancel the entire application or system.
In general, there are three solutions for the protection of service dependence:
① Fuse mode: This mode is mainly a reference circuit blown. If a line voltage is too high, the fuse will blow to prevent fire. Put it in our system, if a target service call is slow or there is a large number of timeouts, at this time, the call of the service is fused, and for subsequent call requests, the target service will not continue to be called, and it will return directly to quickly release resources. If the target service condition improves, resume the call.
② Isolation mode: This mode is like dividing the system request into small islands by type. When a small island is burnt out, it will not affect other small islands. For example, thread pools can be used for resource isolation for different types of requests. Each type of request does not affect each other. If one type of request thread resource is exhausted, subsequent requests of that type are returned directly, and no subsequent resources are called. There are many usage scenarios for this model, such as disassembling a service, using a separate server to deploy important services, or the multi-center recently promoted by the company.
③ Current limit mode: The above fuse mode and isolation mode are both fault-tolerant processing mechanisms after an error, and the current limit mode can be called a prevention mode. The current limiting mode is mainly to set the highest QPS threshold for each type of request in advance, and if it is higher than the set threshold, the request is directly returned without subsequent resources being called. This model cannot solve the problem of service dependency, but only solves the overall resource allocation problem of the system, because requests that are not limited by current may still cause an avalanche effect.
6. What is service fuse and what is service degradation
The fuse mechanism is a micro service link protection mechanism that responds to the avalanche effect.
When a micro service of the fan-out link is unavailable or the response time is too long, the service will be downgraded, and then the call of the micro service of the node will be fused, and the “error” response information will be quickly returned. After detecting that the node microservice call response is normal, the call link is restored. In the Spring Cloud framework, the fuse mechanism is implemented through Hystrix. Hystrix will monitor the status of calls between micro services. When a failed call reaches a certain threshold, the default is that 20 call failures within 5 seconds will start the fuse mechanism. The annotation of the fuse mechanism is @HystrixCommand.
Hystrix service downgrade
In fact, it is the processing of a single thread barrier in the thread pool to prevent the single thread request time from being too long, which causes the resources to be occupied for a long time and cannot be released, resulting in the thread pool being quickly occupied and causing the service to crash.
Hystrix can solve the following problems
① Request timeout downgrade, insufficient thread resources downgrade, after the downgrade can return custom data
② Thread pool isolation and degradation, distributed services can use different thread pools for different services, so as not to affect each other
③ Automatically trigger downgrade and recovery
④ Request cache and request merge
7. What are the advantages and disadvantages of micro services? Tell me about the pits you encountered during project development?
Each service is sufficiently cohesive and small enough that the code is easy to understand so that it can focus on a specific business function or business requirement
With simple development and improved development efficiency, a service may be dedicated to one thing.
Micro services can be developed independently by a small team consisting of 2 to 5 developers.
Micro services are loosely coupled and are functionally meaningful services, whether they are independent in the development or deployment phase.
Micro services can be developed in different languages.
Easy to integrate with third parties, micro services allow easy and flexible ways to integrate automatic deployment through continuous integration tools such as Jenkins, Hudson, bamboo.
Micro services are easy for a developer to understand, modify, and maintain, so that small teams can pay more attention to their work. No value is needed through cooperation.
Micro services allow you to take advantage of the latest fusion technology.
Micro services are just code for business logic, and will not be mixed with HTML, CSS or other interface components.
Each micro service has its own storage capacity and can have its own database. There can also be a unified database.
Developers have to deal with the complexity of distributed systems
The difficulty of multi-service operation and maintenance, with the increase of services, the pressure of operation and maintenance is also increasing
System deployment dependencies
Communication cost between services
System integration test
8. What are the micro service technology stacks you know? Please list one or two
Springboot, Spring, SpringMVC
Service configuration and management
Netflix’s Archaius, Ali’s Diamond, etc.
Service registration and discovery
Eureka, Consul, Zookeeper, etc.
Rest, RPC, gRPC
Hystrix, Envoy, etc.
Ribbon, Nginx, etc.
Service interface call (simplified tool for client to call service)
Feign et al
Kafka, RabbitMQ, ActiveMQ, etc.
Service Configuration Center Management
SpringCloudConfig, Chef, etc.
Service routing (API gateway)
Zuul et al
Zabbix, Nagios, Metrics, Spectator, etc.
Full link tracking
Zipkin, Brave, Dapper, etc.
Docker, OpenStack, Kubernetes, etc.
Data flow operation development kit
SpringCloud Stream (encapsulation and Redis, Rabbit, Kafka, etc. send and receive messages)
Event message bus
Spring Cloud Bus
9. What is Eureka service registration and discovery
Eureka is a sub module of Netflix and one of the core modules. Eureka is a REST-based service that is used to locate services to enable cloud middle-tier service discovery and fail over. Service registration and discovery is very important to the micro service architecture. With service discovery and registration, you only need to use the service identifier to access the service without modifying the configuration file of the service call. The function is similar to dubbo’s registration center, such as Zookeeper.
10. What is the basic architecture of Eureka?
Spring Cloud encapsulates the Eureka module developed by Netflix to implement service registration and discovery (please compare Zookeeper).
Eureka uses the design architecture of CS. Eureka Server serves as the server for the service registration function, it is the service registration center.
The other microservices in the system use Eureka clients to connect to Eureka Server and maintain heartbeat connections. In this way, the maintenance personnel of the system can use the Eureka Server to monitor the normal operation of each microservice in the system. Some other modules of SpringCloud (such as Zuul) can use Eureka Server to discover other micro services in the system and execute related logic.
Eureka contains two components( Eureka Server and Eureka Client)
Eureka Server provides service registration service
After each node is started, it will be registered in EurekaServer, so that the service registry in EurekaServer will store the information of all available service nodes. The information of service nodes can be seen intuitively in the interface
EurekaClient is a Java client
To simplify the interaction of Eureka Server, the client also has a built-in load balancer using round-robin load algorithm. After the application starts, a heartbeat will be sent to Eureka Server (the default period is 30 seconds). If Eureka Server does not receive a node’s heartbeat within multiple heartbeat cycles, EurekaServer will remove the service node from the service registry (default 90 seconds)
11. As a service registration center, how is Eureka better than Zookeeper?
The famous CAP theory points out that a distributed system cannot satisfy C (consistency), A (availability) and P (partition fault tolerance) at the same time. Since partition fault tolerance P must be guaranteed in a distributed system, we can only make a trade-off between A and C.
Therefore, Zookeeper guarantees CP, Eureka is AP.
Zookeeper guarantees CP
When querying the registration center for a list of services, we can tolerate that the registration center returned the registration information a few minutes ago, but we cannot accept the service and directly down and unavailable. In other words, the service registration function requires higher usability than consistency. However, there will be a situation in zk. When the master node loses contact with other nodes due to network failure, the remaining nodes will re-elect leader. The problem is that the election leader is too long, 30 to 120 seconds, and the entire zk cluster is unavailable during the election, which leads to the paralysis of the registration service during the election. In a cloud deployment environment, it is a high probability that the zk cluster loses the master node due to network problems. Although the service can be eventually restored, the long-term election time caused by the long-term unavailability of the registration is intolerable.
Eureka guarantees AP
Eureka understands this, so it is designed to ensure availability first. Each node of Eureka is equal, and the failure of several nodes will not affect the work of normal nodes. The remaining nodes can still provide registration and query services. The Eureka client will automatically switch to other nodes if it finds that the connection fails when registering with an Eureka. As long as one Eureka is still available, it can ensure that the registration service is available (guarantee availability), but only the information found May not be up-to-date (strong consistency is not guaranteed).
In addition, Eureka also has a self-protection mechanism. If more than 85% of the nodes do not have a normal heartbeat within 15 minutes, then Eureka thinks that there is a network failure between the client and the registration center. Happening:
Eureka no longer removes services from the registration list that should expire because they have not received a heartbeat for a long time
Eureka is still able to accept registration and query requests for new services, but it will not be synchronized to other nodes (ie to ensure that the current node is still available)
When the network is stable, the new registration information of the current instance will be synchronized to other nodes
Therefore, Eureka can well cope with the situation that some nodes lose contact due to network failures, instead of paralyzing the entire registration service like zookeeper.
12. What is Ribbon load balancing
Spring Cloud Ribbon is a set of client load balancing tools based on Netflix Ribbon.
Simply put, Ribbon is an open source project released by Netflix. Its main function is to provide software load balancing algorithms for clients and connect Netflix’s middle-tier services together. Ribbon client component provides a series of perfect configuration items such as connection timeout, retry, etc. Simply put, it is to list all the machines behind Load Balancer (LB for short) in the configuration file. Ribbon will automatically help you connect these machines based on certain rules (such as simple polling, random connection, etc.). We are also very easy to use Ribbon to implement a custom load balancing algorithm.
13. What can Ribbon load balancing do?
LB (Load Balancing)
LB, or Load Balance, is an application often used in microservices or distributed clusters.
Load balancing simply means that the user’s request is evenly distributed to multiple services, so as to reach the HA of the system.
Common load balancing software Nginx, LVS, hardware F5, etc.
Correspondingly, middleware such as dubbo and SpringCloud provide us with load balancing. SpringCloud’s load balancing algorithm can be customized.
That is, an independent LB facility (which can be hardware, such as F5, or software, such as nginx) is used between the service consumer and the provider, and the facility is responsible for forwarding the access request to the service provider through a certain strategy ;
Integrate the LB logic into the consumer. The consumer knows from the service registration center which addresses are available, and then selects a suitable server from these addresses.
Note: Ribbon belongs to in-process LB, it is just a class library, integrated in the consumer process, and the consumer obtains the address of the service provider through it.
14. What is Feign load balancing
Feign is a declarative WebService client. Using Feign can make it easier to write a Web Service client. Its use method is to define an interface, and then add annotations on it. It also supports JAX-RS standard annotations. Feign also supports pluggable encoders and decoders. Spring Cloud encapsulates Feign so that it supports Spring MVC standard annotations and HttpMessageConverters. Feign can be used in combination with Eureka and Ribbon to support load balancing.
Feign is a declarative Web service client, which makes it very easy to write a Web service client. You only need to create an interface and add comments to it.
15. What can Feign do
Feign aims to make it easier to write Java Http clients.
Earlier when using Ribbon+RestTemplate, the use of RestTemplate to encapsulate the HTTP request formed a set of templated calling methods. However, in actual development, since there may be more than one call to the service dependency, often an interface will be called in multiple places, so it is common to encapsulate some client classes for each microservice to wrap these calls to dependent services. Therefore, Feign has made further encapsulation on this basis, and he will help us define and implement the definition of dependent service interfaces. Under the implementation of Feign, we only need to create an interface and configure it using annotations (previously, the Mapper annotation was marked on the Dao interface, and now a microservice interface is marked with a Feign annotation), and the service can be completed The provider’s interface binding simplifies the development of automatically encapsulating service call clients when using the Spring cloud Ribbon.
Feign integrates Ribbon
Use Ribbon to maintain the service list information of MicroServiceCloud-Dept, and achieve client load balancing through polling. What is different from Ribbon is that through feign, you only need to define the service binding interface and use a declarative method to elegantly and simply implement the service call.
Feign calls the Rest service through the interface method (previously Ribbon+RestTemplate), the request is sent to the Eureka server (http://MICROSERVICECLOUD-DEPT/dept/list), Directly find the service interface through Feign. Because of the integration of the Ribbon technology when making service calls, it also supports load balancing.
16. What is a Hystrix circuit breaker
Hystrix is an open source library for handling the delay and fault tolerance of distributed systems. In a distributed system, many dependencies will inevitably fail to call, such as timeouts, exceptions, etc. Hystrix can ensure that in the case of a dependency problem, It will not cause overall service failure, avoid cascading failures, and improve the flexibility of distributed systems.
“Breaker” itself is a switching device. When a service unit fails, the failure monitoring of the circuit breaker (similar to a blown fuse) returns an alternative response that can be handled (FallBack) to the caller. Instead of waiting for a long time or throwing an exception that the caller cannot handle, which ensures that the service caller’s thread will not be occupied for a long time and unnecessarily, thereby avoiding the spread of failures in the distributed system, and even avalanche.
17. What can Hystrix circuit breakers do?
① Service degradation
The overall resources are not enough, and some services will be turned off first, and the difficulties will be overcome before opening.
② Service fuse
The fuse mechanism is a micro service link protection mechanism that responds to the avalanche effect.
When a micro service of the fan-out link is unavailable or the response time is too long, the service will be downgraded, and then the call of the micro service of the node will be fused, and the “error” response information will be quickly returned. After detecting that the node micro service call response is normal, the call link is restored. In the Spring Cloud framework, the fuse mechanism is implemented through Hystrix. Hystrix will monitor the status of calls between micro services. When a failed call reaches a certain threshold, the default is that 20 call failures within 5 seconds will start the fuse mechanism. The annotation of the fuse mechanism is @HystrixCommand.
③ Service limit
④ Near real-time monitoring
In addition to isolating calls to dependent services, Hystrix also provides quasi-real-time call monitoring (Hystrix Dashboard), Hystrix will continuously record the execution information of all requests initiated through Hystrix, and display it to users in the form of statistical reports and graphics, including how many requests are executed per second, how many successes, how many failures, etc. Netflix passes
The hystrix-metrics-event-stream project implements the monitoring of the above indicators. Spring
Cloud also provides the integration of Hystrix Dashboard to convert the monitoring content into a visual interface.
18. What is zuul routing gateway
Zuul includes the two most important functions of request routing and filtering:
Among them, the routing function is responsible for forwarding external requests to specific microservice instances, which is the basis for achieving a unified access to external access, and the filter function is responsible for intervening in the processing of requests, and is the basis for implementing functions such as request verification and service aggregation. .Zuul and Eureka are integrated to register Zuul itself as an application under Eureka service governance, and at the same time get news of other microservices from Eureka, that is, after accessing microservices, they are obtained after Zuul jumps.
Note: The Zuul service will eventually be registered in Eureka
Provide three functions: proxy + routing + filtering
19. What is the Spring Cloud Config distributed configuration center
SpringCloud Config provides centralized external configuration support for microservices in the microservice architecture. The configuration server provides a centralized external configuration for all environments of different microservice applications.
20. What can a distributed configuration center do?
① Centralized management of configuration files, different configurations in different environments, dynamic configuration updates, and sub-environment deployment such as
② Dynamically adjust the configuration during operation, no longer need to write configuration files on the machine deployed by each service, the service will pull the configuration information from the configuration center
③ When the configuration changes, the service does not need to restart == to be able to sense the configuration change and apply the new configuration to expose the configuration information in the form of a REST interface