Software is alive, and the architecture you build determines whether the software will have a rough or happy life.

This article is not a tutorial on how to use Spring Cloud, but rather explores what Spring Cloud is, and the context and significance of its birth.

Reprinted from the author of simple books

The original link: www.jianshu.com/p/3899d7f47…

The background,

After 2008, with the rapid development of the Domestic Internet industry, our demand for software system is no longer the low level of “just use it” in the past. Activities such as grabbing red envelopes and Double Eleven Constantly force us to break through the performance limit of software system. Traditional IT enterprises’ development idea of “just use IT” can no longer meet the performance requirements of Internet with high concurrency and large traffic. The distributed system architecture has been the only way to solve this problem in the field of server development. However, due to the inherent complexity of distributed system, it is not as easy to develop a single application by a bunch of frameworks. Therefore, major Internet companies are investing in the development of their own infrastructure. Some of the more famous ones are Ari’s open source project Dubbo, a series of service frameworks developed by Netflix. In such a situation of “a hundred flowers bloom” and duplicate wheels, it is inevitable to emerge a unified standard to simplify the development of distributed systems, and Spring Cloud came into being.

What is Spring Cloud

Spring Cloud is an ordered collection of frameworks. It takes advantage of the development convenience of Spring Boot to subtly simplify the development of distributed system infrastructure, such as service discovery registry, configuration center, message bus, load balancing, circuit breakers, data monitoring, etc., which can be started and deployed with one click using Spring Boot’s development style. Instead of reinventing the wheel, Spring simply combines mature and proven service frameworks developed by various companies and encapsulates them in the Spring Boot style, masking the complex configuration and implementation principles. Finally, a simple and easy to understand, easy to deploy and easy to maintain distributed system development kit was set aside for developers. Spring Cloud is the further encapsulation of multiple open source components of Netflix, and at the same time realizes good integration with Cloud platform and Spring Boot development framework. Spring Cloud is a relatively new microservices framework with only a 1.0 release in 2016. Although Spring Cloud has the shortest time, compared with RPC frameworks such as Dubbo, Spring Cloud provides a full range of distributed system solutions. Spring Cloud provides developers with tools to quickly build in distributed systems (configuration management, service discovery, fuses, routing, micro proxy, control bus, one-time Token, full residence, Leader election, distributed Session, cluster state). Developers using Spring Cloud can quickly launch services or build applications and connect quickly to Cloud platform resources.

As you can see from the figure above, Spring Cloud components work together to support a complete microservices architecture.

  • Eureka is responsible for the registration and discovery of services, which is a good link between services
  • Hystrix monitors calls between services and provides circuit breaker protection for multiple consecutive failures.
  • Hystrix Dashboard,Turbine is responsible for monitoring Hystrix’s fuses and giving them a graphical display
  • Spring Cloud Config provides a unified configuration center service
  • When configuration files change, Spring Cloud Bus is responsible for notifying services to get the latest configuration information
  • All outbound requests and services are forwarded through Zuul, acting as an API gateway
  • Finally, Sleuth+Zipkin was used to record all the request data for our subsequent analysis

Spring Cloud was designed from the very beginning with most of the capabilities required by the evolution of the architecture of Internet companies in mind, such as service discovery registry, configuration center, message bus, load balancing, circuit breakers, data monitoring, etc. These functions are provided in the form of insertion and removal, so that the required components can be reasonably selected for integration during the evolution of the system architecture, making the evolution of the architecture smoother and smoother.

Microservice architecture is a trend, and Spring Cloud provides a standardized, full-site technology solution that may be comparable to the birth of the current Servlet specification, effectively advancing the technological level of server software systems.

Iii. Composition of Spring Cloud

The sub-projects of Spring Cloud can be roughly divided into two categories. One is the encapsulation and abstraction of the existing mature framework “Spring booting”, which is also the largest number of projects. The second type is the implementation of the infrastructure of a part of distributed system. For example, Spring Cloud Stream plays the role of Kafka and ActiveMQ. For our developers who want to quickly practice microservices, the first subproject is enough, such as Spring Cloud Netflix, which is the encapsulation of a set of distributed service framework developed by Netflix, including service discovery and registration, load balancing, circuit breakers, REST client, request routing, etc. This project is one of the sub-projects of Spring Cloud, and the main content is a package of Netflix open source products, which provides self-configurable Netflix OSS integration for Spring Boot applications. With a few simple annotations, developers can quickly configure common modules in their applications and build large distributed systems. Its main modules include: Service discovery (Eureka), circuit breaker (Hystrix), Intelligent Routing (Zuul), client load balancing (Ribbon), etc.

