简介

Dropwizard是一款开发运维友好、高效、RESTful web服务的框架。Dropwizard将稳定、成熟的java生态系统中的库整合为一个简单的、轻量级的包,即跨越了库和框架之间的界限,使得我们可以更关注于业务本身。

Dropwizard 集成的三方包:Jersey,Jetty,Jackson,metrics,其他

HelloWorld

学习程序的常规操作,helloworld。了解大致的应用启动流程。

dropwizard-core

该模块是dropwizard的核心模块,掌握了这个模块,对dw的基本运行机制等大部分的常用功能基本就了解了。

core官方文档

Application

Application类是dw的核心类,使用dw的应用必须需要继承该类,比如helloworld.

Application的核心函数为run,在该部分会完成应用初始化和启动

    public void run(String... arguments) throws Exception {
//注意创建bootstrap对象时赋的其他对象,比如默认使用的ConfigurationSourceProvider为FileConfigurationSourceProvider
final Bootstrap<T> bootstrap = new Bootstrap<>(this);
//添加默认的command,只有check和server。
addDefaultCommands(bootstrap);
//调用app的init方法,在这里可以addBundle,addCommand,设置configuration的替换方法,由应用自身实现
initialize(bootstrap);
//注册metrics,为监控服务器做准备
bootstrap.registerMetrics(); //创建命令执行器,Cli类解析命令行输入参数并执行命令
final Cli cli = new Cli(new JarLocation(getClass()), bootstrap, System.out, System.err);
//根据输入参数执行command,比如利用server Command为应用启动HTTP 服务器
if (!cli.run(arguments)) {
// only exit if there's an error running the command
onFatalError();
}
}

Configuration类

Configuration类是core的核心类之一,YAML配置文件的对象表示

Configuration主要功能实现在dropwizard-confiugration模块实现

dropwizard-confiugration模块的主要功能是加载、解析、绑定和验证配置文件。核心接口ConfigurationFactory

Bootstrap类

Bootstrap类是core模块的核心类之一,官方定位为application的预启动环境。我个人将其作用理解为完成application启动前的环境准备和初始化,比如添加Bundles、Commands或者注册Jackson modules,这样就能允许我们把一些自定义的类型加载到配置中。

其实,根据Bootstrap的构造器我们能大致看出类的功能,Bootstrap主要负责构造器里的对象的管理(get和set),然后提供addBundle和addCommand功能。

    public Bootstrap(Application<T> application) {
this.application = application;
//Jackson的主要类,用于Java对象和Json对象相互转换
this.objectMapper = Jackson.newObjectMapper();
//维护bundle的列表
this.configuredBundles = new ArrayList<>();
//维护command的列表
this.commands = new ArrayList<>();
//Validator负责配置文件解析后的验证功能
this.validatorFactory = Validators.newValidatorFactory();
//度量对象,用于@Timed等
this.metricRegistry = new MetricRegistry();
//配置文件的读取方式,具体见dropwizard-configuration模块
this.configurationSourceProvider = new FileConfigurationSourceProvider();
this.classLoader = Thread.currentThread().getContextClassLoader();
this.configurationFactoryFactory = new DefaultConfigurationFactoryFactory<>();
//HealCheck,负责应用状态检查,可以查看应用是否正在工作
this.healthCheckRegistry = new HealthCheckRegistry();
}
.........
public void addCommand(ConfiguredCommand<T> command) {
commands.add(command);
}
.........
//注意,init()添加bundle时进行了初始化
public void addBundle(ConfiguredBundle<? super T> bundle) {
bundle.initialize(this);
configuredBundles.add(bundle);
}

Environment类

Environment类是dropwizard的应用环境。包含应用提供的所有的的 Resources, servlets, filters, Health Checks, Jersey providers, Managed Objects, Tasks, and Jersey properties。

Environment类的属性如下

    private final String name;
private final MetricRegistry metricRegistry;
private final HealthCheckRegistry healthCheckRegistry; private final ObjectMapper objectMapper;
private Validator validator; private final JerseyContainerHolder jerseyServletContainer;
private final JerseyEnvironment jerseyEnvironment; private final MutableServletContextHandler servletContext;
private final ServletEnvironment servletEnvironment; private final LifecycleEnvironment lifecycleEnvironment; private final MutableServletContextHandler adminContext;
private final AdminEnvironment adminEnvironment; private final ExecutorService healthCheckExecutorService;

属性基本全部为final修饰,主要方法几乎全部为get方法,只有唯一一个set方法。所有,你可以直接把Environment理解为提供各种服务环境或者对象的一个容器,并且维护的对象只有Validator 可变。

dw应用的Jetty服务器是如何起来的run sever x.yml

dropwizard-core的cli包负责执行传入的命令,入口为Cli.run(String... arguments)

Cli.run->ConfiguredCommand.run->EnvironmentCommand.run

    protected void run(Bootstrap<T> bootstrap, Namespace namespace, T configuration) throws Exception {
final Environment environment = new Environment(bootstrap.getApplication().getName(),
bootstrap.getObjectMapper(),
bootstrap.getValidatorFactory(),
bootstrap.getMetricRegistry(),
bootstrap.getClassLoader(),
bootstrap.getHealthCheckRegistry());
//为注册器registry配置生命周期,同app生命周期
//@Timed @Metered 产生的报告和生命周期关联
configuration.getMetricsFactory().configure(environment.lifecycle(),
bootstrap.getMetricRegistry());
//配置Envrionment.
//调用DefaultServerFactory.configure
// 的Registering jersey handler,Registering admin handler。jetty sevrer 必备属性
configuration.getServerFactory().configure(environment);
// 运行bundles,Bundle实现ConfiguredBundle,实现run方法和init方法
bootstrap.run(configuration, environment);
//应用自定义的run方法。用于向environment注册Resource,设置鉴权等
application.run(configuration, environment);
//Runs the command with the given {@link Environment} and {@link Configuration}
//一般为server command--产生并启动app的服务器
run(environment, namespace, configuration);
}

执行完run(environment, namespace, configuration),Jetty服务器就启动成功了。

dropwizard-core模块Server的核心接口为ServerFactory,实现类为DefaultServerFactory。

