From 8814ca1be151410cf4d1767d329f729356bc02f8 Mon Sep 17 00:00:00 2001 From: little_jin <861165942@qq.com> Date: Wed, 10 Sep 2025 22:34:30 +0800 Subject: [PATCH] =?UTF-8?q?feat(qa-service):=20=E6=B7=BB=E5=8A=A0=E4=BA=86?= =?UTF-8?q?qa=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 分工 1. 金贤俊:CreateQa 2. 范鑫国:DeleteQa 3. 周瀚:GetQaById 4. 郑鑫杰:GetQaList 5. 苏志林:UpdateQa 6. 王俊:除上述之外的其他。 --- .idea/compiler.xml | 31 +++ .idea/encodings.xml | 51 ++++- .idea/gradle.xml | 9 + .idea/misc.xml | 29 +++ qa-service/pom.xml | 53 +++-- qa-service/qa-service-adapter/pom.xml | 39 ++++ .../qa-service-adapter/qa-adapter-in/pom.xml | 38 ++++ .../qa-adapter-in/qa-adapter-in-web/pom.xml | 61 ++++++ .../in/web/controller/QaController.java | 83 ++++++++ .../in/web/dto/CreateQaRequestDTO.java | 7 + .../qa/adapter/in/web/dto/QaResponseDTO.java | 14 ++ .../in/web/dto/UpdateQaRequestDTO.java | 6 + .../qa-service-adapter/qa-adapter-out/pom.xml | 38 ++++ .../qa-adapter-out-persistence/pom.xml | 57 +++++ .../persistence/bridge/CreateQaBridge.java | 26 +++ .../persistence/bridge/DeleteQaBridge.java | 19 ++ .../persistence/bridge/GetQaByIdBridge.java | 22 ++ .../persistence/bridge/GetQaListBridge.java | 32 +++ .../persistence/bridge/UpdateQaBridge.java | 23 ++ .../persistence/convertor/QaConvertor.java | 26 +++ .../out/persistence/entity/QaEntity.java | 19 ++ .../out/persistence/mapper/QaMapper.java | 7 + qa-service/qa-service-application/pom.xml | 57 +++++ .../application/command/CreateQaCommand.java | 11 + .../application/command/UpdateQaCommand.java | 9 + .../application/port/in/CreateQaUseCase.java | 8 + .../application/port/in/DeleteQaUseCase.java | 5 + .../application/port/in/GetQaByIdUseCase.java | 7 + .../application/port/in/GetQaListUseCase.java | 9 + .../application/port/in/UpdateQaUseCase.java | 8 + .../application/service/CreateQaService.java | 28 +++ .../application/service/DeleteQaService.java | 16 ++ .../application/service/GetQaByIdService.java | 18 ++ .../application/service/GetQaListService.java | 21 ++ .../application/service/UpdateQaService.java | 26 +++ qa-service/qa-service-bootstrap/pom.xml | 86 ++++++++ .../QaServiceBootstrapApplication.java | 15 ++ .../src/main/resources/application.properties | 27 +++ qa-service/qa-service-common/.gitignore | 33 +++ qa-service/qa-service-common/pom.xml | 55 +++++ .../example/qa/service/common/IdWorker.java | 199 ++++++++++++++++++ qa-service/qa-service-domain/.gitignore | 33 +++ qa-service/qa-service-domain/pom.xml | 57 +++++ .../com/example/qa/service/domain/Qa.java | 44 ++++ .../qa/service/domain/port/CreateQaPort.java | 7 + .../qa/service/domain/port/DeleteQaPort.java | 5 + .../qa/service/domain/port/GetQaByIdPort.java | 7 + .../qa/service/domain/port/GetQaListPort.java | 9 + .../qa/service/domain/port/UpdateQaPort.java | 7 + .../qa/service/domain/valueobject/Answer.java | 7 + .../qa/service/domain/valueobject/QaId.java | 9 + .../service/domain/valueobject/Question.java | 5 + 52 files changed, 1503 insertions(+), 15 deletions(-) create mode 100644 qa-service/qa-service-adapter/pom.xml create mode 100644 qa-service/qa-service-adapter/qa-adapter-in/pom.xml create mode 100644 qa-service/qa-service-adapter/qa-adapter-in/qa-adapter-in-web/pom.xml create mode 100644 qa-service/qa-service-adapter/qa-adapter-in/qa-adapter-in-web/src/main/java/com/example/qa/adapter/in/web/controller/QaController.java create mode 100644 qa-service/qa-service-adapter/qa-adapter-in/qa-adapter-in-web/src/main/java/com/example/qa/adapter/in/web/dto/CreateQaRequestDTO.java create mode 100644 qa-service/qa-service-adapter/qa-adapter-in/qa-adapter-in-web/src/main/java/com/example/qa/adapter/in/web/dto/QaResponseDTO.java create mode 100644 qa-service/qa-service-adapter/qa-adapter-in/qa-adapter-in-web/src/main/java/com/example/qa/adapter/in/web/dto/UpdateQaRequestDTO.java create mode 100644 qa-service/qa-service-adapter/qa-adapter-out/pom.xml create mode 100644 qa-service/qa-service-adapter/qa-adapter-out/qa-adapter-out-persistence/pom.xml create mode 100644 qa-service/qa-service-adapter/qa-adapter-out/qa-adapter-out-persistence/src/main/java/com/example/qa/adapter/out/persistence/bridge/CreateQaBridge.java create mode 100644 qa-service/qa-service-adapter/qa-adapter-out/qa-adapter-out-persistence/src/main/java/com/example/qa/adapter/out/persistence/bridge/DeleteQaBridge.java create mode 100644 qa-service/qa-service-adapter/qa-adapter-out/qa-adapter-out-persistence/src/main/java/com/example/qa/adapter/out/persistence/bridge/GetQaByIdBridge.java create mode 100644 qa-service/qa-service-adapter/qa-adapter-out/qa-adapter-out-persistence/src/main/java/com/example/qa/adapter/out/persistence/bridge/GetQaListBridge.java create mode 100644 qa-service/qa-service-adapter/qa-adapter-out/qa-adapter-out-persistence/src/main/java/com/example/qa/adapter/out/persistence/bridge/UpdateQaBridge.java create mode 100644 qa-service/qa-service-adapter/qa-adapter-out/qa-adapter-out-persistence/src/main/java/com/example/qa/adapter/out/persistence/convertor/QaConvertor.java create mode 100644 qa-service/qa-service-adapter/qa-adapter-out/qa-adapter-out-persistence/src/main/java/com/example/qa/adapter/out/persistence/entity/QaEntity.java create mode 100644 qa-service/qa-service-adapter/qa-adapter-out/qa-adapter-out-persistence/src/main/java/com/example/qa/adapter/out/persistence/mapper/QaMapper.java create mode 100644 qa-service/qa-service-application/pom.xml create mode 100644 qa-service/qa-service-application/src/main/java/com/example/qa/service/application/command/CreateQaCommand.java create mode 100644 qa-service/qa-service-application/src/main/java/com/example/qa/service/application/command/UpdateQaCommand.java create mode 100644 qa-service/qa-service-application/src/main/java/com/example/qa/service/application/port/in/CreateQaUseCase.java create mode 100644 qa-service/qa-service-application/src/main/java/com/example/qa/service/application/port/in/DeleteQaUseCase.java create mode 100644 qa-service/qa-service-application/src/main/java/com/example/qa/service/application/port/in/GetQaByIdUseCase.java create mode 100644 qa-service/qa-service-application/src/main/java/com/example/qa/service/application/port/in/GetQaListUseCase.java create mode 100644 qa-service/qa-service-application/src/main/java/com/example/qa/service/application/port/in/UpdateQaUseCase.java create mode 100644 qa-service/qa-service-application/src/main/java/com/example/qa/service/application/service/CreateQaService.java create mode 100644 qa-service/qa-service-application/src/main/java/com/example/qa/service/application/service/DeleteQaService.java create mode 100644 qa-service/qa-service-application/src/main/java/com/example/qa/service/application/service/GetQaByIdService.java create mode 100644 qa-service/qa-service-application/src/main/java/com/example/qa/service/application/service/GetQaListService.java create mode 100644 qa-service/qa-service-application/src/main/java/com/example/qa/service/application/service/UpdateQaService.java create mode 100644 qa-service/qa-service-bootstrap/pom.xml create mode 100644 qa-service/qa-service-bootstrap/src/main/java/com/example/qa/service/bootstrap/QaServiceBootstrapApplication.java create mode 100644 qa-service/qa-service-bootstrap/src/main/resources/application.properties create mode 100644 qa-service/qa-service-common/.gitignore create mode 100644 qa-service/qa-service-common/pom.xml create mode 100644 qa-service/qa-service-common/src/main/java/com/example/qa/service/common/IdWorker.java create mode 100644 qa-service/qa-service-domain/.gitignore create mode 100644 qa-service/qa-service-domain/pom.xml create mode 100644 qa-service/qa-service-domain/src/main/java/com/example/qa/service/domain/Qa.java create mode 100644 qa-service/qa-service-domain/src/main/java/com/example/qa/service/domain/port/CreateQaPort.java create mode 100644 qa-service/qa-service-domain/src/main/java/com/example/qa/service/domain/port/DeleteQaPort.java create mode 100644 qa-service/qa-service-domain/src/main/java/com/example/qa/service/domain/port/GetQaByIdPort.java create mode 100644 qa-service/qa-service-domain/src/main/java/com/example/qa/service/domain/port/GetQaListPort.java create mode 100644 qa-service/qa-service-domain/src/main/java/com/example/qa/service/domain/port/UpdateQaPort.java create mode 100644 qa-service/qa-service-domain/src/main/java/com/example/qa/service/domain/valueobject/Answer.java create mode 100644 qa-service/qa-service-domain/src/main/java/com/example/qa/service/domain/valueobject/QaId.java create mode 100644 qa-service/qa-service-domain/src/main/java/com/example/qa/service/domain/valueobject/Question.java diff --git a/.idea/compiler.xml b/.idea/compiler.xml index a1757ae..ceae5b4 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -3,6 +3,37 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml index da0415a..f1c7c18 100644 --- a/.idea/encodings.xml +++ b/.idea/encodings.xml @@ -1,4 +1,53 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml index c4be5fb..d93e5c0 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -1,6 +1,15 @@ + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 7280f00..b072915 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -3,4 +3,33 @@ + + + + + + \ No newline at end of file diff --git a/qa-service/pom.xml b/qa-service/pom.xml index 0b70349..7ba8bd5 100644 --- a/qa-service/pom.xml +++ b/qa-service/pom.xml @@ -8,25 +8,37 @@ qa-service qa-service - 17 + 21 UTF-8 UTF-8 - 3.0.2 + 3.2.4 + 2023.0.1.0 + 2023.0.1 + + pom + + + qa-service-bootstrap + qa-service-adapter + qa-service-application + qa-service-domain + qa-service-common + + + - - org.springframework.boot - spring-boot-starter - - - org.springframework.boot - spring-boot-starter-test - test - + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + org.springframework.boot spring-boot-dependencies @@ -34,6 +46,19 @@ pom import + + com.alibaba.cloud + spring-cloud-alibaba-dependencies + ${spring-cloud-alibaba.version} + pom + import + + + + com.baomidou + mybatis-plus-spring-boot3-starter + 3.5.14 + @@ -44,8 +69,8 @@ maven-compiler-plugin 3.8.1 - 17 - 17 + 21 + 21 UTF-8 @@ -54,7 +79,7 @@ spring-boot-maven-plugin ${spring-boot.version} - com.example.qa.service.QaServiceApplication + com.example.qa-service.qa-serviceApplication true diff --git a/qa-service/qa-service-adapter/pom.xml b/qa-service/qa-service-adapter/pom.xml new file mode 100644 index 0000000..0074d0c --- /dev/null +++ b/qa-service/qa-service-adapter/pom.xml @@ -0,0 +1,39 @@ + + + 4.0.0 + com.example + qa-service-adapter + 0.0.1-SNAPSHOT + qa-service-adapter + qa-service-adapter + + pom + + + com.example + qa-service + 0.0.1-SNAPSHOT + + + + qa-adapter-in + qa-adapter-out + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 21 + 21 + UTF-8 + + + + + + diff --git a/qa-service/qa-service-adapter/qa-adapter-in/pom.xml b/qa-service/qa-service-adapter/qa-adapter-in/pom.xml new file mode 100644 index 0000000..473ef75 --- /dev/null +++ b/qa-service/qa-service-adapter/qa-adapter-in/pom.xml @@ -0,0 +1,38 @@ + + + 4.0.0 + com.example + qa-adapter-in + 0.0.1-SNAPSHOT + qa-adapter-in + qa-adapter-in + + pom + + + com.example + qa-service-adapter + 0.0.1-SNAPSHOT + + + + qa-adapter-in-web + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 21 + 21 + UTF-8 + + + + + + diff --git a/qa-service/qa-service-adapter/qa-adapter-in/qa-adapter-in-web/pom.xml b/qa-service/qa-service-adapter/qa-adapter-in/qa-adapter-in-web/pom.xml new file mode 100644 index 0000000..ba230d1 --- /dev/null +++ b/qa-service/qa-service-adapter/qa-adapter-in/qa-adapter-in-web/pom.xml @@ -0,0 +1,61 @@ + + + 4.0.0 + com.example + qa-adapter-in-web + 0.0.1-SNAPSHOT + qa-adapter-in-web + qa-adapter-in-web + + + com.example + qa-adapter-in + 0.0.1-SNAPSHOT + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.projectlombok + lombok + provided + + + org.projectlombok + lombok + + + + com.example + qa-service-application + 0.0.1-SNAPSHOT + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 21 + 21 + UTF-8 + + + + + + diff --git a/qa-service/qa-service-adapter/qa-adapter-in/qa-adapter-in-web/src/main/java/com/example/qa/adapter/in/web/controller/QaController.java b/qa-service/qa-service-adapter/qa-adapter-in/qa-adapter-in-web/src/main/java/com/example/qa/adapter/in/web/controller/QaController.java new file mode 100644 index 0000000..7dc7026 --- /dev/null +++ b/qa-service/qa-service-adapter/qa-adapter-in/qa-adapter-in-web/src/main/java/com/example/qa/adapter/in/web/controller/QaController.java @@ -0,0 +1,83 @@ +package com.example.qa.adapter.in.web.controller; + +import com.example.qa.adapter.in.web.dto.CreateQaRequestDTO; +import com.example.qa.adapter.in.web.dto.UpdateQaRequestDTO; +import com.example.qa.adapter.in.web.dto.QaResponseDTO; +import com.example.qa.service.application.command.CreateQaCommand; +import com.example.qa.service.application.command.UpdateQaCommand; +import com.example.qa.service.application.port.in.*; +import com.example.qa.service.domain.Qa; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@Slf4j +@RequestMapping("/qas") +@RestController +@RequiredArgsConstructor +public class QaController { + + private final GetQaListUseCase getQaListUseCase; + private final CreateQaUseCase createQaUseCase; + private final DeleteQaUseCase deleteQaUseCase; + private final UpdateQaUseCase updateQaUseCase; + private final GetQaByIdUseCase getQaByIdUseCase; + + @GetMapping("") + public List getQas() { + log.info("getQas"); + return getQaListUseCase.getQas(); + } + + @PostMapping() + public Qa createQa(@RequestBody CreateQaRequestDTO createQaRequestDTO){ + + CreateQaCommand command=CreateQaCommand.builder() + .question(createQaRequestDTO.question()) + .answer(createQaRequestDTO.answer()) + .build(); + + return createQaUseCase.createQa(command); + } + + + @DeleteMapping("{id}") + public String deleteQa(@PathVariable("id") Long id){ + deleteQaUseCase.deleteQa(id); + return "success"; + } + + /** + * @author liuxin + * @param updateQaRequestDTO + * @return + */ + @PutMapping("") + public Qa updateQa(@RequestBody UpdateQaRequestDTO updateQaRequestDTO){ + UpdateQaCommand command=UpdateQaCommand.builder() + .id(updateQaRequestDTO.id()) + .question(updateQaRequestDTO.question()) + .answer(updateQaRequestDTO.answer()) + .build(); + Qa qa = updateQaUseCase.updateQa(command); + return qa; + } + + + /** + * @author dengli + * @param id + * @return + */ + @GetMapping("{id}") + public QaResponseDTO getQaById(@PathVariable("id") Long id){ + Qa qa = getQaByIdUseCase.getQaById(id); + QaResponseDTO qaResponseDTO = new QaResponseDTO( + qa.getId().id(), + qa.getQuestion().question(), + qa.getAnswer().answer()); + return qaResponseDTO; + } +} diff --git a/qa-service/qa-service-adapter/qa-adapter-in/qa-adapter-in-web/src/main/java/com/example/qa/adapter/in/web/dto/CreateQaRequestDTO.java b/qa-service/qa-service-adapter/qa-adapter-in/qa-adapter-in-web/src/main/java/com/example/qa/adapter/in/web/dto/CreateQaRequestDTO.java new file mode 100644 index 0000000..1662ec2 --- /dev/null +++ b/qa-service/qa-service-adapter/qa-adapter-in/qa-adapter-in-web/src/main/java/com/example/qa/adapter/in/web/dto/CreateQaRequestDTO.java @@ -0,0 +1,7 @@ +package com.example.qa.adapter.in.web.dto; + +public record CreateQaRequestDTO( + String question, + String answer) { + +} diff --git a/qa-service/qa-service-adapter/qa-adapter-in/qa-adapter-in-web/src/main/java/com/example/qa/adapter/in/web/dto/QaResponseDTO.java b/qa-service/qa-service-adapter/qa-adapter-in/qa-adapter-in-web/src/main/java/com/example/qa/adapter/in/web/dto/QaResponseDTO.java new file mode 100644 index 0000000..1a36793 --- /dev/null +++ b/qa-service/qa-service-adapter/qa-adapter-in/qa-adapter-in-web/src/main/java/com/example/qa/adapter/in/web/dto/QaResponseDTO.java @@ -0,0 +1,14 @@ +package com.example.qa.adapter.in.web.dto; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class QaResponseDTO { + private long id; + private String question; + private String answer; +} diff --git a/qa-service/qa-service-adapter/qa-adapter-in/qa-adapter-in-web/src/main/java/com/example/qa/adapter/in/web/dto/UpdateQaRequestDTO.java b/qa-service/qa-service-adapter/qa-adapter-in/qa-adapter-in-web/src/main/java/com/example/qa/adapter/in/web/dto/UpdateQaRequestDTO.java new file mode 100644 index 0000000..3ec0316 --- /dev/null +++ b/qa-service/qa-service-adapter/qa-adapter-in/qa-adapter-in-web/src/main/java/com/example/qa/adapter/in/web/dto/UpdateQaRequestDTO.java @@ -0,0 +1,6 @@ +package com.example.qa.adapter.in.web.dto; + +public record UpdateQaRequestDTO(Long id, + String question, + String answer) { +} diff --git a/qa-service/qa-service-adapter/qa-adapter-out/pom.xml b/qa-service/qa-service-adapter/qa-adapter-out/pom.xml new file mode 100644 index 0000000..dfc017b --- /dev/null +++ b/qa-service/qa-service-adapter/qa-adapter-out/pom.xml @@ -0,0 +1,38 @@ + + + 4.0.0 + com.example + qa-adapter-out + 0.0.1-SNAPSHOT + qa-adapter-out + qa-adapter-out + + pom + + + com.example + qa-service-adapter + 0.0.1-SNAPSHOT + + + + qa-adapter-out-persistence + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 21 + 21 + UTF-8 + + + + + + diff --git a/qa-service/qa-service-adapter/qa-adapter-out/qa-adapter-out-persistence/pom.xml b/qa-service/qa-service-adapter/qa-adapter-out/qa-adapter-out-persistence/pom.xml new file mode 100644 index 0000000..9d78608 --- /dev/null +++ b/qa-service/qa-service-adapter/qa-adapter-out/qa-adapter-out-persistence/pom.xml @@ -0,0 +1,57 @@ + + + 4.0.0 + com.example + qa-adapter-out-persistence + 0.0.1-SNAPSHOT + qa-adapter-out-persistence + qa-adapter-out-persistence + + com.example + qa-adapter-out + 0.0.1-SNAPSHOT + + + + + org.projectlombok + lombok + provided + + + + com.example + qa-service-domain + 0.0.1-SNAPSHOT + + + + com.baomidou + mybatis-plus-spring-boot3-starter + + + + com.mysql + mysql-connector-j + runtime + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 21 + 21 + UTF-8 + + + + + + diff --git a/qa-service/qa-service-adapter/qa-adapter-out/qa-adapter-out-persistence/src/main/java/com/example/qa/adapter/out/persistence/bridge/CreateQaBridge.java b/qa-service/qa-service-adapter/qa-adapter-out/qa-adapter-out-persistence/src/main/java/com/example/qa/adapter/out/persistence/bridge/CreateQaBridge.java new file mode 100644 index 0000000..3c40338 --- /dev/null +++ b/qa-service/qa-service-adapter/qa-adapter-out/qa-adapter-out-persistence/src/main/java/com/example/qa/adapter/out/persistence/bridge/CreateQaBridge.java @@ -0,0 +1,26 @@ +package com.example.qa.adapter.out.persistence.bridge; + +import com.example.qa.adapter.out.persistence.convertor.QaConvertor; +import com.example.qa.adapter.out.persistence.entity.QaEntity; +import com.example.qa.adapter.out.persistence.mapper.QaMapper; +import com.example.qa.service.domain.Qa; +import com.example.qa.service.domain.port.CreateQaPort; +import jakarta.annotation.Resource; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +@Slf4j +@Component +public class CreateQaBridge implements CreateQaPort { + @Resource + private QaMapper qaMapper; + + @Override + public Qa createQa(Qa qa) { + QaEntity qaEntity = QaConvertor.toEntity(qa); + int result = qaMapper.insert(qaEntity); + //result 指受影响行数 + log.info("result:{}",result); + return qa; + } +} diff --git a/qa-service/qa-service-adapter/qa-adapter-out/qa-adapter-out-persistence/src/main/java/com/example/qa/adapter/out/persistence/bridge/DeleteQaBridge.java b/qa-service/qa-service-adapter/qa-adapter-out/qa-adapter-out-persistence/src/main/java/com/example/qa/adapter/out/persistence/bridge/DeleteQaBridge.java new file mode 100644 index 0000000..01dcc70 --- /dev/null +++ b/qa-service/qa-service-adapter/qa-adapter-out/qa-adapter-out-persistence/src/main/java/com/example/qa/adapter/out/persistence/bridge/DeleteQaBridge.java @@ -0,0 +1,19 @@ +package com.example.qa.adapter.out.persistence.bridge; + +import com.example.qa.adapter.out.persistence.mapper.QaMapper; +import com.example.qa.service.domain.port.DeleteQaPort; +import jakarta.annotation.Resource; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +@Slf4j +@Component +public class DeleteQaBridge implements DeleteQaPort { + @Resource + private QaMapper qaMapper; + @Override + public void deleteQa(Long id) { + int result = qaMapper.deleteById(id); + log.info("result:{}",result); + } +} diff --git a/qa-service/qa-service-adapter/qa-adapter-out/qa-adapter-out-persistence/src/main/java/com/example/qa/adapter/out/persistence/bridge/GetQaByIdBridge.java b/qa-service/qa-service-adapter/qa-adapter-out/qa-adapter-out-persistence/src/main/java/com/example/qa/adapter/out/persistence/bridge/GetQaByIdBridge.java new file mode 100644 index 0000000..c4f0d21 --- /dev/null +++ b/qa-service/qa-service-adapter/qa-adapter-out/qa-adapter-out-persistence/src/main/java/com/example/qa/adapter/out/persistence/bridge/GetQaByIdBridge.java @@ -0,0 +1,22 @@ +package com.example.qa.adapter.out.persistence.bridge; + +import com.example.qa.adapter.out.persistence.convertor.QaConvertor; +import com.example.qa.adapter.out.persistence.entity.QaEntity; +import com.example.qa.adapter.out.persistence.mapper.QaMapper; +import com.example.qa.service.domain.Qa; +import com.example.qa.service.domain.port.GetQaByIdPort; +import jakarta.annotation.Resource; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +@Slf4j +@Component +public class GetQaByIdBridge implements GetQaByIdPort { + @Resource + private QaMapper qaMapper; + @Override + public Qa getQaById(Long id) { + QaEntity qaEntity = qaMapper.selectById(id); + return QaConvertor.toDomain(qaEntity); + } +} diff --git a/qa-service/qa-service-adapter/qa-adapter-out/qa-adapter-out-persistence/src/main/java/com/example/qa/adapter/out/persistence/bridge/GetQaListBridge.java b/qa-service/qa-service-adapter/qa-adapter-out/qa-adapter-out-persistence/src/main/java/com/example/qa/adapter/out/persistence/bridge/GetQaListBridge.java new file mode 100644 index 0000000..970bf7a --- /dev/null +++ b/qa-service/qa-service-adapter/qa-adapter-out/qa-adapter-out-persistence/src/main/java/com/example/qa/adapter/out/persistence/bridge/GetQaListBridge.java @@ -0,0 +1,32 @@ +package com.example.qa.adapter.out.persistence.bridge; + +import com.example.qa.adapter.out.persistence.convertor.QaConvertor; +import com.example.qa.adapter.out.persistence.entity.QaEntity; +import com.example.qa.adapter.out.persistence.mapper.QaMapper; +import com.example.qa.service.domain.Qa; +import com.example.qa.service.domain.port.GetQaListPort; +import jakarta.annotation.Resource; +import org.springframework.stereotype.Component; + +import java.util.ArrayList; +import java.util.List; + +@Component +public class GetQaListBridge implements GetQaListPort { + + @Resource + private QaMapper qaMapper; + + @Override + public List getQas() { + List entities = qaMapper.selectList(null); + + ArrayList list = new ArrayList<>(); + + entities.forEach(qaEntity -> { + Qa qa = QaConvertor.toDomain(qaEntity); + list.add(qa); + }); + return list; + } +} diff --git a/qa-service/qa-service-adapter/qa-adapter-out/qa-adapter-out-persistence/src/main/java/com/example/qa/adapter/out/persistence/bridge/UpdateQaBridge.java b/qa-service/qa-service-adapter/qa-adapter-out/qa-adapter-out-persistence/src/main/java/com/example/qa/adapter/out/persistence/bridge/UpdateQaBridge.java new file mode 100644 index 0000000..db2b5c4 --- /dev/null +++ b/qa-service/qa-service-adapter/qa-adapter-out/qa-adapter-out-persistence/src/main/java/com/example/qa/adapter/out/persistence/bridge/UpdateQaBridge.java @@ -0,0 +1,23 @@ +package com.example.qa.adapter.out.persistence.bridge; + +import com.example.qa.adapter.out.persistence.convertor.QaConvertor; +import com.example.qa.adapter.out.persistence.mapper.QaMapper; +import com.example.qa.service.domain.Qa; +import com.example.qa.service.domain.port.UpdateQaPort; +import jakarta.annotation.Resource; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +@Slf4j +@Component +public class UpdateQaBridge implements UpdateQaPort { + @Resource + private QaMapper qaMapper; + + @Override + public Qa updateQa(Qa qa) { + int result = qaMapper.updateById(QaConvertor.toEntity(qa)); + log.info("result:{}",result); + return qa; + } +} diff --git a/qa-service/qa-service-adapter/qa-adapter-out/qa-adapter-out-persistence/src/main/java/com/example/qa/adapter/out/persistence/convertor/QaConvertor.java b/qa-service/qa-service-adapter/qa-adapter-out/qa-adapter-out-persistence/src/main/java/com/example/qa/adapter/out/persistence/convertor/QaConvertor.java new file mode 100644 index 0000000..664bcef --- /dev/null +++ b/qa-service/qa-service-adapter/qa-adapter-out/qa-adapter-out-persistence/src/main/java/com/example/qa/adapter/out/persistence/convertor/QaConvertor.java @@ -0,0 +1,26 @@ +package com.example.qa.adapter.out.persistence.convertor; + +import com.example.qa.adapter.out.persistence.entity.QaEntity; +import com.example.qa.service.domain.Qa; +import com.example.qa.service.domain.valueobject.Answer; +import com.example.qa.service.domain.valueobject.QaId; +import com.example.qa.service.domain.valueobject.Question; + +public class QaConvertor { + + public static Qa toDomain(QaEntity qaEntity) { + return new Qa( + new QaId(qaEntity.getId()), + new Question(qaEntity.getQuestion()), + new Answer(qaEntity.getAnswer()) + ); + } + + public static QaEntity toEntity(Qa qa) { + return new QaEntity( + qa.getId().getValue(), + qa.getQuestion().getValue(), + qa.getAnswer().getValue() + ); + } +} diff --git a/qa-service/qa-service-adapter/qa-adapter-out/qa-adapter-out-persistence/src/main/java/com/example/qa/adapter/out/persistence/entity/QaEntity.java b/qa-service/qa-service-adapter/qa-adapter-out/qa-adapter-out-persistence/src/main/java/com/example/qa/adapter/out/persistence/entity/QaEntity.java new file mode 100644 index 0000000..f48ed02 --- /dev/null +++ b/qa-service/qa-service-adapter/qa-adapter-out/qa-adapter-out-persistence/src/main/java/com/example/qa/adapter/out/persistence/entity/QaEntity.java @@ -0,0 +1,19 @@ +package com.example.qa.adapter.out.persistence.entity; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@TableName("qa") +public class QaEntity { + @TableId(type= IdType.ASSIGN_ID) + private long id; + private String question; + private String answer; +} diff --git a/qa-service/qa-service-adapter/qa-adapter-out/qa-adapter-out-persistence/src/main/java/com/example/qa/adapter/out/persistence/mapper/QaMapper.java b/qa-service/qa-service-adapter/qa-adapter-out/qa-adapter-out-persistence/src/main/java/com/example/qa/adapter/out/persistence/mapper/QaMapper.java new file mode 100644 index 0000000..4c2f4ab --- /dev/null +++ b/qa-service/qa-service-adapter/qa-adapter-out/qa-adapter-out-persistence/src/main/java/com/example/qa/adapter/out/persistence/mapper/QaMapper.java @@ -0,0 +1,7 @@ +package com.example.qa.adapter.out.persistence.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.example.qa.adapter.out.persistence.entity.QaEntity; + +public interface QaMapper extends BaseMapper { +} diff --git a/qa-service/qa-service-application/pom.xml b/qa-service/qa-service-application/pom.xml new file mode 100644 index 0000000..8d9eb8b --- /dev/null +++ b/qa-service/qa-service-application/pom.xml @@ -0,0 +1,57 @@ + + + 4.0.0 + com.example + qa-service-application + 0.0.1-SNAPSHOT + qa-service-application + qa-service-application + + + com.example + qa-service + 0.0.1-SNAPSHOT + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.projectlombok + lombok + provided + + + + com.example + qa-service-domain + 0.0.1-SNAPSHOT + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 21 + 21 + UTF-8 + + + + + + diff --git a/qa-service/qa-service-application/src/main/java/com/example/qa/service/application/command/CreateQaCommand.java b/qa-service/qa-service-application/src/main/java/com/example/qa/service/application/command/CreateQaCommand.java new file mode 100644 index 0000000..81c6cac --- /dev/null +++ b/qa-service/qa-service-application/src/main/java/com/example/qa/service/application/command/CreateQaCommand.java @@ -0,0 +1,11 @@ +package com.example.qa.service.application.command; + +import lombok.Builder; + +@Builder +public record CreateQaCommand( + Long id, + String question, + String answer +) { +} diff --git a/qa-service/qa-service-application/src/main/java/com/example/qa/service/application/command/UpdateQaCommand.java b/qa-service/qa-service-application/src/main/java/com/example/qa/service/application/command/UpdateQaCommand.java new file mode 100644 index 0000000..2d9dcbc --- /dev/null +++ b/qa-service/qa-service-application/src/main/java/com/example/qa/service/application/command/UpdateQaCommand.java @@ -0,0 +1,9 @@ +package com.example.qa.service.application.command; + +import lombok.Builder; + +@Builder +public record UpdateQaCommand(Long id, + String question, + String answer) { +} diff --git a/qa-service/qa-service-application/src/main/java/com/example/qa/service/application/port/in/CreateQaUseCase.java b/qa-service/qa-service-application/src/main/java/com/example/qa/service/application/port/in/CreateQaUseCase.java new file mode 100644 index 0000000..a11160e --- /dev/null +++ b/qa-service/qa-service-application/src/main/java/com/example/qa/service/application/port/in/CreateQaUseCase.java @@ -0,0 +1,8 @@ +package com.example.qa.service.application.port.in; + +import com.example.qa.service.application.command.CreateQaCommand; +import com.example.qa.service.domain.Qa; + +public interface CreateQaUseCase { + Qa createQa(CreateQaCommand qaCommand); +} diff --git a/qa-service/qa-service-application/src/main/java/com/example/qa/service/application/port/in/DeleteQaUseCase.java b/qa-service/qa-service-application/src/main/java/com/example/qa/service/application/port/in/DeleteQaUseCase.java new file mode 100644 index 0000000..5d28dae --- /dev/null +++ b/qa-service/qa-service-application/src/main/java/com/example/qa/service/application/port/in/DeleteQaUseCase.java @@ -0,0 +1,5 @@ +package com.example.qa.service.application.port.in; + +public interface DeleteQaUseCase { + void deleteQa(Long id); +} diff --git a/qa-service/qa-service-application/src/main/java/com/example/qa/service/application/port/in/GetQaByIdUseCase.java b/qa-service/qa-service-application/src/main/java/com/example/qa/service/application/port/in/GetQaByIdUseCase.java new file mode 100644 index 0000000..ad231c0 --- /dev/null +++ b/qa-service/qa-service-application/src/main/java/com/example/qa/service/application/port/in/GetQaByIdUseCase.java @@ -0,0 +1,7 @@ +package com.example.qa.service.application.port.in; + +import com.example.qa.service.domain.Qa; + +public interface GetQaByIdUseCase { + Qa getQaById(Long id); +} diff --git a/qa-service/qa-service-application/src/main/java/com/example/qa/service/application/port/in/GetQaListUseCase.java b/qa-service/qa-service-application/src/main/java/com/example/qa/service/application/port/in/GetQaListUseCase.java new file mode 100644 index 0000000..839e056 --- /dev/null +++ b/qa-service/qa-service-application/src/main/java/com/example/qa/service/application/port/in/GetQaListUseCase.java @@ -0,0 +1,9 @@ +package com.example.qa.service.application.port.in; + +import com.example.qa.service.domain.Qa; + +import java.util.List; + +public interface GetQaListUseCase { + List getQas(); +} diff --git a/qa-service/qa-service-application/src/main/java/com/example/qa/service/application/port/in/UpdateQaUseCase.java b/qa-service/qa-service-application/src/main/java/com/example/qa/service/application/port/in/UpdateQaUseCase.java new file mode 100644 index 0000000..abf509b --- /dev/null +++ b/qa-service/qa-service-application/src/main/java/com/example/qa/service/application/port/in/UpdateQaUseCase.java @@ -0,0 +1,8 @@ +package com.example.qa.service.application.port.in; + +import com.example.qa.service.application.command.UpdateQaCommand; +import com.example.qa.service.domain.Qa; + +public interface UpdateQaUseCase { + Qa updateQa(UpdateQaCommand command); +} diff --git a/qa-service/qa-service-application/src/main/java/com/example/qa/service/application/service/CreateQaService.java b/qa-service/qa-service-application/src/main/java/com/example/qa/service/application/service/CreateQaService.java new file mode 100644 index 0000000..634222f --- /dev/null +++ b/qa-service/qa-service-application/src/main/java/com/example/qa/service/application/service/CreateQaService.java @@ -0,0 +1,28 @@ +package com.example.qa.service.application.service; + +import com.example.qa.service.application.command.CreateQaCommand; +import com.example.qa.service.application.port.in.CreateQaUseCase; +import com.example.qa.service.domain.Qa; +import com.example.qa.service.domain.port.CreateQaPort; +import com.example.qa.service.domain.valueobject.Answer; +import com.example.qa.service.domain.valueobject.Question; +import jakarta.annotation.Resource; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; + +@Slf4j +@Service +public class CreateQaService implements CreateQaUseCase { + @Resource + private CreateQaPort createQaPort; + @Override + public Qa createQa(CreateQaCommand createQaCommand) { + //command -> domain + Qa qa=new Qa( + new Question(createQaCommand.question()), + new Answer(createQaCommand.answer()) + ); + log.info("qa:{}",qa); + return createQaPort.createQa(qa); + } +} diff --git a/qa-service/qa-service-application/src/main/java/com/example/qa/service/application/service/DeleteQaService.java b/qa-service/qa-service-application/src/main/java/com/example/qa/service/application/service/DeleteQaService.java new file mode 100644 index 0000000..d3a9e3d --- /dev/null +++ b/qa-service/qa-service-application/src/main/java/com/example/qa/service/application/service/DeleteQaService.java @@ -0,0 +1,16 @@ +package com.example.qa.service.application.service; + +import com.example.qa.service.application.port.in.DeleteQaUseCase; +import com.example.qa.service.domain.port.DeleteQaPort; +import jakarta.annotation.Resource; +import org.springframework.stereotype.Service; + +@Service +public class DeleteQaService implements DeleteQaUseCase { + @Resource + private DeleteQaPort deleteQaPort; + @Override + public void deleteQa(Long id) { + deleteQaPort.deleteQa(id); + } +} diff --git a/qa-service/qa-service-application/src/main/java/com/example/qa/service/application/service/GetQaByIdService.java b/qa-service/qa-service-application/src/main/java/com/example/qa/service/application/service/GetQaByIdService.java new file mode 100644 index 0000000..f355980 --- /dev/null +++ b/qa-service/qa-service-application/src/main/java/com/example/qa/service/application/service/GetQaByIdService.java @@ -0,0 +1,18 @@ +package com.example.qa.service.application.service; + +import com.example.qa.service.application.port.in.GetQaByIdUseCase; +import com.example.qa.service.domain.Qa; +import com.example.qa.service.domain.port.GetQaByIdPort; +import jakarta.annotation.Resource; +import org.springframework.stereotype.Service; + +@Service +public class GetQaByIdService implements GetQaByIdUseCase { + + @Resource + private GetQaByIdPort getQaByIdPort; + @Override + public Qa getQaById(Long id) { + return getQaByIdPort.getQaById(id); + } +} diff --git a/qa-service/qa-service-application/src/main/java/com/example/qa/service/application/service/GetQaListService.java b/qa-service/qa-service-application/src/main/java/com/example/qa/service/application/service/GetQaListService.java new file mode 100644 index 0000000..b19f88b --- /dev/null +++ b/qa-service/qa-service-application/src/main/java/com/example/qa/service/application/service/GetQaListService.java @@ -0,0 +1,21 @@ +package com.example.qa.service.application.service; + +import com.example.qa.service.application.port.in.GetQaListUseCase; +import com.example.qa.service.domain.Qa; +import com.example.qa.service.domain.port.GetQaListPort; +import jakarta.annotation.Resource; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class GetQaListService implements GetQaListUseCase { + + @Resource + GetQaListPort getQaListPort; + @Override + public List getQas() { + List qas = Qa.getQas(getQaListPort); + return qas; + } +} diff --git a/qa-service/qa-service-application/src/main/java/com/example/qa/service/application/service/UpdateQaService.java b/qa-service/qa-service-application/src/main/java/com/example/qa/service/application/service/UpdateQaService.java new file mode 100644 index 0000000..e6e801a --- /dev/null +++ b/qa-service/qa-service-application/src/main/java/com/example/qa/service/application/service/UpdateQaService.java @@ -0,0 +1,26 @@ +package com.example.qa.service.application.service; + +import com.example.qa.service.application.command.UpdateQaCommand; +import com.example.qa.service.application.port.in.UpdateQaUseCase; +import com.example.qa.service.domain.Qa; +import com.example.qa.service.domain.port.UpdateQaPort; +import com.example.qa.service.domain.valueobject.Answer; +import com.example.qa.service.domain.valueobject.QaId; +import com.example.qa.service.domain.valueobject.Question; +import jakarta.annotation.Resource; +import org.springframework.stereotype.Service; + +@Service +public class UpdateQaService implements UpdateQaUseCase { + @Resource + private UpdateQaPort updateQaPort; + + @Override + public Qa updateQa(UpdateQaCommand command) { + Qa qa = new Qa( + new QaId(command.id()), + new Question(command.question()), + new Answer(command.answer())); + return updateQaPort.updateQa(qa); + } +} diff --git a/qa-service/qa-service-bootstrap/pom.xml b/qa-service/qa-service-bootstrap/pom.xml new file mode 100644 index 0000000..0ddd8f7 --- /dev/null +++ b/qa-service/qa-service-bootstrap/pom.xml @@ -0,0 +1,86 @@ + + + 4.0.0 + com.example + qa-service-bootstrap + 0.0.1-SNAPSHOT + qa-service-bootstrap + qa-service-bootstrap + + + com.example + qa-service + 0.0.1-SNAPSHOT + + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-test + test + + + + com.example + qa-adapter-in-web + 0.0.1-SNAPSHOT + + + + com.example + qa-adapter-out-persistence + 0.0.1-SNAPSHOT + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-config + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-discovery + + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 21 + 21 + UTF-8 + + + + org.springframework.boot + spring-boot-maven-plugin + ${spring-boot.version} + + com.example.qa.service.bootstrap.qaServiceBootstrapApplication + false + + + + repackage + + repackage + + + + + + + + diff --git a/qa-service/qa-service-bootstrap/src/main/java/com/example/qa/service/bootstrap/QaServiceBootstrapApplication.java b/qa-service/qa-service-bootstrap/src/main/java/com/example/qa/service/bootstrap/QaServiceBootstrapApplication.java new file mode 100644 index 0000000..714d3f9 --- /dev/null +++ b/qa-service/qa-service-bootstrap/src/main/java/com/example/qa/service/bootstrap/QaServiceBootstrapApplication.java @@ -0,0 +1,15 @@ +package com.example.qa.service.bootstrap; + +import org.mybatis.spring.annotation.MapperScan; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication(scanBasePackages = "com.example") +@MapperScan("com.example.qa.adapter.out.persistence.mapper") +public class QaServiceBootstrapApplication { + + public static void main(String[] args) { + SpringApplication.run(QaServiceBootstrapApplication.class, args); + } + +} diff --git a/qa-service/qa-service-bootstrap/src/main/resources/application.properties b/qa-service/qa-service-bootstrap/src/main/resources/application.properties new file mode 100644 index 0000000..2646857 --- /dev/null +++ b/qa-service/qa-service-bootstrap/src/main/resources/application.properties @@ -0,0 +1,27 @@ +server.port=28080 + +spring.application.name=qa-service + + +# Nacos认证信息 +spring.cloud.nacos.discovery.username=nacos +spring.cloud.nacos.discovery.password=nacos +# Nacos 服务发现与注册配置,其中子属性 server-addr 指定 Nacos 服务器主机和端口 +spring.cloud.nacos.discovery.server-addr=192.168.168.128:8848 +# 注册到 nacos 的指定 namespace,默认为 public +spring.cloud.nacos.discovery.namespace=public + +# Nacos帮助文档: https://nacos.io/zh-cn/docs/concepts.html +# Nacos认证信息 +spring.cloud.nacos.config.username=nacos +spring.cloud.nacos.config.password=nacos +spring.cloud.nacos.config.contextPath=/nacos +# 设置配置中心服务端地址 +spring.cloud.nacos.config.server-addr=192.168.168.128:8848 +# Nacos 配置中心的namespace。需要注意,如果使用 public 的 namcespace ,请不要填写这个值,直接留空即可 +# spring.cloud.nacos.config.namespace= +spring.config.import=nacos:${spring.application.name}.properties?refresh=true + + + + diff --git a/qa-service/qa-service-common/.gitignore b/qa-service/qa-service-common/.gitignore new file mode 100644 index 0000000..549e00a --- /dev/null +++ b/qa-service/qa-service-common/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/qa-service/qa-service-common/pom.xml b/qa-service/qa-service-common/pom.xml new file mode 100644 index 0000000..e616cba --- /dev/null +++ b/qa-service/qa-service-common/pom.xml @@ -0,0 +1,55 @@ + + + 4.0.0 + com.example + qa-service-common + 0.0.1-SNAPSHOT + qa-service-common + qa-service-common + + 21 + UTF-8 + UTF-8 + 3.0.2 + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + org.springframework.boot + spring-boot-dependencies + ${spring-boot.version} + pom + import + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 21 + 21 + UTF-8 + + + + + + diff --git a/qa-service/qa-service-common/src/main/java/com/example/qa/service/common/IdWorker.java b/qa-service/qa-service-common/src/main/java/com/example/qa/service/common/IdWorker.java new file mode 100644 index 0000000..ac96e23 --- /dev/null +++ b/qa-service/qa-service-common/src/main/java/com/example/qa/service/common/IdWorker.java @@ -0,0 +1,199 @@ +package com.example.qa.service.common; + +import java.lang.management.ManagementFactory; +import java.net.InetAddress; +import java.net.NetworkInterface; + +/** + * @author wuyunbin + *

名称:IdWorker.java

+ *

描述:分布式自增长ID

+ *
+ *     Twitter的 Snowflake JAVA实现方案
+ * 
+ * 核心代码为其IdWorker这个类实现,其原理结构如下,我分别用一个0表示一位,用—分割开部分的作用: + * 1||0---0000000000 0000000000 0000000000 0000000000 0 --- 00000 ---00000 ---000000000000 + * 在上面的字符串中,第一位为未使用(实际上也可作为long的符号位),接下来的41位为毫秒级时间, + * 然后5位datacenter标识位,5位机器ID(并不算标识符,实际是为线程标识), + * 然后12位该毫秒内的当前毫秒内的计数,加起来刚好64位,为一个Long型。 + * 这样的好处是,整体上按照时间自增排序,并且整个分布式系统内不会产生ID碰撞(由datacenter和机器ID作区分), + * 并且效率较高,经测试,snowflake每秒能够产生26万ID左右,完全满足需要。 + *

+ * 64位ID (42(毫秒)+5(机器ID)+5(业务编码)+12(重复累加)) + * @author Polim + */ +public class IdWorker { + /** + * 时间起始标记点,作为基准,一般取系统的最近时间(一旦确定不能变动) + */ + private final static long TWEPOCH = 1288834974657L; + + /** + * 机器标识位数 + */ + private final static long WORKER_ID_BITS = 5L; + + /** + * 数据中心标识位数 + */ + private final static long DATA_CENTER_ID_BITS = 5L; + + /** + * 机器ID最大值 + */ + private final static long MAX_WORKER_ID = -1L ^ (-1L << WORKER_ID_BITS); + + /** + * 数据中心ID最大值 + */ + private final static long MAX_DATACENTER_ID = -1L ^ (-1L << DATA_CENTER_ID_BITS); + + /** + * 毫秒内自增位 + */ + private final static long SEQUENCE_BITS = 12L; + + /** + * 机器ID偏左移12位 + */ + private final static long WORKER_ID_SHIFT = SEQUENCE_BITS; + + /** + * 数据中心ID左移17位 + */ + private final static long DATACENTER_ID_SHIFT = SEQUENCE_BITS + WORKER_ID_BITS; + + /** + * 时间毫秒左移22位 + */ + private final static long TIMESTAMP_LEFT_SHIFT = SEQUENCE_BITS + WORKER_ID_BITS + DATA_CENTER_ID_BITS; + + private final static long SEQUENCE_MASK = ~(-1L << SEQUENCE_BITS); + + /** + * 上次生产id时间戳 + */ + private static long lastTimestamp = -1L; + + /** + * 0,并发控制 + */ + private long sequence = 0L; + + private final long workerId; + + /** + * 数据标识id部分 + */ + private final long datacenterId; + + public IdWorker() { + this.datacenterId = getDatacenterId(); + this.workerId = getMaxWorkerId(datacenterId); + } + + /** + * @param workerId 工作机器ID + * @param datacenterId 序列号 + */ + public IdWorker(long workerId, long datacenterId) { + if (workerId > MAX_WORKER_ID || workerId < 0) { + throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", MAX_WORKER_ID)); + } + if (datacenterId > MAX_DATACENTER_ID || datacenterId < 0) { + throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", MAX_DATACENTER_ID)); + } + this.workerId = workerId; + this.datacenterId = datacenterId; + } + + /** + * 获取下一个ID + * + * @return + */ + public synchronized long nextId() { + long timestamp = timeGen(); + if (timestamp < lastTimestamp) { + throw new RuntimeException(String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp)); + } + + if (lastTimestamp == timestamp) { + // 当前毫秒内,则+1 + sequence = (sequence + 1) & SEQUENCE_MASK; + if (sequence == 0) { + // 当前毫秒内计数满了,则等待下一秒 + timestamp = tilNextMillis(lastTimestamp); + } + } else { + sequence = 0L; + } + lastTimestamp = timestamp; + // ID偏移组合生成最终的ID,并返回ID + + return ((timestamp - TWEPOCH) << TIMESTAMP_LEFT_SHIFT) + | (datacenterId << DATACENTER_ID_SHIFT) + | (workerId << WORKER_ID_SHIFT) | sequence; + } + + private long tilNextMillis(final long lastTimestamp) { + long timestamp = this.timeGen(); + while (timestamp <= lastTimestamp) { + timestamp = this.timeGen(); + } + return timestamp; + } + + private long timeGen() { + return System.currentTimeMillis(); + } + + /** + *

+ * 获取 MAX_WORKER_ID + *

+ */ + protected static long getMaxWorkerId(long datacenterId) { + StringBuilder mpid = new StringBuilder(); + mpid.append(datacenterId); + String name = ManagementFactory.getRuntimeMXBean().getName(); + if (!name.isEmpty()) { + /* + * GET jvmPid + */ + mpid.append(name.split("@")[0]); + } + /* + * MAC + PID 的 hashcode 获取16个低位 + */ + return (mpid.toString().hashCode() & 0xffff) % (IdWorker.MAX_WORKER_ID + 1); + } + + /** + *

+ * 数据标识id部分 + *

+ */ + protected static long getDatacenterId() { + long id = 0L; + try { + InetAddress ip = InetAddress.getLocalHost(); + NetworkInterface network = NetworkInterface.getByInetAddress(ip); + if (network == null) { + id = 1L; + } else { + byte[] mac = network.getHardwareAddress(); + id = ((0x000000FF & (long) mac[mac.length - 1]) + | (0x0000FF00 & (((long) mac[mac.length - 2]) << 8))) >> 6; + id = id % (IdWorker.MAX_DATACENTER_ID + 1); + } + } catch (Exception e) { + System.out.println(" getDatacenterId: " + e.getMessage()); + } + return id; + } + + + + +} diff --git a/qa-service/qa-service-domain/.gitignore b/qa-service/qa-service-domain/.gitignore new file mode 100644 index 0000000..549e00a --- /dev/null +++ b/qa-service/qa-service-domain/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/qa-service/qa-service-domain/pom.xml b/qa-service/qa-service-domain/pom.xml new file mode 100644 index 0000000..a561c5e --- /dev/null +++ b/qa-service/qa-service-domain/pom.xml @@ -0,0 +1,57 @@ + + + 4.0.0 + com.example + qa-service-domain + 0.0.1-SNAPSHOT + qa-service-domain + qa-service-domain + + + com.example + qa-service + 0.0.1-SNAPSHOT + + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.projectlombok + lombok + provided + + + + com.example + qa-service-common + 0.0.1-SNAPSHOT + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 21 + 21 + UTF-8 + + + + + + + diff --git a/qa-service/qa-service-domain/src/main/java/com/example/qa/service/domain/Qa.java b/qa-service/qa-service-domain/src/main/java/com/example/qa/service/domain/Qa.java new file mode 100644 index 0000000..8760abb --- /dev/null +++ b/qa-service/qa-service-domain/src/main/java/com/example/qa/service/domain/Qa.java @@ -0,0 +1,44 @@ +package com.example.qa.service.domain; + +import com.example.qa.service.common.IdWorker; +import com.example.qa.service.domain.port.GetQaListPort; +import com.example.qa.service.domain.valueobject.Answer; +import com.example.qa.service.domain.valueobject.QaId; +import com.example.qa.service.domain.valueobject.Question; +import lombok.Getter; +import lombok.Setter; +import lombok.ToString; + +import java.util.List; + +@Setter +@Getter +@ToString +public class Qa { + private QaId id; + private Question question; + private Answer answer; + + public Qa() { + } + + public Qa(QaId id, Question question, Answer answer) { + this.id = id; + this.question = question; + this.answer = answer; + } + + public Qa( Question question, Answer answer) { + this.id= genId() ; + this.question = question; + this.answer = answer; + } + + public static List getQas(GetQaListPort getQaListPort){ + return getQaListPort.getQas(); + } + + public QaId genId(){ + return new QaId(new IdWorker().nextId()); + } +} diff --git a/qa-service/qa-service-domain/src/main/java/com/example/qa/service/domain/port/CreateQaPort.java b/qa-service/qa-service-domain/src/main/java/com/example/qa/service/domain/port/CreateQaPort.java new file mode 100644 index 0000000..5ac3602 --- /dev/null +++ b/qa-service/qa-service-domain/src/main/java/com/example/qa/service/domain/port/CreateQaPort.java @@ -0,0 +1,7 @@ +package com.example.qa.service.domain.port; + +import com.example.qa.service.domain.Qa; + +public interface CreateQaPort { + Qa createQa(Qa qa); +} diff --git a/qa-service/qa-service-domain/src/main/java/com/example/qa/service/domain/port/DeleteQaPort.java b/qa-service/qa-service-domain/src/main/java/com/example/qa/service/domain/port/DeleteQaPort.java new file mode 100644 index 0000000..d7b8363 --- /dev/null +++ b/qa-service/qa-service-domain/src/main/java/com/example/qa/service/domain/port/DeleteQaPort.java @@ -0,0 +1,5 @@ +package com.example.qa.service.domain.port; + +public interface DeleteQaPort { + void deleteQa(Long id); +} diff --git a/qa-service/qa-service-domain/src/main/java/com/example/qa/service/domain/port/GetQaByIdPort.java b/qa-service/qa-service-domain/src/main/java/com/example/qa/service/domain/port/GetQaByIdPort.java new file mode 100644 index 0000000..1068e1c --- /dev/null +++ b/qa-service/qa-service-domain/src/main/java/com/example/qa/service/domain/port/GetQaByIdPort.java @@ -0,0 +1,7 @@ +package com.example.qa.service.domain.port; + +import com.example.qa.service.domain.Qa; + +public interface GetQaByIdPort { + Qa getQaById(Long id); +} diff --git a/qa-service/qa-service-domain/src/main/java/com/example/qa/service/domain/port/GetQaListPort.java b/qa-service/qa-service-domain/src/main/java/com/example/qa/service/domain/port/GetQaListPort.java new file mode 100644 index 0000000..ad52920 --- /dev/null +++ b/qa-service/qa-service-domain/src/main/java/com/example/qa/service/domain/port/GetQaListPort.java @@ -0,0 +1,9 @@ +package com.example.qa.service.domain.port; + +import com.example.qa.service.domain.Qa; + +import java.util.List; + +public interface GetQaListPort { + List getQas(); +} diff --git a/qa-service/qa-service-domain/src/main/java/com/example/qa/service/domain/port/UpdateQaPort.java b/qa-service/qa-service-domain/src/main/java/com/example/qa/service/domain/port/UpdateQaPort.java new file mode 100644 index 0000000..9cf9aed --- /dev/null +++ b/qa-service/qa-service-domain/src/main/java/com/example/qa/service/domain/port/UpdateQaPort.java @@ -0,0 +1,7 @@ +package com.example.qa.service.domain.port; + +import com.example.qa.service.domain.Qa; + +public interface UpdateQaPort { + Qa updateQa(Qa qa); +} diff --git a/qa-service/qa-service-domain/src/main/java/com/example/qa/service/domain/valueobject/Answer.java b/qa-service/qa-service-domain/src/main/java/com/example/qa/service/domain/valueobject/Answer.java new file mode 100644 index 0000000..18f0139 --- /dev/null +++ b/qa-service/qa-service-domain/src/main/java/com/example/qa/service/domain/valueobject/Answer.java @@ -0,0 +1,7 @@ +package com.example.qa.service.domain.valueobject; + +public record Answer(String answer) { + public String getValue(){ + return answer; + } +} diff --git a/qa-service/qa-service-domain/src/main/java/com/example/qa/service/domain/valueobject/QaId.java b/qa-service/qa-service-domain/src/main/java/com/example/qa/service/domain/valueobject/QaId.java new file mode 100644 index 0000000..ea201ec --- /dev/null +++ b/qa-service/qa-service-domain/src/main/java/com/example/qa/service/domain/valueobject/QaId.java @@ -0,0 +1,9 @@ +package com.example.qa.service.domain.valueobject; + +public record QaId(long id) { + + public long getValue(){ + return id; + } + +} diff --git a/qa-service/qa-service-domain/src/main/java/com/example/qa/service/domain/valueobject/Question.java b/qa-service/qa-service-domain/src/main/java/com/example/qa/service/domain/valueobject/Question.java new file mode 100644 index 0000000..aee0aba --- /dev/null +++ b/qa-service/qa-service-domain/src/main/java/com/example/qa/service/domain/valueobject/Question.java @@ -0,0 +1,5 @@ +package com.example.qa.service.domain.valueobject; + +public record Question(String question) { + public String getValue(){return question;} +} -- Gitee