Spring Cloud Netflix is a big boss, whose position is second only to that of the big one. All services of the big one depend on it and integrate with various Netflix OSS components to form the core of micro services. Its younger brothers mainly include Eureka, Hystrix, Zuul, Archaius… There are too many

3.1 Spring Cloud Eureka service discovery

Service Center, Cloud Service Discovery, a REST-based service for locating services to enable cloud middle-tier service discovery and failover. This is the most awesome little brother of SpringCloud, the service center, any little brother needs the support of other little brother need to get from here, similarly you have any special martial arts skills quickly reported, convenient for other little brother to call in the future; The advantage of it is that you don’t need to directly find a variety of younger brother support, you only need to go to the service center to get it, and you don’t need to know where other younger brother to provide support, or several younger brother to support, anyway, just use it, the service center to ensure stability and quality. Spring Cloud Eureka provides the functions of service discovery and service registration in distributed environment. A RESTful service that locates mid-tier services running in an AWS Region. It consists of two components: Eureka server and Eureka client. The Eureka server serves as the service registry server. The Eureka client is a Java client designed to simplify interactions with servers, act as a polling load balancer, and provide failover support for services. Netflix uses a separate client in its production environment that provides weighted load balancing based on traffic, resource utilization, and error status.

3.2 Spring Cloud Ribbon Client load Balancing

The Ribbon provides client-side software load balancing algorithms. The Ribbon client component provides a comprehensive set of configuration options, such as connection timeout, retry, retry algorithm, and more. The Ribbon comes with pluggable, customizable load balancing components. Here are some load balancing strategies used:

  • Simple polling load balancing
  • Weighted response time load balancing
  • Region aware polling load balancing
  • Random load balancing

The Ribbon also provides the following functions:

  • Easy integration with service discovery components such as Netflix’s Eureka
  • Use Archaius for runtime configuration
  • Expose o&M metrics using JMX and publish using Servo
  • A variety of pluggable serialization options
  • Asynchronous and batch operations (coming soon)
  • Automatic SLA Framework (coming soon)
  • System Administration/Metrics Console (coming soon)

Example ribbon Architecture

  • A service registry, Eureka Server, with port 8761
  • The service-hi project runs two instances of port 8762,8763, each registered with the service registry
  • The sercvice- Ribbon port is 8764 and registers with the service registry
  • When the sercvice ribbon calls the HI interface of service-hi from the restTemplate interface, the ribbon takes turns to call the HI interface of service-hi: 8762 and 8763.

3.3、Spring Cloud Config

The configuration Center, also known as the Configuration Management toolkit, allows you to centrally manage the configuration of a cluster on a remote server. It currently supports local storage, Git and Subversion. In the future, everyone’s weapons, guns and fire and other things are gathered together, do not casually bring, convenient for unified management, upgrade equipment in the future.

By centralizing the configuration information, you can configure Spring Cloud Bus to dynamically modify the configuration file. This is still static and needs to work with The Spring Cloud Bus for dynamic configuration updates.

Spring Cloud Config is our usual configuration hub. Spring Cloud Config- Extracts the configuration of an application from a local file and places it on a central server, essentially migrating the configuration information from the local to the Cloud. Thus, it can provide better management and publishing capabilities. Spring Cloud Config consists of a server and a client. The server releases the configuration file stored in Git (SVN) to the REST interface. The client can obtain the configuration from the REST interface on the server. However, clients are not actively aware of configuration changes to obtain new configurations, requiring each client to trigger its own /refresh via POST.

3.4 Spring Cloud Hystrix fuse

Fuse, a fault tolerant management tool designed to provide greater fault tolerance for delays and failures by controlling the nodes of services and third-party libraries through a circuit breaker mechanism. For example, suddenly someone is sick, but you still need its support, and then after calling it, it does not respond for a long time, but you do not know, waiting for the response; There is a chance that other boys are also calling your martial arts stunts, and when there are too many requests, there will be a serious blockage affecting the boss’s overall plan. That’s where Hystrix comes in, when Hystrix sees a guy who’s not in a good state and he’s unstable, they take him off immediately and they bring him in, or they say don’t wait this guy’s not going to be able to do this today, just do what you need to do and get out of the line.

