dropwizard-core模組解析
簡介
Dropwizard是一款開發運維友好、高效、RESTful web服務的框架。Dropwizard將穩定、成熟的java生態系統中的庫整合為一個簡單的、輕量級的包,即跨越了庫和框架之間的界限,使得我們可以更關注於業務本身。
Dropwizard 整合的三方包:Jersey,Jetty,Jackson,metrics,其他
HelloWorld
學習程式的常規操作,ofollow,noindex" target="_blank">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做一些瞭解。