1. 第三方数据服务数据是保存在第三方,也许那天就不能访问了,所以这里用nosql redis保存
2. 首先还是创建一个子模块index-gather-store-service,然后修改pom文件
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.cashbook.trend</groupId>
<artifactId>trendParentProject</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>cn.how2j.trend</groupId>
<artifactId>index-gather-store-service</artifactId>
<dependencies>
<!-- springboot web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3. 然后创建一个启动程序IndexGatherStoreApplication
package cn.cashbook.trend;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.NetUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableEurekaClient
public class IndexGatherStoreApplication {
public static void main(String[] args) {
int port = 0;
int defaultPort = 8001;
int eurekaServerPort = 8761;
if (NetUtil.isUsableLocalPort(eurekaServerPort)) {
System.err.printf("检查到端口%d 未启用,判断 eureka 服务器没有启动,本服务无法使用,故退出%n", eurekaServerPort);
System.exit(1);
}
if (null != args && 0 != args.length) {
for (String arg : args) {
if (arg.startsWith("port=")) {
String strPort = StrUtil.subAfter(arg, "port", true);
if (NumberUtil.isNumber(strPort)) {
port = Convert.toInt(strPort);
}
}
}
}
if (NetUtil.isUsableLocalPort(port)) {
System.err.printf("端口%d被占用了,无法启动%n", port);
System.exit(1);
}
new SpringApplicationBuilder(IndexGatherStoreApplication.class).properties("server.port=" + port).run(args);
}
@Bean
RestTemplate restTemplate() {
return new RestTemplate();
}
}
4. 写一个java普通类,用以表示项目所需的基本实体
package cn.cashbook.trend.pojo;
import java.io.Serializable;
public class Index implements Serializable {
String name;
double money;
String category;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
}
5. 创建一个服务类,获取codes.json的数据,利用工具类resTemplate获取数据
package cn.cashbook.trend.service;
import cn.cashbook.trend.pojo.Index;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Service
public class IndexService {
private List<Index> indexes;
@Autowired
RestTemplate restTemplate;
public List<Index> fetch_indexes_from_third_part() {
List<Map> temp = restTemplate.getForObject("http://127.0.0.1:8090/indexes/codes.json",List.class);
return map2Index(temp);
}
private List<Index> map2Index(List<Map> temp) {
List<Index> indexes = new ArrayList<>();
for(Map map : temp) {
String category = map.get("category").toString();
String money = map.get("money").toString();
String name = map.get("name").toString();
Index index = new Index();
index.setCategory(category);
index.setMoney(Double.valueOf(money));
index.setName(name);
indexes.add(index);
}
return indexes;
}
}
6. 写一个控制类,用以调用方法
package cn.cashbook.trend.web;
import cn.cashbook.trend.pojo.Index;
import cn.cashbook.trend.service.IndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class IndexController {
@Autowired
IndexService indexService;
@GetMapping("/getMoney")
public List<Index> get() throws Exception {
return indexService.fetch_indexes_from_third_part();
}
}
7. 写一个配置文件
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
spring:
application:
name: index-gather-store-service
8. 然后启动http://127.0.0.1:8001/getMoney