OSGI初识
1. OSGI介绍
1.1 OSGI概念
OSGI的英文全称是Open Service Gateway Initiative(开放式地服务网关协议),OSGI是一个标准,它致力于提供给java项目一个模块化的底层环境,以及一些列通用的服务,用户权限控制服务,登录服务,数据库查询服务。所以可以把这些服务做成一个个bundle,以便可以被重复使用。
和普通的JVM程序相比,OSGI的程序天生拥有动态模块的特点,不同的模块(Bundle)有着独立的生命周期,可以独立进行安装、启动、停止、卸载的操作,模块间的依赖性管理业由OSGI提供。
1.2 OSGI的bundle与普通jar区别
A very pratical advantage of osgi is that every bundle must define its exported java packages and its required dependencies.This way you can effectively control the provided API and the dependencies of your plug-ins.
The OSGI specification defines the OSGI bundle as the unit of modularization.Technically OSGI bundles are .jar files with additional meta information. This meta information is stored in the META-INF folder in the MANIFEST.MF file.The MANIFEST.MF file is part fos a standard jar specification to which OSGI adds additional metadata.Any non-OSGI runtime will ignore the OSGI metadata.Therefore OSGI bundles can be used without restrictions in non-OSGI java environments.
1.3 流行的OSGI框架
常见的OSGI框架有:Apache Felix(ServiceMix默认的OSGI框架), Eclipse Equinox, Spring DM
支持OSGI的web容器也有很多,一般使用的是eclipse virgo
2. OSGI优点
- OSGI是一个基于Java的模块系统的框架,是模块化领域的一个工具,基于包层次的信息隐藏,可以提高系统的可重用性、灵活性和可维护性,提升软件系统的设计。
- OSGI支持热部署,可以在不停止服务运行时动态更新其服务内容,做到即插即用,实现动态的可管理的系统。
3. OSGI缺陷
- 部署比较麻烦,如果是web项目,tomcat怎么部署,一种方法是,tomcat和项目打成一个bundle进行部署
- maven管理项目的时候,如果需要依赖其他jar,需要引入这些架包,但是开放的项目不能直接和已经部署的bundle关联,需要一个个引入。或者可以把这些bundle包发布到maven仓库中,来进行引用
- bundle是按照功能来划分的,一个项目会有很多bundle,所以架构设计很重要。
4. OSGI其他资料
Refference:
1.http://www.vogella.com/articles/OSGi/article.html