Logchain学习总结

一、Python知识准备

源码https://github.com/WilliamPourmajidi/LCaaS

1.LCaaS_timer = TimeKeeper("ICSETimerLCaaS.csv")

Timekeeper是一个用于通过向InfluxDB发送测量值来检测实时Python代码的库。(需要Python 2.7或3.2+以及InfluxDB 0.9.0+。)

您可以使用上下文管理器或装饰函数来记录完成该函数或代码块所需时间的挂钟时间:

from timekeeper import TimeKeeper

tk = TimeKeeper(
"udp+influxdb://localhost/databasename",
prefix="location-1.cluster-1.app-1.", # if you prefer Graphite style over tags
tags={"host": "location-1.cluster-1.app-1"},
)

@tk.decorate("some_slow_function", tags={"foo": "bar"})
def slowpoke():
	sleep(9001)

def slowpoke2():
	with tk.context("some_other_slow_function", tags={"foo": "baz"}):
	sleep(9001)

influxdb是目前比较流行的时间序列数据库。

  • 何谓时间序列数据库?
    什么是时间序列数据库,最简单的定义就是数据格式里包含Timestamp字段的数据,比如某一时间环境的温度,CPU的使用率等。但是,有什么数据不包含Timestamp呢?几乎所有的数据其实都可以打上一个Timestamp字段。时间序列数据的更重要的一个属性是如何去查询它,包括数据的过滤,计算等等。

Influxdb
Influxdb是一个开源的分布式时序、时间和指标数据库,使用go语言编写,无需外部依赖。它有三大特性:

  • 时序性(Time Series):与时间相关的函数的灵活使用(诸如最大、最小、求和等);
  • 度量(Metrics):对实时大量数据进行计算;
  • 事件(Event):支持任意的事件数据,换句话说,任意事件的数据我们都可以做操作。

同时,它有以下几大特点:

  • schemaless(无结构),可以是任意数量的列;
  • min, max, sum, count, mean, median 一系列函数,方便统计;
  • Native HTTP API, 内置http支持,使用http读写;
  • Powerful Query Language 类似sql;
  • Built-in Explorer 自带管理工具。
二、学习使用Firebase
1. 使用已有的Google账号注册登录
2.创建项目

步骤如下:
(1)点击“添加项目”
在这里插入图片描述(2)完善项目添加信息
在这里插入图片描述
(3)项目创建完成
在这里插入图片描述
(4)项目的关键信息
将对应的关键信息填入代码中相应的位置:

config = {
  "apiKey": "apiKey",
  "authDomain": "projectId.firebaseapp.com",
  "databaseURL": "https://databaseName.firebaseio.com",
  "storageBucket": "projectId.appspot.com"
   "messagingSenderId": "***************",
   "serviceAccount": "serviceAccountKey.json"
}

firebase = pyrebase.initialize_app(config)
auth = firebase.auth()
user = auth.create_user_with_email_and_password("random@**.com", "password")
# user['idToken']
db = firebase.database()

在这里插入图片描述在这里插入图片描述在这里插入图片描述
(5)为firebase-adminsdk创建私钥
在这里插入图片描述
在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述生成json格式的文件

{
  "type": "service_account",
  "project_id": "****-2018",
  "private_key_id": "66051dc7440c********62e7980f94e7",
  "private_key": "-----BEGIN PRIVATE KEY-----\***************************w==\n-----END PRIVATE KEY-----\n",
  "client_email": "tester2018@****-2018.iam.gserviceaccount.com",
  "client_id": "1078*****7189676396",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://accounts.google.com/o/oauth2/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/tester2018%40bcaas-2018.iam.gserviceaccount.com"
}

(6)为项目添加用户
在这里插入图片描述在这里插入图片描述

三、遇到的错误总结
1、该项目运行所需要的Python库不能安装(在cmd下安装会遇到很多问题)

在这里插入图片描述
解决方法

方法1: 在Pycharm下创建该项目的虚拟运行环境,然后在terminal中用pip install ***安装
在这里插入图片描述方法2:有些包在该环境下也不能安装,可以到python官方的第三方库的仓库PyPI中下载whl文件来进行安装,比如下载TimeKeeper:
在这里插入图片描述点击链接将文件保存到合适的位置