启动Jetty Server为命令为server command,启动类为ServerCommand

    protected void run(Environment environment, Namespace namespace, T configuration) throws Exception {
final Server server = configuration.getServerFactory().build(environment);
try {
server.addLifeCycleListener(new LifeCycleListener());
cleanupAsynchronously();
server.start();
}

为了更好的理解应用启动过程,需要对Jetty做一些了解。

dropwizard-core模块和应用启动分析的更多相关文章

  1. Elasticsearch Transport 模块创建及启动分析

    Elasticsearch 通信模块的分析从宏观上介绍了ES Transport模块总体功能,于是就很好奇ElasticSearch是怎么把服务启动起来,以接收Client发送过来的Index索引操作 ...

  2. AngularJS标准Web业务流程开发框架—1.AngularJS模块以及启动分析

    前言: AngularJS中提到模块是自定义的模块标准,提到这不得不说AngularJS是框架中的老大哥,思想相当的前卫..在这框架满天横行的时代,AngularJS有些思想至今未被超越,当然仁者见仁 ...

  3. ASP.NET Core模块概述

    原文地址:ASP.NET Core Module overview By Tom Dykstra, Rick Strahl, and Chris Ross ASP.NET Core模块(ANCM)让你 ...

  4. Nginx学习笔记(八) Nginx进程启动分析

    Nginx进程启动分析 worker子进程的执行循环的函数是ngx_worker_process_cycle (src/os/unix/ngx_process_cycle.c). 其中,捕获事件.分发 ...

  5. python爬虫主要就是五个模块:爬虫启动入口模块,URL管理器存放已经爬虫的URL和待爬虫URL列表,html下载器,html解析器,html输出器 同时可以掌握到urllib2的使用、bs4(BeautifulSoup)页面解析器、re正则表达式、urlparse、python基础知识回顾(set集合操作)等相关内容。

    本次python爬虫百步百科,里面详细分析了爬虫的步骤,对每一步代码都有详细的注释说明,可通过本案例掌握python爬虫的特点: 1.爬虫调度入口(crawler_main.py) # coding: ...

  6. 第3阶段——内核启动分析之start_kernel初始化函数(5)

    内核启动分析之start_kernel初始化函数(init/main.c) stext函数启动内核后,就开始进入start_kernel初始化各个函数, 下面只是浅尝辄止的描述一下函数的功能,很多函数 ...

  7. Spring第三篇【Core模块之对象依赖】

    前言 在Spring的第二篇中主要讲解了Spring Core模块的使用IOC容器创建对象的问题,Spring Core模块主要是解决对象的创建和对象之间的依赖关系,因此本博文主要讲解如何使用IOC容 ...

  8. Spring第二篇【Core模块之快速入门、bean创建细节、创建对象】

    前言 上篇Spring博文主要引出了为啥我们需要使用Spring框架,以及大致了解了Spring是分为六大模块的-.本博文主要讲解Spring的core模块! 搭建配置环境 引入jar包 本博文主要是 ...

  9. Spring之Core模块

    Core模块主要的功能是实现了控制反转与依赖注入.Bean配置以及加载.Core模块中有Beans.BeanFactory.BeanDefinitions.ApplicationContext等概念 ...

随机推荐

  1. Postman系列一:Postman安装及使用过程中遇到的问题

    一:Postman的简介.下载安装及界面说明 1.Postman的简单介绍 Postman是一款强大的网页调试和发送网页HTTP请求的工具,Postman让开发和测试人员做API(接口)测试变得更加简 ...

  2. NLP(十四)自制序列标注平台

    背景介绍   在平时的NLP任务中,我们经常用到命名实体识别(NER),常用的识别实体类型为人名.地名.组织机构名,但是我们往往也会有识别其它实体的需求,比如时间.品牌名等.在利用算法做实体识别的时候 ...

  3. MVC+EF Core 完整教程20--tag helper详解

    之前我们有一篇:“动态生成多级菜单”,对使用Html Helper做了详细讲述,并且自定义了一个菜单的 Html Helper: https://www.cnblogs.com/miro/p/5541 ...

  4. 如何以python风格高逼格的改成购物车逻辑

    之前有一篇博文写到关于购物车的业务逻辑,分别运用cookie和redis存储未登录和登录用户的购物车数据,虽然已经很好的完成了业务逻辑,但是会发现代码的冗余很严重,也不够具有python特色,今天就让 ...

  5. .net软件开发脚本规范-SQL脚本标准

    一. SQL脚本标准 各文件夹存放的脚本说明 存储过程:除“基础_”开头的所有存储过程,包含新增.修改.删除.列表.提交.审核. 基础数据:“基础_”开头的存储过程,用于下拉列表的数据加载公共方法. ...

  6. nginx对特定参数限流

    接到一个需求, 需要对请求(GET)里面的某个参数  的特定的值, 进行限流; 因为不限流的话, 不知道什么时候这个id的请求飙一下, 服务端就被压死了... 就像这样: /index.html?id ...

  7. offsetX、clientX、screenX、pageX、layerX

    pageX/Y 原点相对于文档窗口左上角. offsetX/Y 原点相对于触发事件元素的左上角,需要注意的是,offset是有负值的,如果你的元素有边框,那么offset会是负值. (黑色为鼠标点击点 ...

  8. #第 12 篇:解锁博客侧栏,GoGoGo!

    作者:HelloGitHub-追梦人物 文中涉及的示例代码,已同步更新到 HelloGitHub-Team 仓库 我们的博客侧边栏有四项内容:最新文章.归档.分类和标签云.这些内容相对比较固定和独立, ...

  9. 并发新特性—Executor框架与线程池

    http://blog.csdn.net/ns_code/article/details/17465497 Executor框架简介 在Java5之后,并发编程引入了一堆新的启动.调度和管理线程的AP ...

  10. Leetcode之深度优先搜索(DFS)专题-473. 火柴拼正方形(Matchsticks to Square)

    Leetcode之深度优先搜索(DFS)专题-473. 火柴拼正方形(Matchsticks to Square) 深度优先搜索的解题详细介绍,点击 还记得童话<卖火柴的小女孩>吗?现在, ...