Lesson 1-2: What has been updated in Spring Boot 2.0 (Part 1)

Spring Boot 2.0.0.RELEASE was officially released on March 1, 2018. This is the first major revision of Spring Boot 1.0 four years after the release, so how new functions and features are worth paying attention to! In the official Spring Boot blog, we learned: Spring Boot 2.0 version has undergone 17 months of development, and 215 different users have provided more than 6,800 submissions.

We interpret the updated technologies of Spring Boot 2.0 into three categories:

  • The first category is upgrading the basic environment;
  • The second category, default software replacement and optimization;
  • The third category is the introduction of new technologies.

Basic environment upgrade

Minimum JDK 8, supports JDK 9, no longer supports Java 6 and 7

Spring Boot 2.0 requires Java 8 as the minimum version, and many existing APIs have been updated to take advantage of Java 8 features. For example, default methods on interfaces, function callbacks, and new APIs such as javax.time. If you are using Java 7 or earlier, you need to upgrade your JDK before developing Spring Boot 2.0 applications.

Spring Boot 2.0 has passed the test and can run normally under JDK 9. At the same time, Spring Boot 2.0 announced that it will no longer support Java 6 and 7. As far as I know, the basic environment of most Internet companies in China is still running under JDK 7 or 6, so consider upgrading The Spring Boot 2.0 team needs to consider this factor.

Dependent component upgrade

Spring Boot 2.0 is built on Spring Framework 5. This Spring Boot upgrade has also upgraded some of the third-party components it depends on, mainly as follows:

  • Jetty 9.4, Jetty is an open source Servlet container, which provides a running environment for Java-based Web content, such as JSP and Servlet. Jetty is written in the Java language, and its API is released as a set of JAR packages.
  • Tomcat 8.5 and Apache Tomcat 8.5.x are designed to replace 8.0.x and fully support Java 9.
  • Flyway 5, Flyway is a database version management tool that is independent of database application, management and tracking of database changes. In layman's terms, Flyway can manage the SQL scripts of different people just like SVN manages the codes of different people, so as to achieve database synchronization.
  • Hibernate 5.2, Hibernate is a very popular ORM framework.
  • Gradle 3.4, Spring Boot's Gradle plug-in has been largely rewritten, with major improvements.
  • Thymeleaf 3.0, Thymeleaf 3 has a very large performance improvement over Thymeleaf 2.

Default software replacement and optimization

HikariCP

The default connection pool has been switched from Tomcat to HikariCP. HikariCP is a high-performance JDBC connection pool. Hikari means "light" in Japanese.

HikariCP is known as the fastest database connection pool in the Java industry. The official website provides performance comparisons of data connection pools such as c3p0, dbcp2, tomcat, vibur, and Hikari.

Regarding why Hikari's performance is so outstanding, the official website gives the following instructions:

  • Bytecode reduction: optimize the code until the compiled bytecode is the least, so that the CPU cache can load more program codes;
  • Optimize proxy and interceptor: reduce the code, for example, HikariCP's Statement proxy only has 100 lines of code;
  • Custom array type (FastStatementList) instead of ArrayList: avoid range check every time get() is called, and avoid scanning from beginning to end when calling remove();
  • Custom collection type (ConcurrentBag): Improve the efficiency of concurrent reading and writing;
  • Other optimizations for BoneCP defects, such as the study of method calls that take more than one CPU time slice.

Security

Spring Security is a top-level project in the Spring community and the Security framework officially recommended by Spring Boot. In addition to the regular Authentication and Authorization, Spring Security also provides advanced features such as ACLs, LDAP, JAAS, CAS, etc. to meet the security needs of complex scenarios.

It is relatively troublesome to integrate before using Spring Boot, but Spring Boot implements Spring Security functions based on Java configuration. Spring Boot 2.0 greatly simplifies the default security configuration and makes it easy to add custom security.

Spring Boot 2.0 is very easy to use Spring Security 5.0 to protect responsive applications. When it detects the presence of Spring Security, it will automatically configure it by default.

OAuth 2.0

OAuth 2.0 is a continuation of the OAuth protocol, but it is not backward compatible with OAuth 1.0, that is, OAuth 1.0 is completely abolished. OAuth 2.0 focuses on the simplicity of client developers. Either through the organization's approved interaction between the resource owner and the HTTP service provider on behalf of the user, or allowing third-party applications to gain access on behalf of the user.

