Spring Cloud never understand to give up _microservice

Source: Internet
Author: User
Tags webhook
Why

When we use a new technology, one of the first questions to ask is why: why use this technology. Or ask: What is the problem that this technique can solve. I also want to write a micro-service article, and the advantages and disadvantages of micro-service in the micro-service architecture, when a large system is split into a micro-service system, not only includes function splitting, but also includes system splitting, code splitting, database splitting, cache splitting, etc., deployment, maintenance, call relations, scheduling, monitoring and fail of multiple systems. Over will become a series of problems. At the same time, the more the micro-service system is divided, the longer the call link may be, the call chain monitoring, and the whole link trace will become a problem. The laws of nature and nature are hidden in the night, and God says let Newton be born, and everything is illuminated. That's how spring Cloud was born. Spring Cloud is for service governance.

For a chestnut, when a large system is split into 5 small business systems, the most easily thought back-end architecture is: the front-end needs to maintain multiple systems

It is obvious that the client needs to maintain 5 business system addresses, and it is likely that an action needs to invoke more than 1 business systems to complete, and that there is no guarantee of transactional. The following schema appears: There is no strong association between the subsystems. The API Gateway and the various business systems invoke relationships through load balancing, and the client only needs to invoke the API gateway. It seems to solve the split problem, call problem, and client side problem. But because of the existence of load balancing devices, there is no longer a strong association between the subsystems, the subsystems appear to be unrelated systems, each provides services, and when a subsystem responds slowly, it may cause API gateway or other caller system to slow down, or even cause an avalanche of the entire architecture. Naturally, this problem can set the call timeout to some extent, but spring cloud can provide a more elegant solution. This structure, in order to solve the scheduling, monitoring, fail over, full link trace and other issues, but also need to access other third-party systems or tools, and spring cloud for these problems provide a complete set of solutions. Introduction to Spring Cloud

Let's take a look at what components Spring Cloud contains:

These 6 graphs from https://springcloud.cc/include all of the components that spring cloud now have, as well as the role of each component. Here I introduce 10.

Spring Cloud Config Remote Configuration service. Remote configuration is an essential middleware, remote configuration of the characteristics of the general need: multi-node main standby, configuration, dynamic modification, configuration localization cache, dynamic modification of real-time push and so on. Config allows the configuration file to be placed on git or SVN, and the integration with spring boot is easy, but the disadvantage is that after modifying the configuration on Git, you can only request each service interface one at a time, allowing them to update the configuration without modifying the configuration's push message. Also, if you want to modify the configuration file, do some reinitialization (such as the capacity of the thread pool changes, etc.), you will need some work around method, so it is recommended that if there are other scenarios, do not recommend the choice of Spring Cloud Config.

Spring Cloud bus events, message buses, for propagating state changes in clusters (for example, configuration change events). Often used in conjunction with Spring Cloud CONFIG. Spring Cloud Config itself does not provide real-time updates to the service that is registered. For example, we configure the configuration on Git, then when modifying GitHub configuration content, can configure Webhook to a config-server, but Config-server will not be the configuration update real-time push to the various services. The role of the bus is to link everyone on a single line, all of the server sharing status, and the other server will receive the same hook status when Webhook to one of the servers on buses. But the use of the bus relies on Mq,bus to directly inherit RABBITMQ & Kafka, just configure the address directly in spring, but for other types of MQ, some manual configuration is required. The biggest problem is that it is obviously a bit of a loss to get your system into MQ simply because of Spring cloud bus. I understand that the system should be on the basis of meeting existing business requirements, the simpler the better, the less dependent the shorter the link, the more can reduce the risk of the problem.

Eureka the service discovery component of Spring cloud. This component needs a lot of space and it's best to speak with Consul. Eureka is responsible for service registration and service discovery, in order to be highly available, generally requires multiple Eureka servers to be registered with each other to form clusters. Eureka server synchronization follows a very simple principle: you can propagate and synchronize information as long as there is one edge connecting the nodes. Eureka Internal for registered service mainly through the heartbeat to monitor whether the service has been hung up, the default heartbeat time is 15s. This means that when a service provider is hung up, the service subscriber may not be able to find it for the longest time after 30s. Service startup after Eureka, together with a list of services to the local cache, services registered update, Eureka will be pushed to each service. Eureka also has strategies to prevent the avalanche of all service heartbeats caused by the instability of a service's network. Eureka has a Web page that allows you to see all the service registrations and Eureka cluster status on the page. The Eureka Support Service oneself voluntarily drops oneself, requests the service the following address, may let the services from the Eureka up and down oneself, simultaneously the service process also can stop itself. Curl-h ' Accept:application/json '-x POST Localhost:${management.port}/shutdown

Consul is also a service Discovery tool, with its own key-value storage services, health checks, and Web pages. It sounded a little taller than Eureka, which used the gossip protocol and the Raft protocol, but his disadvantage was that it was harder to maintain than the Eureka. Service registration is a key node of a micro-service architecture. So we're choosing Eureka at this stage, and then the remote configuration is using Spring Cloud CONFIG. If you want to go to the container and the arrangement, will look at the specific situation to choose. However, later found that in fact consul provides an official Docker mirror, the direct use of Docker-consul cluster User Services found, the operation of the cost will be a straight line down, the following will consider the Eureka + Spring Cloud Config Consul.

