Finology 大数据金融

通过大数据以量化金融

最近在使用一个性能测试框架,locust,是用Python写的。模拟测试逻辑时,可用Python来编写需要进行测试的逻辑。

这篇文章讲讲如何使用 requests 这个库,以发送http请求。

Get请求

1
2
3
4
5
6
7
8
url = 'http://localhost/api'
payload = {'id': 100}
headers = {'token': 'xxxxxxx'}
with self.client.get(url, params=payload, headers=headers) as response:
if response.status_code == 200:
if response.text != '':
res_dict = json.loads(response.text)
...

Post请求

当我们使用json方式提交请求时,其实是不用在代码里面指定Content-type: applicaion/json的,只需要使用json参数就可以了。

1
2
3
4
5
6
7
8
url = 'http://localhost/api'
payload = {'id': 100}
headers = {'token': 'xxxxxxx'}
with self.client.post(url, json=payload, headers=headers) as response:
if response.status_code == 200:
if response.text != '':
res_dict = json.loads(response.text)
...

上传文件有点特殊,也不需要指定Content-type,使用files参数即可。java服务端,使用MultipartFile来接收这个文件即可。

1
2
3
4
5
6
7
8
url = 'http://localhost/api'
file = {'file': open('/opt/test.txt', 'rb')}
headers = {'token': 'xxxxxxx'}
with self.client.post(url, files=file, headers=headers) as response:
if response.status_code == 200:
if response.text != '':
res_dict = json.loads(response.text)
...

这篇文章讲讲在macOS系统中,如何一步一步创建一个基于Anaconda的项目。

首先我们得确定已经在macOS中安装好了Anaconda。

新建Python环境

先查看一下当前环境,只有一个base。

1
2
3
4
$ conda env list
# conda environments:
#
base * /opt/anaconda3

以克隆方式创建一个和Anaconda base(root)环境一样的环境,取名叫py37

1
2
3
$ conda create --name py37 --clone base
Source: /opt/anaconda3
Destination: /opt/anaconda3/envs/py37

按提示激活命令激活环境。

1
$ conda activate py37

新建Python项目

File -> New -> Project…

选择Python项目,New一个Project SDK

添加Python解析器

选择 Conda Environment,使用Conda来管理包。

我们前面使用命令行的方式创建一个env,所以选择Existing Environment。

指定Interpreter,为刚才新建环境py37目录下的python。

/opt/anaconda3/envs/py37/bin/python

然后下一步,确定就创建好了。

检查

创建好项目后,我们写两行python代码。

1
2
3
import pandas as pd

print('hello world')

检查,pandas, print下面没有红线,按Command,同时鼠标点击pandas或print,均能查看得到源代码,运行时,也会正常运行,说明配置一切正常。

我们再看看项目结构。

启动 sentinel dashboard

在官网下载 sentinel-dashboard-1.8.1.jar,并运行

1
java -Dserver.port=8081 -Dcsp.sentinel.dashboard.server=localhost:8081 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.8.1.jar

8081端口是指访问dashboard的端口。

启动以后,可以通过链接 http://localhost:8081 访问到页面,用户名和密码都是 sentinel

把项目加入到 dashboard

写一个标准的Springboot项目。

pom.xml文件里面添加如下依赖:

1
2
3
4
5
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>${sentinel.version}</version>
</dependency>

在项目的 application.yml 配置文件里面添加如下内容:

1
2
3
4
5
spring:
cloud:
sentinel:
transport:
dashboard: localhost:8081

在需要加入限流的业务逻辑里面添加如下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
@Service
public class UserServiceImpl implements UserService {

// 限流的规则设置
@PostConstruct
private void initFlowRules() {

List<FlowRule> rules = new ArrayList<>();
FlowRule flowRule = new FlowRule();
flowRule.setResource("UserFlowRule");
// 设置QPS为1
flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
flowRule.setCount(1);

rules.add(flowRule);
FlowRuleManager.loadRules(rules);
}

@Override
@SentinelResource(value = "UserFlowRule", blockHandler = "degradeMethod")
public String sayHello(String name) {
System.out.println("executed...");
return "hello, " + name;
}

/**
* 降级方法
* @param name
* @param blockException
* @return
*/
public String degradeMethod(String name, BlockException blockException) {
return "限流" + blockException.getRule().getResource();
}

}
0%