Skip to content

Commit

Permalink
perf($oss-center): DDD modularized microservice
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnymillergh committed Feb 3, 2022
1 parent 4a827f4 commit 3f1b72b
Show file tree
Hide file tree
Showing 41 changed files with 462 additions and 267 deletions.
24 changes: 24 additions & 0 deletions oss-center/oss-center-biz/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?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">
<modelVersion>4.0.0</modelVersion>

<!-- Sub-module Basics -->
<artifactId>oss-center-biz</artifactId>
<name>Muscle and Fitness Server :: OSS Center - Biz</name>
<description>Object Storage Service (OSS) Center for M&amp;F.</description>
<parent>
<groupId>com.jmsoftware.maf</groupId>
<artifactId>oss-center</artifactId>
<version>0.0.8-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<dependencies>
<dependency>
<groupId>com.jmsoftware.maf</groupId>
<artifactId>oss-center-domain</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package com.jmsoftware.maf.osscenter;
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.jmsoftware.maf.osscenter.read.service;
package com.jmsoftware.maf.osscenter.service;

import com.jmsoftware.maf.osscenter.read.entity.SerializableStatObjectResponse;
import com.jmsoftware.maf.osscenter.response.SerializableStatObjectResponse;
import org.springframework.http.ResponseEntity;
import org.springframework.lang.Nullable;
import org.springframework.util.unit.DataSize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;