Ribbon: Client load Balancing component. Service discovery, each service in the local know how many machines they want to invoke, what the IP of the machine is, what the port number is, then this service needs to have a load balancing policy locally, for each request select a target machine to call, And ribbon do is the choice of load balancing strategy. Ribbon provides a variety of load balancing strategies, including Bestavailablerule, Availabilityfilteringrule, Weightedresponsetimerule, Retryrule, Roundrobinrule, Randomrule, Zoneavoidancerule, etc., remember correctly, the default is Zoneavoidancerule. Of course, you can customize your own load-balancing strategy, such as if the invoked service requires a grayscale release or A/b test, you can customize it at the Ribbon level.

Feign-declarative, templated HTTP client. The nature of the call between micro-services or HTTP requests, if you need to write the request code for each request, increase the request parameters, while processing the request results, there will be a lot of duplication of work, and feign very elegant to help us solve this problem, only need to define a interface, Fegin knows how the parameters should be set at the time of the HTTP request. At the same time, feign also integrates ribbon, as long as the ribbon,feign is dependent on the Micro service by default, the load balancing strategy defined by Ribbon is used. Most importantly, feign is not only used in Eureka or ribbon micro-service systems, any system, as long as the use of HTTP calls to Third-party services, can be used to help us resolve the HTTP request of the code repeatedly written.

A

Hystrix circuit breaker, similar to a circuit breaker in a physical circuit. Under normal circumstances, when a service provider is slow to respond due to network, database, or performance reasons, the caller is likely to accumulate a large number of request threads in a short period of time, eventually causing the caller down, or even the entire system crashing. After joining the Hystrix, if Hystrix discovers that one of the service's machine calls is very slow or multiple calls fail, it will break the road in a short time and all requests will not be sent to this machine. If a service has all the machines hung up, hystrix will quickly fail and return immediately to ensure that the callee does not accumulate a large number of threads. Feign the Hystrix is integrated by default. As mentioned above, when using Eureka, when a service provider is hung up, the service subscriber may not be aware of the longest after 30s, then this 30s will have a large number of call failures. If the hystrix is integrated in the system, the dead service provider will be cut off immediately, so that the request can no longer be forwarded to this machine, a large number of reduced call failure. After the Hystrix performs the circuit breaker, it does not mean that the road will be broken forever, but will try to request this road slowly within a certain time interval, if the request succeeds, the circuit breaker will recover. One thing to note is that when Hystrix is doing a circuit breaker, all call requests are placed in a thread pool, and the thread pool is clear and isolated. For example, gateway, the integration of 5 sub-business systems, one of the systems may be a very large number of calls, and the other four system calls are very small, if there is no thread pool, it is obvious that the first system of a large number of calls will affect the subsequent four system call performance. The Hystrix thread pool is the same as the Java standard thread pool, and some parameters can be configured: Coresize, MaximumSize, Maxqueuesize, Queuesizerejectionthreshold, Allowmaximumsizetodivergefromcoresize, Keepalivetimeminutes, and so on, if a certain subsystem of the number of calls suddenly increased, more than the capacity of the thread pool, will also quickly fail, direct return, Play a role in demoting and protecting the system itself. Of course Hystrix also support the non thread pool, in the local request thread to do the call, that is, Semaphore mode, the official does not recommend, unless the system QPS really big.

Zuul is a gateway component. Provides a framework for dynamic routing, monitoring, resiliency, security, and other edge services. Zuul master needs to simply configure the properties file, do not need to write specific code can be implemented to forward the request to the corresponding services up. can also customize some of the filter to do verification, isolation, limit flow, file processing, etc., for the gateway, the use of Zuul can reduce a lot of code. But I have not used, do not understand, now our gateway is mainly based on feignclient, ribbon, hystrix to achieve. Zuul also integrates these components by default. be interested in studying and studying.

Turbine is a tool used by aggregation server to send event flow data to monitor the metrics situation of hystrix under cluster. In a complex distributed system, the nodes of the same service often need to be deployed hundreds or even thousands of times, in many cases, the operators want to be able to put the same service node state as a whole cluster of the form, so as to better grasp the state of the entire system. Turbine provides the aggregation of multiple hystrix.stream into a single data source for dashboard display.

Spring Cloud Starters Spring boot hot-swappable, provides default configuration, out-of-the-box dependencies. Starter is a very basic part of the Spring boot framework. You can customize starter.

In our current background system, all of the above except Spring Cloud bus, consul and Zuul are used. Talking is cheap, github address: Https://github.com/chxfantasy/spring-cloud-demo Author: What if Link: https://www.jianshu.com/p/ 7468643ead77 Source: Jane Book copyright belongs to the author. Commercial reprint please contact the author to obtain authorization, non-commercial reprint please indicate the source.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.