A Cricuit Breaker is a facility that automatically fuses (switches on) when remote services are unavailable and automatically restores (switches on) when remote services are restored. Spring Cloud provides circuit breakers, resource isolation, and self-healing capabilities through Netflix’s Hystrix component.

A circuit breaker prevents an application from trying multiple times to perform an operation that is likely to fail, allowing it to continue without waiting for recovery or wasting CPU cycles when it determines that the failure is persistent. Breaker mode also enables the application to detect whether the fault has been resolved. If the problem appears to have been corrected, the application can try to invoke the action.

Circuit breakers add stability and flexibility to a system, providing stability while the system recovers from failure and minimizing the impact of this failure on performance. It helps to quickly reject requests for an operation that is likely to fail, rather than waiting for the operation to time out (or not return) to keep the system’s response time. If the breaker raises the time of each state change event, this information can be used to monitor the health of components of the system protected by the breaker, or to alert administrators when the breaker trips to be in the open state.

Netflix open-source the Hystrix component to implement the circuit breaker model, and SpringCloud integrates this component. In a microservice architecture, it is very common for a single request to invoke multiple services, as shown below:

Failure of lower-level services can cause cascading failures. When the unavailability of calls to a particular service reaches a threshold (Hystric is 20 calls in 5 seconds) the circuit breaker will be opened.

The fallback method can directly return a fixed value to avoid interlocking failures when the circuit is open.

In microservice architectures, there are often multiple service layers called, and the failure of the underlying service may lead to cascading failures that make the entire system unusable, a phenomenon known as the service avalanche effect. Service avalanche effect is a process in which the unavailability of “service providers” leads to the unavailability of “service consumers” and gradually amplifies the unavailability.

As shown in the figure below, A is the service provider, B is the service consumer of A, and C and D are the service consumers of B. An avalanche effect occurs when the unavailability of A causes the unavailability of B and snowballs the unavailability to C and D.

In this case, the entire service organization needs to have the function of fault isolation to prevent the failure of a service from affecting the whole world. The Hystrix component plays this role in Spring Cloud. Hystrix immediately notifies the calling end of the call failure when a service is invoked for N consecutive times and does not respond, preventing the calling end from waiting and affecting the whole service. Hystrix intervals will check this service again and continue to provide service if it is restored.

In Hystrix Dashboards and Turbine, it is important to monitor fuses when they occur and quickly respond to resolve the problem to prevent further spread of the fault. There are now two tools for monitoring fuse breaks: Hystrix-Dashboard and Turbine

Hystrix- Dashboard is a real-time monitoring tool for Hystrix. Through the Hystrix Dashboard, you can intuitively view the request response time and request success rate of each Hystrix Command. But with the Hystrix Dashboard, you can only see service information within a single app, which isn’t enough. We need a tool to aggregate data from multiple services in the system and display it on the Hystrix Dashboard. The tool is Turbine. The monitoring effect is as follows:

3.5. Spring Cloud Zuul service gateway, intelligent routing

In the microservice architecture mode, the number of back-end service instances is generally dynamic, and it is difficult for clients to find the access address information of dynamically changed service instances. Therefore, in order to simplify the calling logic of the front end, API Gateway is usually introduced as a lightweight Gateway in the project based on microservices. At the same time, relevant authentication logic is also implemented in the API Gateway to simplify the complexity of calling each other between internal services.

Zuul is the technology in the Spring Cloud architecture that supports API Gateway landing. Spring Cloud Zuul Routing is an integral part of the microservices architecture, providing dynamic routing, monitoring, resilience, security and other edge services. Zuul is a Netflix load balancer based on JVM routing and server side.

Its specific role is service forwarding, receiving and forwarding all internal and external client calls. Zuul can be used as a unified access point for resources, as well as perform permissions verification and similar functions on the gateway.

Zuul is a framework for providing edge services such as dynamic routing, monitoring, resilience, and security on cloud platforms. Zuul is the front door for all requests from the backend of the Web site for devices and Netflix streaming applications. Be sure to pass through Zuul first when other families send to look for the elder brother to do business, and see if there is any knife to intercept back, or if you need to find the younger brother directly to take over.

