An overview of the
Fizz 2.0 uses a modular design, including:
-
The Fizz-spring-boot-starter module facilitates the fast integration of the fizz gateway by third parties.
-
Fizz – Bootstrap module, demo module, show how the third party to integrate fizz, suggest the third party to use this module as the skeleton application gateway, and secondary development.
The following is an example of the gateway logging plug-in.
Integrate fizz gateway
Create the Spring Boot Maven project in 2.2.13.RELEASE
Adjust the pom
Configure the properties
< properties > < Java version > 1.8 < / Java version > < fizz. Version > 2.0.0 < / fizz. Version > < spring - framework version > 5.2.15. RELEASE < / spring - framework. Version > < reactor - bom version > Dysprosium - SR20 < / reactor - bom version > < lettuce. Version > 5.3.7. RELEASE < / lettuce. Version > < netty version > 4.1.65. Final < / netty version > <. Httpcore version > 4.4.14 < / httpcore version > < log4j2 version > 2.13.3 < / log4j2 version > < Commons - lang3. Version > 3.12.0 < / Commons - lang3. Version > < lombok version > 1.18.20 < / lombok version > < apache. Dubbo. Version > 2.7.5 < / apache. Dubbo. Version > < GRPC version > 1.16.1 < / GRPC version > < mockito. Version > 3.4.6 < / mockito version > < curator. Version > 4.0.1 < / curator version > < zookeeper version > 3.5.9 < / zookeeper version > < / properties > <! These properties specify the version that the gateway depends on -->Copy the code
Add fizz gateway dependency
<dependency>
<groupId>com.fizzgate</groupId>
<artifactId>fizz-common</artifactId>
<version>${fizz.version}</version>
</dependency>
<dependency>
<groupId>com.fizzgate</groupId>
<artifactId>fizz-spring-boot-starter</artifactId>
<version>${fizz.version}</version>
</dependency>
Copy the code
Adjust the configuration
In the fizz-bootstrap module resources, copy the js directory, application.yml, log4j2-spring. XML to the corresponding location of the project, and adjust:
Manage the back-end Redis for the gateway;
If not for performance testing, you can adjust the log level in log4j2-spring. XML to info/debug.
Project startup class
Copies of fizz – the bootstrap FizzBootstrapApplication. Java source directory, the project name can be adjusted, run to start the class, without error log, that introduce fizz.
Development log plug-in
Plug-in development includes adding plug-in definitions, writing plug-in filters, and managing background application configurations.
Plug-in definition added in management background
The plug-in name must be unique, and the plug-in ID must be unique. Order indicates the plug-in execution order and the plug-in display order on the interface. Form definition: [{"field":"logReqId", "label":" Print request ID log ", "Component ":"radio", "dataType":" Boolean ", "default":false, "Options" : [{" label ":" ", "value" : true}, {" label ":" no ", "value" : false}]}, {" field ":" appendFizzRsv." "Label ":" Add fizzRsv header "," Component ":"radio", "dataType":" Boolean ", "default":false, "options":[{"label":" yes ", "Value ":true}, {"label":" no ", "value":false}]}] The front end of the management backend generates a configuration form for the plug-in based on thisCopy the code
Writing plug-in filters
@Component(logpluginFilter.log_plugin_filter) public class LogPluginFilter extends PluginFilter { private static final Logger log = LoggerFactory.getLogger(LogPluginFilter.class); public static final String LOG_PLUGIN_FILTER = "logPlugin"; @Override public Mono<Void> doFilter(ServerWebExchange exchange, Map<String, Object> config, String fixedConfig) { String rid = exchange.getRequest().getId(); Boolean logReqId = (Boolean) config.get("logReqId"); // Whether to print the request ID log, as defined above, Can if through management background configuration (logReqId = = null | | logReqId) {the info (exchange. GetRequest () getURI (). The toString () + "request id:" + rids); } Boolean appendFizzRsv = (Boolean) config.get("appendFizzRsv"); if (appendFizzRsv == null || appendFizzRsv) { WebUtils.appendHeader(exchange, "FIZZ-RSV", rid); } return WebUtils.transmitSuccessFilterResultAndEmptyMono(exchange, LOG_PLUGIN_FILTER, null); }} The plug-in must be a Spring Component (or child annotation), and the project startup class scanBasePackages must override the PKG of the plug-in; The plug-in implements two functions, logging the request ID, adding the Fizz-RSV request header when forwarding the request, and the function can be turned on or off.Copy the code
#Application of plug-in
right
Routing application plug-in:
“Print request ID log”, “Add fizzRsv request header”, corresponding to
public Mono<Void> doFilter(ServerWebExchange exchange, Map<String, Object> config, String fixedConfig)
Copy the code
Config logReqId and appendFizzRsv key, restart the project, and access the previous route. If the corresponding log output is displayed, the plug-in takes effect.