소스 검색

首次提交

huangzhiyong 6 년 전
부모
커밋
697fedcbd0

+ 1 - 1
.gitignore

@ -8,7 +8,7 @@
*.jar
*.war
*.ear
*.iml
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

+ 111 - 0
pom.xml

@ -0,0 +1,111 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         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>com.yihu.ehr</groupId>
        <artifactId>ehr-cloud-parent</artifactId>
        <version>1.13.1</version>
    </parent>
    <artifactId>svr-eip-dfs</artifactId>
    <version>1.1.0</version>
    <packaging>jar</packaging>
    <name>svr-eip-dfs</name>
    <description>eip file and zbus mq module</description>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>netty-codec-http</artifactId>
                    <groupId>io.netty</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>netty-transport</artifactId>
                    <groupId>io.netty</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>netty-transport-native-epoll</artifactId>
                    <groupId>io.netty</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>netty-handler</artifactId>
                    <groupId>io.netty</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>netty-buffer</artifactId>
                    <groupId>io.netty</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>netty-codec</artifactId>
                    <groupId>io.netty</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongo-java-driver</artifactId>
        </dependency>
        <dependency>
            <groupId>io.zbus</groupId>
            <artifactId>zbus</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot-version}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <mainClass>com.yihu.hos.dfs.HosDfsApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

+ 35 - 0
src/main/java/com/yihu/hos/dfs/HosDfsApplication.java

@ -0,0 +1,35 @@
package com.yihu.hos.dfs;
import com.yihu.hos.dfs.configuration.ZbusConfiguration;
import io.zbus.mq.server.MqServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableEurekaClient
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
public class HosDfsApplication implements CommandLineRunner {
    private static final Logger logger = LoggerFactory.getLogger(HosDfsApplication.class);
    @Autowired
    private ZbusConfiguration zbusConfiguration;
    public static void main(String[] args) {
        SpringApplication.run(HosDfsApplication.class, args);
    }
    @Override
    public void run(String... strings) throws Exception {
        final MqServer server = new MqServer("zbus.xml");
        server.start();
        if (logger.isInfoEnabled()) {
            logger.info("zbus 启动成功!");
        }
    }
}

+ 24 - 0
src/main/java/com/yihu/hos/dfs/configuration/ZbusConfiguration.java

@ -0,0 +1,24 @@
package com.yihu.hos.dfs.configuration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
/**
 * Created by l4qiang on 2017-04-10.
 */
@Configuration
public class ZbusConfiguration {
    @Value("${hos.zbus.port}")
    private Integer zbusPort;
    @Value("${hos.zbus.store}")
    private String zbusStore;
    public Integer getZbusPort() {
        return zbusPort;
    }
    public String getZbusStore() {
        return zbusStore;
    }
}

+ 52 - 0
src/main/java/com/yihu/hos/dfs/controller/DFSController.java

@ -0,0 +1,52 @@
package com.yihu.hos.dfs.controller;
import com.yihu.hos.dfs.service.DFSService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
/**
 * 基于Mongo的文件系统
 * <p>跨域注解
 * Created by l4qiang on 2017-04-10.
 */
@CrossOrigin
@RestController
@RequestMapping(value = "/dfs")
public class DFSController {
    @Autowired
    private DFSService dfsService;
    @RequestMapping(value = "file/{fileId}/info", method = RequestMethod.GET)
    public ResponseEntity info(@PathVariable("fileId") String fileId) {
        return dfsService.info(fileId);
    }
    @RequestMapping(value = "file/{fileId}", method = RequestMethod.GET)
    public ResponseEntity retrieve(@PathVariable("fileId") String fileId, HttpServletResponse httpServletResponse) {
        return dfsService.retrieve(fileId, httpServletResponse);
    }
    @RequestMapping(value = "file/{fileId}/download", method = RequestMethod.GET)
    public ResponseEntity download(@PathVariable("fileId") String fileId) {
        return dfsService.download(fileId);
    }
    @RequestMapping(value = "file", method = RequestMethod.POST)
    public ResponseEntity store(@RequestPart MultipartFile file) {
        return dfsService.store(file);
    }
    @RequestMapping(value = "file/{fileId}", method = RequestMethod.POST)
    public ResponseEntity update(@PathVariable("fileId") String fileId, @RequestParam("file") MultipartFile file) {
        return dfsService.update(fileId, file);
    }
    @RequestMapping(value = "file/{fileId}", method = RequestMethod.DELETE)
    public ResponseEntity delete(@PathVariable("fileId") String fileId) {
        return dfsService.delete(fileId);
    }
}

