Monitor-Network
根据 ServiceLoader,监控 Okhttp 网络恳求,完成阻拦、解析、转发、存储、上报等功能。选用组件化开发方法,各事务组件均可独自拆分运用,同时供给export对外调用组件,外部调用无需关心内部完成,组件之间集成完全解耦
Github 地址 > github.com/MannaYang/M…
集成组件介绍
Components-stone
- 供给基础依靠办理build.gradle.kts(可修正依靠版别)
- 供给DataStore/Gson/RetrofitProvider对外调用办理类(可替换)
- 供给NetworkInterceptor大局阻拦器(Okhttp Interceptor)(如需替换,需大局处理接口注解)
- 供给Application生命周期分发办理,根据@AutoService注解生成并解耦( 如需替换,需大局处理接口注解)
Components-http-export
该组件为对外集成组件,可直接依靠
- HttpDataResult.kt 供给对外获取Interceptor阻拦器解析实体model
- HttpInterceptorListener.kt 供给对外转发实体model数据接口,可供上层事务获取网络恳求数据并自定义处理,参阅app组件模块[HttpInterceptor.kt]
Components-http
该组件是http-export内部事务完成组件,处理网络恳求白名单、解析、转发等事务操作,不可直接依靠,建议上层business事务集成
- HttpInterceptor.kt 阻拦原始okhttp网络恳求,并拼装实体模型HttpResult.kt
- HttpResultParser.kt 解析原始恳求header、body、formBody并组合[HttpDataResult]模型
- DispatchProvider.kt 经过异步协程与ServiceLoader转发http处理完成的数据模型
Components-room-export
该组件为对外集成组件,可直接依靠
- HttpDataEntity.kt 数据库表映射相关实体模型,供外部组合数据并存储数据源,参阅app组件HttpInterceptor.kt
- HttpDataManage.kt 数据库对外操作接口,供给增修改查及自定义查询sql功能
- QueryFilter.kt 自定义查询过滤条件,参阅app组件MainActivity.kt
Components-room
该组件是room-export内部事务完成组件,供给room database、table、dao完成
- HttpDataBaseLifecycle.kt 生命周期内初始化room相关组件,获取dao操作实例
- HttpDataDaoProvider.kt 实体模型与Table表映射联系相关
- HttpDataManage.kt 数据库增修改查详细完成,相关room-export组件中[HttpDataManage.kt]
Components-report-export
该组件为对外集成组件,可直接依靠
- WorkReport.kt 对外敞开事务查询过滤实体、事务上报渠道区分,相关WorkManager-InputData参数
Components-report
该组件是report-export内部事务完成组件,供给WorkManager后台上报功能
- dingtalk 钉钉工作空间及鉴权服务与数据上报(仅作参阅),详细完成可换为实践上报事务渠道
- markdown 格式化上报报文(仅作参阅),详细按实践事务要求处理
- work 处理数据上报及冗余数据处理(仅作参阅),详细按实践事务要求处理
Components-app
上层事务集成组件壳,包括一切export及内部完成组件,拼装额定公共参数(用户信息、设备信息、运用信息)
kapt("com.google.auto.service:auto-service:1.1.1")
api("com.google.auto.service:auto-service-annotations:1.1.1")
implementation(project(":stone"))
implementation(project(":http"))
implementation(project(":http-export"))
implementation(project(":room"))
implementation(project(":room-export"))
implementation(project(":report"))
implementation(project(":report-export"))
详细调用办法在[MainActivity.kt]与[HttpInterceptor.kt],运用内BaseApplication应替换为项目实践运用类
运用说明
项目中为了演示作用选用钉钉上报,因而需要请求企业运用AppKey/SecretKey/AgentId/进行鉴权以,经过以上key信息获取UserIds完成音讯推送告诉, 实践运用中引荐上签到日志中台或相关日志渠道,作为全链路监控的一环
钉钉敞开渠道 > open.dingtalk.com/
gradle.properties 需配置以下内容,相关影响鉴权办法方位 : DingRepository.kt
# Ding key config
dingAppKey=
dingSecretKey=
dingAgentId=
dingUsersId=
实践调用代码示例
DingProvider.kt
suspend fun reportWorkSpace(title: String, content: String, success: (Boolean) -> Unit) {
...
...
...
}
可自定义DataServerProvider.kt ,处理实践数据上报渠道
suspend fun reportDataServer(title: String, content: String, success: (Boolean) -> Unit) {
//do something
}
suspend fun reportDataOther(title: String, content: String, success: (Boolean) -> Unit) {
//do something
}
演示作用截图 screenshot
ServiceLoader
根据@AutoService注解生成的metadata可在各自组件build目录检查,例如:
- report/build/intermediates/java_res/debug/out/META_INF/services/*
- report/build/intermediates/runtime_library_classes_dir/debug/META_INF/services/*
- report/build/tmp/kapt3/classes/debug/META_INF.services
运用方法可参阅各事务组件中含有@AutoService class,例如:
auto-service官方文档地址 > github.com/google/auto…
Declaration Interface
/**
* Define lifecycle methods what you want to use
*/
interface ApplicationLifecycle {
/** Lifecycle onAttachBaseContext */
fun onAttachBaseContext(context: Context)
/** Lifecycle onCreate */
fun onCreate(application: Application)
/** marking priority,0 is highest priority,next is 1,2,3...100 ,you can custom it */
fun priority(): Int
}
Annotation Class
/**
* Add google [@AutoService] annotation,to implementation it
*/
@AutoService(ApplicationLifecycle::class)
class ApplicationLifecycleProxy : ApplicationLifecycle {
override fun onAttachBaseContext(context: Context) {
//do somethings
}
override fun onCreate(application: Application) {
//do somethings
DataStoreProvider.initDataStore(application)
}
override fun priority(): Int {
//in base application context,do somethings highest priority,so you should return 0
return 0
}
}
ServiceLoader.load
/**
* With google auto-service,it will collect all @AutoService annotation class
*/
class ServiceLoaderProxy {
private val loader: ServiceLoader<ApplicationLifecycle> by lazy {
ServiceLoader.load(ApplicationLifecycle::class.java)
}
val lifecycleQueue by lazy { loader.sortedWith(compareBy { it.priority() }) }
}
引证三方库
- Google JetPacks > developer.android.com/jetpack
- Kotlin Coroutines > github.com/Kotlin/kotl…
- Retrofit & Okhttp > github.com/square/retr… & github.com/square/okht…
- Gson > github.com/google/gson
- auto-service > github.com/google/auto…