Nginx-like, reverse proxy functionality, but Netflix has added some of its own features to work with other components.

3.6、Spring Netflix Archaius

Configuration management API, a set of configuration management apis that provide dynamic typed properties, thread-safe configuration operations, polling framework, callback mechanism, and more. The configuration can be dynamically obtained by reading the content from the configuration source every 60 seconds (configurable by default). In this way, the modified content can take effect without restarting the service after modifying the configuration file. The premise is that archaius API is used to read the content.

3.7. Spring Cloud Bus

Event, message bus, for propagating state changes in clusters (for example, configuration change events), can be combined with Spring Cloud Config for hot deployment. Equivalent to the water Margin line eight hundred miles of taibao Dai Zong, to ensure that the information between the younger brother to remain unblocked.

Distributed message queue, is the Kafka, MQ encapsulation; Event, message bus, for propagating state changes in clusters (for example, configuration change events), can be combined with Spring Cloud Config for hot deployment. Spring Cloud Bus connects distributed nodes through lightweight message brokers. This is used to broadcast state changes (such as configuration changes) or other message instructions. ** One of the core ideas of Spring Bus is to extend Spring Boot applications through a distributed initiator, which can also be used to establish a communication channel between multiple applications. ** Currently the only way to implement this is to use the AMQP message broker as a channel, and the Settings for the same features (some depending on channel Settings) are in the documentation for more channels. Spring Cloud Bus is widely translated as message bus in China. It can be understood as the management and dissemination of messages in all distributed projects, in fact, the dissemination of messages in distributed systems using MQ broadcast mechanism, currently commonly used Kafka and RabbitMQ. There are many things that can be done by using the mechanism of bus, among which configuration center client refresh is one of the typical application scenarios. We use a diagram to describe the mechanism used by bus in configuration center.

According to this figure, we can see the steps of using Spring Cloud Bus to do configuration update:

  1. Submit code triggers POST to send bus/refresh to client A
  2. Client A receives A request to update the configuration from the Server and sends it to the Spring Cloud Bus
  3. Spring Cloud Bus receives the message and notifies the other clients
  4. Other clients receive the notification and request the Server to obtain the latest configuration
  5. All clients have obtained the latest configuration

3.8 Spring Cloud Security

A package of Spring Security that works with Netflix, the Security toolkit, to add Security controls to your applications, mainly OAuth2. Add security controls to your applications based on spring Security’s security toolkit. The younger brother is very ox nose is responsible for the security of the whole gang, set up different doors to visit specific resources, can not leak the secret sunflower treasure book.

3.9. Spring Cloud Zookeeper

Encapsulation of Zookeeper so that it can be configured for use by other Spring Cloud sub-projects; You can operate the Zookeeper tool package, which is used to register and discover services using Zookeeper. ZooKeeper is a distributed, open source distributed application coordination service. It is an open source implementation of Google’s Chubby and an important component of Hadoop and Hbase. It provides consistency services for distributed applications, including configuration and maintenance, domain name service, distributed synchronization, and group service. ZooKeeper aims to encapsulate key services that are complex and error-prone, and provide users with easy-to-use interfaces, efficient performance, and stable functions. You can operate the Zookeeper tool package, which is used to discover and configure Zookeeper services.

3.10. Spring Cloud Stream

The data flow; Data flow operation development kit, package with Redis,Rabbit, Kafka and more send and receive messages. Spring Cloud Stream is a framework for creating message-driven microservice applications. Spring Cloud Stream is created based on Spring Boot to build individual/industrial Spring applications using Spring Integration to provide connectivity to message brokers. Data flow operation development kit, package with Redis,Rabbit, Kafka and more send and receive messages. A business involves multiple tasks that are triggered by events, which is what Spring Cloud Stream does.

3.11、Spring Cloud Sleuth

With the increasing number of services, the analysis of the invocation chain will become more and more complex, such as the invocation relationship between services, the invocation chain corresponding to a certain request, and the consumption time between the invocation, etc. Monitoring these information becomes a problem. In the actual use, we need to monitor the indicators of services and communication between services, and these data will be the main basis for us to improve the system architecture. Therefore, distributed link tracking becomes very important, and Spring Cloud provides specific solutions: Spring Cloud Sleuth and Zipkin.

