part 1:请求处理
使用PasteDeploy模块来实现 WSGI Services 时,都需要加载一个 paste.ini 文件,文件用来定义服务过滤和请求路由,类似于springMvc的拦截器。pecan是一个对象路由框架,这里的请求路由是版本v1或者v2对应的不同处理。在api_paste.ini文件中定义如下:
[pipeline:main] pipeline = request_id api-server [app:api-server] paste.app_factory = caesarlinsa.api.app:app_factory [filter:request_id] paste.filter_factory = oslo_middleware:RequestId.factory
pipeline中定义请求顺序,请求先通过request_id这个拦截器,然后再到api-server这个服务。【app: api-server】定义web服务名api-server
paste.app_factory = caesarlinsa.api.app:app_factory 服务的入口方法caesarlinsa.api.app中的def app_factory()返回application服务对象,详见下:
def app_factory(global_config, **local_conf): return VersionSelectorApplication()
VersionSelectorApplication类的定义如下,用于对不同版本路径处理,如果是v1返回404 Not Found。
class VersionSelectorApplication(object): def __init__(self): pc = get_pecan_config() def not_found(environ, start_response): start_response('404 Not Found', []) return [] self.v1 = not_found self.v2 = setup_app(pecan_config=pc)# load_app()加载配置文件api_paste.ini创建application服务 def __call__(self, environ, start_response): if environ['PATH_INFO'].startswith('/v1/'): return self.v1(environ, start_response) return self.v2(environ, start_response)
使用pecan创建application使用方法pecan.make_app,VersionSelectorApplication是对pecan返回的application的封装,self.v2(environ,start_response)
part2: 开启服务
def build_server(): app = load_app() # Create the WSGI server and start it host, port = cfg.CONF.api.host, cfg.CONF.api.port LOG.info('Starting server in PID %s' % os.getpid()) LOG.info("Configuration:") cfg.CONF.log_opt_values(LOG, logging.INFO) if host == '0.0.0.0': LOG.info( 'serving on 0.0.0.0:%s, view at http://127.0.0.1:%s' % (port, port)) else: LOG.info("serving on http://%s:%s" % (host, port)) serving.run_simple(cfg.CONF.api.host, cfg.CONF.api.port, app, processes=CONF.api.workers)
建立服务web服务器,等待请求
以上只是个人感悟,具体可参考我的github: https://github.com/CaesarLinsa/ceilometer_TestCase