+ 128 - 0
src/main/java/com/yihu/hos/dfs/service/DFSService.java

@ -0,0 +1,128 @@
package com.yihu.hos.dfs.service;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mongodb.gridfs.GridFSDBFile;
import com.mongodb.gridfs.GridFSFile;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.gridfs.GridFsOperations;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.Optional;
/**
 * Mongodb file system
 * Created by l4qiang on 2017-04-10.
 */
@Service
public class DFSService {
    @Autowired
    private GridFsOperations gridFsOperations;
    @Autowired
    private ObjectMapper objectMapper;
    public ResponseEntity info(String fileId) {
        try {
            Query query = Query.query(Criteria.where("_id").is(new ObjectId(fileId)));
            GridFSDBFile gridFSDBFile = gridFsOperations.findOne(query);
            if (gridFSDBFile == null) {
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body("file was not found");
            }
            ObjectNode objectNode = objectMapper.createObjectNode();
            objectNode.put("id", gridFSDBFile.getId().toString());
            objectNode.put("filename", gridFSDBFile.getFilename());
            objectNode.put("contentType", gridFSDBFile.getContentType());
            objectNode.put("length", gridFSDBFile.getLength());
            objectNode.put("uploadDate", gridFSDBFile.getUploadDate().toString());
//            objectNode.put("aliases", gridFSDBFile.getAliases().toString());
            objectNode.put("md5", gridFSDBFile.getMD5());
            String info = objectMapper.writeValueAsString(objectNode);
            return ResponseEntity.ok()
                    .body(info);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
        }
    }
    public ResponseEntity retrieve(String fileId, HttpServletResponse httpServletResponse) {
        try {
            Query query = Query.query(Criteria.where("_id").is(new ObjectId(fileId)));
            GridFSDBFile gridFSDBFile = gridFsOperations.findOne(query);
            if (gridFSDBFile == null) {
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body("file was not found");
            }
            ServletOutputStream outputStream = httpServletResponse.getOutputStream();
            org.apache.commons.io.IOUtils.copy(gridFSDBFile.getInputStream(), outputStream);
            outputStream.close();
            return ResponseEntity.ok().build();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
        }
    }
    public ResponseEntity download(String fileId) {
        Query query = Query.query(Criteria.where("_id").is(new ObjectId(fileId)));
        GridFSDBFile gridFSDBFile = gridFsOperations.findOne(query);
        if (gridFSDBFile == null) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("file was not found");
        }
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; fileName=\"" + gridFSDBFile.getFilename() + "\"")
                .body(Optional.of(gridFSDBFile.getInputStream()));
    }
    public ResponseEntity store(MultipartFile file) {
        try {
            InputStream inputStream = file.getInputStream();
            String fileName = file.getOriginalFilename();
            GridFSFile gridFSFile = gridFsOperations.store(inputStream, fileName);
            if (gridFSFile != null) {
                return ResponseEntity.status(HttpStatus.OK).body(gridFSFile.getId().toString());
            }
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("file was fail to store");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
        }
    }
    public ResponseEntity update(String fileId, MultipartFile file) {
        ResponseEntity delete = delete(fileId);
        if (delete.getStatusCode() != HttpStatus.OK) {
            return delete;
        }
        return store(file);
    }
    public ResponseEntity delete(String fileId) {
        try {
            Query query = Query.query(Criteria.where("_id").is(new ObjectId(fileId)));
            gridFsOperations.delete(query);
            return ResponseEntity.status(HttpStatus.OK).body("file was deleted");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
        }
    }
}

+ 54 - 0
src/main/resources/application.yml

@ -0,0 +1,54 @@
server:
  port: 9010
