Why do you need to learn about Spring Cloud

Both commercial and consumer applications are very simple at the beginning of a business, and we often implement them as monolithic applications. However, as the business evolves, product ideas become more and more complex and the application of monolithic structures becomes more and more complex. This leads to the following problems for the application:

  • Code structure chaos: the complexity of the business, resulting in a large amount of code, management will become more and more difficult. At the same time, it poses a huge challenge to rapidly iterate the business;
  • Slow development: As developers work on a set of code at the same time, it is difficult to avoid code conflicts. The development process will be accompanied by the constant conflict resolution process, which will seriously affect the development efficiency;
  • High cost of troubleshooting: The process of finding and fixing bugs in an online business may be simple. However, because there is only one set of code that needs to be recompiled, packaged, and brought online, it is expensive.

As the application of monomer structure increases with the complexity of the system, various problems will be exposed. In recent years, microservice architectures have gradually replaced monolithic architectures, and this trend will become more and more popular. Spring Cloud is the most commonly used micro-service development framework at present and has been widely applied in enterprise-level development.

What is the Spring Cloud

The Spring Cloud is an ordered collection of frameworks. It takes advantage of the development convenience of Spring Boot to cleverly simplify the development of distributed system infrastructure, such as service discovery registration, configuration center, intelligent routing, message bus, load balancing, circuit breakers, data monitoring, etc., can be used to do one-button startup and deployment of Spring Boot development style.

Spring Cloud does not reinvent the wheel. It simply combines mature and proven service frameworks developed by various companies, and repackages them in the Spring Boot style to remove complex configuration and implementation principles. The result is a distributed system development kit that is easy to understand, deploy and maintain.

Design goals and advantages and disadvantages

Design goals

Coordinating microservices to simplify distributed system development.

The advantages and disadvantages

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

Advantages:

  • Produced by the Spring family, Spring is unrivalled in enterprise-level development frameworks and has a great power to ensure subsequent updates and improvements
  • The components are rich and the functions are complete. The Spring Cloud provides very complete support for micro-service architectures. For example, configuration management, service discovery, circuit breakers, micro-service gateways, etc.
  • The Spring Cloud community is active and full of tutorials, making it easy to find a solution to a problem
  • Service splitting has finer granularity and lower coupling degree, which is conducive to resource reuse and development efficiency improvement
  • It can more accurately develop and optimize the service plan, and improve the maintainability of the system
  • Reduce the cost to the team, can you develop in parallel, don’t worry about how others are developing, focus on your own development first
  • Microservices can be cross-platform and can be developed in any language
  • Suitable for Internet era, shorter product iteration cycle

Disadvantages:

  • Too many micro services, high management cost, is not conducive to the maintenance of the system
  • The high cost of distributed system development (fault tolerance, distributed transactions, etc.) poses a challenge to the team

In general, the advantages outweigh the disadvantages. At present, it seems that Spring Cloud is a very complete set of distributed framework. At present, many enterprises start to use micro-services, and the advantages of Spring Cloud are obvious. Therefore, it is a good choice for students who want to study micro-service architecture to learn Spring Cloud.

The development prospect of Spring Cloud

Spring Cloud is a boon to small and medium sized Internet companies, which often do not have the strength or sufficient capital to develop their own distributed system infrastructure. Using Spring Cloud as a one-stop solution can greatly reduce development costs while keeping pace with business growth.

At the same time, with the popularity of micro-service architecture and Docker container concept in recent years, Spring Cloud will also have a place in the future more and more “Cloud” software development style neutral, especially in a variety of distributed solutions to provide standardized, all-station technical solutions. The significance may be comparable to the birth of the Servlet specification, effectively promoting the progress of the technical level of the server software system.

The overall architecture

The main project

Spring Cloud subprojects can be roughly divided into two categories. One is the encapsulation and abstraction of the existing mature framework “Spring Boot “, which is also the largest number of projects. The second type is the implementation of infrastructure that develops part of the distributed system, such as Spring Cloud Stream, which plays the role of Kafka and ActiveMQ.

