Finology 大数据金融

通过大数据以量化金融

启动 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();
}

}

Anaconda的基本操作,可以参考这篇文章 macOS下Anaconda的安装及环境切换(Python2/Python3)

这篇文章讲讲如何clone一个和Anaconda base(root)一样的环境,也就是和base(root)有一样的多的包的环境。

复制的命令如下:

1
conda create --name <new_env_name> --clone <copied_env_name>
1
conda create --name new_env --clone base

创建Python项目的时候,

New Project -> Project SDK (New…) -> Add Interpreter -> Conda Environment -> Existing Environment,
这里面就填刚才新建的新环境路径。

安装包

首先要切换环境,linux是source activate <env>,而Windows系统是activate <env>

安装包命令:

conda install <package>,如果没有的话,那就用pip来安装。

pip install <package>

if

1
[fun(item) for item in list if condition]

if else

1
[fun1(item) if condition else fun2(item) for item in list]

if else if

1
[fun1(item) if condition1 else fun2(item) if condition2 fun3(item) else fun4(item) for item in list]
0%