Spring Cloud 5 major components

Spring Cloud Family Bucket is used out of the box. It is used by small and medium-sized companies and contains many components (gateway, configuration center, fuses, downgrades). .). This article introduces the core components of Eureka, Ribbon, Feign, Hystrix, Zuul.

1.Eureka

Each machine in Eureka is peer-to-peer, and the machines in the cluster have equal status, and each service can be registered and discovered to any Eureka instance. After any Eureka in the cluster receives a write request, it will automatically synchronize to all Eureka instances. Unlike the consistent synchronization mechanism CP used by Dubbo, Eureka uses AP. The timeliness of using the default configuration service to go online and offline service perception is very bad, and it may take tens of seconds or even minutes.

The principle of the service registration center is shown in the figure below:

share picture

< /p>

where service A, service B, Service C has Eureka Client components, which are responsible for registering service registration information to Eureka services.

Eureka is a registry, which saves a registry to record the machine information and port number of each service.

2. Feign

The order service registration center does know where service A, service B, and service C are, and which port numbers are also monitored NS.

But when they need to communicate with each other, do they have to write a lot of code manually to allow them to establish a network connection , Encapsulate the parameters they need to pass, send the request, and process the corresponding data?

If you publish an interface based on SpringCloud, it is actually based on the http protocol, and the one published externally is the most common The SpringMVC interface.

Feign, by annotating an interface, he will generate a dynamic proxy based on the interface marked by the annotation, and then focus on Feign’s dynamics When the agent calls his method, it will generate a request in http protocol format at the bottom layer,

IP: port number/order/create ? productId=1

Finally, for this address, initiate a request and parse the response.

span>

3.Ribbon

if Service A is deployed on two machines. How does Feign know which machine to request? Ribbon comes in handy

Feign uses HttpClient at the bottom layer. First, you have to use Ribbon from the local Eureka registry cache Get a list of each other’s machines

Then proceed to Ribbon to select a machine based on the load balancing algorithm

Feign will send an Http request for this machine

4 .Hystrix

Hystrix is ​​an isolation, fusing, and downgrading framework.

Isolation: When service A requests to call service B and C, there is a thread pool when service B is requested, and service C is a Thread Pool. They do not affect each other

Fuse: When service A completes a logical business, it needs to call service B, if service B hangs If it is dropped, it will be stuck for a few seconds each time it is called, which will affect the user’s operating experience. We generally fuse service B directly, and do not go through the network request for a few seconds. This process is fuse.

Downgrade: Service B is blown, and service B can’t do nothing at this time. At this time, let’s downgrade. At this time, you need to record a message in the database table or log file, saying that a certain user failed because of service B, and the operation was unsuccessful, and the parameters were added. When the service B responds again, you can manually fill in the records based on these records. record.

span>

5.Zuul

this The component is mainly responsible for network routing. When the number of Spring Cloud clusters you deploy reaches several hundred, each service instance has its own name. When you need to call it, it is impossible to use the name of each service instance. To call, a centralized gateway is needed at this time.

With a gateway, if the front-end and mobile end want to call the back-end system, it doesn’t matter how many service instances there are in the back-end, and all requests Just go directly to the gateway, and the gateway will forward the request to all microservices in the background.

span>

Summary:

Eureka: When the service starts, Eureka Client will register the service to Eureka Server

At the same time, Eureka Client can also pull registration from Eureka Server in turn Table, so as to know where other services are

Ribbon: When a request is initiated between services, load balancing is performed based on the Ribbon. Choose one of multiple machines

Feign: Based on the dynamic proxy mechanism, according to the annotations, splice the request URL address, select the machine, and initiate Request

Hystrix: Requests between services go through different Hystrix thread pools, avoiding service avalanches

Zuul: Provide a unified request entry, Zuul will forward these requests to the corresponding service

Leave a Comment

Your email address will not be published.