Spring Cloud China
What is Spring Cloud?
Spring Cloud is an ordered collection of frameworks that integrate and encapsulate well-developed modules in the market, thereby reducing the development cost of each module.
Advantages and disadvantages of Spring Cloud
Annotation-based, no configuration files, decoupled
Spring Cloud vs. Dubbo
Dubbo only implements service governance, while Spring Cloud implements all aspects of microservices architecture, and service governance is just one aspect. Spring Cloud is based on Spring Boot and HTTP, while Dubbo is based on RPC.
SpringCloud distributed development
Through Spring Cloud Eureka to realize the highly available service registry and realize the registration and discovery of microservices; Spring Cloud Ribbon or Feign can be used to implement interface invocation of load balancing between services. Meanwhile, in order to make the distributed system more robust, it can avoid using Spring Cloud Hystrix to wrap service invocation, realize thread isolation and add circuit breaker mechanism. To avoid cascading failures caused by individual service exceptions in microservices architecture.
Five commonly used Spring Cloud subprojects
Service discovery — Netflix Eureka
Customer service load balancing – Netflix Ribbon
Circuit breaker — Netflix Hystrix
Service Gateway — Netflix Zuul
Distributed configuration — Spring Cloud Config
Service discovery — Netflix Eureka
**** Service registry: Eureka-Server
Role: The service registry provides service registration functions
Service provider: Eureka-Client
Function: Registers a service into a service registry
Customer service load balancing – Netflix Ribbon
In Spring Cloud, there are two service invocation methods, Ribbon+RestTemplate and Feign.
When the Ribbon works with Eureka, the Ribbon automatically obtains a list of service provider addresses from the Eureka Server and requests one of the service provider instances based on the load balancing algorithm.
Circuit breaker — Netflix Hystrix
In a distributed architecture, when a service unit fails, an error response is returned to the caller through the fault monitoring of a broken router (similar to blowing a fuse), rather than waiting for a long time. In this way, threads will not be held for a long time due to invocation of faulty services, and the spread of faults in distributed systems is avoided.
Spring Cloud Hystrix implements a series of service protection features such as router disconnection and thread isolation to address the above problems. It is based on the Netflix Hystrix implementation, which aims to provide greater fault tolerance for delays and failures by controlling those nodes that access remote systems, services, and third-party libraries.
Hystrix has powerful features such as service degradation, service circuit breaker, thread and signal isolation, request caching, request consolidation, and service monitoring.
Service Gateway — Netflix Zuul
The microservice interface implemented on the server side usually has a certain permission verification mechanism, such as the verification of user login status. At the same time, to prevent clients from being tampered with requests and other security considerations, some signature verification mechanisms are added. In the microservice architecture, each module needs to be modified. For simplicity, a mechanism is also needed to solve the redundancy problem of pre-verification in accessing the microservice interface in the microservice architecture.
In order to solve the above series of problems, API gateway came into being
API gateway is a more intelligent application server, it is similar to the definition of object-oriented design patterns in the Facade pattern, it is like the appearance of the micro service architecture of the whole system, all the external client access need to pass it for scheduling and filter, it besides request routing and load balancing, calibration filter, and other functions.
By integrating with Eureka, Spring Cloud Zuul registers its services with Eureka and obtains instance information for all other microservices from Eureka. By default, Zuul will use the service name as the ContextPath to create the route map, which in some cases can be used for most routing needs. In addition to some special ones (such as compatibility with older urls), some special configurations are required. However, compared to the previous architecture, the implementation of API gateway through the introduction of Spring Cloud Zuul has been significantly reduced.
Secondly, for redundant signature verification, login verification is redundant in the microservice architecture. A single service can exist independently, but after they are stripped and separated, they are not called to each microservice, but unified calls are made in API gateway service providers to pre-filter microservice interfaces, so as to realize the interception and verification of microservice interfaces. Spring Cloud Zuul provides a filtering mechanism that supports filtering tasks well.
Distributed configuration — Spring Cloud Config
- Config-server Configures the server and manages the configuration information of the service
- Config-client Indicates the client. The client invokes the exposed interface of the server to obtain configuration information