Spring Cloud Config

Centralized configuration management tool, unified external configuration management in the distributed system, the default use of Git to store configuration, can support client configuration refresh, encryption, decryption operations.

Spring Cloud Netflix

Netflix OSS open source component integration, including Eureka, Hystrix, Ribbon, Feign, Zuul and other core components.

  • Eureka: Service governance component, including a registry on the server side and a service discovery mechanism on the client side
  • Ribbon: A load-balanced service invocation component with various load-balanced invocation strategies.
  • Hystrix: A service fault tolerant component that implements a circuit breaker mode to provide fault tolerance for service-dependent errors and delays;
  • Feign: A declarative service invocation component based on Ribbon and Hystrix;
  • Zuul: API gateway component that provides routing and filtering capabilities for requests.

Spring Cloud Bus

A message bus for propagating cluster state changes, using lightweight message brokers to link nodes in a distributed system, can be used to dynamically refresh service configurations in a cluster.

Spring Cloud Consul

Service governance component based on Hashicorp Consul.

Spring Cloud Security

Security toolkit, support for load balancing OAuth2 client and login authentication in Zuul agent.

Spring Cloud Sleuth

Distributed request link tracing for Spring Cloud applications, with support for Zipkin, HTrace, and log-based tracing (for example, ELK).

Spring Cloud Stream

Lightweight event-driven microservices framework that uses a simple declarative model to send and receive messages. The main implementations are Apache Kafka and RabbitMQ.

Spring Cloud Task

A microservices framework for quickly building short, limited data processing tasks to add functional and non-functional features to an application.

Spring Cloud Zookeeper

Service governance component based on Apache ZooKeeper.

Spring Cloud Gateway

API gateway component that provides routing and filtering capabilities for requests.

Spring Cloud OpenFeign

A declarative service invocation component based on Ribbon and Hystrix that dynamically creates interface implementations based on Spring MVC annotations for service invocation has replaced Feign as a first class citizen in Spring Cloud 2.0.

Spring Cloud version relationships

Spring Cloud is a composite project made up of many subprojects, each with a different release tempo. In order to manage the Spring Cloud version dependencies with the sub-projects, a listing is published that includes the sub-project versions for a particular Spring Cloud version.

In order to avoid confusion between the Spring Cloud version number and the subproject version number, Spring Cloud versions are named by name rather than version number. These versions are named by the name of the London Metro station, which corresponds to the version chronological order in alphabetical order, such as Angel is the first version. Brixton is the second version.

When the Spring Cloud releases have reached a critical point or a major BUG has been resolved, it releases a version of Service Releases (SRX). For example, Greenwich.SR2 is the second SRX version of Greenwich released by Spring Cloud. The latest version of Spring Cloud is Hoxton.

Spring Cloud and SpringBoot versions correspond

Spring Cloud and sub-project version correspondence

Note: The Hoxton version is built on SpringBoot 2.2.x and does not work with the 1.5.x version. As SpringBoot version 1.5.x stops maintenance in August 2019, the Edgware version will also stop maintenance.

What is the difference between SpringBoot and SpringCloud?

SpringBoot focuses on developing individual microservices quickly and easily.

SpringCloud is a global microservice coordination and organization governance framework, which integrates and manages individual microservices developed by SpringBoot.

Integration services for configuration management, service discovery, circuit breakers, routing, micro-agents, event buses, global locks, decision campaigns, distributed sessions, and more are provided between micro-services

SpringBoot can use the development project independently of the SpringCloud, but the SpringCloud is dependent on the SpringBoot

While SpringBoot focuses on fast and easy development of individual micro-services, SpringCloud focuses on a global service governance framework.

When developing distributed microservices using Spring Boot, we face the following problems

(1) Complexity associated with distributed systems – This overhead includes network issues, latency costs, bandwidth issues, and security issues.

(2) Service discovery – Service discovery tools manage how processes and services in the cluster find and talk to each other. It involves a service directory in which services are registered, and then the ability to find and connect to services in that directory.

