首页 > 其他分享 >graylog rest servcie 启动&集成说明

graylog rest servcie 启动&集成说明

时间:2022-10-03 00:23:31浏览次数:73  
标签:addSystemRestResource graylog2 server rest class graylog servcie configuration f

参考处理

  • 模块定义Graylog2Module
protected void addSystemRestResource(Class<?> restResourceClass) {
    systemRestResourceBinder().addBinding().toInstance(restResourceClass);
}
 
private Multibinder<Class<?>> systemRestResourceBinder() {
    return Multibinder.newSetBinder(
            binder(),
            new TypeLiteral<Class<?>>() {},
            Names.named(SYSTEM_REST_RESOURCES)
    );
}
  • 注册类型
@Override
protected void configure() {
    addSystemRestResource(DocumentationBrowserResource.class);
    addSystemRestResource(DocumentationResource.class);
    addSystemRestResource(CodecTypesResource.class);
    addSystemRestResource(InputTypesResource.class);
    addSystemRestResource(LoadBalancerStatusResource.class);
    addSystemRestResource(MetricsResource.class);
    addSystemRestResource(SystemPluginResource.class);
    addSystemRestResource(SystemResource.class);
    addSystemRestResource(ThroughputResource.class);
}
  • jersey 集成
    JerseyService 基于buildResourceConfig 使用systemRestResources 定义的服务基于guice 注入,使用的jersey-container-grizzly2-http 模块
 
private ResourceConfig buildResourceConfig(final boolean enableCors,
                                               final Set<Resource> additionalResources) {
        final Map<String, String> packagePrefixes = ImmutableMap.of(
                RESOURCE_PACKAGE_WEB, HttpConfiguration.PATH_WEB,
                "", HttpConfiguration.PATH_API
        );
 
        final ResourceConfig rc = new ResourceConfig()
                .property(ServerProperties.BV_SEND_ERROR_IN_RESPONSE, true)
                .property(ServerProperties.WADL_FEATURE_DISABLE, true)
                .property(ServerProperties.MEDIA_TYPE_MAPPINGS, mediaTypeMappings())
                .register(new PrefixAddingModelProcessor(packagePrefixes, graylogConfiguration))
                .register(new AuditEventModelProcessor(pluginAuditEventTypes))
                .registerClasses(
                        ShiroSecurityContextFilter.class,
                        ShiroRequestHeadersBinder.class,
                        VerboseCsrfProtectionFilter.class,
                        JacksonJaxbJsonProvider.class,
                        JsonProcessingExceptionMapper.class,
                        JsonMappingExceptionMapper.class,
                        JacksonPropertyExceptionMapper.class,
                        AnyExceptionClassMapper.class,
                        MissingStreamPermissionExceptionMapper.class,
                        WebApplicationExceptionMapper.class,
                        BadRequestExceptionMapper.class,
                        RestAccessLogFilter.class,
                        NodeIdResponseFilter.class,
                        RequestIdFilter.class,
                        XHRFilter.class,
                        NotAuthorizedResponseFilter.class,
                        WebAppNotFoundResponseFilter.class)
                // Replacing this with a lambda leads to missing subtypes - https://github.com/Graylog2/graylog2-server/pull/10617#discussion_r630236360
                .register(new ContextResolver<ObjectMapper>() {
                    @Override
                    public ObjectMapper getContext(Class<?> type) {
                        return objectMapper;
                    }
                })
                .register(new UserContextBinder())
                .register(MultiPartFeature.class)
                .registerClasses(systemRestResources)
                .registerResources(additionalResources);
 
        exceptionMappers.forEach(rc::registerClasses);
        dynamicFeatures.forEach(rc::registerClasses);
        containerResponseFilters.forEach(rc::registerClasses);
        additionalComponents.forEach(rc::registerClasses);
 
        if (enableCors) {
            LOG.info("Enabling CORS for HTTP endpoint");
            rc.registerClasses(CORSFilter.class);
        }
 
        if (LOG.isDebugEnabled()) {
            rc.registerClasses(PrintModelProcessor.class);
        }
 
        return rc;
    }

服务启动

private void startUpApi() throws Exception {
        final Set<Resource> pluginResources = prefixPluginResources(PLUGIN_PREFIX, pluginRestResources);
 
        final SSLEngineConfigurator sslEngineConfigurator = configuration.isHttpEnableTls() ?
                buildSslEngineConfigurator(
                        configuration.getHttpTlsCertFile(),
                        configuration.getHttpTlsKeyFile(),
                        configuration.getHttpTlsKeyPassword()) : null;
 
        final HostAndPort bindAddress = configuration.getHttpBindAddress();
        final String contextPath = configuration.getHttpPublishUri().getPath();
        final URI listenUri = new URI(
                configuration.getUriScheme(),
                null,
                bindAddress.getHost(),
                bindAddress.getPort(),
                isNullOrEmpty(contextPath) ? "/" : contextPath,
                null,
                null
        );
 
        apiHttpServer = setUp(
                listenUri,
                sslEngineConfigurator,
                configuration.getHttpThreadPoolSize(),
                configuration.getHttpSelectorRunnersCount(),
                configuration.getHttpMaxHeaderSize(),
                configuration.isHttpEnableGzip(),
                configuration.isHttpEnableCors(),
                pluginResources);
 
        apiHttpServer.start();
 
        LOG.info("Started REST API at <{}>", configuration.getHttpBindAddress());
    }
 
