Introduction to vernacular spring cloud

Time:2021-1-19

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.

Introduction to vernacular spring cloud

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 understandSpring CloudIt’s a one-stop solution for microservice system architecture. In the process of building microservices, we need to do the followingService discovery registrationConfiguration centerMessage busload balancing Circuit breakerData 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 earliestReleaseeditionAngel, the secondReleaseeditionBrixton(British place name), and thenCamdenDalstonEdgwareFinchleyGreenwichHoxton

Eureka: a service discovery framework for spring cloud

EurekaIt’s based onREST(representative state transition) services, mainly inAWSThe cloud is used for location services to achieve load balancing and middle tier server failover. We call this serviceEurekaThe server. Eureka also comes with aJavaClient component ofEureka ClientIt makes it easier to interact with services. The client also has a built-in load balancer to perform basic cyclic load balancing. stayNetflixMore complex load balancers willEurekaPackaged to provide weighted load balancing based on traffic, resource usage, error conditions and other factors to provide excellent flexibility.

in general,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 servicesConsumerAnd those landlords are the same as those in micro servicesProvider. consumerConsumerThe provider needs to be calledProviderSome 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.

Introduction to vernacular spring cloud

Now, that’s our model.

Introduction to vernacular spring cloud

However, there will be some problems at this time.

  1. 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
  2. Does the tenant have to do the sameregisterWhat about it? Otherwise, how can party B come?
  3. 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

Introduction to vernacular spring cloud

All right, let’s finish this example!

We can see aboutEurekaYou’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).

Service registration

Official explanation: whenEurekaClient toEureka 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)metaData)。

Service renewal

Official explanation:EurekaThe customer sends a heartbeat every 30 seconds (by default) to renew the contract. Let me know by renewing my contractEureka ServerTheEurekaThe customer still exists and there is no problem. Normally, ifEureka ServerNot received in 90 secondsEurekaIt will remove the instance from its registry.

Combined with intermediary understanding: landlord (provider)Eureka Client Provider)Tell the intermediary (server) regularlyEureka 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

Official explanation: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 toEurekaThe 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.EurekaClient andEurekaThe server can communicate in JSON / XML format. By defaultEurekaClient uses compressionJSONFormat 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) regularlyEureka 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:DiscoveryManager.getInstance().shutdownComponent();

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.

Service elimination

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 regularlyEureka 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 areNetflixIt’s officialEurekaArchitecture diagram, you will find that it is no different from the intermediary diagram we drew earlier.

Introduction to vernacular spring cloud

Of course, there are many components that can serve as service discovery:ZookeeperConsulEurekaAnd so on.

More aboutEurekaYou 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

What is?RestTemplate?

It’s not aboutRibbonIs that right? What’s the pointRestTemplateWhat happened? Don’t worry. Listen to me.

I don’t listen. I don’t listen. I don’t listen.

Let me just say it!RestTemplateyesSpringProvides a client class to access HTTP servicesHow to say? That is, the call between microservices is usedRestTemplate. 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.

@Autowired

If you’re interested in the source code, you’ll find what we said aboveEurekaIn the frameregisterRenewal of contractThe bottom layer is usedRestTemplate

Why do you need ribbon?

RibbonyesNetflixOne 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.

Introduction to vernacular spring cloud

If we don’t do something at this timeEqualization operationIf we’re rightSecond kill system 1Making a large number of calls, while the other two don’t request at all, results inSecond 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?

thereforeRibbonThere 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.

Introduction to vernacular spring cloud

Its working principle isConsumerAfter 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 famousNignxIt’s time to go, but withRibbonThe 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.

Introduction to vernacular spring cloud

We can see thatNginxAll requests are received for load balancing, while forRibbonFor example, it is load balancing on the consumer side. As shown in the figure below.

Introduction to vernacular spring cloud

Please note thatRequestThe location of theNginxIn, the request goes to the load balancer first, and in theRibbonLoad balancing is performed on the client before the request is made.

Several load balancing algorithms of ribbon

Load balancing, no matterNginxstillRibbonAll need the support of its algorithm, if I remember correctlyNginxPolling and weighted polling algorithms are used. And inRibbonThere are more load balancing scheduling algorithms in, which are used by defaultRoundRobinRulePolling policy.

  • RoundRobinRule: polling policy.RibbonDefault policy. If after a round of polling, no availableprovider, which can poll up to 10 rounds. If not, returnnull
  • RandomRule: random policy, from all availableproviderRandomly choose one from the list.
  • RetryRule: retry policy. First according toRoundRobinRuleStrategy acquisitionproviderIf 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 ChinaRibbonYou’re OK in the middleCustom load balancing algorithmYou just need to implementIRuleInterface, and then modify the configuration file or customize itJava ConfigClass.

What is open feign

YesEurekaRestTemplateRibbon, we can happily call between services, but usingRestTemplateIt’s still inconvenient. We have to make such a call every time.