(3) Redundancy – Redundancy in distributed systems.

(4) Load Balancing — Load balancing improves the distribution of workloads across multiple computing resources, such as computers, computer clusters, network links, central processing units, or disk drives.

(5) Performance – Problems Performance problems due to various operational overhead.

(6) Deployment Complexity – DevOps skill requirements.

What does service registration and discovery mean? How is Spring Cloud implemented?

When we start a project, we usually do all the configuration in a properties file. As more and more services are developed and deployed, adding and modifying these properties becomes more complex. Some services may decline, and some locations may change. Manually changing properties can cause problems.

Eureka service registration and discovery can help in this situation. Because all services are registered on the Eureka server and lookup is done by invoking the Eureka server, there is no need to deal with any changes and processing of the service location.

What is the difference between Spring Cloud and Dubbo?

Dubbo is the RPC Spring Cloud REST API

(2) Dubbo is ZooKeeper, SpringCloud is Eureka, and it can also be ZooKeeper

(3) Service gateway, Dubbo itself is not implemented, but can only be integrated through other third-party technologies. SpringCloud has Zuul routing gateway, which serves as a routing server to distribute consumer requests. SpringCloud supports circuit breakers, and supports version control for perfectly integrated configuration files with Git. The Business Bus implements a number of micro-service architecture elements such as configuration file updates and service autowiring.

What is the meaning of load balancing?

In computing, load balancing can improve the distribution of workload across computing resources such as computers, clusters of computers, network links, central processing units, or disk drives. Load balancing is designed to optimize resource usage, maximize throughput, minimize response time and avoid overloading any single resource. Using multiple components for load balancing rather than a single component may improve reliability and availability through redundancy. Load balancing usually involves specialized software or hardware, such as multilayer switches or domain name system server processes.

What is Hystrix? How does it achieve fault tolerance?

Hystrix is a delay and fault tolerant library designed to isolate points of access between remote systems, services, and third-party libraries, stopping cascading failures and achieving resilience in complex distributed systems when failures are inevitable.

Typically, for systems developed using a microservice architecture, many microservices are involved. These microservices collaborate with each other.

Consider the following microservices

Assume that if microservice 9 in the figure above fails, then using the traditional method we will propagate an exception. But it can still cause the whole system to crash.

As the number of microservices increases, the problem becomes more complex. The number of microservices can be as high as 1000. This is where Hystrix comes in and we will use Hystrix’s Fallback method functionality in this case. We have two services, employee-consumer, that use the services exposed by employee-consumer.

The simplified diagram is shown below

Now suppose that for some reason, the employee-producer exposes a service that throws an exception. We define a fallback method using Hystrix in this case. This backup method should have the same return type as the exposed service. If an exception occurs in the exposed service, the fallback method returns some value.

What is a Hystrix Circuit Breaker? Do we need it?

For some reason, the Employee-Consumer exposure service throws an exception. Using Hystrix in this case we defined a fallback method. If an exception occurs in the exposed service, the fallback method returns some default values.

If the exception in the firstPage method() continues to occur, the Hystrix circuit will break, and the employee consumer will skip the firsPage method altogether and call the fallback method directly. The purpose of a circuit breaker is to allow time for the first page method or other methods that the first page method might call and to cause an exception recovery. What can happen is that the problem that caused the exception has a better chance of being recovered under a smaller load.

What is Netflix Feign? What are its advantages?

Feign is a Java client linker inspired by Retrofit, JAXRS-2.0, and WebSocket.

Feign’s first goal is to unify the complexity of constraint denominators into HTTP APIs, regardless of their stability.

In the employee-consumer example, we used a REST service exposed by the employee-producer using the REST template.

But we have to write a lot of code to perform the following steps

(1) Use functional areas for load balancing.

(2) Get the service instance, and then get the basic URL.

(3) Leverage REST templates to consume services. The preceding code looks like this