Expand All @@ -18,12 +17,6 @@
**/
@Validated
public interface ReadResourceService {
String BUCKET_OBJECT_NAME_REGEX = "^.+/.+$";
DataSize TINY_CHUNK_SIZE = DataSize.ofBytes(512);
DataSize SMALL_CHUNK_SIZE = DataSize.ofMegabytes(1);
DataSize MEDIUM_CHUNK_SIZE = DataSize.ofMegabytes(4);
DataSize LARGE_CHUNK_SIZE = DataSize.ofMegabytes(8);

/**
* Get single resource, especially pictures, or other small size file.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.jmsoftware.maf.osscenter.write.service;
package com.jmsoftware.maf.osscenter.service;

import com.jmsoftware.maf.common.domain.osscenter.write.ObjectResponse;
import com.jmsoftware.maf.osscenter.write.entity.MergeResourceChunkPayload;
import com.jmsoftware.maf.osscenter.write.entity.UploadResourceChunkPayload;
import com.jmsoftware.maf.osscenter.payload.MergeResourceChunkPayload;
import com.jmsoftware.maf.osscenter.payload.UploadResourceChunkPayload;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.multipart.MultipartFile;

Expand All @@ -18,8 +18,6 @@
**/
@Validated
public interface WriteResourceService {
long MAX_CHUNK_NUMBER = 999;

/**
* Upload single resource string.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.jmsoftware.maf.osscenter.read.service.impl;
package com.jmsoftware.maf.osscenter.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.io.NioUtil;
import com.jmsoftware.maf.osscenter.read.entity.SerializableStatObjectResponse;
import com.jmsoftware.maf.osscenter.read.service.ReadResourceService;
import com.jmsoftware.maf.osscenter.response.SerializableStatObjectResponse;
import com.jmsoftware.maf.osscenter.service.ReadResourceService;
import com.jmsoftware.maf.springcloudstarter.minio.MinioHelper;
import io.minio.StatObjectResponse;
import lombok.RequiredArgsConstructor;
Expand All @@ -18,6 +18,9 @@
import javax.validation.constraints.NotBlank;
import java.util.List;

import static com.jmsoftware.maf.osscenter.constant.Chunk.LARGE_CHUNK_SIZE;
import static com.jmsoftware.maf.osscenter.constant.Chunk.TINY_CHUNK_SIZE;

/**
* <h1>ReadResourceServiceImpl</h1>
* <p>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.jmsoftware.maf.osscenter.write.service.impl;
package com.jmsoftware.maf.osscenter.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.jmsoftware.maf.common.domain.osscenter.write.ObjectResponse;
import com.jmsoftware.maf.osscenter.read.service.ReadResourceService;
import com.jmsoftware.maf.osscenter.write.entity.MergeResourceChunkPayload;
import com.jmsoftware.maf.osscenter.write.entity.UploadResourceChunkPayload;
import com.jmsoftware.maf.osscenter.write.service.WriteResourceService;
import com.jmsoftware.maf.osscenter.payload.MergeResourceChunkPayload;
import com.jmsoftware.maf.osscenter.payload.UploadResourceChunkPayload;
import com.jmsoftware.maf.osscenter.service.WriteResourceService;
import com.jmsoftware.maf.springcloudstarter.minio.MinioHelper;
import io.minio.ComposeSource;
import lombok.Cleanup;
Expand All @@ -29,6 +28,8 @@
import java.util.Objects;
import java.util.stream.Collectors;

import static com.jmsoftware.maf.osscenter.constant.Chunk.TINY_CHUNK_SIZE;

/**
* <h1>WriteResourceServiceImpl</h1>
* <p>
Expand Down Expand Up @@ -86,17 +87,19 @@ public ObjectResponse uploadSingleResource(@NotNull MultipartFile multipartFile)
*/
@Override
@SneakyThrows
public ObjectResponse uploadResourceChunk(@NotNull MultipartFile multipartFile,
@Valid @NotNull UploadResourceChunkPayload payload) {
if (StrUtil.isBlank(multipartFile.getOriginalFilename())) {
public ObjectResponse uploadResourceChunk(
@NotNull MultipartFile multipartFile,
@Valid @NotNull UploadResourceChunkPayload payload
) {
if (CharSequenceUtil.isBlank(multipartFile.getOriginalFilename())) {
throw new IllegalArgumentException("File name required");
}
MediaType mediaType = null;
if (StrUtil.isBlank(payload.getBucket())) {
if (CharSequenceUtil.isBlank(payload.getBucket())) {
mediaType = this.parseMediaType(multipartFile);
}
// bucketName is either mediaType of given 'bucket'
val bucketName = StrUtil.isBlank(payload.getBucket()) ?
val bucketName = CharSequenceUtil.isBlank(payload.getBucket()) ?
Objects.requireNonNull(mediaType).getType() : payload.getBucket();
val orderedFilename = String.format("%s.chunk%s", payload.getFilename(),
NumberUtil.decimalFormat("000", payload.getChunkNumber()));
Expand All @@ -121,7 +124,7 @@ public ObjectResponse mergeResourceChunk(@Valid @NotNull MergeResourceChunkPaylo
@Cleanup val firstChunk = this.minioHelper.getObject(payload.getBucket(),
CollUtil.getFirst(payload.getObjectList()),
0,
ReadResourceService.TINY_CHUNK_SIZE.toBytes());
TINY_CHUNK_SIZE.toBytes());
val mediaType = this.parseMediaType(firstChunk);
val headers = new HashMap<String, String>(4);
headers.put("Content-Type", mediaType.toString());
Expand All @@ -138,8 +141,8 @@ public ObjectResponse mergeResourceChunk(@Valid @NotNull MergeResourceChunkPaylo

private String validateObject(List<String> objectList) {
val objectNameSet = objectList.stream().map(object -> {
val lastIndexOfDot = StrUtil.lastIndexOfIgnoreCase(object, ".");
return StrUtil.subPre(object, lastIndexOfDot);
val lastIndexOfDot = CharSequenceUtil.lastIndexOfIgnoreCase(object, ".");
return CharSequenceUtil.subPre(object, lastIndexOfDot);
}).collect(Collectors.toSet());
if (CollUtil.size(objectNameSet) != 1) {
log.error("Object list is not valid! {}", objectNameSet);
Expand All @@ -152,7 +155,7 @@ private MediaType parseMediaType(MultipartFile multipartFile) throws IOException
val tika = new Tika();
val detectedMediaType = tika.detect(multipartFile.getInputStream());
log.info("Detected media type: {}", detectedMediaType);
if (StrUtil.isBlank(detectedMediaType)) {
if (CharSequenceUtil.isBlank(detectedMediaType)) {
throw new IllegalStateException("Media extension detection failed!");
}
return MediaType.parse(detectedMediaType);
Expand All @@ -162,7 +165,7 @@ private MediaType parseMediaType(InputStream inputStream) throws IOException {
val tika = new Tika();
val detectedMediaType = tika.detect(inputStream);
log.info("Detected media type: {}", detectedMediaType);
if (StrUtil.isBlank(detectedMediaType)) {
if (CharSequenceUtil.isBlank(detectedMediaType)) {
throw new IllegalStateException("Media extension detection failed!");
}
return MediaType.parse(detectedMediaType);
Expand Down
186 changes: 186 additions & 0 deletions oss-center/oss-center-bootstrap/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
<?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">
<modelVersion>4.0.0</modelVersion>

<!-- Sub-module Basics -->
<artifactId>oss-center-bootstrap</artifactId>
<name>Muscle and Fitness Server :: OSS Center - Bootstrap</name>
<description>Object Storage Service (OSS) Center for M&amp;F.</description>
<parent>
<groupId>com.jmsoftware.maf</groupId>
<artifactId>oss-center</artifactId>
<version>0.0.8-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<!-- Build Settings -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>build-info</id>
<phase>compile</phase>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>${maven-pmd-plugin.version}</version>
<configuration>
<sourceEncoding>${project.build.sourceEncoding}</sourceEncoding>
<targetJdk>${java.version}</targetJdk>
<printFailingErrors>true</printFailingErrors>
<rulesets>
<ruleset>rulesets/java/ali-comment.xml</ruleset>
<ruleset>rulesets/java/ali-concurrent.xml</ruleset>
<ruleset>rulesets/java/ali-constant.xml</ruleset>
<ruleset>rulesets/java/ali-exception.xml</ruleset>
<ruleset>rulesets/java/ali-flowcontrol.xml</ruleset>
<ruleset>rulesets/java/ali-naming.xml</ruleset>
<ruleset>rulesets/java/ali-oop.xml</ruleset>
<ruleset>rulesets/java/ali-orm.xml</ruleset>
<ruleset>rulesets/java/ali-other.xml</ruleset>
<ruleset>rulesets/java/ali-set.xml</ruleset>
</rulesets>
</configuration>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.alibaba.p3c</groupId>
<artifactId>p3c-pmd</artifactId>
<version>${p3c-pmd.version}</version>
</dependency>
</dependencies>
</plugin>

<!-- https://github.com/GoogleContainerTools/jib/blob/master/jib-maven-plugin/README.md -->
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>${jib-maven-plugin.version}</version>
<dependencies>
<dependency>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-spring-boot-extension-maven</artifactId>
<version>${jib-spring-boot-extension-maven.version}</version>
</dependency>
</dependencies>
<executions>
<!-- Bind `jib:dockerBuild` to a Maven lifecycle `verify`. Jib will build your image directly to a Docker daemon.
<execution>
<id>buildDockerImagePhase</id>
<phase>verify</phase>
<goals>
<goal>dockerBuild</goal>
</goals>
</execution> -->
<!-- Bind `jib:build` to a Maven lifecycle `install`. Jib will build and push image to image registry. -->
<execution>
<id>buildAndPushDockerImagePhase</id>
<phase>install</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
<configuration>
<from>
<!-- Don't use alpine or slim version, https://hub.docker.com/r/adoptopenjdk/openjdk11/tags?page=1&ordering=last_updated&name=jre-11 -->
<image>adoptopenjdk/openjdk11:${adoptopenjdk11.tag}</image>
</from>
<to>
<image>docker.io/ijohnnymiller/${project.parent.artifactId}.${project.artifactId}</image>
<tags>
<tag>${git.commit.id.abbrev}-${project.version}</tag>
</tags>
</to>
<container>
<!-- The root directory on the container where the app's contents are placed. -->
<appRoot>/${project.artifactId}</appRoot>
<jvmFlags>
<jvmFlag>-Dfile.encoding=${project.build.sourceEncoding}</jvmFlag>
</jvmFlags>
<ports>
<port>${oss-center.port}</port>
</ports>
<creationTime>USE_CURRENT_TIMESTAMP</creationTime>
</container>
<pluginExtensions>
<pluginExtension>
<implementation>
com.google.cloud.tools.jib.maven.extension.springboot.JibSpringBootExtension
</implementation>
<properties>
<excludeDevtools>true</excludeDevtools>
</properties>
</pluginExtension>
</pluginExtensions>
</configuration>
</plugin>

<!-- https://github.com/git-commit-id/git-commit-id-maven-plugin -->
<plugin>
<groupId>io.github.git-commit-id</groupId>
<artifactId>git-commit-id-maven-plugin</artifactId>
<version>5.0.0</version>
<executions>
<execution>
<id>get-the-git-info</id>
<goals>
<goal>revision</goal>
</goals>
<phase>initialize</phase>
</execution>
</executions>
<configuration>
<generateGitPropertiesFile>true</generateGitPropertiesFile>
<generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties
</generateGitPropertiesFilename>
<includeOnlyProperties>
<includeOnlyProperty>^git.build.(time|version)$</includeOnlyProperty>
<includeOnlyProperty>^git.commit.id.(abbrev|full)$</includeOnlyProperty>
</includeOnlyProperties>
<commitIdGenerationMode>full</commitIdGenerationMode>
</configuration>
</plugin>
</plugins>
</build>

<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jxr-plugin</artifactId>
<version>2.3</version>
</plugin>
</plugins>
</reporting>

<dependencies>
<dependency>
<groupId>com.jmsoftware.maf</groupId>
<artifactId>oss-center-web</artifactId>
</dependency>
<dependency>
<groupId>com.jmsoftware.maf</groupId>
<artifactId>oss-center-message</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
@SuppressWarnings("scwjava_Createprivateconstructorforutilityclassallfieldsmethodsarestatic")
public class OssCenterApplication {
public static void main(String[] args) {
val stopWatch = new StopWatch();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package com.jmsoftware.maf.osscenter;
Loading

0 comments on commit 3f1b72b

Please sign in to comment.