OAuth 2.0 is an authorization framework, or authorization standard, which enables third-party applications or clients to obtain limited access to user account information on HTTP services (such as Google and GitHub). OAuth 2.0 works by delegating user authentication to the service hosting the user account and authorizing the client to access the user account.

Spring Boot 2.0 migrated the Spring Security OAuth project to Spring Security. No longer providing a separate dependency package, Spring Boot 2.0 provides OAuth 2.0 client support through Spring Security 5.

Micrometer

Micrometer is a measurement library for monitoring indicators that allows you to adjust the JVM application code without vendor lock-in.

Spring Boot 2.0 enhances the integration of Micrometer and no longer provides its own indicator API. Rely on micrometer.io to meet all application monitoring needs.

Micrometer includes support for size indicators. When paired with a size monitoring system, the size indicators can effectively access specific specified metrics and can drill down within their size range.

Metrics can be output to various systems and out-of-the-box Spring Boot 2.0 provides support for Atlas, Datadog, Ganglia, Graphite, Influx, JMX, New Relic, Prometheus, SignalFx, StatsD and Wavefront, and can also use simple memory Medium metric.

After integration, provide JVM indicators (including CPU, memory, threads and GC), Logback, Tomcat, Spring MVC & provide RestTemplate.

Redis uses Lettuce by default

Redis introduces Lettuce by default, replacing the previous Jedis as the underlying Redis connection method.

Lettuce is a scalable, thread-safe Redis client for synchronous, asynchronous and reactive use. Multiple threads can share the same RedisConnection. It uses the excellent Netty NIO framework to efficiently manage multiple connections and supports advanced Redis features such as Sentinel, clustering, pipeline, automatic reconnection, and Redis data model.

Most people use Jedis in China, so lettuce should be studied more in the future.

Configure attribute binding

In Spring Boot 2.0, the binding mechanism using @ConfigurationProperties has been redesigned, limiting the binding rules, and fixing many inconsistencies in Spring Boot 1.x.

The new Binder API can also directly use @ConfigurationProperties in the code. For example, the following binds the PersonName object in List:

List<PersonName> people = Binder.get(environment)
    .bind("my.property", Bindable.listOf(PersonName.class))
    .orElseThrow(IllegalStateException::new);

The configuration source can be expressed in YAML like this:

my:
  property:
  - first-name: Jane
    last-name: Doe
  - first-name: John
    last-name: Doe
Converter support

Binding uses a new ApplicationConversionService class, which provides some additional useful conversions. The most noticeable is the duration type and delimiter string of the converter.

The Duration converter allows the duration in either ISO-8601 format, or a simple string (such as 10m, 10 minutes). The existing properties have been changed to use Duration by default. The @DurationUnit annotation ensures backward compatibility by setting the unit used if it is not specified. For example, properties that require seconds in Spring Boot 1.5 must now be @DurationUnit(ChronoUnit.SECONDS) to ensure a simple value, for example, 10 actually uses a value of 10s.

Delimited string conversion allows you to simply bind String to Collection or Array without having to separate commas. For example, the LDAP base-dn attribute uses @Delimiter(Delimiter.NONE), so the LDAP DN (usually containing a comma) will not be misinterpreted.

Actuator improvements

Actuator endpoints have been greatly improved in Spring Boot 2.0. All HTTP Actuator endpoints are now exposed under the /actuator path, and the generated JSON payload has been improved.

Many endpoints are not exposed by default now. If you are upgrading an existing application from Spring Boot 1.5, be sure to check the migration guide and pay special attention to the management.endpoints.web.exposure.include property.

Spring Boot 2.0 improves the JSON payload returned from many endpoints.

Many endpoints now have JSON that more accurately reflects the underlying data. For example, the /actuator/conditions terminal (/autoconfig in Spring Boot 1.5) now has a top-level contexts key to group the results into the ApplicationContext.

test

Some supplements and adjustments have been made to the tests in Spring Boot 2.0:

  • @WebFluxTest has added a new annotation to support "slice" testing of WebFlux applications.
  • Converter and GenericConverter beans now automatically scan for @WebMvcTest and @WebFluxTest.
  • @AutoConfigureWebTestClient has been added to WebTestClient for testing. This annotation will be automatically applied to @WebFluxTest tests.
  • A new ApplicationContextRunner test utility has been added to make it easy to test your automatic configuration, and we have moved most of our internal test suites to this new model.

other

