Is dubbo SPI showing off?

Introduction

The full name of SPI is Service Provider Interface. As the name implies, it is an interface that provides services.

Generally, SPI is only used when building a framework, which is an abstraction layer and a standard process, but the specific service layer must be customizable, depending on the specific business, you can use SPI.

For example, data-driven, the java.sql package in the JDK is basically an abstraction layer, and it is not related to a specific database. How to implement it is to use SPI.

Of course, to implement SPI, you also need to understand SPI.

To summarize briefly:

I define an abstract interface and provide corresponding tool classes for interface programming. The person who provides the service will implement it. Others use my interface, and I will use the specific implementation.

Advantages of SPI

  1. Uniform standards, you can use a common abstraction layer, otherwise each type of database driver has its own standards and implementations, so that everyone's learning costs are high.

  2. The standard is unified, and the tools can use the unified standard, which can facilitate the enrichment of the ecology without having to make a set of adaptations for each channel.

Take java.sql.Driver as an example. JDK defines java.sql.Driver to tell you what to do. If you do this, users can directly use the tools in the java.sql package provided by the JDK to operate the database. No need to care how to implement this kind of driver of MySQL or how to implement the kind of driver of MongoDB.

It’s a little bit to the design mode of the intermediary mode, all look for an intermediary, do not care about other.

SPI interface implementation process

  1. Add a /META-INF/services directory to the classpath path. Generally, the specific classpath directory does not matter, it can be placed in the lib directory of the JDK, but in order to let others use your jar package, it is best to put it in your own jar. in the bag.

  2. Add a file with the fully qualified name of the interface to be implemented under the /META-INF/services directory. For example, java.sql.Driver, the content in the file is the fully qualified class name that you implement this interface, for example, com.mysql.cj.jdbc.Driver. If there are multiple implementation classes, one per line.

  3. Implement the corresponding interface

spi-Driver

The specific processing logic is in the ServiceLoader class:

The hasNextService method of the LazyIterator internal class in ServiceLoader is the implementation of loading configuration resource files.

The parseLine method of ServiceLoader is the implementation of specific parsing configuration files.

SPI interface providing process

Given the SPI interface, provide the corresponding tool class, you can refer to the loadInitialDrivers method of Java DriverManager.

dubbo与SPI

Instead of using the ServiceLoader of the JDK, dubbo wrote an ExtensionLoader to implement the corresponding logic.

dubbo-spi

Reflection

Do we really need to use SPI? Is it not good to use the interface directly and provide a registration method?

Of course, this has to be a specific issue, specific analysis.

JDK's java.sql.Driver deals with database drivers, and it is more appropriate to use SPI, because it can shield the relationship between users and tripartite drivers and reduce user learning costs.

Dubbo's use of SPI is not impossible, but personally feels unnecessary.

For example, Dubbo's Filter, which is very personalized, is usually a specific implementation, that is, I use it and I will implement it myself. Is there a third party that provides some filters and then makes a jar package for everyone to use?

Don't talk about Filter, how many third-party jar packages are used by Dubbo?

I have implemented Filter by myself. Of course, I don’t need to talk about the cost of learning. As a result, I have to get a configuration file according to the rules. Isn't it good for an interface to register Filter? Is it inconvenient to use annotations and give an annotation tool class?

Guess you like

Origin blog.csdn.net/trayvontang/article/details/108130748