在这里插入图片描述安装方式:
在这里插入图片描述方法2:有些包前两种方式均不能安装,比如Pyrebase,可以到python官方的第三方库的仓库PyPI中下载tar.gz文件解压,进行安装
在这里插入图片描述
安装方式:
在这里插入图片描述

2.运行过程中遇到认证问题

requests.exceptions.HTTPError: [Errno 400 Client Error: Bad Request for url: https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword?key=APIKey]
相关代码:

config = {
  "apiKey": "apiKey",
  "authDomain": "projectId.firebaseapp.com",
  "databaseURL": "https://databaseName.firebaseio.com",
  "storageBucket": "projectId.appspot.com"
}
firebase = pyrebase.initialize_app(config)
auth = firebase.auth()
user = auth.create_user_with_email_and_password("random@gmail.com", "crazy_password")

这个问题是弄混了firebase的登录账号密码和项目下账号密码的关系,运行时代码的账号密码为登录的账号密码,该项目下并没有账号密码,解决方法如下:

  • 点击Authentication,在用户下点击添加用户,输入该项目的用户的账号密码,然后代人user = auth.create_user_with_email_and_password()
    在这里插入图片描述
    修改之后,再次点击运行:
    在这里插入图片描述点击链接查看:
    在这里插入图片描述成功!

参考:[1] https://www.jianshu.com/p/d2935e99006e

@Aspect @Component public class CheckRequestAuthAspect { private static final Logger LOGGER = LoggerFactory.getLogger(CheckRequestAuthAspect.class); @Conf(value = "NebulaService.AppAuth.MandatoryApiList", as = "json") private List<String> mandatoryApiList; @Around(value = "@annotation(checkRequestAuth)") public Object execute(ProceedingJoinPoint pjp, CheckRequestAuth checkRequestAuth) throws Throwable { // 强制取AbstractAppAuthRequest,如果失败报异常,让上层切面捕获 AbstractAppAuthRequest requestDto = (AbstractAppAuthRequest) pjp.getArgs()[0]; // 获取请求接口基本信息 ServiceRequest serviceReq = ServiceInvokeHelper.getCurrentRequest(); String service = serviceReq.getService(); String methodName = serviceReq.getMethodName(); boolean authPassed; switch (checkRequestAuth.authType()) { case BASIC: authPassed = basicAuth(requestDto); break; case CONFIG_CENTER: authPassed = configCenterAuth(requestDto); break; default: throw new RuntimeException("CheckAppAuth authType error, unsupported authType: " + checkRequestAuth.authType()); } if (mandatoryApiList != null && mandatoryApiList.contains(methodName)) { // 配置的接口需要强制校验appName不为空 authPassed = requestDto != null && StringUtils.isNotBlank(requestDto.getAppName()); } if (!authPassed) { LOGGER.warn(LogChain.text("Auth failed for service:%s, method:%s", service, methodName)); return BaseResponse.fail(CONFIG_APP_AUTH_ERROR); } // 校验通过开始执行接口逻辑 return pjp.proceed(); } /** * 验证请求参数中附带的授权信息 */ private boolean basicAuth(final AbstractAppAuthRequest requestDto) { if (requestDto == null || requestDto.getAppName() == null) { return true; } if (StringUtils.equalsIgnoreCase(requestDto.getAppName(), requestDto.getAuthCode())) { return true; } LOGGER.warn(LogChain.text("basicAuth failed for request:" + JsonUtil.toJson(requestDto))); return false; } /** * 白名单验证,验证配置中心appName存在并和authCode匹配 */ private boolean configCenterAuth(final AbstractAppAuthRequest requestDto) { if (requestDto != null && requestDto.getAppName() != null && requestDto.getAuthCode() != null) { return ArrayUtils.contains(AppNameAuthConfig.getAuthCodesByAppName(requestDto.getAppName()), requestDto.getAuthCode()); } LOGGER.warn(LogChain.text("configCenterAuth failed for request:" + JsonUtil.toJson(requestDto))); return false; } }详细解释下这个类的作用
09-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值