@Controller
public class ConsumerControllerClient {
@Autowired
private LoadBalancerClient loadBalancer;
public void getEmployee() throws RestClientException, IOException {
 ServiceInstance serviceInstance=loadBalancer.choose("employee-producer");
 System.out.println(serviceInstance.getUri());
 String baseUrl=serviceInstance.getUri().toString();
 baseUrl=baseUrl+"/employee";
 RestTemplate restTemplate = new RestTemplate();
 ResponseEntity<String> response=null;
 try{
  response=restTemplate.exchange(baseUrl,
     HttpMethod.GET, getHeaders(),String.class);
 }
 catch (Exception ex)
  {
  System.out.println(ex);
 }
 System.out.println(response.getBody());
}

The previous code, with the chance of exceptions like NullPointer, was not optimal. We’ll see how we can use Netflix Feign to make calling easier and cleaner. If the Netflix Ribbon dependency is also in the classpath, then Feign will be responsible for load balancing by default.

What is Spring Cloud Bus? Do we need it?

Consider the following situation: We have multiple applications that use Spring Cloud Config to read properties, and Spring Cloud Config reads those properties from Git.

The following example shows multiple Employee producer modules retrieving the Eureka registered property from the Employee Config Module.

What happens if you assume that the Eureka registration property in Git changes to point to another Eureka server. In this case, we would have to restart the service to get the updated properties.

There is another way to use the executor endpoint/refresh. But we’ll have to call this URL separately for each module. For example, if Employee Producer1 is deployed on port 8080, then http:// localhost: 8080 / REFRESH is called. Also for Employee Producer2 http://localhost:8081 / refresh, etc. Again, this is very troublesome. This is where the Spring Cloud Bus comes into play.

Spring Cloud Bus provides the ability to refresh a configuration across multiple instances. Therefore, in the above example, if we refresh Employee Producer1, all the other required modules will be refreshed automatically. This is especially useful if we have multiple microservices up and running. This is achieved by connecting all the microservices to a single message broker. Whenever an instance is refreshed, this event is subscribed to all micro-services that are listening to this broker, and they are refreshed as well. Flush of any single instance can be achieved by using an endpoint/bus/refresh.

Spring Cloud Circuit Breaker

When a service calls another service and there is a problem due to network or internal reasons, the caller will wait for the response of the caller. As more service requests to these resources lead to more requests waiting, there is a chain effect (avalanche effect).

The circuit breaker has a fully open state: a period of time to reach a certain number of times can not be called and multiple monitoring no signs of recovery so that the circuit breaker is fully open the next request will not be requested to the service

Half-open: The circuit breaker will send part of the request to this service if there is a sign of recovery within a short period of time. The circuit breaker will be closed during normal invocation

Shut down: The service can be called normally when it is still in a normal state

What is the Spring Cloud Config?

In the distributed system, because of the large number of services, in order to facilitate the unified management of service configuration files, real-time update, so the distributed configuration central component is needed. In the Spring Cloud, there is a distributed configuration center component called Spring Cloud Config, which enables configuration services to be stored in the configuration service’s memory (that is, locally), as well as in a remote Git repository. In the Spring Cloud Config component, there are two roles, one is the Config Server, the other is the Config Client.

Use:

(1) Add POM dependencies

(2) Add relevant configuration to the configuration file

(3) Add annotation @EnableConfigServer to boot class

What is the Spring Cloud Gateway?

Spring Cloud Gateway is the second generation Gateway framework launched by Spring Cloud, which replaces Zuul Gateway. As a traffic gateway, it plays a very important role in the micro-service system. The common functions of the gateway include routing and forwarding, permission verification, and flow limiting control.

Using a RouteLocatorBuilder bean to create a route, the RouteLocatorBuilder allows you to add predicates and filters. Predicates assertions, as the name implies, depend on the rules of a specific request. Processed by specific routes, Filters are the filters used to make various determinations and modifications to the request.

Write in the last

Welcome to pay attention to my public number [calm as code], massive JAVA related articles, learning materials will be updated in it, the finishing of the information will be placed in it.

Feel good to write on the point of a like, add a focus on bai! Point of attention, don’t get lost, keep updating!!