private HttpServer setUp(URI listenUri,
                             SSLEngineConfigurator sslEngineConfigurator,
                             int threadPoolSize,
                             int selectorRunnersCount,
                             int maxHeaderSize,
                             boolean enableGzip,
                             boolean enableCors,
                             Set<Resource> additionalResources) {
        final ResourceConfig resourceConfig = buildResourceConfig(enableCors, additionalResources);
        final HttpServer httpServer = GrizzlyHttpServerFactory.createHttpServer(
                listenUri,
                resourceConfig,
                sslEngineConfigurator != null,
                sslEngineConfigurator,
                false);
 
        final NetworkListener listener = httpServer.getListener("grizzly");
        listener.setMaxHttpHeaderSize(maxHeaderSize);
 
        final ExecutorService workerThreadPoolExecutor = instrumentedExecutor(
                "http-worker-executor",
                "http-worker-%d",
                threadPoolSize);
        listener.getTransport().setWorkerThreadPool(workerThreadPoolExecutor);
 
        // The Grizzly default value is equal to `Runtime.getRuntime().availableProcessors()` which doesn't make
        // sense for Graylog because we are not mainly a web server.
        // See "Selector runners count" at https://grizzly.java.net/bestpractices.html for details.
        listener.getTransport().setSelectorRunnersCount(selectorRunnersCount);
 
        listener.setDefaultErrorPageGenerator(errorPageGenerator);
 
        if (enableGzip) {
            final CompressionConfig compressionConfig = listener.getCompressionConfig();
            compressionConfig.setCompressionMode(CompressionConfig.CompressionMode.ON);
            compressionConfig.setCompressionMinSize(512);
        }
 
        return httpServer;
    }

说明

以上是graylog 关于rest api 定义以及启动的简单说明,设计上还是比较方便的,值得学习参考

参考资料

https://github.com/google/guice/wiki/Multibindings
https://eclipse-ee4j.github.io/jersey/
https://github.com/Graylog2/graylog2-server/blob/626be1f0d80506705b5ba41fbea33c2ec0164bc0/graylog2-server/src/main/java/org/graylog2/shared/rest/resources/RestResourcesSharedModule.java
https://github.com/Graylog2/graylog2-server/blob/626be1f0d80506705b5ba41fbea33c2ec0164bc0/graylog2-server/src/main/java/org/graylog2/plugin/inject/Graylog2Module.java
https://github.com/Graylog2/graylog2-server/blob/626be1f0d80506705b5ba41fbea33c2ec0164bc0/graylog2-server/src/main/java/org/graylog2/shared/initializers/JerseyService.java

标签:addSystemRestResource,graylog2,server,rest,class,graylog,servcie,configuration,f
From: https://www.cnblogs.com/rongfengliang/p/16749826.html

相关文章

  • 拼接Json数据可以直接提交到AB框架Rest服务端(主从表)
    //=============================================================================== let_this=this letsalesorderdata=_this.params _this.salesorder.a......
  • graylog 的schema
    graylogschema实际上是graylog的字段模型,都是graylog应用内部核心的实体,如果从实际来说并不是什么通用schema更多是一个字段的参考文档,可以方便使用参考资料https://......
  • Rest映射原理
    查看WebMvcAutoConfiguration,默认配置了OrderedHiddenHttpMethodFilter@Configuration(proxyBeanMethods=false)@ConditionalOnWebApplication(type=Type.SERVLET)@Cond......
  • graylog 的journal message 实现简单说明
    graylog的journalmessage做为graylog背压的处理还是比较重要的一个模块,以下说明下具体实现功能接口图  简单调用关系  启动入口LocalKafkaJournal类......
  • graylog 的journal message 存储简单说明
    graylogjournalmessage是一个数据的背压处理策略,默认graylog对于消息是基于disruptor处理的,里边包含了buffer,但是如果消息比较多,来不及处理的journalmessage存储就......
  • Restful中如何传递参数
    HTTP传递参数的三种方式1、URL:适合定位;长度有限2、QueryString:灵活;长度有限3、请求报文体:灵活;长度不限制;不支持GET、DELETEURL:资源定位QueryString:URL之外的额外数据请......
  • graylog 插件模型之PluginModule
    graylog的PluginModule可以简化graylog插件的开发,方便我们进行扩展参考功能类结构从下图可以看出是依赖了guice进行的包装,代码中好多都是支持依赖PluginModule开......
  • graylog grn介绍
    grn是graylog的资源名称,属于一种urn,从功能上类似aws的arn主要用来进行权限以及资源分配管理参考格式grn:<cluster>:<tenant>:<scope>:<type>:<entity>......
  • 【Django-rest-framework框架】第04回 视图集
    目录1.两个视图基类1.1GenericAPIview属性和方法1.2基于APIView写5个接口1.3基于GenericAPIview写5个接口2.5个视图扩展类3.9个视图子类4.视图集5.源码分析ViewSet......
  • Spring Boot构建RESTful API与单元测试
    首先,回顾并详细说明一下@Controller、@RestController、@RequestMapping注解。@Controller:修饰class,用来创建处理http请求的对象@RestController:Spring4之后加入的注......