Spring Cloud 简明教程
Spring Cloud - Introduction
在我们关注 Spring Cloud 之前,让我们简要概述微服务架构以及 Spring Boot 在创建微服务中的作用。
Before we look at Spring Cloud, let’s have a brief overview on Microservice Architecture and the role of Spring Boot in creating microservices.
Microservice Architecture
微服务架构是一种应用程序开发风格,其中应用程序被分解成小型服务,并且这些服务彼此之间松散耦合。以下是使用微服务架构的主要优点:
Microservice architecture is a style of application development where the application is broken down into small services and these services have loose coupling among them. Following are the major advantages of using microservice architecture −
-
Easy to maintain − Microservices are small in size and are supposed to handle only single business task. So, they are simple to develop and maintain.
-
Independent Scaling & Deployment − Microservices have their individual deployment pattern and cadence. So, each service can be scaled based on the load which that service is supposed to cater to. Each service can be deployed based on its schedule.
-
Independent Technology Usage − Microservices have their code base segregated from the deployment environment, so the language and the technology that a microservice needs to use can be decided based on the use-case. There is no need to have a common stack to be used in all microservices.
有关微服务架构的更多详细信息,请访问 Microservice Architecture
More details about Microservice Architecture can be found at Microservice Architecture
Spring Boot
Spring Boot 是一个基于 Java 的框架,用于创建微服务架构中使用的微服务。它进一步缩短了开发 Spring 应用程序所需的时间。以下是它提供的主要好处:
Spring Boot is a Java-based framework which is used to create microservices which are used in microservice architecture. It further brings down the time needed to develop a Spring application. Following are the major benefits it provides −
-
It is easy to understand and develop a Spring application
-
Increases productivity
-
Reduces the development time
有关 Spring Boot 的更多信息,请访问 Spring Boot
More info on Spring Boot can be found at −Spring Boot
Spring Cloud
Spring Cloud 提供了一组组件,这些组件在云中构建分布式应用程序时很有用。我们可以自己开发这些组件,但是这会浪费开发和维护此样板代码的时间。
Spring Cloud provides a collection of components which are useful in building distributed applications in cloud. We can develop these components on our own, however that would waste time in developing and maintaining this boilerplate code.
这就是 Spring Cloud 发挥作用的地方。它为分布式环境中观察到的常见问题提供了开箱即用的云模式。它试图解决的一些模式是:
That is where Spring Cloud comes into picture. It provides ready-to-use cloud patterns for common problems which are observed in a distributed environment. Some of the patterns which it attempts to address are −
-
Distributed Messaging
-
Load Balancing
-
Circuit Breakers
-
Routing
-
Distributed Logging
-
Service Registration
-
Distributed Lock
-
Centralized Configuration
这就是为什么它成为开发需要高可伸缩性、性能和可用性的应用程序时非常有用的框架。
That is why, it becomes a very useful framework in developing applications which require high scalability, performance, and availability.
在本教程中,我们将介绍 Spring Cloud 的以上组件。
In this tutorial, we are going to cover the above-listed components of Spring Cloud.
Benefits of Using Spring Cloud
-
Developers focus on Business Logic − Spring Cloud provides all the boilerplate code to implement common design patterns of the cloud. Developers thus can focus on the business logic without the need to develop and maintain this boilerplate code.
-
Quick Development Time − As the developers get the boilerplate for free, they can quickly deliver on the required projects while maintaining code quality.
-
Easy to use − Spring Cloud projects can easily be integrated with existing Spring Projects.
-
Active Project − Spring Cloud is actively maintained by Pivotal that is the company behind Spring. So, we get all the new features and bug-fixes for free just by upgrading the Spring Cloud version.
微服务架构有多个优点;但是,它最关键的缺点之一是在分布式环境中部署它。对于分布式系统,我们有一些常见问题经常出现,例如:
Microservice architecture has multiple advantages; however, one of its most critical drawbacks is its deployment in a distributed environment. And with the distributed systems, we have some common problems that frequently creep up, for example −
-
How does service A know where to contact service B, i.e., address of service B?
-
How do multiple services communicate with each other, i.e., what protocol to use?
-
How do we monitor various services in our environment?
-
How do we distribute the configuration of the service with the service instance?
-
How do we link the calls which travel across services for debugging purposes?
-
and so on…
这些是 Spring Cloud 尝试解决并提供通用解决方案的一系列问题。
These are the set of problems which Spring Cloud tries to address and provide common solution to.
虽然 Spring Boot 用于快速应用程序开发,但将它与 Spring Cloud 一起使用可以减少我们开发和部署在分布式环境中的微服务的集成时间。
While Spring Boot is used for quick application development, using it along with Spring Cloud can reduce time to integrate our microservices which we develop and deploy in a distributed environment.
Spring Cloud Components
现在让我们来看看 Spring Cloud 提供的各种组件以及这些组件解决的问题
Let us now take a look at the various components which Spring Cloud provides and the problems these components solve
Problem |
Components |
Distributed Cloud Configuration |
Spring Cloud Configuration, Spring Cloud Zookeeper, Spring Consul Config |
Distributed Messaging |
Spring Stream with Kafka, Spring Stream with RabbitMQ |
Service Discovery |
Spring Cloud Eureka, Spring Cloud Consul, Spring Cloud Zookeeper |
Logging |
Spring Cloud Zipkin, Spring Cloud Sleuth |
Spring Service Communication |
Spring Hystrix, Spring Ribbon, Spring Feign, Spring Zuul |
我们将在接下来的章节中研究其中的几个组件。
We will look at a few of these components in the upcoming chapters.
Difference between Spring Cloud and Spring Boot
这是在开始使用 Spring Cloud 时出现的一个非常常见的问题。实际上,这里不存在比较。Spring Cloud 和 Spring Boot 都用于实现不同的目标。
This a very common question that arises when starting with Spring Cloud. Actually, there is no comparison here. Both Spring Cloud and Spring Boot are used to achieve different goals.
Spring Boot 是一个用于更快速应用程序开发的 Java 框架,并在微服务架构中专门使用。
Spring Boot is a Java framework which is used for quicker application development, and is specifically used in Microservice architecture.
Spring Cloud 用于集成这些微服务,以便它们能够在分布式环境中轻松地一起协同工作并相互通信
Spring cloud is used for integrating these microservices so that they can easily work together in a distributed environment and can communicate with each other
事实上,为了获得更少开发时间等最大收益,建议将 Spring Boot 与 Spring Cloud 一起使用。
In fact, to avail maximum benefits like less development time, it is recommended to use Spring Boot along with Spring Cloud.