飞道的博客

RESTTemplate 请求第三方 RESTful API,@Scheduled的使用以及Spring Aop面向切面编程部分知识

486人阅读  评论(0)

Aop面向切面编程

Aop不是替代oop,oop的补充(架构模式)

通用的流程代码,从业务代码中抽离出来,定义(封装)成独立的,可插拔的组件,根据需要以装配的形式动态插入需要的业务方法
Spring Aop提供了基础的Aop支持,基于JDK,动态代理和CGLib库实现的,方法代理

AspectJ 提供强大的复杂的AOP支持。基于字节码生成,在编译,类加载,运行时都可以,不止方法还可以是字段


RESTTemplate 请求第三方 RESTful API(以火币为例) 


API文档: 

 


 


工程目录:

 


工程所需的依赖:

pom.xml


  
  1. <?xml version= "1.0" encoding= "UTF-8"?>
  2. <project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion> 4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version> 2.2.5.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.newer</groupId>
  12. <artifactId>kline</artifactId>
  13. <version> 0.1</version>
  14. <name>kline</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version> 11</java.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-web</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-devtools</artifactId>
  27. <scope>runtime</scope>
  28. <optional> true</optional>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.springframework.boot</groupId>
  32. <artifactId>spring-boot-starter-test</artifactId>
  33. <scope>test</scope>
  34. <exclusions>
  35. <exclusion>
  36. <groupId>org.junit.vintage</groupId>
  37. <artifactId>junit-vintage-engine</artifactId>
  38. </exclusion>
  39. </exclusions>
  40. </dependency>
  41. </dependencies>
  42. <build>
  43. <plugins>
  44. <plugin>
  45. <groupId>org.springframework.boot</groupId>
  46. <artifactId>spring-boot-maven-plugin</artifactId>
  47. </plugin>
  48. </plugins>
  49. </build>
  50. </project>

 


Tick.java


  
  1. package com.newer.kline;
  2. import java.util.Arrays;
  3. public class Tick {
  4. // 字段名称 数据类型 描述
  5. // id long 调整为新加坡时间的时间戳,单位秒,并以此作为此K线柱的id
  6. // amount float 以基础币种计量的交易量
  7. // count integer 交易次数
  8. // open float 本阶段开盘价
  9. // close float 本阶段收盘价
  10. // low float 本阶段最低价
  11. // high float 本阶段最高价
  12. // vol float 以报价币种计量的交易量
  13. long id;
  14. double amount;
  15. int count;
  16. double open;
  17. double close;
  18. double low;
  19. double high;
  20. double vol;
  21. // double [] kib;
  22. // double [] ask;
  23. public Tick() {
  24. }
  25. public long getId() {
  26. return id;
  27. }
  28. public void setId(long id) {
  29. this.id = id;
  30. }
  31. public double getAmount() {
  32. return amount;
  33. }
  34. public void setAmount(double amount) {
  35. this.amount = amount;
  36. }
  37. public int getCount() {
  38. return count;
  39. }
  40. public void setCount(int count) {
  41. this.count = count;
  42. }
  43. public double getOpen() {
  44. return open;
  45. }
  46. public void setOpen(double open) {
  47. this.open = open;
  48. }
  49. public double getClose() {
  50. return close;
  51. }
  52. public void setClose(double close) {
  53. this.close = close;
  54. }
  55. public double getLow() {
  56. return low;
  57. }
  58. public void setLow(double low) {
  59. this.low = low;
  60. }
  61. public double getHigh() {
  62. return high;
  63. }
  64. public void setHigh(double high) {
  65. this.high = high;
  66. }
  67. public double getVol() {
  68. return vol;
  69. }
  70. public void setVol(double vol) {
  71. this.vol = vol;
  72. }
  73. @Override
  74. public String toString() {
  75. return "Tick [id=" + id + ", amount=" + amount + ", count=" + count + ", open=" + open + ", close=" + close
  76. + ", low=" + low + ", high=" + high + ", vol=" + vol + "]";
  77. }
  78. }

