当前位置:网站首页>Sringboot plugin framework implements pluggable plug-in services
Sringboot plugin framework implements pluggable plug-in services
2022-07-24 13:43:00 【Solitary goose treads the snow】
Sringboot-plugin-framework Implement pluggable plug-in Services
1. R & D background
For different The project connects the repetitive work of SMS services of many different service providers , According to the above framework , In our work, we use this framework to realize a short message service .
2. Platform profile
function : The top-level unified interface of SMS service , No matter the task provider's SMS can be accessed through a unified interface , Abstract the common ground into a platform , Different service providers use plug-ins to make pluggable , So that users can quickly develop a plug-in iteratively , It can be quickly used in the project , At present, the use effect is ok .
The SMS platform is mainly based on jdk1.8 and springboot-plugin-framework frame , The framework is based on Pf4f plug-in unit , Dynamic , Delete , newly added , install , Stop plug-ins and other functions .
The platform realizes unified SMS entry , Shield the differences between different local templates , Achieve unified configuration , Unified treatment , At the same time, there are blacklist verification and SMS frequency verification functions .
The platform has strong scalability , Later, it can support the establishment of SMS management system and the realization of SMS data statistics function .
3. The platform architecture

Use commonly used MVC framework ,controller The layer provides a unified SMS sending entry , Simultaneous adoption AOP Do blacklist verification and SMS sending frequency audit , In this way, code decoupling can be realized in the way of non embedded business code .
Service Integrate springboot-plugin-framework The framework provides pluggable business plug-ins , Used to implement gateway requests from different docking parties , At the same time, the heterogeneity of different local templates in the lower layer is shielded , The platform obtains from the database .
In the later stage, SMS statistical function and online configuration template can be added according to the demand , SMS sending parameters and other requirements .
The database keeps SMS sending data 、 Template data 、 System parameters and other data , For subsequent statistics, only , The specific details of SMS are uniformly output to the log , The database is not preserved .
4. Platform details
4.1 Unified SMS entry
4.1.1 visit
access :POST The way ,JSON Format
entrance :ip:8099/message/message/send
4.1.2 Parameters
Input parameters :
| Chinese name | English name | type | If required | remarks |
|---|---|---|---|---|
| Project type , Which bureau is it | projectType | String | Y | There are already two built-in : dongguan :dgcsb qingyuan :qycsb |
| Gateway Type : Which gateway does the same office use | gatewayType | String | Y | There are already two built-in : Dongguan Unicom :dgcsb-unicom Qingyuan mobile :qycsb-mobile The names of different sites need to be determined by colleagues who develop plug-ins at the back end , And make sure that each plug-in has a different name |
| Phone number | telephoneNumber | String | Y | |
| Whether to use template | useTemplate | boolean | Y | true: yes ;fase: no |
| The complete content of the SMS | messageContent | String | N | If you don't use templates , You need to deliver the text messages by yourself |
| Templates ID | templateId | String | N | If you use a template , Templates ID Need to communicate with the author , Configure the template in the template table first |
| Template parameter list | paramList | List< String > | N | Using templates , Template parameters need to be passed , Specific details need to match the template table |
Output parameters :
Result Class
| Chinese name | English name | type | If required | remarks |
|---|---|---|---|---|
| flag | boolean | Y | ||
| message | String | Y | ||
| data | Object | N | ||
| success | boolean | Y | ||
| code | String | N |
Case study :
Case study :
Input :
{
"projectType":"dgcsb",
"gatewayType":"dgcsb-unicom",
"telephoneNumber":"18267162262",
"useTemplate":true,
"messageContent":null,
"paramList":["2040","12","05"],
"templateId":"dg_cgtb_1"
}
Output :
{
"flag": true,
"message": " Send successfully ",
"data": null,
"success": true,
"code": null
}
4.2 Blacklist verification
With AOP Realization , The blacklist verification is related to the local point , When configuring blacklists for projects in different offices , You need to specify the project type project_type, Please refer to the database table :black_list_info.
4.3 SMS sending frequency verification
With AOP Realization , utilize redis The characteristics of the database , Realize telephone atomicity verification . Refer to the database table for specific configuration details system_settings Medium time_freq and phone_freq Parameter configuration , At present, the default is 1 A number can only be sent once per minute .
4.4 Template adaptation
The platform provides two ways of template adaptation :
- a: Parameters useTemplate=true, Users use database templates , But you need to specify the template ID:templateId And template parameter list :paramList;
- b: Parameters useTemplate=false, Users use database templates , You need to specify the content of the message :messageContent;
For some sites that need to apply for templates on the platform of the service provider , It can be summarized in a Under the circumstances , The platform will automatically adapt to the template of the service provider ID, But users need to configure templates , Indicate the template of the service provider in the template table ID(provider_template_id), For details, please refer to the template table template_info.
4.5 Plug in integration
Developers need to be in messagePlatform Medium message-plugins Create a new sub module in the module , With dgcsb-unciom For example :
4.5.1 pom
Appoint pom Inheritance relationships , modify pom as follows :
<parent>
<groupId>com.xxx</groupId>
<artifactId>message-plugins</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
With <scope>provided</scope> Way to introduce the package of the main program
<dependency>
<groupId>com.xxx</groupId>
<artifactId>message-main</artifactId>
<version>${version}</version>
<scope>provided</scope>
</dependency>
4.5.2 Plug in boot
Inherit springboot-plugin-framework In the framework BasePlugin abstract class , Here is the role of plug-in guidance , Then delete Main function class .
public class DgcsbDefinePlugin extends BasePlugin {
public DgcsbDefinePlugin(PluginWrapper wrapper) {
super(wrapper);
}
}
4.5.3 Business implementation
Realization message-main Interface in the main program :ISendMessage Here is the realization of business functions , There are two main ways to achieve this ,
public interface ISendMessage {
Logger log = LoggerFactory.getLogger(ISendMessage.class);
/** * @param messageParam * @return java.lang.Object * @description: Specific sending logic * @author: * @date: 2022/1/20 */
Object send(MessageParam messageParam) throws Exception;
/** * @param result * @return boolean * @description: Return result verification * @author: * @date: 2022/1/20 */
boolean checkWhetherSendSuccess(Object result);
......
}
The overall business process of SMS sending can be referred to message-main In the main program SendMessageService Class handle Method .
4.5.4 Plug in profile
If the plug-in has a configuration file , Need to be in 2 Add annotations to the classes in @ConfigDefinition, Here you can specify the name of the configuration file , Be careful , This name and plug-in's rsources The name of the configuration file in should be strictly unified
@ConfigDefinition Set to :
@ConfigDefinition(fileName="dgcsb-unicom.yml",devSuffix="-dev", prodSuffix="-prod")
- fileName: Basic file name
- devSuffix: File name suffix in development environment , The final mapping is :
dgcsb-unicom-dev.yml - prodSuffix: File name suffix in production environment , The final mapping is :
dgcsb-unicom-prod.yml
stay resources Add below dgcsb-unicom-dev.yml and dgcsb-unicom-prod.yml file , Corresponding to the configuration files in the development environment and production environment respectively .
Instructions for reading the development environment configuration file
The configuration file of the development environment will be from resources The read , So remember to store the configuration file in the current plug-in resources Under the table of contents , And ensure the runtime target->classes The configuration file exists in the directory , Otherwise, the startup phase will report an exception that the configuration file cannot be found .
The file read is : dgcsb-unicom-dev.yml
Instructions for reading configuration files in production environment
In the production environment, the configuration file will have the following reading order :
- From the currently configured plug-in jar Load the configuration file under the directory . Unable to load 2 step .
- from
pluginConfigFilePathLoad the configuration file under the configuration directory . Unable to load 3 step . - From the current jar Load the configuration file in the package .
The file read is : dgcsb-unicom-prod.yml
For details, please refer to Framework document .
Be careful : Currently, the plug-in configuration file does not support .properties Format , It is recommended that plug-ins adopt .yml perhaps .yaml Format .
4.6 Plug in dynamic switching
In production environment , If users need to switch the underlying plug-ins , With the interface parameters unchanged , You can manually switch the underlying plug-ins .
Available in table plugin_switch_tbl Middle configuration , The timer will be based on system_settings In the table switch_time_cron The expression of regular full load table plugin_switch_tbl Data in the , According to the configuration parameters , Underlying replacement plug-ins .
5. Project deployment
The deployment configuration files of development environment and production environment are different .
5.1 dev Deploy
If the plug-in needs to be added to the main program in the development environment ,
- In the main program Springboot Add the following configuration to the configuration file :
# Plug-in configuration
plugin:
# Plug in running mode . dev: development environment , prod: Production environment
runMode: dev
# The directory where the plug-in is stored , You can configure multiple
pluginPath:
- Specify the directory storage directory to the plug-in ( As described above `plugins` Catalog )
- In the plug-in module
resourcesCreate a new plug-in boot file under the directoryplugin.propertiesAdd the following to the file :
plugin.id=example-plugin1
plugin.class= Plug in boot class full package path . That is inheritance BasePlugin The full package path of the class
plugin.version= Version number
plugin.provider=StarBlues( Optional )
plugin.description= Plug-in description ( Optional )
plugin.requires= The version required by the main program ( Optional )
Configuration instructions :
plugin.id: plug-in unit id
plugin.class: Plug in boot class full package path . That is, the class package name in step 3
plugin.version: The plug-in version
plugin.provider: Plugin author ( Optional )
plugin.description: Plug-in description ( Optional )
plugin.requires: The version required by the main program , Configured with the main program version To verify whether the plug-in can be installed
Be careful :plugin.properties just dev Environment boot file ,prod Do not apply !
5.2 prod Deploy
1.pom Add configuration , The purpose is to plugin.properties The boot file in is written to the plug-in jar Of MANIFEST.MF in .
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>${maven.jar.plugin}</version>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
<manifestEntries>
<Plugin-Id>dgcsb-unicom</Plugin-Id>
<Plugin-Class>com.iflytek.dgcsbunicom.config.DgcsbDefinePlugin</Plugin-Class>
<Plugin-Version>0.0.1-SNAPSHOT</Plugin-Version>
<Plugin-Provider>xwliu7</Plugin-Provider>
<Plugin-Dependencies/>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
here Plugin-Id And plugin.properties equally .
For details, please refer to the framework document .
6. Dynamically loading plug-ins
The platform provides an interface to import new plug-ins during system startup ,
With prod Environment, for example , Upload the plug-in to prod Environment non project plug-in directory , Call interface
172.30.12.103:8099/message/plugin/installByPath # Just an example
Parameters use form-data Format ,key=path,value=/lxw/message-platform/test/qycsb-mobile-0.0.1-SNAPSHOT.jar
Parameter specifies new jar The catalog of ,post Just call the mode
The principle is that the framework provides interfaces PluginOperator Here is the operability plug-in , Please refer to the source code for details .
7. Dynamically stop plug-ins
Similar to the dynamic stop plug-in ,
With prod Environment, for example , Call interface
localhost:8099/message/plugin/stop/qycsb-mobile
POST The way , among qycsb-mobile It's a plug-in ID,qycsb-mobile
Refer to the source code for details .
8. Later extension point
- In view of the small concurrency of the current project , You can consider expanding the project later , Adapt to large concurrency ;
- In the later stage, the management system of SMS service can be added , Make visual configuration ;
- Follow up and discuss with colleagues in the Group , Perfect the platform ;
To be continued …
9. Reference documents
Framework document :
http://www.starblues.cn/doc/main-doc/%E6%89%93%E5%8C%85%E8%AF%B4%E6%98%8E.html
Framework case :
https://gitee.com/starblues/springboot-plugin-framework-example
The framework source code :
https://gitee.com/starblues/springboot-plugin-framework-parent
Another document :
http://www.starblues.cn/extension-doc/%E8%87%AA%E5%AE%9A%E4%B9%89%E6%89%A9%E5%B1%95.html
10. Database table
-- The blacklist
CREATE TABLE `black_list_info` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`project_type` varchar(20) COLLATE utf8mb4_bin DEFAULT NULL COMMENT ' Project type ',
`phone_number` varchar(20) COLLATE utf8mb4_bin NOT NULL,
`crt_tm` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`crt_user` varchar(20) COLLATE utf8mb4_bin DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE,
KEY `index_pro_Phone` (`project_type`,`phone_number`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT=' SMS blacklist ';
-- SMS sending save table
CREATE TABLE `message_info` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`project_type` varchar(20) COLLATE utf8mb4_bin NOT NULL COMMENT ' Site type ',
`gateway_type` varchar(20) COLLATE utf8mb4_bin NOT NULL COMMENT ' Gateway Type : Which gateway does the same office use ',
`phone_number` varchar(20) COLLATE utf8mb4_bin NOT NULL COMMENT ' Phone number ',
`message_content` varchar(128) COLLATE utf8mb4_bin DEFAULT NULL COMMENT ' The message content ',
`return_content` varchar(128) COLLATE utf8mb4_bin DEFAULT NULL COMMENT ' The content returned by the SMS interface ',
`is_success` tinyint(1) NOT NULL DEFAULT '0' COMMENT ' Whether the transmission is successful : 0: success ;1: Failure ',
`crt_tm` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT=' SMS sending save table ';
-- SMS template table
CREATE TABLE `template_info` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`template_id` varchar(20) COLLATE utf8mb4_bin NOT NULL COMMENT ' Templates ID',
`template_content` varchar(512) COLLATE utf8mb4_bin NOT NULL COMMENT ' Template content ',
`crt_tm` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`crt_user` varchar(20) COLLATE utf8mb4_bin DEFAULT NULL COMMENT ' founder ',
`mdf_tm` datetime NOT NULL ON UPDATE CURRENT_TIMESTAMP,
`mdf_user` varchar(20) COLLATE utf8mb4_bin DEFAULT NULL COMMENT ' Modifier ',
`provider_template_id` varchar(20) COLLATE utf8mb4_bin DEFAULT NULL COMMENT ' Corresponding to the template provided by the operator ID',
`delete_flag` tinyint(1) NOT NULL DEFAULT '0' COMMENT ' Logical deletion 0 No, 1: Delete logo ',
`remark` varchar(100) COLLATE utf8mb4_bin DEFAULT NULL COMMENT ' Template description ',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT=' SMS template table ';
-- System configuration table
CREATE TABLE `system_settings` (
`module` varchar(100) COLLATE utf8mb4_bin NOT NULL COMMENT ' Module type ',
`property` varchar(100) COLLATE utf8mb4_bin DEFAULT NULL COMMENT ' Property name ',
`property_value` varchar(100) COLLATE utf8mb4_bin DEFAULT NULL COMMENT ' Property value ',
`remark` varchar(100) COLLATE utf8mb4_bin DEFAULT NULL,
`is_valid` tinyint(1) DEFAULT '0' COMMENT ' Whether it works 0: It works , 1: Invalid ',
`crt_tm` datetime DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
-- Telephone frequency setting
INSERT INTO `system_settings` (`module`, `property`, `property_value`, `remark`, `is_valid`, `crt_tm`) VALUES ('frequency_limit', 'time_freq', '1', ' Time frequency , Units of minutes ', 0, '2022-01-24 15:55:38');
INSERT INTO `system_settings` (`module`, `property`, `property_value`, `remark`, `is_valid`, `crt_tm`) VALUES ('frequency_limit', 'phone_freq', '1', ' Call frequency ', 0, '2022-01-24 15:55:38');
-- Plug in switching cron
INSERT INTO `system_settings` (`module`, `property`, `property_value`, `remark`, `is_valid`, `crt_tm`) VALUES ('message_switch', 'switch_time_cron', '0 */10 * * * ?', ' SMS platform plug-in switching cron', 0, '2022-02-24 16:14:40');
-- Plug in switch table
CREATE TABLE `plugin_switch_tbl` (
`source_plugin` varchar(20) COLLATE utf8mb4_bin DEFAULT NULL COMMENT ' Source gateway ',
`dest_plugin` varchar(20) COLLATE utf8mb4_bin DEFAULT NULL COMMENT ' Destination gateway : Gateway to be switched ',
`switch_flag` tinyint(1) DEFAULT NULL COMMENT ' Switch logo :0: Don't switch ;1: Start switching '
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT=' SMS platform gateway backup switching mapping table ';
INSERT INTO `plugin_switch_tbl` (`source_plugin`, `dest_plugin`, `switch_flag`) VALUES ('dgcsb-unicom', 'qycsb-mobile', 0);
INSERT INTO `plugin_switch_tbl` (`source_plugin`, `dest_plugin`, `switch_flag`) VALUES ('qycsb-mobile', 'szPcb-unicom', 0);
INSERT INTO `plugin_switch_tbl` (`source_plugin`, `dest_plugin`, `switch_flag`) VALUES ('szPcb-unicom', 'qycsb-mobile', 0);
-- 2022-.3-02 New script
alter table message_info add column msg_id VARCHAR(100) DEFAULT null comment ' Unique identifier returned by SMS submission ';
边栏推荐
- 2021-07-09
- 使用Activiti创建数据库表报错,
- Mongodb uses mongotemplate operations to add, delete, modify, query, page, sort, aggregate (including embedded data), file upload and download
- 网络安全——服务漏洞扫描与利用
- HCIP第十三天
- CSDN garbage has no bottom line!
- Bayesian width learning system based on graph regularization
- 第六章 总线
- 简易订单管理系统小练习
- 交换机链路聚合详解【华为eNSP】
猜你喜欢

Network security - Web information collection

Network security - use exchange SSRF vulnerabilities in combination with NTLM trunking for penetration testing

Bayesian width learning system based on graph regularization

Packaging class (mutual conversion between types)

基于群体熵的机器人群体智能汇聚度量

Representation and basic application of regular expressions

Aike AI frontier promotion (7.24)

Group intelligence decision-making in an open environment: concepts, challenges and leading technologies

网络安全——Web信息收集

Network security - Cookie injection
随机推荐
position: -webkit-sticky; /* for Safari */ position: sticky;
Can communication protocol (I)
How to verify the domain name after applying for SSL digital certificate?
FlinkTable&SQL(六)
Two stacks implement one queue
Flex layout
Exploration of sustainable learning ability to support the application of ecological evolution of Pengcheng series open source large models
网络安全——过滤绕过注入
Group knowledge map: distributed knowledge transfer and federated map reasoning
Selenium environment configuration and eight elements positioning
Network security - Web information collection
Flink高级特性和新特性(八)
Number of palindromes in Li Kou question
WSDM 22 | 基于双曲几何的图推荐
H5py Quick Start Guide
ICML2022 | 分支强化学习
Group intelligence decision-making in an open environment: concepts, challenges and leading technologies
网络安全——文件上传白名单绕过
网络安全——文件上传黑名单绕过
数据修改插入