数据监控&告警Demo(Oracl_http-post)
作者:互联网
数据监控&告警(Oracl_http-post)
一.java_http接口
PostController.java
package com.springboot.test_springboot.post_controller;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/applog")
@ResponseBody
public class PostController {
@PostMapping("/post")
public String getAvroInfo(@RequestBody JSONObject avroInfo){
System.out.println(avroInfo.toString());
// Producer producer=KafkaUtils.getKafkaProducer();
// producer.send(new ProducerRecord<String, String>("user_message", avroInfo));
return avroInfo.getString("data");
}
}
二.python_主程序
ConPost.py
# encoding: utf-8
"""
python post请求
"""
import json
import requests
def connection_post(string):
url = "http://127.0.0.1:9999/applog/post"
headers = {'content-type': 'application/json'}
requestData = string
ret = requests.post(url, json=requestData, headers=headers)
data = ''
if ret.status_code == 200:
data=json.loads(ret.text)
return data
# data=connection_post({'data':100})
# print(data)
ConOracle.py
# encoding: utf-8
"""
python 连接oracle
"""
import cx_Oracle as cx
import pandas.io.sql as sql
def connection_oracle(string):
host = '127.0.0.1'
port = 1521
database = 'oracle'
username = "scott"
password = "scott"
dsn = cx.makedsn(host, port, database)
connection = cx.connect(username, password, dsn)
cursor = connection.cursor()
data=sql.read_sql(string,connection)
connection.commit()
cursor.close()
connection.close()
return data.iloc[0,0]
# string_sql="""
#
# select sum(deptno) from dept
#
# """
#
# data=connection_oracle(string_sql)
#
# print(data)
main.py
# This is a sample Python script.
# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
from math import fabs
import com.jdbc.ConOracle as jdbc
import com.http.ConPost as http
def print_hi(name):
# Use a breakpoint in the code line below to debug your script.
print(f'this: {name}') # Press Ctrl+F8 to toggle the breakpoint.
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
print_hi('数据误差反馈')
# See PyCharm help at https://www.jetbrains.com/help/pycharm
data_from_oracle=float(jdbc.connection_oracle("select sum(deptno) from dept"))
data_from_http=float(http.connection_post({'data':130}))
print('data_from_oracle: '+str(data_from_oracle))
print('data_from_http: '+str(data_from_http))
result=fabs((data_from_http-data_from_oracle)/data_from_oracle)
print('result: '+str((data_from_http-data_from_oracle)/data_from_oracle))
if result>= 0.3:
print("发送邮件:数据异常")
elif result>= 0.2:
print("告警数据误差过大")
elif result >= 0.1:
print("数据无异常")
else:
print("数据正常")
三.其他
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springboot</groupId>
<artifactId>test_springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>test_springboot</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.75</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.14</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.6.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
python引用
标签:http,Demo,Oracl,oracle,connection,print,post,data 来源: https://blog.csdn.net/qq_31316733/article/details/117202193