This is my ninth day of the August Challenge
This series is the rearrangement of the previous series. With the development of the project and the use of the project, many things in the previous series have changed, and there are some things that were not mentioned in the previous series, so restart the series to rearrange, welcome to leave messages and exchange, thank you! ~
Before understanding Spring Cloud, let’s first understand the relationship between Spring Framework, Spring Boot and Spring Cloud. How can a simple Bean develop a Spring Cloud with microservice features?
Spring Beans are objects that the Spring framework manages at runtime. Spring beans are the basic building blocks of any Spring application. Most of the application logic code you write will be in Spring Beans. Then we use beans for short Spring beans.
BeanFactory is at the heart of the Spring container, a container that manages all of the beans. Typically, BeanFactory implementations use a lazy loading approach, which means that beans are instantiated only if we get them directly through a call to the getBean() method.
ApplicationContext adds the following to BeanFactory:
- Locating and loading resourcesBased on the
ResourcePatternResolver
(It’s actually a wildcardResourceLoader
) to locate and load various files or network resources - environmentBased on the
EnvironmentCapable
. eachApplicationContext
Are allEnvironment
, thisEnvironment
, including Profile definitions and Properties. A Profile configuration is a named logical group defined by beans that are registered with the container only when a given Profile configuration is activated. Properties is a Spring property group, These properties can come from properties files, JVM Properties, System environment variables, JNDI, servlet Context Parameters, specialized Properties objects, Maps, and so on. - Initialization of the Bean
- A more complete Bean lifecycle, including
BeanPostProcessor
As well asBeanFactoryPostProcessor
Various treatments of - internationalizationCore classes,
MessageSource
- Event publishingBased on the
ApplicationEventPublisher
Will:ApplicationEvent
Or any other type of event, to all listeners
ApplicationContext contains a BeanFactory inside, adds other components, implements richer functionality, and, unlike the lazy loading of the BeanFactory, is preloaded. Each Bean is instantiated after ApplicationContext is started.
On top of ApplicationContext, the Spring framework introduces a number of features. One of the most common is the Spring Web application. In the past, Spring Web applications were run embedded in servlet containers, and most enterprise applications were configured and deployed to run on servlet containers. This adds to the learning curve for developers about the corresponding servlet container, which includes:
- Web. XML and other Servlet-oriented configuration concepts
- War file directory structure
- Specific configuration of different containers (e.g. exposed port configuration, thread configuration, etc.)
- Complex class loading hierarchy
- Monitoring management related facilities configured outside the application
- Logs related
- Application context configuration, and so on
The above configuration is not uniform among different containers, and developers need to understand these configuration features of the container in addition to the spring configuration. These complex configuration features make learning barriers higher, and as the technology evolves, fewer and fewer developers master Servlet principles. When it comes to enterprise application development, the simpler the application framework, the more likely developers are to adopt it. Therefore, Mike Youngstrom proposes Improved Support for ‘containerless’ Web Application Architectures, The intent is to simplify the configuration of Spring application development with a built-in Servlet container and certain classes that are loaded by default to form a specific ApplicationContext.
Spring Boot, on the basis of ApplicationContext, the implementation of Spring Boot specific ApplicationContext, And by adding different ApplicationEvent listeners to realize the unique life cycle and configuration and SPI loading mechanism (spring.factories and application.properties), based on this, the following functions are implemented:
- The built-in servlet container provides a unified abstraction of the container, i.e
WebServer
. Currently included: Tomcat(TomcatWebServer
), Jetty (JettyWebServer
), the Undertow (UndertowWebServer
), Netty (NettyWebServer
) - The configuration of different servlet containers can be configured in application.yml using the same key. For example, the exposed port is no longer configured in a different servlet container, but directly in Application.yml
server.port
Can. - Instead of constructing a WAR package to be deployed into a servlet container, you can run it directly as a JAR package.
- Users do not care about the creation and management of ApplicationContext, but can use it directly.
- There is only one ClassLoader, not a separate ClassLoader like the servlet container
On the basis of Spring Boot, Spring Cloud adds interfaces and implementations of microservice-related components. Different Components of Spring Cloud system have different interfaces and implementations. However, the common component interfaces are in the Spring-Cloud-Commons project. The interfaces for microservice components include:
- Service Registration interface
- Service Discovery Interface
- Load balancing interface
- Circuit breaker interface
Components that implement these interfaces generate different sub-ApplicationContext based on Spring Cloud’s NamedContextFactory for invoking or controlling different microservices, differentiated by microservice name. This NamedContextFactory is examined in a section of this series.
In this section, we combed through the Bean to BeanFactory, encapsulating the ApplicationContext on top of the BeanFactory, And the relationships of ApplicationContext, which is mainly based on annotations, Spring Boot of Spring Factory SPI, and Spring Cloud, which adds microservice abstraction on top of Spring Boot. Next, we’ll take a closer look at an important abstraction in Spring Cloud – NamedContextFactory
Wechat search “my programming cat” to follow the public account, every day, easy to improve technology, win a variety of offers: