Commit 29d11138 authored by ymwang's avatar ymwang

动态网络拓扑下发

parent 71a72ef2
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>cnf-distributed-business-computing</artifactId>
<groupId>top.ninwoo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cnf-distributed-business-topu</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>top.ninwoo</groupId>
<artifactId>cnf-client-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
#Generated by Maven
#Sun Aug 16 17:08:54 CST 2020
version=1.0-SNAPSHOT
groupId=top.ninwoo
artifactId=cnf-distributed-business-topu
......@@ -20,7 +20,7 @@
<module>dbc-business-utils</module>
<module>dbc-commom-api</module>
<module>cnf-distri-test</module>
<module>cnf-distributed-business-topu</module>
<module>distributed-business-cloudTest</module>
</modules>
<artifactId>cnf-distributed-business-computing</artifactId>
......
......@@ -34,7 +34,7 @@ public class TestController {
return result;
}
/* @GetMapping("sendFile")
/* @GetMapping("sendFile")
public String sendFile(String name, Long fileId) {
byte[] bytes = fileService.readFile(name);
return fileService.sendFile(fileId, bytes);
......
......@@ -14,7 +14,7 @@ import top.ninwoo.commonapi.po.SplitResult;
import top.ninwoo.commonapi.service.TransferService;
import javax.annotation.PostConstruct;
import java.io.FileOutputStream;
import java.io.*;
import java.util.*;
/**
......@@ -23,6 +23,9 @@ import java.util.*;
@Service
public class FileService {
@Value("${dfs.directory.output}")
private String directoryPath;
@Value("${dfs.directory.output}")
private String directoryOutput;
......@@ -43,6 +46,7 @@ public class FileService {
private String[] ipList = null;
@PostConstruct
public void init() {
//ipList = getIpList(11112l,"dfs_server");
......@@ -52,7 +56,6 @@ public class FileService {
}
public String[] getIpList(Long clusterId, String appName) {
//List<String> ipListS = networkService.getIpListByAppName(clusterId, appName);//用于存储容器ip的list集合
......@@ -73,6 +76,41 @@ public class FileService {
return containerIp;
}
// 从文件中读取数据
public byte[] readFile(String fileName) {
FileInputStream fis = null;
ByteArrayOutputStream bos = null;
try {
File file = new File(fileName);
fis = new FileInputStream(file);
bos = new ByteArrayOutputStream();
byte[] b = new byte[1024 * 10];//存取读到的多个字节
int len;//每次读取的有效字节的个数
while ((len = fis.read(b)) != -1) {
bos.write(b, 0, len);
}
return bos.toByteArray();
} catch (Exception e) {
if(fis != null) {
try {
fis.close();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
if(bos != null) {
try {
bos.close();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
throw new RuntimeException("文件读取错误" + e.getMessage());
}
}
/**
* 发送文件到分布式节点
* @param fileId
......@@ -83,13 +121,16 @@ public class FileService {
public String sendFile(Long fileId, byte[] fileBytes) {
// 通过集群服务器接口获取当前集群的节点数量
//ipList = this.getIpList(clusterId,"dfs_server");
//读入图片数据
//byte[] fileBytes = readFile("1.jpg");
for (int i = 0; i < wxName_list.length; i++){
ipList = getIpList(clusterId,wxName_list[i]);//通过卫星名字获取卫星ip
}
int sliceNum = ipList.length;
//int sliceNum = 30;
int origNum = 6;
int sliceNum = ipList.length; //根据卫星节点个数确定冗余后的数据碎片个数
int origNum = 6; //自己设定的一个图片被分割的原始数据的个数
// todo 这里需要制定文件的fileId
SplitResult splitResult = transferService.fileSplit(fileBytes, sliceNum, origNum);
......@@ -118,6 +159,8 @@ public class FileService {
if (!response.getStatusCode().is2xxSuccessful()) {
return "failed!";
}
saveToFile(fileSlice,directoryOutput);
return response.getBody();
}
......@@ -186,4 +229,32 @@ public class FileService {
fos.write(fileByte, 0, fileByte.length);
fos.close();
}
//在client将数据碎片传送至各个server上时,将数据包中的字节流存储在节点上
private void saveToFile(FileSlice fileSlice, String file_path) {
byte[] slice_data = fileSlice.getFileBytes();
File targetFile = new File(file_path);
if (targetFile.exists()) {
targetFile.delete();
}
//为什么创建父类
createParents(targetFile);
try {
targetFile.createNewFile();
FileOutputStream fileOutputStream = new FileOutputStream(targetFile);
fileOutputStream.write(slice_data);
} catch (IOException e) {
e.printStackTrace();
}
}
private void createParents(File f) {
String parentName = f.getParent();
File parent = new File(parentName);
if (!parent.exists()) {
createParents(parent);
parent.mkdir();
}
}
}
\ No newline at end of file
......@@ -4,7 +4,7 @@ bishe.app.app-name=joliu
#bishe.app.cloud-url=192.168.31.156:9090
bishe.app.cloud-url=192.168.31.198:9091
dfs.directory.output=/tmp/static/
dfs.directory.output=/tmp/static/images/
#thymeleaf
spring.thymeleaf.prefix=classpath:/templates/
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment