Updated on July 18, 2019Copy the code

preface

Today, we’ll continue to look at springCloud. There are a lot of components in springCloud, and the most outstanding manufacturer is NetFlix. It can be said that this manufacturer almost owns some of the most important services in springCloud, such as: Service Discovery — Netflix Eureka, Customer side load Balancing — Netflix Ribbon, Circuit breaker — Netflix Hystrix, Service Gateway — Netflix Zuul. That’s why the same vendor announced last year that it would no longer be updating Eureka and Hystrix, which actually made a huge change to the springCloud ecosystem.

Of course, even so, in the actual production process, we still use most of the Components of NetFlix, because these components are no longer updated, but in recent years, I believe the market is still NetFlix, because their components are really mature and meet the market requirements.

Therefore, our topic today is springCloud components, which of course includes Components from NetFlix manufacturers.

Let’s go party!!!

Ps: everyone go to see “band summer” this variety show, in which the hedgehog band’s lead singer Zijian, is said to be the programmer who can sing the most ( ̄▽ ̄)~*.

Components and analysis of springCloud

Before we begin to understand the components of springCloud, we need to understand what microservices are. After all, the foundation of our springCloud is SpringBoot, and SpringBoot is microservices.

1. Micro services

Microservices are about splitting a project into multiple projects that run independently of each other. Data processing and calls are communicated via Http or Socket. These small Web services can be compiled and deployed independently and communicate with each other through their exposed API interfaces. They collaborate with each other, providing functionality to the user as a whole, but can be expanded independently.

Hence the derived programming thinking of microservices: HTTP restful.

There must be some people who ask why they don’t use RPC to program. The performance index of RPC, which is high efficiency and low latency, is believed to be the dream of many micro services. If a request requires more than 3 services and each service has complex business logic, and even involves many IO operations, is RPC more secure?

We’ll leave that for later, but today we’ll get to the point.

Ii. Usage scenarios of microservice components

1. We divided the whole system into several subsystems according to the business. 2: Each subsystem can deploy multiple applications, and multiple applications use load balancing (Netflix Ribbon). 3: A service registry is required, all services are registered in the registry, and load balancing is achieved by using certain policies through services registered in the registry center (Netflix Eureka). 4: All clients access the background service through the same gateway address. Through route configuration, the gateway determines which service processes a URL request. Load balancing is also used when requests are forwarded to the service (Netflix Zuul). 5: Services sometimes need to access each other. For example, if there is a user module, other services need to fetch the user data of the user service while processing some business (Feign, which is essentially the Ribbon+Hystrix service invocation with annotations, the code looks better). 6: A circuit breaker is needed to deal with timeouts and errors during service invocations in a timely manner, preventing the entire system from crashing due to problems with one of the services (Netflix Hystrix). 7: Also need a monitoring function to monitor the time spent on each service invocation etc. (Turbin). 8: also need a configuration center, support local repository, SVN, Git, Jar configuration mode, similar to regulations, everyone from here to obtain the specified configuration. (Config)Copy the code

There are so many components in springCloud, but in real development, we might not use so many components, so in real development, we don’t need to learn them all at once, just focus on the important ones. Of course, today we will try to take a quick look at all the components.

Component analysis of srpingCloud

Now let’s start to learn about springCloud components, please put on your earphones, I recommend a song, recently a favorite song “Say It Again”, is a domestic band “Penicillin” song, also participated in the “Band summer” variety show. Lol ヾ(danjun ╹◡╹) Blue “That’s right, I’m a fan of tap water.

Next, I’ll break down some of the important components of springCloud in descending order of importance.

1. Netflix Eureka — Service discovery

There is no doubt that Eureka is the most important component of springCloud.

We need to download Eureka official source code and Spring Cloud Netflix adaptation Eureka code respectively.

Native Eureka code: github.com/Netflix/eur…

Spring Cloud adaptation for Eureka: github.com/spring-clou…

Ps: If you haven’t downloaded Git, please download git to download the source code on Github.

Service Discovery (Eureka) is one of the core components of NetFlix. It is responsible for registering services and managing the list of services. All microservices need to be registered on Eureka. Each service does not need the IP and port of each other, and calls each other through the SERVICE Discovery Center API (a REST service registered in the registry). There is, of course, an additional expense — an expense that all clients must implement some kind of logic to interact with this central point, thus adding an additional network round trip before fulfilling the service request.

SpringCloud uses Eureka for service discovery. When a microservice registers with Eureka, it must send a heartbeat message to Eureka, which then determines if the microservice is online.

If you open multiple servers for the same microservice, Eureka will also display different servers for the same microservice in the registration discovery center, which also relies on Eureka for load-balancing functions similar to nginx distribution services.

At this point, we found that we didn’t know enough about SpringBoot in the previous learning process. Because the actual decoupling of Springboot development is the direct separation of back-end and front-end, not to say that the use of Springboot or springCloud can not be integrated front-end development, but springCloud and Springboot itself is for the separation of front-end and front-end. (in springCloud, the apis for back-end interfaces are registered with Eureka, and front-end developers can even find the apis directly in Eureka.)

Conclusion:

1. It is a pure servlet application and needs to be deployed as a WAR package. Jersey framework is used to realize its RESTful HTTP interface. 3. Synchronization between peers and service registration are all realized through HTTP protocol. Scheduled tasks (such as sending heartbeat messages, periodically clearing expired services, and synchronizing nodes) are implemented using the BUILT-IN Timer of the JDK. 5. Memory caching is implemented using Google's Guava packageCopy the code

There is no need to ask why HTTP is used. Is there anything on the market that does not support HTTP? This is actually one of the reasons why HTTP is used.

2. Netflix Ribbon — Customer service load balancing

The Ribbon is an open source project released by Netflix. Its main function is to provide a client-side software load balancing algorithm that connects Netflix’s middle-tier services together.

We list all the machines in the configuration file for load balancing, and the Ribbon automatically helps us connect them based on certain rules (simple polling, random linking, etc.).

The Ribbon client component provides a complete set of configuration options (connection timeouts, retries, etc.), and you can easily implement custom load balancing algorithms using the Ribbon.

With the integration of Ribbon and Eureka, service consumers can directly invoke services without caring about the service address or port number.

The Ribbon is a soft load balancing client software that can be used in conjunction with other clients that require it. This is just one example.

Here are some basic configuration items:

Random AvailabilityFilteringRule RoundRobinRule: polling RandomRule: : The services that are in the trip state of the circuit breaker due to multiple access failures and the number of concurrent connections exceeds the threshold are filtered out. Then the remaining service list is accessed according to the polling policy. WeightedResponseTimeRule: Weights all services based on average response time. The faster the response time, the higher the service weight, the higher the probability of being selected. RetryRule: The system obtains services based on the RoundRobinRule policy. If the services fail to be obtained, the system tries again within a specified period to obtain available services. BestAvailableRule: Filters out the services that are in the circuit breaker trip state due to multiple access failures and selects the service with the least concurrency. ZoneAvoidanceRule: Default rule that selects a Server based on the performance of the zone where the Server is located and the availability of the Server.Copy the code

If you need to change it, it’s easy. I found some methods on the Internet and posted one:

3. Netflix Zuul — Service Gateway

In the next talent, a lot of things are not good to explain, so can only be moved from the network to a part of the data. I just saw an illustration of Eureka and the Ribbon on a blog.

But in the actual development, we on each service interface will add some permissions check, so in this case, if we are in every time I call the service interface for permission to check, then we must in every time I call the service interface must be combined with permissions check code, this is obviously increased our workload.

Moreover, if the number of microservices is large, the maintenance of some interface invocation rules and service list will become very troublesome, and even require the company to determine the rules of the interface well before development.

Therefore, API gateway (routing gateway) came into being.

Before there was a gateway, the client service consisted of two steps:

1. Obtain all addresses of a client service from EurekaServer 2. Select a machine to access through the Ribbon load balancingCopy the code

After the gateway is imported, the client invokes the gateway in the url format of http://gateway/ service ID/ URL. The gateway execution consists of two steps:

1. Use the service ID to find the client service. 2.Copy the code

Path explanation:

Gateway: indicates the ID of the gateway service registered in Eureka. Service ID: indicates the ID of the client service registered in Eureka. Url: indicates the URL for accessing the client service.Copy the code

On the basis of the previous picture, we post the diagram with the routing gateway:

These are the two main features of Zuul: filtering (permission control) and routing and forwarding.

4. Netflix Hystrix — Circuit breaker

The Chinese translation for Hystrix is porcupine, which is a pig covered with spines. Its main function is – self-protection. Hystrix has features such as service degradation, fuses, thread pool isolation, semaphore isolation, caching, and so on, basically covering the problems encountered when calling dependent services in microservices.

To ensure high availability, individual services are typically deployed in clusters. Due to network or its own reasons, the service cannot be guaranteed to be 100% available. If a single service has problems, the thread blocking will occur when calling the service. At this time, if a large number of requests flood in, the thread resources of the Servlet container will be consumed and the service will break down. The "avalanche" effect of service failures is the spread of service-to-service dependencies, which can have catastrophic consequences for the entire microservice system.Copy the code

When executing a call service method, if there is a problem with the call method, such as a request timeout, an exception thrown, a thread pool rejection, or a meltdown, a degrade method is defined for the method so that it can be executed in the event of a problem and an alternate return is implemented. We’ve already implemented service degradation by annotating @hystrixCommand (fallbackMethod = “defaultMethod”) to methods that need to be degraded in case of a problem.

FallbackMethod Specifies the method to execute after the downgrade. Methods defined in this class can be public, private, or protected. After the annotation methods problems, such as overtime not return (execution. The isolation. Thread. TimeoutinMilliseconds to configure), will be executed backup method, return to the standby methods return values. Of course, the degraded method can also define the next level of degraded method, implementation as above.

The above mentioned method can also trigger service degradation if it throws an exception. However, if we have a custom exception and need to throw the exception to the upper layer for operation, we do not want Hystrix to catch the custom exception performing service degradation. You can use @hystrixCommand (ignoreExceptions ={myexception.class}) to define exceptions that are ignored to catch. Multiple exceptions are separated by commas. It is also possible to pass a thrown exception to a degraded method via an input parameter to achieve different handling of different types of exceptions.

summary

At this point, I believe you have an overview of springCloud, although in reality, springCloud is much more complex than I have described so far. Tomorrow, we’ll continue our look at springCloud.