First of all, let me show you a picture. If you don’t understand this picture very well, I hope you will suddenly realize it after reading my article.
What is spring cloud
Building a distributed system doesn’t need to be complex and error prone. Spring cloud provides a simple and easy to accept programming model for the most common distributed system pattern, helping developers build flexible, reliable and coordinated applications. Spring cloud is built on top of spring boot, which makes it easy for developers to start and quickly apply it in production.
The official is indeed the official. The introduction is so methodical.
What I understand
Spring CloudIt’s a one-stop solution for microservice system architecture. In the process of building microservices, we need to do the followingService discovery registration 、Configuration center 、Message bus 、load balancing 、Circuit breaker 、Data monitoringSpring cloud provides us with a simple programming model, which enables us to easily build microservice projects on the basis of spring boot.
Version of spring cloud
Of course, this is just a digression.
Spring CloudThe version number of is not the number version number we usually see, but some strange words. These words are the names of London underground station. At the same time, according to the order of the alphabet to correspond to the version time order, such as: the earliest
Angel, the second
Brixton(British place name), and then
Eureka: a service discovery framework for spring cloud
EurekaIt’s based on
REST(representative state transition) services, mainly in
AWSThe cloud is used for location services to achieve load balancing and middle tier server failover. We call this service
EurekaThe server. Eureka also comes with a
JavaClient component of
Eureka ClientIt makes it easier to interact with services. The client also has a built-in load balancer to perform basic cyclic load balancing. stay
NetflixMore complex load balancers will
EurekaPackaged to provide weighted load balancing based on traffic, resource usage, error conditions and other factors to provide excellent flexibility.
EurekaIt’s a service discovery framework. What is service and what is discovery?
For example, we usually rent a house to find an intermediary.
When there is no intermediary, we need a landlord to find out whether there is a house to rent, which obviously will be very difficult. First, you can’t find many houses for you to choose with one person’s ability. Moreover, you don’t want to go on looking like this (you have been looking for so long, you can only make do without suitable ones).We here are equivalent to those in micro services
ConsumerAnd those landlords are the same as those in micro services
ConsumerThe provider needs to be called
ProviderSome of the services provided are just like we need to rent their houses now.
But if only between the tenant and the landlord, their efficiency is very low, the landlord can’t find the tenant, can’t make money, the tenant can’t find the landlord, can’t live. So, later, the landlord must have thought of broadcasting his own housing information (for example, posting a small advertisement on the street), so that the landlord has completed his task (publishing the housing resources), but there are two problems. First, other non tenants can receive this kind of rental news, which is nothing in the real world, but it will appear in the computer worldresource consumption It’s a problem. Second, it’s still very difficult for the tenant to find you. Just imagine that I need to rent a house, and I also need to look for small advertisements on the street one by one. Is it troublesome?
Then what shall I do? Of course, we won’t be so stupid. The first thing we do is look for itintermediaryAh, it provides us with a unified housing supply. We consumers just need to go to it to find it. And for the landlord, they just need to release the housing source in the intermediary.
Now, that’s our model.
However, there will be some problems at this time.
- What if the landlord doesn’t want to sell the house after registration? Do we need to let the landlord knowTerm renewal？ If the landlord does not renew the contract, is it necessary to remove them from the registration list of the intermediaryremove 。
- Does the tenant have to do the sameregisterWhat about it? Otherwise, how can party B come?
- Can an intermediary do itchain storeWhat about it? If this store cannot be used because of some force majeure factors, can we change to another chain store?
To solve the above problems, let’s rebuild the above pattern diagram
All right, let’s finish this example!
We can see about
EurekaYou’ll find how easy it is to understand.
Service discoveryIn fact, it is a “intermediary”, which has three roles in the whole processService provider (renting house), service consumer (tenant), service intermediary (housing intermediary)。
Service provider: is to provide some services to the outside world.
Service consumers: users who need to use some services.
Service intermediaryIn fact, it is a “bridge” between service providers and service consumers. Service providers can register themselves with service intermediaries, while service consumers can find service providers registered in service intermediaries if they need to consume some services (use some functions).
Official explanation: when
Eureka ServerWhen registering, it provides its ownmetadata, such as IP address, port, health indicator, URL, home page, etc.
Combined with intermediary understanding: landlord (provider)
Eureka Client Provider）In the mediation (server)
Eureka Server）Where to register the information of the house, such as area, price, location, etc. (metadata)
EurekaThe customer sends a heartbeat every 30 seconds (by default) to renew the contract. Let me know by renewing my contract
EurekaThe customer still exists and there is no problem. Normally, if
Eureka ServerNot received in 90 seconds
EurekaIt will remove the instance from its registry.
Combined with intermediary understanding: landlord (provider)
Eureka Client Provider）Tell the intermediary (server) regularly
Eureka Server）My house also rent (renew), intermediary (server)
Eureka Server）Keep the information of the house after receiving it.
Get the registration list information, fetch registers：
EurekaThe client obtains the registry information from the server and caches it locally. The client will use this information to find other services for remote invocation. The registration list information is updated periodically (every 30 seconds). Each time the registration list information is returned, it may be related to
EurekaThe client cache information is different,
EurekaClient automatic processing. If the registration list information cannot be matched in time due to some reason,
EurekaThe client will retrieve the whole registry information.
EurekaThe server caches the registration list information, compresses the information of the whole registry and each application, and the compressed content is exactly the same as the content without compression.
EurekaThe server can communicate in JSON / XML format. By default
EurekaClient uses compression
JSONFormat to get the registration list information.
Combined with intermediary understanding: tenants (consumers)
Eureka Client Consumer）Disintermediation (server)
Eureka Server）There is a list of all the housing information (client list)
Eureka Client List）In order to obtain the latest information, the tenant will report to the intermediary (server) regularly
Eureka Server）Get and update the local list there.
Service offline cancel：
Official explanation: the Eureka client sends a cancellation request to the Eureka server when the program is closed. After sending the request, the client instance information will be deleted from the server instance registry. The offline request will not be completed automatically. It needs to call the following contents:
Combined with intermediary understanding: landlord (provider)
Eureka Client Provider）Tell the intermediary (server)
Eureka Server）My house is no longer rented, and the intermediary will remove the registered house information from the list.
Official explanation: by default,When the Eureka client does not send service renewal to the Eureka server for 90 consecutive seconds (three renewal cycles), that is, heartbeat, the Eureka server will delete the service instance from the service registration list, i.e. service rejection.
Combined with intermediary understanding: landlord (provider)
Eureka Client Provider）The intermediary (server) is contacted regularly
Eureka Server）Tell him my house is still rented (renewed), if the intermediary (server)
Eureka Server）If you don’t receive the information from the provider for a long time, the intermediary will remove the information from the service.
Here we are
EurekaArchitecture diagram, you will find that it is no different from the intermediary diagram we drew earlier.
Of course, there are many components that can serve as service discovery:
EurekaAnd so on.
EurekaYou can go to the official website to check your knowledge (self-protection, initial registration strategy, etc.) or check another article of mineDeep understanding of Eureka。
The ribbon of load balancing
It’s not about
RibbonIs that right? What’s the point
RestTemplateWhat happened? Don’t worry. Listen to me.
I don’t listen. I don’t listen. I don’t listen.
Let me just say it!
SpringProvides a client class to access HTTP servicesHow to say? That is, the call between microservices is used
RestTemplate. For example, at this time, consumer B needs to call the service provided by provider a, which we need to write. Like the pseudo code below.
If you’re interested in the source code, you’ll find what we said above
EurekaIn the frameregister、Renewal of contractThe bottom layer is used
Why do you need ribbon?
NetflixOne of the company’s open source load balancing projects is a client / in-process load balancer,Running on the consumer side。
Let’s take another example. For example, we designed a second kill system, but for the sake of the whole systemHigh availability, we need to make this system into a cluster, and at this time, our consumers can have multiple ways to call the seckill system, as shown in the figure below.
If we don’t do something at this timeEqualization operationIf we’re right
Second kill system 1Making a large number of calls, while the other two don’t request at all, results in
Second kill system 1Collapse, and the other two become puppets. So why do we need to be a cluster? What’s the significance of our high availability?
RibbonThere it is. Notice the bold words on it——Running on the consumer side. refer to,
RibbonIt is a load balancer running on the consumer side, as shown in the figure below.
Its working principle is
ConsumerAfter getting the list of all the services, the clientinsideuseLoad balancing algorithmTo call multiple systems.
Comparison between nginx and ribbon
mentionload balancing We have to mention the famous
NignxIt’s time to go, but with
RibbonThe difference is that it’s aCentralizedLoad balancer for.
What is centralized? The simple understanding is thatCentralize all requests before load balancing. As shown in the figure below.
We can see that
NginxAll requests are received for load balancing, while for
RibbonFor example, it is load balancing on the consumer side. As shown in the figure below.
Please note that
RequestThe location of the
NginxIn, the request goes to the load balancer first, and in the
RibbonLoad balancing is performed on the client before the request is made.
Several load balancing algorithms of ribbon
Load balancing, no matter
RibbonAll need the support of its algorithm, if I remember correctly
NginxPolling and weighted polling algorithms are used. And in
RibbonThere are more load balancing scheduling algorithms in, which are used by default
RoundRobinRule: polling policy.
RibbonDefault policy. If after a round of polling, no available
provider, which can poll up to 10 rounds. If not, return
RandomRule: random policy, from all available
providerRandomly choose one from the list.
RetryRule: retry policy. First according to
providerIf the acquisition fails, it will be retried within the specified time limit. The default time limit is 500 milliseconds.
There are many more. I won’t mention them one by one here. What you need to know most is the default polling algorithm, and you can change the default load balancing algorithm. You only need to make changes in the configuration file.
providerName: ribbon: NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
Of course, in China
RibbonYou’re OK in the middleCustom load balancing algorithmYou just need to implement
IRuleInterface, and then modify the configuration file or customize it
What is open feign
Ribbon, we can happily call between services, but using
RestTemplateIt’s still inconvenient. We have to make such a call every time.
This is called every time
APIIs it too much trouble, can I like itCall the original code to call between services?
Smart kids must have thought of it, so use itmappingAh, just like the mapping between domain name and IP address. We can map the called service code to the consumer side, so that we can“Seamless development”la
OpenFeignAlso running on the consumer side, using
RibbonLoad balancing, so
OpenFeignBuilt in directly
Open FeignThen we can write happily
ConsumerThe end of the code.
//Use the @ feignclient annotation to specify the name of the provider
And then we’re in
ControllerIt can be called as before
What is the fusing and demoting of hystrix
In a distributed environment, there will inevitably be some failures in many service dependencies. Hystrix is a library that helps you control the interaction between these distributed services by adding latency tolerance and fault-tolerant logic. Hystrix achieves this goal by isolating access points between services, stopping cascading failures between services, and providing backup options, all of which can improve the overall flexibility of the system.
on the whole
HystrixIt’s one that can be carried outFusingandDemotionBy using it, the flexibility of the whole system can be improved.
So what is fusing and demoting? For another example, our whole microservice system is like this. Service a calls service B, and service B calls Service C again. But for some reasons, service C can’t support it. At this time, a large number of requests will block in service C.
Service C is blocked. After all, it’s just a system crash. But please note that at this time, because Service C can’t return a response, the request that service B calls Service C will block. Similarly, if service B blocks, service a will block and crash.
Notice why the blocking crashes. Because these requests will consume the threads, IO and other resources of the system, and the system server will collapse after consuming them.
That’s what it’s calledService avalanche. Mom, two up thereFusingandDemotionYou didn’t explain it to me clearly. What are you talking about nowService avalanche ？
Don’t worry. Listen to me.
If you don’t listen, I have to go on!
so-calledFusingIs an effective solution to the service avalanche. When the request failure rate in the specified time window reaches the set threshold, the system will pass theCircuit breakerDisconnect the request link directly.
That is to say, when service B calls Service C in the specified time window, the call failure rate reaches a certain value, then
HystrixThen the requests between services B and C will be disconnected automatically to avoid service avalanche.
In fact, what we’re talking about hereFusingThat’s what I mean
HystrixInCircuit breaker modeYou can use simple
@HystrixCommandAnnotation to mark a method, so that
HystrixWill useCircuit breakerTo “wrap” this method, whenever the call time exceeds the specified time (default is 1000ms), the circuit breaker will interrupt the call to this method.
Of course, you can set many properties of this annotation, such as setting the timeout, like this.
However, I looked up some blogs and found that they all willFusingandDemotionIn my understanding, the concept of,Demotion is for better user experience. When a method calls abnormally, another code logic is executed to give a user-friendly reply. This corresponds to
HystrixOfBackup processingpattern. You can set the
fallbackMethodTo set alternate code logic for a method. For example, when a hot news appears at this time, we will recommend it to the user to view the details, and then the user will query the details of the news through the ID. however, because this news is too hot (for example, what * easy right recently), a large number of users accessing it at the same time may cause the system to crash, so let’s do itservice degradation , some requests will be demoted, such as the current number of people is too large, please check later, and so on.
//A fallback method call is specified
What is the rest of hystrix
When I read the book “spring micro service pragmatic war”, I also came into contact with oneBulkhead modeIt’s a new concept. Without bulkhead mode, service a calls service B, which is called by defaultUse the same batch of threads to executeWhen a service has performance problems, all threads will be flushed and waiting for processing, and new requests will be blocked at the same time, eventually leading to program crash. Bulkhead mode isolates remote resource calls in their own thread pool so that a single underperforming service can be controlled without crashing the program.
I recommend you to understand the specific principleBulkhead modeDon’t explain too much. And of course
HystrixDashboardIt isFor real-time monitoring
HystrixThe information of each index ofHere, I will throw this problem out, hoping to search for those who don’t know.
Micro service gateway — zuul
Zuul is the front door for all requests from devices and web sites to the back end of Netflix streaming applications. As a boundary service application, zuul is built for dynamic routing, monitoring, flexibility and security. It also has the ability to route requests to multiple Amazon auto scaling groups according to the situation
We learned from it
EurekaThen we knewService provideryesconsumeradopt
Eureka ServerTo visit, i.e
Eureka ServeryesService providerIt’s a unified entrance. So there are so many problems in the whole applicationconsumerIt needs to be called by the user. How should the user access these at this timeConsumer EngineeringWhat about it? Of course, you can visit these projects directly as before. But this way has no unified consumer engineering call entrance, which is not easy to access and manage, and zuul is such a caseconsumerIt’s a unified entrance.
If you have learned the front-end, you must know the router, such as the routing in flutter, Vue, and react. With zuul, you will find that the routing function is basically the same as the front-end configuration routing.
I do flutter occasionally.
You should be very familiar with the gateway. Simply speaking, the gateway is the only external entrance of the system, which is between the client and the server, and is used for processing requestsauthentication 、Current limiting、 route、monitorAnd other functions.
Yes, the gateway has some functions,
ZuulThe key to this isRouting and filteringYes, in the official documents
ZuulThat’s the title
Zuul’s routing function
Originally, I wanted to copy some code for you, but after thinking about it, because the configuration of each code is relatively scattered and looks scattered, I decided to draw a diagram for you to explain.
For example, at this time, we have already asked
Eureka ServerRegistered two
ProvicerWe’ll add another one at this time
ZuulGateway should be like this.
Emmm, there’s a lot of information. Let me explain. I will not explain the previous knowledge.
EurekaWhat are the benefits of registration?
You are stupid,
Eureka ServerAfter registration, can I get all the information as long as I register
ConsumerWhat’s your message?
What’s the advantage of getting information?
I get the information. Can I get all of it
ConsumerWhat is the metadata (name, IP, port)?
What’s the benefit of getting this metadata? If we get it, can we do it directlyRoute mapping？ For example, the original user calls
localhost:8001/studentInfo/updateCan we call this request in this way?
localhost:9000/consumer1/studentInfo/updateWhat about it? Did you suddenly realize that?
The URL here is not restful in order to let more people understand it.
If you understand the above, you can understand about
ZuulThe most basic configuration, see below.
Then add the
@EnableZuulProxyJust annotations. Yes, it’s that simple.
This is very simple, that is, we can add a uniform prefix in front of it. For example, what we just called is
localhost:9000/consumer1/studentInfo/updateWe’re here at this time
yamlAdd the following to the configuration file.
zuul: prefix: /zuul
So we need to go through
Routing policy configuration
You will find that the previous access method (using the service name directly) needs to expose the micro service name to the user, which will cause security problems. Therefore, you can customize the path to replace the microservice name, that is, to customize the routing policy.
zuul: routes: consumer1: /FrancisQ1/** consumer2: /FrancisQ2/**
You can use it at this time
Service name screening
Don’t think you are good at this time. You can try it. After you configure the routing policy, you can still access the microservice name. At this time, you need to mask the service name.
zuul: ignore-services: "*"
ZuulYou can also specify a masked path URI, that is, as long as the user request contains the specified URI path, the request will not be able to access the specified service. In this way, the user’s rights can be restricted.
zuul: ignore-patterns: **/auto/**
So we can filter out requests about auto.
- Represents the matching of multiple arbitrary paths
- Represents any path at the matching level
Sensitive request header masking
By default, like
Set-CookieSensitive request header information such as
zuulIf the mask is removed, we can remove these default masks. Of course, we can also add request headers to be masked.
Zuul’s filtering function
If the routing function is
ZuulIf you’re kidding, thenfilternamely
ZuulIt’s my weapon. After all, all requests go through the gateway (zuul), so we can do all kinds of filtering, so we can achieve itCurrent limiting，Grayscale Publishing，Authority controlwait.
Simple implementation of a request time log printing
To achieve their own definition
FilterWe just need to inherit
ZuulFilterThe filter class is then written as
@ComponentJust add the annotation to the spring container.
Before I show you the code, I’d like to explain to you some points about filters.
PreIt’s filtering before the request,
RoutingRouting filter is the routing strategy we mentioned above, and
PostThe post filter is in the
ResponseA filter that was previously filtered. You can look at the above figure for understanding, and I will give the corresponding notes below.
//Add spring container
The above simple implementation of the request time log printing function, do you feel it
ZuulHow powerful is the filtering function?
No, OK, let’s come again.
Token bucket current limiting
Of course, it’s not just token bucket current limiting,
ZuulAs long as it is a current limiting job, it can do it. Here I just give a simple example.
Let me first explain what isToken bucket current limitingLet’s go.
First of all, we will have a bucket. If it is not full, it will be filled with a certain amount of waterFixed rateA token will be put in it. When a request comes, the first step is to get the token from the bucket. If it is not obtained, the request will be rejected. If it is obtained, the request will be released. It’s very simple, ah ha ha
Let’s go through it
ZuulTo achieve the token bucket current limiting.
package com.lgq.zuul .filter;
So we can control the number of requests to two in a second. Do you think it’s cool?
Others about zuul
ZuulThe function of the filter is definitely not only the two I have implemented above, it can also be implementedAuthority verificationIncluding the ones I mentioned aboveGrayscale Publishingwait.
ZuulAs a gateway, it certainly existsSingle point problemIf we want to make sure
ZuulHigh availability, we need to carry out
ZuulIn this case, additional load balancers such as
Spring cloud configuration management — config
Why use for configuration management?
When our microservice system began to grow slowly, so many
Eureka Server 、
ZuulThe system will have its own configuration. At this time, we may need to change the configuration of some applications when the project is running. If we do not carry out unified management of configuration, we can only change the configuration of some applicationsGo to the next application to find the configuration file, then modify the configuration file and restart the application。
First of all, for a distributed system, we should not modify the configuration file for each application separately. Moreover, for restarting applications, the service cannot be accessed, so we directly abandon the availability, which we do not want to see.
So is there a wayCan we not only manage the configuration file uniformly, but also modify the configuration file dynamically when the project is running?
That’s what I’m going to introduce today
Spring Cloud Config 。
Framework for configuration management
Spring Cloud ConfigOne, you can choose according to your needs（
disconfApollo, etc.). And for
ConfigFor example, some places are not so satisfactory.
What is config
Spring Cloud ConfigIt provides server and client support for external configuration in distributed system. use
ConfigServer, which can manage the external properties of applications in all environments in a central location.
Spring Cloud ConfigIt is able to store the configuration files of each application / system / module in theUnified place and then managed(GIT or SVN).
If you think about it, our application will load the configuration file only when it is started, then our
Spring Cloud ConfigAn interface is exposed to the startup application to obtain the configuration file it wants. The application obtains the configuration file and then initializes it. Here’s the picture.
Of course, you will have a question here. If I change the corresponding configuration file in the GIT when the application is running, will the started application that depends on this configuration file change its corresponding configuration?
The answer is No.
what? How to modify the configuration file dynamically? That’s not what happenedConfiguration driftIs that right? You scum man, you cheat me again!
Don’t worry. You can use it
githubIt can ensure that the configuration information in the client is updated after the configuration file of the remote library is updated.
Oh, that’s about the same. I’ll find out how to use it.
Wait, listen to me,
WebhooksAlthough it can be solved, you will find that it is not suitable for production environment at all, so you will not use it.
And generally we use it
Spring Cloud ConfigDynamic refresh of configuration.
Lead to spring cloud bus
An event bus used to link services and service instances to a distributed messaging system. It is useful to propagate state changes in a cluster (for example, configuration change events).
You can simply understand it as
Spring Cloud BusIt’s the role ofManaging and broadcasting messages in Distributed SystemsWhich is the broadcast mode in the message engine system. Of course, asMessage busOf
Spring Cloud BusYou can do a lot of things, not just the client configuration refresh function.
Spring Cloud BusAfter that, we just need to create a simple request and add
@ResfreshScopeAnnotation can be used to dynamically modify the configuration. I’ve drawn a picture for you to understand.
In this article, I take you to a preliminary understanding
Spring CloudThey have
EurekaService discovery framework
RibbonIn process load balancer
Open FeignService call mapping
HystrixService degradation fuse
ConfigMicro service unified configuration center
If you can understand the picture at the beginning of the article at this time, it means that you are right
Spring CloudWe have a certain understanding of microservice architecture.