Android网络请求监控与数据上报

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

该组件为对外集成组件,可直接依赖

Components-http

该组件是http-export内部业务实现组件,处理网络请求白名单、解析、转发等业务操作,不可直接依赖,建议上层business业务集成

Components-room-export

该组件为对外集成组件,可直接依赖

Components-room

该组件是room-export内部业务实现组件,提供room database、table、dao实现

Components-report-export

该组件为对外集成组件,可直接依赖

  • WorkReport.kt 对外开放业务查询过滤实体、业务上报平台区分,关联WorkManager-InputData参数

Components-report

该组件是report-export内部业务实现组件,提供WorkManager后台上报功能

  • dingtalk 钉钉工作空间及鉴权服务与数据上报(仅作参考),具体实现可换为实际上报业务平台
  • markdown 格式化上报报文(仅作参考),具体按实际业务要求处理
  • work 处理数据上报及冗余数据处理(仅作参考),具体按实际业务要求处理

Components-app

上层业务集成组件壳,包含所有export及内部实现组件,组装额外公共参数(用户信息、设备信息、应用信息)

scss 复制代码
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

ini 复制代码
# Ding key config
dingAppKey=
dingSecretKey=
dingAgentId=
dingUsersId=

实际调用代码示例

DingProvider.kt

kotlin 复制代码
suspend fun reportWorkSpace(title: String, content: String, success: (Boolean) -> Unit) {
  ...
  ...
  ...
}

可自定义DataServerProvider.kt ,处理实际数据上报平台

kotlin 复制代码
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

kotlin 复制代码
/**
 * 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

kotlin 复制代码
/**
 * 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

kotlin 复制代码
/**
 * 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() }) }
}

引用三方库

  1. Google JetPacks > developer.android.com/jetpack
  2. Kotlin Coroutines > github.com/Kotlin/kotl...
  3. Retrofit & Okhttp > github.com/square/retr... & github.com/square/okht...
  4. Gson > github.com/google/gson
  5. auto-service > github.com/google/auto...
相关推荐
心态特好2 小时前
从缓存到分库分表:MySQL 高并发海量数据解决方案全解析
数据库·mysql·缓存
TDengine (老段)2 小时前
TDengine 数据函数 LEAST 用户手册
大数据·数据库·sql·时序数据库·tdengine
骇客野人2 小时前
【软考备考】 NoSQL数据库有哪些,键值型、文档型、列族型、图数据库的特点与适用场景
数据库·nosql
BD_Marathon2 小时前
【MySQL】管理
数据库·mysql
倔强的石头_2 小时前
金仓多模数据库平替 MongoDB:电子证照国产化的技术实操与价值突破
数据库
Go高并发架构_王工2 小时前
MySQL性能优化案例分析:从问题到解决方案
数据库·mysql·性能优化
二十三之歌2 小时前
Redis 中文学习手册
数据库·redis·学习
web安全工具库3 小时前
告别刀耕火种:用 Makefile 自动化 C 语言项目编译
linux·运维·c语言·开发语言·数据库·算法·自动化
disanleya3 小时前
怎样安全地开启MySQL远程管理权限?
数据库·mysql
【非典型Coder】4 小时前
Statement和PreparedStatement区别
数据库