HomeController.java


  
  1. package com.newer.kline;
  2. import org.springframework.http.HttpEntity;
  3. import org.springframework.http.HttpHeaders;
  4. import org.springframework.http.HttpMethod;
  5. import org.springframework.web.bind.annotation.GetMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7. import org.springframework.web.client.RestTemplate;
  8. @RestController
  9. public class HomeController {
  10. // 注入业务逻辑或数据操作
  11. // 从第三方API接口获得数据
  12. @GetMapping( "/kline")
  13. public String kline() {
  14. // 服务端从第三方(火币)获得数据
  15. // RestTemplate是java的Http客户端:ttpURLConnection, Apache HttpComponents, and others.
  16. // Vue中 axios:JS Http客户端
  17. RestTemplate restTemplate= new RestTemplate();
  18. String url = "https://api.huobi.me/market/detail/merged?symbol=btcusdt";
  19. // 第一种方式:发送GET请求,从响应中获得相应的数据
  20. // return restTemplate.getForEntity(url, String.class).getBody();
  21. // 第二种方式:发送HTTP任意请求
  22. HttpHeaders headers= new HttpHeaders();
  23. // headers.put(HttpHeaders.AUTHORIZATION);
  24. // 设置HTTP请求头
  25. headers.set(HttpHeaders.CONTENT_TYPE, "application/json");
  26. headers.set(HttpHeaders.AUTHORIZATION, "令牌");
  27. HttpEntity requestEntity= new HttpEntity<>( null,headers);
  28. // 发送HTTP任意请求,POST,PUT,DELETE
  29. return restTemplate.exchange(
  30. url,
  31. HttpMethod.GET,
  32. requestEntity,
  33. String.class)
  34. .getBody();
  35. }
  36. }

这样,我们就可以从第三方API接口获取数据


@Scheduled的使用

如果我们想从第三方API接口不断获得实时更新的数据,可以采用@Scheduled 中的频率和时间间隔。

KlineJob.java


  
  1. package com.newer.kline;
  2. import java.util.Date;
  3. import org.springframework.http.HttpEntity;
  4. import org.springframework.http.HttpMethod;
  5. import org.springframework.scheduling.annotation.Scheduled;
  6. import org.springframework.stereotype.Component;
  7. import org.springframework.web.client.RestTemplate;
  8. @Component
  9. public class KlineJob {
  10. // 假设执行耗费时间为5秒
  11. // 时间 频率 延时(时间间隔)
  12. // 0 0 0
  13. // 1 10 15
  14. // 2 20 30
  15. // 3 30 45
  16. // 4 40 60
  17. // 固定的频率
  18. @Scheduled(fixedRate = 1000* 10)
  19. public void job() {
  20. // System.out.println("固定的频率"+new Date().toLocaleString());
  21. //// 5
  22. // try {
  23. // Thread.sleep(5000);
  24. // } catch (InterruptedException e) {
  25. // // TODO Auto-generated catch block
  26. // e.printStackTrace();
  27. // }
  28. //
  29. // HttpClient,UrlConnection发送Http请求
  30. // RestTemplate(WebClient) 访问其他的RESTful API
  31. // RestController 自己定义RESTful API
  32. RestTemplate restTemplate= new RestTemplate();
  33. String url= "https://api.huobi.me/market/detail/merged?symbol=ethusdt";
  34. HttpMethod method=HttpMethod.GET;
  35. HttpEntity requestEntity= null;
  36. String json= restTemplate.exchange(
  37. url,
  38. method,
  39. requestEntity,
  40. String.class).getBody();
  41. // 写入MongoDB
  42. // 写入文档数据库
  43. System.out.println(json);
  44. }
  45. // 固定的延时
  46. @Scheduled(fixedDelay = 1000* 10)
  47. public void job2() {
  48. System.out.println( "固定延时"+ new Date().toLocaleString());
  49. try {
  50. Thread.sleep( 5000);
  51. } catch (InterruptedException e) {
  52. // TODO Auto-generated catch block
  53. e.printStackTrace();
  54. }
  55. }
  56. }

KlineApplication.java(@EnableScheduling)


  
  1. package com.newer.kline;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.scheduling.annotation.EnableScheduling;
  5. @EnableScheduling
  6. @SpringBootApplication
  7. public class KlineApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(KlineApplication.class, args);
  10. }
  11. }

控制台输出:

可以看到程序从第三方API接口在不断的获取实时的数据。


 以上就是RESTTemplate 请求第三方 RESTful API,@Scheduled的使用以及Spring Aop面向切面编程部分知识,有问题的小伙伴,欢迎留言!!!


转载:https://blog.csdn.net/weixin_44364444/article/details/105819274
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场