Service tracking; Log collection toolkit that encapsulates Dapper,Zipkin, and HTrace operations. The log collection toolkit, which encapsulates Dapper and log-based tracing as well as Zipkin and HTrace operations, implements a distributed tracing solution for SpringCloud applications.

Introduction to the

Spring Cloud Sleuth’s main function is to provide tracking solutions in distributed systems, and it is compatible with Zipkin support. You just need to introduce the corresponding dependencies in the POM file.

Service Tracking analysis

In microservice architecture, services are divided by services. Through REST invocation, an interface exposed to the outside may require the cooperation of many services to complete the interface function. If any service on the link fails or the network times out, the interface invocation will fail. As businesses expand, services call each other more and more complex.

As more and more services become available, the analysis of the invocation chain becomes more complex. The calling relationship between them might be as follows:

The term

  • Span: The basic unit of work, for example, is that sending an RPC in a newly created span is equivalent to sending a response request to an RPC. The span is uniquely identified by a 64-bit ID, the trace is represented by another 64-bit ID, and the span has other data information. For example, summaries, timestamp events, tags, SPAN ids, and progress ids (usually IP addresses) span is constantly started and stopped, while recording time information. When you create a span, you must stop it at some point in the future.
  • Trace: A tree structure consisting of a series of spans. For example, if you’re running a distributed big data project, you might want to create a Trace.
  • Annotation: Annotations used to record the presence of an event in time, some core annotations define the start and end of a request, This annotion describes the start of the span sr-server Received – the Server gets the request and is ready to start processing it, If the network delay is obtained by subtracting the CS timestamp from its SR, ss-server Sent – annotation indicates that the request processing is complete (when the request is returned to the client), Cr-client Received – indicates the end of span, and the Client has successfully Received the reply from the server. If cr subtracts the CS timestamp to get all the time it takes for the client to get a reply from the server graphically Span and Trace using Zipkin annotations in a system:

3.12. Spring Cloud Feign uses HTTP to request remote services

In Spring Cloud Netflix stack, each microservice exposes its own service in the form of HTTP interface, so it must use HTTP client when calling remote service. We can use JDK native URLConnection, Apache’s Http Client, Netty’s asynchronous Http Client, and Spring’s RestTemplate. But Feign is the easiest and most elegant to use. Feign is a declarative, templated HTTP client. Using Feign in Spring Cloud, we can make remote service requests using HTTP have the same coding experience as local methods, without the developer perceiving that it is a remote method, much less an HTTP request. With Feign, we can make HTTP remote calls completely transparent to developers and get a coding experience consistent with calling local methods. This is similar to the way remote services are exposed in Ali Dubbo, except that Dubbo is based on a private binary protocol, while Feign is essentially an HTTP client. If you’re building a micro service with Spring Cloud Netflix, Feign is the best choice.

Feign is a declarative pseudo Http client that makes writing Http clients much easier. With Feign, you just create an interface and annotate it. It has pluggable annotations that use Feign annotations and JAX-RS annotations. Feign supports pluggable encoders and decoders. Feign integrates the Ribbon by default, and when combined with Eureka, implements load balancing by default.

In short:

  • Feign uses interface-based annotations
  • Feign integrated the Ribbon

3.13. Spring Cloud for Cloud Foundry

Cloud Foundry is the industry’s first open source PaaS Cloud platform from VMware. It supports multiple frameworks, languages, runtime environments, Cloud platforms and application services, enabling developers to deploy and extend applications in seconds. Not having to worry about any infrastructure issues is really a solution that integrates with CloudFoundry, taking CloudFoundry in its lap.

3.14、Spring Cloud Cluster

Spring Cloud Cluster will replace Spring Integration. Provide basic support for clustering in distributed systems, such as: elections, cluster state consistency, global locking, tokens, and other common state patterns abstraction and implementation. If you want to organize different gangs into a unified whole, Spring Cloud Cluster already provides you with a lot of tools for organizing into a unified whole.

3.15、Spring Cloud Consul

Consul is a service software that supports distributed and highly available service discovery and configuration sharing in multiple data centers. Developed by HashiCorp in Go, Consul is open source based on the Mozilla Public License 2.0 protocol. Consul supports health checks and allows HTTP and DNS protocols to call apis to store key-value pairs. Spring Cloud Consul encapsulates Consul operations, and Consul is a service discovery and configuration tool that seamlessly integrates with the Docker container.