There are also some minor adjustments and improvements:

  • @ConditionalOnBean now uses logical AND instead of logical OR when determining whether conditions are met.
  • Unconditional classes are now included in the automatic configuration.
  • The spring CLI application now includes encodepassword which can be used to create Spring Security's compatible hash password command.
  • Scheduled tasks (ie @EnableScheduling) can be reviewed using the scheduledtasks executor endpoint.
  • The loggers drive terminal now allows you to reset a log default level.
  • Spring Session users can now find and delete sessions through the sessions executor endpoint.
  • Use spring-boot-starter-parent Now Maven-based applications-parameters use flags by default.

"Proficient in Spring Boot 42" .

Introduction of new technology

Support HTTP/2

HTTP/2 is the second-generation HTTP protocol. Tomcat, Undertow and Jetty have all supported HTTP/2 in Spring Boot's web container selection.

Compared with HTTP/1.x, HTTP/2 has made great changes and optimizations in the underlying transmission:

  • HTTP/2 uses binary format to transmit data, not the text format of HTTP/1.x. The binary format brings more advantages and possibilities in protocol analysis and optimization expansion.
  • HTTP/2 uses HPACK to compress and transmit the message header, which can save the network traffic occupied by the message header; while HTTP/1.x will carry a lot of redundant header information for each request, which wastes a lot of bandwidth resources; header compression can be very A good solution to the problem.
  • Multiplexing, to put it bluntly, is that all requests are completed concurrently through a TCP connection. Although HTTP/1.x can also request concurrent requests through the pipeline, the response between multiple requests will be blocked, so the pipeline has not been widely used so far, and HTTP/2 has achieved true concurrent requests. At the same time, the flow also supports priority and flow control.
  • Server Push: The server can push resources to the client faster. For example, the server can actively push the JS and CSS files to the client without the client parse the HTML and then send these requests. When the client needs it, it is already on the client.

Embedded Netty server

Since WebFlux does not depend on the Servlet API, we can now provide support for Netty as an embedded server for the first time. The spring-boot-starter-webflux startup POM will pull Netty 4.1 and Ractor Netty.

Note: You can only use Netty as a reactive server, and does not provide blocking Servlet API support.

Kotlin support

Spring Boot 2.0 now includes support for Kotlin 1.2.x and provides runApplication, a way to run Spring Boot applications using Kotlin. We also exposed and leveraged Kotlin's support for other Spring projects (such as Spring Framework, Spring Data, and Reactor) that have been added to its latest version.

JOOQ support

JOOQ is a toolkit for accessing relational databases based on Java. JOOQ not only absorbs the simplicity and security of traditional ORM operation data, but also retains the flexibility of native SQL. It is more like an intermediate layer between ORMS and JDBC. For code farmers who like to write SQL, JOOQ can completely satisfy your desire for control, which can be the feeling of writing SQL in Java code.

Support Quartz

Spring Boot 1.0 does not provide support for Quartz. Various integration solutions have appeared before. Spring Boot 2.0 provides the simplest integration method.

Responsive programming

The name of the WebFlux module is spring-webflux, and the Flux in the name comes from the class Flux in Reactor. Spring WebFlux has a new non-blocking functional Reactive Web framework that can be used to build asynchronous, non-blocking, event-driven services, and it performs very well in terms of scalability.

The key expected benefit of non-blocking is the ability to scale with a small fixed number of threads and less memory. On the server side WebFlux supports two different programming models:

  • The annotation-based @Controller and other annotations also support Spring MVC;
  • Functional, Java 8 Lambda style routing and processing.

By default, Spring Boot 2.0 uses Netty WebFlux, because Netty is widely used in the asynchronous non-blocking space. Asynchronous non-blocking connections can save more resources and provide higher responsiveness. By comparing Servlet 3.1 non-blocking I/O, there is not much use, because the cost of using it is relatively high, Spring WebFlux opens a practical way.

Use Spring WebFlux/WebFlux.fn to provide reactive Web programming support. WebFlux is a brand-new non-blocking functional Reactive Web framework that can be used to build asynchronous, non-blocking, event-driven services, and is very scalable. Well, this feature comes from Spring 5.0.

Spring Boot 2.0 also provides automatic configuration of reactive programming, such as Reactive Spring Data, Reactive Spring Security, etc.

Click to learn about "Proficient in Spring Boot 42" .

Finally, the new technology of Spring Boot 2.0 also has an interesting easter egg design, which we will introduce in the next lecture. At the same time, I will explain in detail some changes from Spring Boot 1.0 to Spring Boot 2.0 API, as well as my personal suggestions on whether to upgrade Spring Boot.

Guess you like

Origin blog.csdn.net/ityouknow/article/details/108729178