Aop面向切面编程
Aop不是替代oop,oop的补充(架构模式)
通用的流程代码,从业务代码中抽离出来,定义(封装)成独立的,可插拔的组件,根据需要以装配的形式动态插入需要的业务方法
Spring Aop提供了基础的Aop支持,基于JDK,动态代理和CGLib库实现的,方法代理AspectJ 提供强大的复杂的AOP支持。基于字节码生成,在编译,类加载,运行时都可以,不止方法还可以是字段
RESTTemplate 请求第三方 RESTful API(以火币为例)
API文档:
工程目录:
工程所需的依赖:
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.2.5.RELEASE</version>
-
<relativePath/> <!-- lookup parent from repository -->
-
</parent>
-
<groupId>com.newer</groupId>
-
<artifactId>kline</artifactId>
-
<version>
0.1</version>
-
<name>kline</name>
-
<description>Demo project
for Spring Boot</description>
-
-
<properties>
-
<java.version>
11</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-devtools</artifactId>
-
<scope>runtime</scope>
-
<optional>
true</optional>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter-test</artifactId>
-
<scope>test</scope>
-
<exclusions>
-
<exclusion>
-
<groupId>org.junit.vintage</groupId>
-
<artifactId>junit-vintage-engine</artifactId>
-
</exclusion>
-
</exclusions>
-
</dependency>
-
</dependencies>
-
-
<build>
-
<plugins>
-
<plugin>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-maven-plugin</artifactId>
-
</plugin>
-
</plugins>
-
</build>
-
-
</project>
Tick.java
-
package com.newer.kline;
-
-
import java.util.Arrays;
-
-
public
class Tick {
-
-
// 字段名称 数据类型 描述
-
// id long 调整为新加坡时间的时间戳,单位秒,并以此作为此K线柱的id
-
// amount float 以基础币种计量的交易量
-
// count integer 交易次数
-
// open float 本阶段开盘价
-
// close float 本阶段收盘价
-
// low float 本阶段最低价
-
// high float 本阶段最高价
-
// vol float 以报价币种计量的交易量
-
-
long id;
-
double amount;
-
int count;
-
double open;
-
double close;
-
double low;
-
double high;
-
double vol;
-
// double [] kib;
-
// double [] ask;
-
-
public Tick() {
-
-
}
-
-
public long getId() {
-
return id;
-
}
-
-
public void setId(long id) {
-
this.id = id;
-
}
-
-
public double getAmount() {
-
return amount;
-
}
-
-
public void setAmount(double amount) {
-
this.amount = amount;
-
}
-
-
public int getCount() {
-
return count;
-
}
-
-
public void setCount(int count) {
-
this.count = count;
-
}
-
-
public double getOpen() {
-
return open;
-
}
-
-
public void setOpen(double open) {
-
this.open = open;
-
}
-
-
public double getClose() {
-
return close;
-
}
-
-
public void setClose(double close) {
-
this.close = close;
-
}
-
-
public double getLow() {
-
return low;
-
}
-
-
public void setLow(double low) {
-
this.low = low;
-
}
-
-
public double getHigh() {
-
return high;
-
}
-
-
public void setHigh(double high) {
-
this.high = high;
-
}
-
-
public double getVol() {
-
return vol;
-
}
-
-
public void setVol(double vol) {
-
this.vol = vol;
-
}
-
-
@Override
-
public String toString() {
-
return
"Tick [id=" + id +
", amount=" + amount +
", count=" + count +
", open=" + open +
", close=" + close
-
+
", low=" + low +
", high=" + high +
", vol=" + vol +
"]";
-
}
-
-
-
}
HomeController.java
-
package com.newer.kline;
-
-
-
-
import org.springframework.http.HttpEntity;
-
import org.springframework.http.HttpHeaders;
-
import org.springframework.http.HttpMethod;
-
import org.springframework.web.bind.annotation.GetMapping;
-
import org.springframework.web.bind.annotation.RestController;
-
import org.springframework.web.client.RestTemplate;
-
-
@RestController
-
public
class HomeController {
-
-
// 注入业务逻辑或数据操作
-
-
-
// 从第三方API接口获得数据
-
@GetMapping(
"/kline")
-
public String kline() {
-
-
// 服务端从第三方(火币)获得数据
-
-
// RestTemplate是java的Http客户端:ttpURLConnection, Apache HttpComponents, and others.
-
// Vue中 axios:JS Http客户端
-
RestTemplate restTemplate=
new RestTemplate();
-
-
String url =
"https://api.huobi.me/market/detail/merged?symbol=btcusdt";
-
-
// 第一种方式:发送GET请求,从响应中获得相应的数据
-
// return restTemplate.getForEntity(url, String.class).getBody();
-
-
-
// 第二种方式:发送HTTP任意请求
-
-
HttpHeaders headers=
new HttpHeaders();
-
// headers.put(HttpHeaders.AUTHORIZATION);
-
-
// 设置HTTP请求头
-
headers.set(HttpHeaders.CONTENT_TYPE,
"application/json");
-
headers.set(HttpHeaders.AUTHORIZATION,
"令牌");
-
-
HttpEntity requestEntity=
new HttpEntity<>(
null,headers);
-
-
// 发送HTTP任意请求,POST,PUT,DELETE
-
return restTemplate.exchange(
-
url,
-
HttpMethod.GET,
-
requestEntity,
-
String.class)
-
.getBody();
-
-
-
}
-
}
这样,我们就可以从第三方API接口获取数据
@Scheduled的使用
如果我们想从第三方API接口不断获得实时更新的数据,可以采用@Scheduled 中的频率和时间间隔。
KlineJob.java
-
package com.newer.kline;
-
-
-
-
import java.util.Date;
-
-
import org.springframework.http.HttpEntity;
-
import org.springframework.http.HttpMethod;
-
import org.springframework.scheduling.annotation.Scheduled;
-
import org.springframework.stereotype.Component;
-
import org.springframework.web.client.RestTemplate;
-
-
@Component
-
public
class KlineJob {
-
-
// 假设执行耗费时间为5秒
-
// 时间 频率 延时(时间间隔)
-
// 0 0 0
-
// 1 10 15
-
// 2 20 30
-
// 3 30 45
-
// 4 40 60
-
-
-
// 固定的频率
-
@Scheduled(fixedRate =
1000*
10)
-
public void job() {
-
// System.out.println("固定的频率"+new Date().toLocaleString());
-
//// 5
-
// try {
-
// Thread.sleep(5000);
-
// } catch (InterruptedException e) {
-
// // TODO Auto-generated catch block
-
// e.printStackTrace();
-
// }
-
//
-
-
// HttpClient,UrlConnection发送Http请求
-
// RestTemplate(WebClient) 访问其他的RESTful API
-
// RestController 自己定义RESTful API
-
RestTemplate restTemplate=
new RestTemplate();
-
String url=
"https://api.huobi.me/market/detail/merged?symbol=ethusdt";
-
HttpMethod method=HttpMethod.GET;
-
HttpEntity requestEntity=
null;
-
String json= restTemplate.exchange(
-
url,
-
method,
-
requestEntity,
-
String.class).getBody();
-
-
// 写入MongoDB
-
-
// 写入文档数据库
-
System.out.println(json);
-
-
}
-
-
// 固定的延时
-
@Scheduled(fixedDelay =
1000*
10)
-
public void job2() {
-
System.out.println(
"固定延时"+
new Date().toLocaleString());
-
try {
-
Thread.sleep(
5000);
-
}
catch (InterruptedException e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
-
}
-
}
KlineApplication.java(@EnableScheduling)
-
package com.newer.kline;
-
-
import org.springframework.boot.SpringApplication;
-
import org.springframework.boot.autoconfigure.SpringBootApplication;
-
import org.springframework.scheduling.annotation.EnableScheduling;
-
-
@EnableScheduling
-
@SpringBootApplication
-
public
class KlineApplication {
-
-
public static void main(String[] args) {
-
SpringApplication.run(KlineApplication.class, args);
-
-
-
-
}
-
-
}
控制台输出:
可以看到程序从第三方API接口在不断的获取实时的数据。
以上就是RESTTemplate 请求第三方 RESTful API,@Scheduled的使用以及Spring Aop面向切面编程部分知识,有问题的小伙伴,欢迎留言!!!
转载:https://blog.csdn.net/weixin_44364444/article/details/105819274
查看评论