当前位置:网站首页>Microkernel structure understanding

Microkernel structure understanding

2022-07-06 03:36:00 m0_ sixty million seven hundred and twenty-five thousand two hu

The basic structure

Microkernel architecture consists of two types of components : The core system (core system) And plug-in modules (plug-in modules). The core system is responsible for general functions unrelated to specific business functions , For example, module loading 、 Inter module communication, etc ; The plug-in module is responsible for implementing specific business logic .

The architectural essence of microkernel is Encapsulate the changes in the plug-in , So as to achieve the purpose of rapid and flexible expansion , Without affecting the stability of the overall system .

Key points of design

The key technologies of the core system design of microkernel are : Plug-in management 、 Plug in connection and plug-in communication

Plug-in management

The core system needs to know which plug-ins are currently available , How to load these plug-ins , When to load the plug-in . The common implementation method is the plug-in registry mechanism , The core system provides plug-in registry ( It can be a configuration file , It can also be code , It can also be a database ), The plug-in registry contains information about each plug-in module , Including its name 、 Location 、 Loading time ( Start loading , Load on demand ) etc. .

The plug-in connection

Generally speaking , The core system must formulate the connection specifications between plug-ins and core systems , Then the plug-in is implemented according to the specification , The core system can be loaded according to the specification .
Common connection mechanisms are OSGi(Eclipse Use )、 Message schema 、 Dependency injection (Spring Use ), Even using distributed protocols is possible , such as RPC perhaps HTTP Web The way .

Plug in communication

Plug ins are completely decoupled when designing , But in the process of actual business operation , There will be a business process that requires the cooperation of multiple plug-ins , So the core system needs to provide plug-in communication mechanism .

OSGI

OSGi Is a plug-in standard , Instead of a working framework ,Eclipse Adopted OSGi The framework is called Equinox, Similar implementations include Apache Of Felix、Spring Of Spring DM.

Plug-in management

The module layer implements the plug-in management function .OSGi in , The plug-in is called Bundle, Every Bundle It's a Java Of JAR file , Every Bundle It contains a metadata file MANIFEST.MF, This file contains Bundle Basic information of .
Such as ,Bundle The name of 、 describe 、 developers 、classpath, And the packages that need to be imported and exported ,OSGi The core system will load this information into the system for subsequent use :

// MANIFEST.MF 
	Bundle-ManifestVersion: 2 
	Bundle-Name:UserRegister
	Bundle-SymbolicName: com.test.userregister 
	Bundle-Version: 1.0 
	Bundle-Activator: com.test.UserRegisterActivator
	 
	Import-Package: org.log4j;version="2.0", 
	..... 
	Export-Package: com.test.userregister;version="1.0", 

The plug-in connection

The life cycle layer implements the plug-in connection function , Provides execution time module management 、 Module to the bottom OSGi Framework access . The lifecycle layer precisely defines Bundle Life cycle operations ( install 、 to update 、 start-up 、 stop it 、 uninstall ),Bundle Each operation must be implemented according to the specification .

public class UserRegisterActivator implements BundleActivator {
     
	 
	 public void start(BundleContext context) {
     
	     UserRegister.instance = new UserRegister (); 
	 } 
	 
	 public void stop(BundleContext context) {
     
	     UserRegister.instance = null; 
	 } 
	} 

Plug in communication

The service layer realizes the function of plug-in communication .OSGi It provides a function of service registration , It is used by plug-ins to register the services they can provide to OSGi Core service registry , If a service wants to use other services , Then you can search the available service centers directly in the service registry .

//  Registration service 
public class UserRegisterActivator implements BundleActivator {
    
//  stay  start()  of use  BundleContext.registerService()  Registration service 
public void start(BundleContext context) {
    
context.registerService(UserRegister.class.getName(), new UserRegisterImpl(), null);
}
//  No need to  stop()  Log out of service , because  Bundle  When it stops, it will automatically log out of the  Bundle  Registered services in 
public void stop(BundleContext context) {
    }
}
//  Retrieval service 
public class Client implements BundleActivator {
    
public void start(BundleContext context) {
    
// 1.  Retrieve indirect from the service registry “ The service reference ”
ServiceReference ref = context.getServiceReference(UserRegister.class.getName());
// 2.  Use “ The service reference ” To access the instance of the service object 
((UserRegister) context.getService(ref)).register();
}
public void stop(BundleContext context) {
    }
}

The service registration here is not the plug-in registration in the plug-in management function , In fact, it is the mechanism of communication between plug-ins .

Rules engine

The rule engine also belongs to a specific implementation of micro kernel architecture from the perspective of structure , The execution engine can be regarded as a microkernel , The execution engine parses the configured business flow , Implement the conditions and rules , In this way to support the flexibility of the business .

The rule engine is charging 、 insurance 、 Promotion and other business areas are widely used .

Open source JBoss Drools

Compare the design key points of micro kernel architecture :

Plug-in management

The rules in the rule engine are the plug-ins of the microkernel architecture , The engine is the kernel of the microkernel architecture . Rules can be loaded and executed by the engine . Rule engine architecture , Rules are generally saved in the rule base , A database is usually used to store .

The plug-in connection

The rule engine specifies the language of rule development , Business people need to write rule files based on rule language , Then the rule engine loads and executes the rule file to complete the business function , therefore , The plug-in connection implementation mechanism of rule engine is actually rule language .

Plug in communication

The rules of the rule engine communicate with each other through data flow and event flow , Because a single rule does not need to rely on other rules , Therefore, there is no active communication between rules , Rules only need to output data or events , The engine passes data or events to the next rule .

-------- source 《 Geek course 》∙ Study summary

原网站

版权声明
本文为[m0_ sixty million seven hundred and twenty-five thousand two hu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202132314444388.html