当前位置:网站首页>Fizz gateway secondary development integration tutorial

Fizz gateway secondary development integration tutorial

2022-06-24 11:16:00 openapplus

summary

fizz 2.0 Modular design is adopted , Include :

  • fizz-spring-boot-starter modular , Facilitate third-party rapid integration fizz gateway .
  • fizz-bootstrap modular , Demo module , Show how third parties integrate fizz, It is recommended that the third party use this module as the skeleton application gateway , And carry out secondary development .

The following is an example of the gateway log plug-in to introduce the topic .

Integrate fizz gateway

establish spring boot maven engineering , Suggest 2.2.13.RELEASE edition

adjustment pom

To configure 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  -->

Join in fizz Gateway depends on

        <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>

Adjust configuration

Copy fizz-bootstrap modular resources Under the js Catalog 、application.yml、log4j2-spring.xml, To the corresponding location of the current project , adjustment :

Manage the background for the gateway redis;

If not for performance testing , Adjustable log4j2-spring.xml The log level in is info/debug.

Project start class

Copy fizz-bootstrap Of FizzBootstrapApplication.java To the project source directory , Name adjustable , Run startup class , If there is no error log , Description Introduction fizz success .

Development log plug-in

Plug in development includes adding plug-in definitions 、 Write plug-in filters 、 Manage background application configuration .

Add plug-in definitions in the management background

     The plug-in name is also the name of the plug-in  id  Unique ,order  Is the execution order of the plug-in , It is also the display order of plug-ins on the interface .
    
     Form definition :
    [
        {
            "field":"logReqId",
            "label":" Print request id journal ",
            "component":"radio",
            "dataType":"boolean",
            "default":false,
            "options":[
                {
                    "label":" yes ",
                    "value":true
                },
                {
                    "label":" no ",
                    "value":false
                }
            ]
        },
        {
            "field":"appendFizzRsv",
            "label":" add to fizzRsv Request header ",
            "component":"radio",
            "dataType":"boolean",
            "default":false,
            "options":[
                {
                    "label":" yes ",
                    "value":true
                },
                {
                    "label":" no ",
                    "value":false
                }
            ]
        }
    ]
     The front end of the management background generates the configuration form of the plug-in based on this 

Write plug-in filters

    @Component(LogPluginFilter.LOG_PLUGIN_FILTER) //  With the plug-in above  id  Agreement 
    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  journal , That is, the above definition , It can be configured through background management 
            if (logReqId == null || logReqId) {
                log.info(exchange.getRequest().getURI().toString() + "  Request id: " + rid);
            }
            Boolean appendFizzRsv = (Boolean) config.get("appendFizzRsv");
            if (appendFizzRsv == null || appendFizzRsv) {
                WebUtils.appendHeader(exchange, "FIZZ-RSV", rid);
            }
            return WebUtils.transmitSuccessFilterResultAndEmptyMono(exchange, LOG_PLUGIN_FILTER, null); //  Save the plug-in execution results , And back to 
        }
    }

     The plug-in must be a  spring  Of  Component( Or sub annotation ), Project startup class  scanBasePackages  To override the plug-in  pkg;
     The plug-in implements two functions , Record request  id  journal , Add... When forwarding a request  FIZZ-RSV  Request header , And the function can be turned on or off .

# Application plug-in

Yes

Routing application plug-in :

Configured above " Print request id journal "、" add to fizzRsv Request header ", Corresponding

public Mono<Void> doFilter(ServerWebExchange exchange, Map<String, Object> config, String fixedConfig) 

in config Of logReqId and appendFizzRsv key, Restart project , Access the previous route , If there is a corresponding log output , Indicates that the plug-in is in effect .

原网站

版权声明
本文为[openapplus]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/06/20210604151112971F.html