---
spring:
  profiles: dev
  data:
    mongodb:
      host: 172.19.103.57
      port: 27017
      username: esb
      password: esb
      authenticationDatabase: admin
      gridFsDatabase: dfs
      database: runtime
hos:
  zbus:
    port: 9020
    store: ./store
---
spring:
  profiles: test
  data:
    mongodb:
      host: 172.19.103.57
      port: 27017
      username: esb
      password: esb
      authenticationDatabase: admin
      gridFsDatabase: dfs
      database: runtime
hos:
  zbus:
    port: 9020
    store: ./store
---
spring:
  profiles: prod
  data:
    mongodb:
      host: 10.176.97.9
      port: 29000
      username: jkzlesb
      password: fgu$^ezl
      authenticationDatabase: admin
      gridFsDatabase: dfs
      database: runtime
hos:
  zbus:
    port: 9020
    store: ./store

+ 40 - 0
src/main/resources/bootstrap.yml

@ -0,0 +1,40 @@
spring:
  application:
    name: hos-dfs
security:
  basic:
    enabled: false
---
spring:
  profiles: dev
  cloud:
    config:
      username: user
      password: configuration
      uri: ${spring.config.uri:http://172.19.103.73:1221}
      label: ${spring.config.label:dev}
---
spring:
  profiles: test
  cloud:
    config:
      username: user
      password: configuration
      uri: ${spring.config.uri:http://172.19.103.73:1221}
      label: ${spring.config.label:dev}
---
spring:
  profiles: prod
  cloud:
    config:
      username: user
      password: configuration
      uri: ${spring.config.uri}
      label: ${spring.config.label}

+ 73 - 0
src/main/resources/zbus.xml

@ -0,0 +1,73 @@
<?xml version="1.0" encoding="UTF-8"?>
<zbus>
    <serverHost>0.0.0.0</serverHost>
    <serverPort>9020</serverPort>
    <serverName/> <!-- Public IP/Domain name -->
    <monitor enabled="true" port="15555"/> <!-- If port not configured, same as server, could be the same port! -->
    <sslEnabled certFile="ssl/zbus.crt" keyFile="ssl/zbus.key">false</sslEnabled>
    <mqPath>./store</mqPath>
    <verbose>false</verbose>
    <trackerOnly>false</trackerOnly>
    <cleanMqInterval>3000</cleanMqInterval> <!-- Milliseconds -->
    <reportToTrackerInterval>30000</reportToTrackerInterval> <!-- Milliseconds -->
    <!--
    <trackerList>
        <serverAddress>
            <address>localhost:15556</address>
            <sslEnabled certFile="ssl/zbus.crt">false</sslEnabled>
            <token></token>
        </serverAddress>
    </trackerList>
    -->
    <auth enabled="false" class="">
        <token value="" operation="js, css, img, ssl, tracker, track_sub, track_pub">
            <topic value="*"/>
        </token>
        <token value="admin" operation="admin"/>
        <token value="MyTopic_Group1" operation="declare,produce,consume,track_sub">
            <topic value="MyTopic">
                <consumeGroup>Group1</consumeGroup>
            </topic>
        </token>
        <token value="produce_all" operation="produce,track_sub"> <!-- rpc = produce -->
            <topic value="*"/>
        </token>
        <token value="myrpc_client" operation="produce, track_sub">
            <topic value="MyRpc"/>
        </token>
        <token value="myrpc_service" operation="declare,track_sub,consume,route"> <!-- route required to reply -->
            <topic value="MyRpc"/>
        </token>
        <token value="myrpc_all" operation="*">
            <topic value="MyRpc"/>
        </token>
        <token value="tracker" operation="server, tracker, track_sub">
            <topic value="MyTopic">
                <consumeGroup>Group1</consumeGroup>
            </topic>
        </token>
        <token value="track_pub_token" operation="track_pub"/>
        <token value="rpc_service" operation="declare,track_sub,consume,route">
            <topic value="*"/>
        </token>
    </auth>
</zbus>

+ 16 - 0
src/test/java/com/yihu/hos/dfs/HosFdsApplicationTests.java

@ -0,0 +1,16 @@
package com.yihu.hos.dfs;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class HosFdsApplicationTests {
	@Test
	public void contextLoads() {
	}
}