@Autowired

This is called every timeRestRemplateOfAPIIs 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, usingRibbonLoad balancing, soOpenFeignBuilt in directlyRibbon

Imported inOpen FeignThen we can write happilyConsumerThe end of the code.

//Use the @ feignclient annotation to specify the name of the provider

And then we’re inControllerIt can be called as beforeServiceLayer code.

@RestController

Essential hystrix

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 wholeHystrixIt’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.

Introduction to vernacular spring cloud

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.

Introduction to vernacular spring cloud

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.

Introduction to vernacular spring cloud

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, thenHystrixThen 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 meanHystrixInCircuit breaker modeYou can use simple@HystrixCommandAnnotation to mark a method, so thatHystrixWill 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.

@HystrixCommand(

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 toHystrixOfBackup processingpattern. You can set thefallbackMethodTo 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 courseHystrixDashboardIt isFor real-time monitoringHystrixThe 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 itEurekaThen we knewService provideryesconsumeradoptEureka ServerTo visit, i.eEureka 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 limitingroutemonitorAnd other functions.

Introduction to vernacular spring cloud

Yes, the gateway has some functions,ZuulBasically. andZuulThe key to this isRouting and filteringYes, in the official documentsZuulThat’s the title

Router and Filter : Zuul

Zuul’s routing function

Simple configuration

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 askedEureka ServerRegistered twoConsumer, threeProvicerWe’ll add another one at this timeZuulGateway should be like this.

Introduction to vernacular spring cloud

Emmm, there’s a lot of information. Let me explain. I will not explain the previous knowledge.

first,ZuulNeed toEurekaWhat are the benefits of registration?

You are stupid,ConsumerAll toEureka ServerAfter registration, can I get all the information as long as I registerConsumerWhat’s your message?

What’s the advantage of getting information?

I get the information. Can I get all of itConsumerWhat 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 callsConsumer1Interface forlocalhost: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 aboutZuulThe most basic configuration, see below.

server:

Then add the@EnableZuulProxyJust annotations. Yes, it’s that simple.

Unified prefix

This is very simple, that is, we can add a uniform prefix in front of it. For example, what we just called islocalhost:9000/consumer1/studentInfo/updateWe’re here at this timeyamlAdd the following to the configuration file.

zuul:
  prefix: /zuul

So we need to go throughlocalhost:9000/zuul/consumer1/studentInfo/updateTo visit.

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 timelocalhost:9000/zuul/FrancisQ1/studentInfo/updateWe visited.

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: "*"

Path masking

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, likeCookieSet-CookieSensitive request header information such aszuulIf 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 isZuulIf you’re kidding, thenfilternamelyZuulIt’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 limitingGrayscale PublishingAuthority controlwait.

Simple implementation of a request time log printing

To achieve their own definitionFilterWe just need to inheritZuulFilterThe 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.

Introduction to vernacular spring cloud

Filter type:PreRoutingPost. PrepositionPreIt’s filtering before the request,RoutingRouting filter is the routing strategy we mentioned above, andPostThe post filter is in theResponseA 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
//Lombok's log

The above simple implementation of the request time log printing function, do you feel itZuulHow 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.

Introduction to vernacular spring cloud

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 itZuulTo 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.

of course,ZuulAs a gateway, it certainly existsSingle point problemIf we want to make sureZuulHigh availability, we need to carry outZuulIn this case, additional load balancers such asNginx

Spring cloud configuration management — config

Why use for configuration management?

When our microservice system began to grow slowly, so manyConsumerProviderEureka ServerZuulThe 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 todaySpring Cloud Config

Framework for configuration managementSpring Cloud ConfigOne, you can choose according to your needs(disconfApollo, etc.). And forConfigFor example, some places are not so satisfactory.

What is config

Spring Cloud ConfigIt provides server and client support for external configuration in distributed system. useConfigServer, which can manage the external properties of applications in all environments in a central location.

In short,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 ourSpring 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.

Introduction to vernacular spring cloud

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 itWebhooksThis isgithubIt 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.

Introduction to vernacular spring cloud

And generally we use itBusMessage bus+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 asSpring Cloud BusIt’s the role ofManaging and broadcasting messages in Distributed SystemsWhich is the broadcast mode in the message engine system. Of course, asMessage busOfSpring Cloud BusYou can do a lot of things, not just the client configuration refresh function.

And haveSpring 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.

Introduction to vernacular spring cloud

summary

In this article, I take you to a preliminary understandingSpring CloudThey have

  • EurekaService discovery framework
  • RibbonIn process load balancer
  • Open FeignService call mapping
  • HystrixService degradation fuse
  • ZuulMicroservice gateway
  • ConfigMicro service unified configuration center
  • BusMessage bus

If you can understand the picture at the beginning of the article at this time, it means that you are rightSpring CloudWe have a certain understanding of microservice architecture.