3.16. Spring Cloud Data Flow

Data Flow is a unified programming model and managed service for developing and performing a wide range of Data processing patterns including ETL, batch and continuous operations. Spring Cloud Data Flow is a native cloud-configurable service for composable microservices running in modern environments. Spring Cloud Data Flow enables developers to create and orchestrate data pipelines for common use cases like data extraction, real-time analytics, and data import/export. Spring Cloud Data Flow is a redesign of Spring XD based on the native Cloud. The project aims to simplify the development of big data applications. Spring XD’s stream processing and batch module refactoring are Spring Boot-based stream and Task/Batch microservices, respectively. These applications are now automated deployment units and they have native support for modern operating environments like Cloud Foundry, Apache YARN, Apache Mesos, and Kubernetes. Spring Cloud Data Flow provides a set of models and best practices for distributed streaming and batch data channels based on microservices.

3.17. Spring Cloud Task

Spring Cloud Task mainly deals with Task management and scheduling of short-lived microservices, such as certain scheduled tasks running once in the evening or certain data analysis tasks running several times temporarily.

3.18. Spring Cloud Connectors

Spring Cloud Connectors simplify the process of connecting to services and getting operations from the Cloud platform, and are highly scalable to build your own Cloud platform with Spring Cloud Connectors. It makes it easy for cloud applications to connect to the backend on various PaaS platforms, such as databases and message broker services.

3.19. Spring Cloud Starters

A Spring Boot-style startup project that provides out-of-the-box dependency management for Spring Cloud.

3.20. Spring Cloud CLI

Spring Boot CLI allows you to quickly build cloud components from the command line.

3.21, Netflix Turbine

Turbine is a tool for aggregating servers to send event stream data to monitor Hystrix metrics under the cluster.

What is the relationship with Spring Boot

Spring Boot is a set of Spring fast configuration scaffolding, you can quickly develop a single micro service based on Spring Boot, Spring Cloud is a Cloud application development tool based on Spring Boot implementation; Spring Boot focuses on a single entity for fast and easy integration, while Spring Cloud is a global service governance framework. Spring Boot uses the concept of default greater than configuration, many integration solutions have been selected for you, can not configure, spring Cloud is based on a large part of the Implementation of Spring Boot, can not be based on Spring Boot? Can’t. Spring Boot can be independently used for development projects without Spring Cloud, but Spring Cloud cannot be separated from Spring Boot, which is a dependency.

Spring -> Spring Boot > Spring Cloud.

V. Advantages of Spring Cloud **

There are so many microservices frameworks such as Dubbo and Kubernetes, why use Spring Cloud?

  • Produced by the Spring family, Spring is unrivaled in the enterprise development framework, with a big head, which can guarantee the subsequent update and improvement. Dubbo, for example, is almost dead right now
  • Have Spring Boot this independent dry will be able to save a lot of things, large and small live Spring Boot are engaged in pretty good. As a big guy of micro service governance, consider very comprehensive, almost all aspects of service governance are considered, convenient development out of the box.
  • Spring Cloud is highly active, tutorials are plentiful, and it’s easy to find solutions to problems
  • In a few lines of code, various platform functions such as fusing, balancing responsibility and service center are completed
  • Spring Cloud also has the disadvantage that it can only be developed in Java and is not suitable for small independent projects.

Vi. Spring Cloud prospect

Spring Cloud is a boon for small and medium sized Internet companies, which often do not have the resources or financial resources to develop their own distributed system infrastructure. Using Spring Cloud’s one-stop solution can significantly reduce development costs while taking their business in stride. At the same time, with the popularity of microservice architecture and Docker container concept in recent years, Spring Cloud will also have a neutral place in the future more and more “Cloud” software development style, especially in the current various distributed solutions to provide standardized, full-site technology solutions. The significance may be comparable to the birth of the current Servlet specification, effectively advancing the technological level of the server software system.

Make progress together, learn and share

Welcome to pay attention to my public number [calm as code], massive Java related articles, learning materials will be updated in it, sorting out the data will be placed in it.

If you think it’s written well, click a “like” and add a follow! Point attention, do not get lost, continue to update!!