├── hexagon.png ├── .gitignore ├── command-query-crud.png ├── hexagonal-architecture-demo-application ├── src │ └── main │ │ └── java │ │ └── io │ │ └── github │ │ └── hexagonframework │ │ └── demo │ │ └── application │ │ ├── model │ │ ├── RegisterUserCommand.java │ │ └── PlaceOrderCommand.java │ │ ├── HelloApplicationService.java │ │ └── OrderApplicationService.java └── pom.xml ├── hexagonal-architecture-demo-adapter ├── hexagonal-architecture-demo-adapter-dubbo │ ├── hexagonal-architecture-demo-adapter-dubbo-api │ │ ├── src │ │ │ └── main │ │ │ │ └── java │ │ │ │ └── io │ │ │ │ └── github │ │ │ │ └── hexagonframework │ │ │ │ └── demo │ │ │ │ └── dubbo │ │ │ │ └── api │ │ │ │ ├── model │ │ │ │ ├── HelloQuery.java │ │ │ │ └── HelloCommand.java │ │ │ │ ├── HelloService.java │ │ │ │ ├── DemoConstants.java │ │ │ │ └── exception │ │ │ │ └── DemoStateException.java │ │ └── pom.xml │ ├── hexagonal-architecture-demo-adapter-dubbo-service │ │ ├── src │ │ │ └── main │ │ │ │ └── java │ │ │ │ └── io │ │ │ │ └── github │ │ │ │ └── hexagonframework │ │ │ │ └── demo │ │ │ │ └── dubbo │ │ │ │ └── service │ │ │ │ └── DubboHelloService.java │ │ └── pom.xml │ └── pom.xml ├── hexagonal-architecture-demo-adapter-mybatis │ ├── src │ │ └── main │ │ │ ├── java │ │ │ └── io │ │ │ │ └── github │ │ │ │ └── hexagonframework │ │ │ │ └── demo │ │ │ │ └── mybatis │ │ │ │ ├── mapper │ │ │ │ ├── OrderMapper.java │ │ │ │ └── OrderItemMapper.java │ │ │ │ └── MybatisOrderRepository.java │ │ │ └── resources │ │ │ └── META-INF │ │ │ └── mappers │ │ │ ├── OrderMapper.xml │ │ │ └── OrderItemMapper.xml │ └── pom.xml ├── hexagonal-architecture-demo-adapter-rest │ ├── hexagonal-architecture-demo-adapter-rest-service │ │ ├── src │ │ │ └── main │ │ │ │ └── java │ │ │ │ └── io │ │ │ │ └── github │ │ │ │ └── hexagonframework │ │ │ │ └── demo │ │ │ │ └── rest │ │ │ │ └── service │ │ │ │ ├── model │ │ │ │ └── Greeting.java │ │ │ │ └── GreetingController.java │ │ └── pom.xml │ ├── hexagonal-architecture-demo-adapter-rest-client │ │ └── pom.xml │ └── pom.xml └── pom.xml ├── hexagonal-architecture-demo-domain ├── src │ └── main │ │ └── java │ │ └── io │ │ └── github │ │ └── hexagonframework │ │ └── demo │ │ └── domain │ │ ├── model │ │ ├── OrderItem.java │ │ └── Order.java │ │ └── repository │ │ └── OrderRepository.java └── pom.xml ├── hexagonal-architecture-demo-common └── pom.xml ├── hexagonal-architecture-demo-main ├── src │ └── main │ │ ├── resources │ │ ├── logback.xml │ │ └── application.yml │ │ └── java │ │ └── io │ │ └── github │ │ └── hexagonframework │ │ └── demo │ │ └── DemoLauncher.java └── pom.xml ├── README.md ├── pom.xml └── LICENSE /hexagon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hexagonframework/hexagonal-architecture-demo/HEAD/hexagon.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .settings 2 | .idea 3 | target/ 4 | data/ 5 | 6 | .project 7 | .classpath 8 | .factorypath 9 | *.iml -------------------------------------------------------------------------------- /command-query-crud.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hexagonframework/hexagonal-architecture-demo/HEAD/command-query-crud.png -------------------------------------------------------------------------------- /hexagonal-architecture-demo-application/src/main/java/io/github/hexagonframework/demo/application/model/RegisterUserCommand.java: -------------------------------------------------------------------------------- 1 | package io.github.hexagonframework.demo.application.model; 2 | 3 | public class RegisterUserCommand { 4 | } 5 | -------------------------------------------------------------------------------- /hexagonal-architecture-demo-adapter/hexagonal-architecture-demo-adapter-dubbo/hexagonal-architecture-demo-adapter-dubbo-api/src/main/java/io/github/hexagonframework/demo/dubbo/api/model/HelloQuery.java: -------------------------------------------------------------------------------- 1 | package io.github.hexagonframework.demo.dubbo.api.model; 2 | 3 | public class HelloQuery { 4 | } 5 | -------------------------------------------------------------------------------- /hexagonal-architecture-demo-adapter/hexagonal-architecture-demo-adapter-dubbo/hexagonal-architecture-demo-adapter-dubbo-api/src/main/java/io/github/hexagonframework/demo/dubbo/api/model/HelloCommand.java: -------------------------------------------------------------------------------- 1 | package io.github.hexagonframework.demo.dubbo.api.model; 2 | 3 | public class HelloCommand { 4 | } 5 | -------------------------------------------------------------------------------- /hexagonal-architecture-demo-adapter/hexagonal-architecture-demo-adapter-dubbo/hexagonal-architecture-demo-adapter-dubbo-api/src/main/java/io/github/hexagonframework/demo/dubbo/api/HelloService.java: -------------------------------------------------------------------------------- 1 | package io.github.hexagonframework.demo.dubbo.api; 2 | 3 | public interface HelloService { 4 | String say(String word); 5 | } 6 | -------------------------------------------------------------------------------- /hexagonal-architecture-demo-application/src/main/java/io/github/hexagonframework/demo/application/model/PlaceOrderCommand.java: -------------------------------------------------------------------------------- 1 | package io.github.hexagonframework.demo.application.model; 2 | 3 | import lombok.Value; 4 | 5 | import java.util.List; 6 | 7 | @Value 8 | public class PlaceOrderCommand { 9 | private int userId; 10 | } 11 | -------------------------------------------------------------------------------- /hexagonal-architecture-demo-domain/src/main/java/io/github/hexagonframework/demo/domain/model/OrderItem.java: -------------------------------------------------------------------------------- 1 | package io.github.hexagonframework.demo.domain.model; 2 | 3 | import lombok.Data; 4 | 5 | @Data 6 | public class OrderItem { 7 | private long orderItemId; 8 | 9 | private long orderId; 10 | 11 | private int userId; 12 | 13 | private String status; 14 | } 15 | -------------------------------------------------------------------------------- /hexagonal-architecture-demo-adapter/hexagonal-architecture-demo-adapter-dubbo/hexagonal-architecture-demo-adapter-dubbo-api/src/main/java/io/github/hexagonframework/demo/dubbo/api/DemoConstants.java: -------------------------------------------------------------------------------- 1 | package io.github.hexagonframework.demo.dubbo.api; 2 | 3 | public class DemoConstants { 4 | /** 5 | * Dubbo service 版本号 6 | */ 7 | public static final String DUBBO_VERSION_100 = "1.0.0"; 8 | 9 | } 10 | -------------------------------------------------------------------------------- /hexagonal-architecture-demo-adapter/hexagonal-architecture-demo-adapter-dubbo/hexagonal-architecture-demo-adapter-dubbo-api/src/main/java/io/github/hexagonframework/demo/dubbo/api/exception/DemoStateException.java: -------------------------------------------------------------------------------- 1 | package io.github.hexagonframework.demo.dubbo.api.exception; 2 | 3 | public class DemoStateException extends RuntimeException { 4 | 5 | public DemoStateException() { } 6 | 7 | public DemoStateException(String message) { 8 | super(message); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /hexagonal-architecture-demo-adapter/hexagonal-architecture-demo-adapter-mybatis/src/main/java/io/github/hexagonframework/demo/mybatis/mapper/OrderMapper.java: -------------------------------------------------------------------------------- 1 | package io.github.hexagonframework.demo.mybatis.mapper; 2 | 3 | import io.github.hexagonframework.demo.domain.model.Order; 4 | import org.apache.ibatis.annotations.Mapper; 5 | 6 | @Mapper 7 | public interface OrderMapper { 8 | void createIfNotExistsTable(); 9 | 10 | void truncateTable(); 11 | 12 | Long insert(Order model); 13 | 14 | void delete(Long orderId); 15 | 16 | void dropTable(); 17 | } 18 | -------------------------------------------------------------------------------- /hexagonal-architecture-demo-domain/src/main/java/io/github/hexagonframework/demo/domain/repository/OrderRepository.java: -------------------------------------------------------------------------------- 1 | package io.github.hexagonframework.demo.domain.repository; 2 | 3 | import io.github.hexagonframework.demo.domain.model.Order; 4 | import io.github.hexagonframework.demo.domain.model.OrderItem; 5 | 6 | import java.util.List; 7 | 8 | public interface OrderRepository { 9 | void save(Order order); 10 | void update(Order order); 11 | void delete(Long id); 12 | Order orderOfId(Long id); 13 | List allOrderItemsOfOrderId(Long orderId); 14 | } 15 | -------------------------------------------------------------------------------- /hexagonal-architecture-demo-adapter/hexagonal-architecture-demo-adapter-rest/hexagonal-architecture-demo-adapter-rest-service/src/main/java/io/github/hexagonframework/demo/rest/service/model/Greeting.java: -------------------------------------------------------------------------------- 1 | package io.github.hexagonframework.demo.rest.service.model; 2 | 3 | public class Greeting { 4 | private final long id; 5 | private final String content; 6 | 7 | public Greeting(long id, String content) { 8 | this.id = id; 9 | this.content = content; 10 | } 11 | 12 | public long getId() { 13 | return id; 14 | } 15 | 16 | public String getContent() { 17 | return content; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /hexagonal-architecture-demo-common/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | hexagonal-architecture-demo 7 | io.github.hexagonframework 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | hexagonal-architecture-demo-common 13 | 14 | 15 | -------------------------------------------------------------------------------- /hexagonal-architecture-demo-adapter/hexagonal-architecture-demo-adapter-dubbo/hexagonal-architecture-demo-adapter-dubbo-service/src/main/java/io/github/hexagonframework/demo/dubbo/service/DubboHelloService.java: -------------------------------------------------------------------------------- 1 | package io.github.hexagonframework.demo.dubbo.service; 2 | 3 | import com.alibaba.dubbo.config.annotation.Service; 4 | import io.github.hexagonframework.demo.dubbo.api.HelloService; 5 | import org.springframework.stereotype.Component; 6 | 7 | @Service(interfaceClass = HelloService.class) 8 | @Component 9 | public class DubboHelloService implements HelloService { 10 | public String say(String words) { 11 | return "Hello " + words; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /hexagonal-architecture-demo-adapter/hexagonal-architecture-demo-adapter-mybatis/src/main/java/io/github/hexagonframework/demo/mybatis/mapper/OrderItemMapper.java: -------------------------------------------------------------------------------- 1 | package io.github.hexagonframework.demo.mybatis.mapper; 2 | 3 | import io.github.hexagonframework.demo.domain.model.OrderItem; 4 | import org.apache.ibatis.annotations.Mapper; 5 | 6 | import java.util.List; 7 | 8 | @Mapper 9 | public interface OrderItemMapper { 10 | void createIfNotExistsTable(); 11 | 12 | void truncateTable(); 13 | 14 | Long insert(OrderItem model); 15 | 16 | void delete(Long orderItemId); 17 | 18 | List selectAll(); 19 | 20 | void dropTable(); 21 | } 22 | -------------------------------------------------------------------------------- /hexagonal-architecture-demo-application/src/main/java/io/github/hexagonframework/demo/application/HelloApplicationService.java: -------------------------------------------------------------------------------- 1 | package io.github.hexagonframework.demo.application; 2 | 3 | import com.alibaba.dubbo.config.annotation.Reference; 4 | import io.github.hexagonframework.demo.dubbo.api.HelloService; 5 | import org.springframework.stereotype.Service; 6 | 7 | import java.util.concurrent.atomic.AtomicLong; 8 | 9 | @Service 10 | public class HelloApplicationService { 11 | private final AtomicLong counter = new AtomicLong(); 12 | 13 | @Reference(url = "dubbo://127.0.0.1:20880", lazy = true) 14 | HelloService helloService; 15 | 16 | public String greeting(String name) { 17 | return helloService.say(name); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /hexagonal-architecture-demo-adapter/hexagonal-architecture-demo-adapter-rest/hexagonal-architecture-demo-adapter-rest-client/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | hexagonal-architecture-demo-adapter-rest 7 | io.github.hexagonframework 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | hexagonal-architecture-demo-adapter-rest-client 13 | 14 | 15 | -------------------------------------------------------------------------------- /hexagonal-architecture-demo-main/src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | ${log.context.name} 9 | 10 | 11 | 12 | ${log.pattern} 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /hexagonal-architecture-demo-domain/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | hexagonal-architecture-demo 7 | io.github.hexagonframework 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | hexagonal-architecture-demo-domain 13 | 14 | 15 | 16 | org.projectlombok 17 | lombok 18 | 19 | 20 | -------------------------------------------------------------------------------- /hexagonal-architecture-demo-adapter/hexagonal-architecture-demo-adapter-dubbo/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | hexagonal-architecture-demo-adapter 7 | io.github.hexagonframework 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | hexagonal-architecture-demo-adapter-dubbo 13 | pom 14 | 15 | hexagonal-architecture-demo-adapter-dubbo-api 16 | hexagonal-architecture-demo-adapter-dubbo-service 17 | 18 | 19 | -------------------------------------------------------------------------------- /hexagonal-architecture-demo-adapter/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | hexagonal-architecture-demo 7 | io.github.hexagonframework 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | hexagonal-architecture-demo-adapter 13 | pom 14 | 15 | hexagonal-architecture-demo-adapter-rest 16 | hexagonal-architecture-demo-adapter-dubbo 17 | hexagonal-architecture-demo-adapter-mybatis 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /hexagonal-architecture-demo-adapter/hexagonal-architecture-demo-adapter-dubbo/hexagonal-architecture-demo-adapter-dubbo-api/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | hexagonal-architecture-demo-adapter-dubbo 7 | io.github.hexagonframework 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | hexagonal-architecture-demo-adapter-dubbo-api 13 | 14 | 15 | org.projectlombok 16 | lombok 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /hexagonal-architecture-demo-domain/src/main/java/io/github/hexagonframework/demo/domain/model/Order.java: -------------------------------------------------------------------------------- 1 | package io.github.hexagonframework.demo.domain.model; 2 | 3 | import lombok.Builder; 4 | import lombok.Data; 5 | 6 | import java.util.ArrayList; 7 | import java.util.List; 8 | 9 | @Data 10 | @Builder 11 | public class Order { 12 | private Long orderId; 13 | 14 | private int userId; 15 | 16 | private String status; 17 | 18 | private List orderItems; 19 | 20 | public void addOrderItem(OrderItem orderItem) { 21 | if (orderItems == null) { 22 | orderItems = new ArrayList<>(); 23 | } 24 | orderItems.add(orderItem); 25 | } 26 | 27 | public void clearOrderItems() { 28 | if (orderItems != null) { 29 | orderItems.clear(); 30 | } 31 | } 32 | 33 | public int sizeOfOrderItems() { 34 | int size = 0; 35 | if (orderItems != null) { 36 | size = orderItems.size(); 37 | } 38 | return size; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /hexagonal-architecture-demo-main/src/main/java/io/github/hexagonframework/demo/DemoLauncher.java: -------------------------------------------------------------------------------- 1 | package io.github.hexagonframework.demo; 2 | 3 | import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration; 4 | import io.github.hexagonframework.demo.application.OrderApplicationService; 5 | import io.github.hexagonframework.demo.application.model.PlaceOrderCommand; 6 | import org.springframework.boot.SpringApplication; 7 | import org.springframework.boot.autoconfigure.SpringBootApplication; 8 | import org.springframework.context.ApplicationContext; 9 | 10 | import java.util.Arrays; 11 | 12 | @SpringBootApplication 13 | @EnableDubboConfiguration 14 | public class DemoLauncher { 15 | public static void main(String[] args) { 16 | ApplicationContext applicationContext = SpringApplication.run(DemoLauncher.class, args); 17 | 18 | // test only 19 | for (int userId=1; userId<100; userId++) { 20 | PlaceOrderCommand placeOrderCommand = new PlaceOrderCommand(userId); 21 | applicationContext.getBean(OrderApplicationService.class).placeOrder(placeOrderCommand); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /hexagonal-architecture-demo-adapter/hexagonal-architecture-demo-adapter-rest/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | hexagonal-architecture-demo-adapter 7 | io.github.hexagonframework 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | hexagonal-architecture-demo-adapter-rest 13 | pom 14 | 15 | hexagonal-architecture-demo-adapter-rest-service 16 | hexagonal-architecture-demo-adapter-rest-client 17 | 18 | 19 | 20 | 21 | io.github.hexagonframework 22 | hexagonal-architecture-demo-application 23 | 24 | 25 | -------------------------------------------------------------------------------- /hexagonal-architecture-demo-application/src/main/java/io/github/hexagonframework/demo/application/OrderApplicationService.java: -------------------------------------------------------------------------------- 1 | package io.github.hexagonframework.demo.application; 2 | 3 | import io.github.hexagonframework.demo.application.model.PlaceOrderCommand; 4 | import io.github.hexagonframework.demo.domain.model.Order; 5 | import io.github.hexagonframework.demo.domain.model.OrderItem; 6 | import io.github.hexagonframework.demo.domain.repository.OrderRepository; 7 | import org.springframework.stereotype.Service; 8 | 9 | import javax.annotation.Resource; 10 | import java.util.List; 11 | 12 | @Service 13 | public class OrderApplicationService { 14 | @Resource 15 | private OrderRepository orderRepository; 16 | 17 | public List findAllOrderItemByOrderId(long orderId){ 18 | return null; 19 | } 20 | 21 | public Long placeOrder(PlaceOrderCommand placeOrderCommand) { 22 | Order order = Order.builder() 23 | .userId(placeOrderCommand.getUserId()) 24 | .status("INIT") 25 | .build(); 26 | orderRepository.save(order); 27 | return order.getOrderId(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /hexagonal-architecture-demo-adapter/hexagonal-architecture-demo-adapter-mybatis/src/main/resources/META-INF/mappers/OrderMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CREATE TABLE IF NOT EXISTS t_order (order_id BIGINT AUTO_INCREMENT, user_id INT NOT NULL, status VARCHAR(50), PRIMARY KEY (order_id)); 6 | 7 | 8 | 9 | TRUNCATE TABLE t_order; 10 | 11 | 12 | 13 | DROP TABLE IF EXISTS t_order; 14 | 15 | 16 | 17 | INSERT INTO t_order ( 18 | user_id, status 19 | ) 20 | VALUES ( 21 | #{userId,jdbcType=INTEGER}, 22 | #{status,jdbcType=VARCHAR} 23 | ) 24 | 25 | 26 | 27 | DELETE FROM t_order WHERE order_id = #{orderId,jdbcType=INTEGER} 28 | 29 | -------------------------------------------------------------------------------- /hexagonal-architecture-demo-adapter/hexagonal-architecture-demo-adapter-rest/hexagonal-architecture-demo-adapter-rest-service/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | hexagonal-architecture-demo-adapter-rest 7 | io.github.hexagonframework 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | hexagonal-architecture-demo-adapter-rest-service 13 | 14 | 15 | 16 | io.github.hexagonframework 17 | hexagonal-architecture-demo-application 18 | 19 | 20 | 21 | org.springframework.boot 22 | spring-boot-starter-web 23 | true 24 | 25 | 26 | -------------------------------------------------------------------------------- /hexagonal-architecture-demo-adapter/hexagonal-architecture-demo-adapter-rest/hexagonal-architecture-demo-adapter-rest-service/src/main/java/io/github/hexagonframework/demo/rest/service/GreetingController.java: -------------------------------------------------------------------------------- 1 | package io.github.hexagonframework.demo.rest.service; 2 | 3 | import io.github.hexagonframework.demo.application.HelloApplicationService; 4 | import io.github.hexagonframework.demo.rest.service.model.Greeting; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RequestParam; 8 | import org.springframework.web.bind.annotation.RestController; 9 | 10 | import java.util.concurrent.atomic.AtomicLong; 11 | 12 | @RestController 13 | public class GreetingController { 14 | private static final String template = "Hello, %s!"; 15 | private final AtomicLong counter = new AtomicLong(); 16 | 17 | @Autowired 18 | HelloApplicationService helloApplicationService; 19 | 20 | @RequestMapping("/greeting") 21 | public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) { 22 | return new Greeting(counter.incrementAndGet(), 23 | helloApplicationService.greeting(name)); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /hexagonal-architecture-demo-adapter/hexagonal-architecture-demo-adapter-dubbo/hexagonal-architecture-demo-adapter-dubbo-service/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | hexagonal-architecture-demo-adapter-dubbo 7 | io.github.hexagonframework 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | hexagonal-architecture-demo-adapter-dubbo-service 13 | 14 | 15 | 16 | io.github.hexagonframework 17 | hexagonal-architecture-demo-adapter-dubbo-api 18 | 19 | 20 | io.github.hexagonframework 21 | hexagonal-architecture-demo-application 22 | 23 | 24 | com.alibaba.spring.boot 25 | dubbo-spring-boot-starter 26 | true 27 | 28 | 29 | -------------------------------------------------------------------------------- /hexagonal-architecture-demo-adapter/hexagonal-architecture-demo-adapter-mybatis/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | hexagonal-architecture-demo-adapter 7 | io.github.hexagonframework 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | hexagonal-architecture-demo-adapter-mybatis 13 | 14 | 15 | 16 | io.github.hexagonframework 17 | hexagonal-architecture-demo-domain 18 | 19 | 20 | org.springframework 21 | spring-context 22 | true 23 | 24 | 25 | com.alibaba 26 | druid 27 | 28 | 29 | mysql 30 | mysql-connector-java 31 | 32 | 33 | org.mybatis.spring.boot 34 | mybatis-spring-boot-starter 35 | 36 | 37 | -------------------------------------------------------------------------------- /hexagonal-architecture-demo-application/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | hexagonal-architecture-demo 7 | io.github.hexagonframework 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | hexagonal-architecture-demo-application 13 | 14 | 15 | 16 | io.github.hexagonframework 17 | hexagonal-architecture-demo-domain 18 | 19 | 20 | io.github.hexagonframework 21 | hexagonal-architecture-demo-adapter-dubbo-api 22 | 23 | 24 | org.springframework 25 | spring-context 26 | true 27 | 28 | 29 | com.alibaba.spring.boot 30 | dubbo-spring-boot-starter 31 | true 32 | 33 | 34 | org.projectlombok 35 | lombok 36 | provided 37 | 38 | 39 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # hexagonal-architecture-demo 2 | > 基于DDD和六边形架构的微服务DEMO 3 | 4 | ## Command Query CRUD Responsibility Segregation 5 | Not every piece of software is equally important... 6 | Not every piece will decide about company / product success or can cause not reversible 7 | negative business consequences like materialise brand risk or money loses. 8 | On the other hand scalability or non functional requirements are different for different activities in software. 9 | 10 | To accommodate to those differences, separate architectural patterns are applied: 11 | 12 | ![Command Query CRUD Responsibility Segregation](command-query-crud.png) 13 | 14 | 15 | ## Hexagonal Architecture 16 | Only the most valuable part of that enterprise software is embedded in hexagonal architecture - 17 | complex business processing modeled in form of the Domain Model. 18 | 19 | ![Domain Model embedded in hexagonal architecture](hexagon.png) 20 | 21 | **Application Services** - providing entry point to Domain Model functionality, 22 | Application Services are ports for Primary / Driving Adapters like RESTfull endpoints. 23 | 24 | **Domain Model** - Object Oriented (in that case) piece of software modeling business rules, invariants, 25 | calculations and processing variants. 26 | Thanks to hexagon can be as clean and simple as possible - separating essential complexity of pure business 27 | from accidental complexity of technical choices, free of technical and convention constraints. 28 | 29 | **Ports** - contract defined by Domain Model expressing expectations from external resources (services, database or other models). 30 | Declared interfaces alongside with IN-OUT parameters are Ports for Secondary / Driven Adapters like repository implementation. 31 | 32 | **Adapters** - integration of the technology (REST, database, external services, etc.) with the Domain Model. 33 | Making useful application from the Domain Model and the technology. -------------------------------------------------------------------------------- /hexagonal-architecture-demo-main/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | application: 3 | name: dubbo-spring-boot-starter 4 | dubbo: 5 | server: true 6 | registry: N/A 7 | 8 | mybatis: 9 | mapper-locations: classpath*:/META-INF/mappers/*Mapper.xml 10 | 11 | sharding: 12 | jdbc: 13 | datasource: 14 | names: ds_0,ds_1 15 | ds_0: 16 | type: com.alibaba.druid.pool.DruidDataSource 17 | driver-class-name: com.mysql.jdbc.Driver 18 | url: jdbc:mysql://localhost:3306/demo_ds_0 19 | username: root 20 | password: root1@3$ 21 | ds_1: 22 | type: com.alibaba.druid.pool.DruidDataSource 23 | driver-class-name: com.mysql.jdbc.Driver 24 | url: jdbc:mysql://localhost:3306/demo_ds_1 25 | username: root 26 | password: root1@3$ 27 | config: 28 | sharding: 29 | # 默认数据库分片策略 30 | default-database-strategy: 31 | inline: 32 | sharding-column: user_id 33 | algorithm-expression: ds_${user_id % 2} 34 | default-key-generator-class: io.shardingjdbc.core.keygen.DefaultKeyGenerator 35 | 36 | tables: 37 | t_order: 38 | actual-data-nodes: ds_${0..1}.t_order_${0..1} 39 | table-strategy: 40 | inline: 41 | sharding-column: order_id 42 | algorithm-expression: t_order_${order_id % 2} 43 | key-generator-column-name: order_id 44 | t_order_item: 45 | actual-data-nodes: ds_${0..1}.t_order_item_${0..1} 46 | table-strategy: 47 | inline: 48 | sharding-column: order_id 49 | algorithm-expression: t_order_item_${order_id % 2} 50 | key-generator-column-name: order_item_id 51 | 52 | bindingTables: 53 | - t_order,t_order_item 54 | 55 | props: 56 | sql.show: true -------------------------------------------------------------------------------- /hexagonal-architecture-demo-adapter/hexagonal-architecture-demo-adapter-mybatis/src/main/java/io/github/hexagonframework/demo/mybatis/MybatisOrderRepository.java: -------------------------------------------------------------------------------- 1 | package io.github.hexagonframework.demo.mybatis; 2 | 3 | import io.github.hexagonframework.demo.domain.model.Order; 4 | import io.github.hexagonframework.demo.domain.model.OrderItem; 5 | import io.github.hexagonframework.demo.domain.repository.OrderRepository; 6 | import io.github.hexagonframework.demo.mybatis.mapper.OrderItemMapper; 7 | import io.github.hexagonframework.demo.mybatis.mapper.OrderMapper; 8 | import org.springframework.stereotype.Repository; 9 | import org.springframework.util.Assert; 10 | 11 | import javax.annotation.Resource; 12 | import java.util.List; 13 | 14 | @Repository 15 | public class MybatisOrderRepository implements OrderRepository { 16 | @Resource 17 | OrderMapper orderMapper; 18 | 19 | @Resource 20 | OrderItemMapper orderItemMapper; 21 | 22 | @Override 23 | public void save(Order order) { 24 | orderMapper.createIfNotExistsTable(); 25 | orderItemMapper.createIfNotExistsTable(); 26 | Assert.notNull(order, "订单不能为空"); 27 | Long orderId = orderMapper.insert(order); 28 | if (order.sizeOfOrderItems() > 0) { 29 | List orderItems = order.getOrderItems(); 30 | for (OrderItem orderItem : orderItems) { 31 | orderItem.setOrderId(orderId); 32 | orderItemMapper.insert(orderItem); 33 | } 34 | } 35 | } 36 | 37 | @Override 38 | public void update(Order order) { 39 | 40 | } 41 | 42 | @Override 43 | public void delete(Long id) { 44 | 45 | } 46 | 47 | @Override 48 | public Order orderOfId(Long id) { 49 | return null; 50 | } 51 | 52 | @Override 53 | public List allOrderItemsOfOrderId(Long orderId) { 54 | return null; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /hexagonal-architecture-demo-adapter/hexagonal-architecture-demo-adapter-mybatis/src/main/resources/META-INF/mappers/OrderItemMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | CREATE TABLE IF NOT EXISTS t_order_item (order_item_id BIGINT, order_id BIGINT, user_id INT NOT NULL, status VARCHAR(50), PRIMARY KEY (order_item_id)); 13 | 14 | 15 | 16 | TRUNCATE TABLE t_order_item; 17 | 18 | 19 | 20 | DROP TABLE IF EXISTS t_order_item; 21 | 22 | 23 | 24 | INSERT INTO t_order_item ( 25 | order_id, user_id, status 26 | ) 27 | VALUES ( 28 | #{orderId,jdbcType=INTEGER}, 29 | #{userId,jdbcType=INTEGER}, 30 | #{status,jdbcType=VARCHAR} 31 | ) 32 | 33 | 34 | 35 | DELETE FROM t_order_item WHERE order_id = #{orderId,jdbcType=INTEGER} 36 | 37 | 38 | 46 | -------------------------------------------------------------------------------- /hexagonal-architecture-demo-main/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | hexagonal-architecture-demo 7 | io.github.hexagonframework 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | hexagonal-architecture-demo-main 13 | 14 | 15 | 16 | io.github.hexagonframework 17 | hexagonal-architecture-demo-adapter-dubbo-service 18 | 19 | 20 | io.github.hexagonframework 21 | hexagonal-architecture-demo-adapter-rest-service 22 | 23 | 24 | io.github.hexagonframework 25 | hexagonal-architecture-demo-adapter-mybatis 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-starter-web 35 | 36 | 37 | com.alibaba.spring.boot 38 | dubbo-spring-boot-starter 39 | 40 | 41 | io.shardingjdbc 42 | sharding-jdbc-core-spring-boot-starter 43 | 44 | 45 | 46 | 47 | 48 | 49 | org.springframework.boot 50 | spring-boot-maven-plugin 51 | 52 | 53 | 54 | 55 | 56 | 57 | spring-releases 58 | https://repo.spring.io/libs-release 59 | 60 | 61 | 62 | 63 | spring-releases 64 | https://repo.spring.io/libs-release 65 | 66 | 67 | 68 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | io.github.hexagonframework 8 | hexagonal-architecture-demo 9 | pom 10 | 1.0-SNAPSHOT 11 | 12 | 13 | hexagonal-architecture-demo-domain 14 | hexagonal-architecture-demo-adapter 15 | hexagonal-architecture-demo-application 16 | hexagonal-architecture-demo-main 17 | hexagonal-architecture-demo-common 18 | 19 | 20 | 21 | 1.8 22 | ${java.version} 23 | ${java.version} 24 | UTF-8 25 | zh_CN 26 | 27 | 1.0-SNAPSHOT 28 | 29 | 2.0.2 30 | 31 | 2.0.0 32 | 1.5.10.RELEASE 33 | 34 | 1.1.2 35 | 5.1.30 36 | 1.3.2 37 | 5.1.4 38 | 39 | 40 | 41 | 42 | 43 | org.springframework.boot 44 | spring-boot-dependencies 45 | ${spring-boot.version} 46 | pom 47 | import 48 | 49 | 50 | io.github.hexagonframework 51 | hexagonal-architecture-demo-domain 52 | ${project.version} 53 | 54 | 55 | io.github.hexagonframework 56 | hexagonal-architecture-demo-application 57 | ${project.version} 58 | 59 | 60 | io.github.hexagonframework 61 | hexagonal-architecture-demo-adapter-rest-service 62 | ${project.version} 63 | 64 | 65 | io.github.hexagonframework 66 | hexagonal-architecture-demo-adapter-rest-client 67 | ${project.version} 68 | 69 | 70 | io.github.hexagonframework 71 | hexagonal-architecture-demo-adapter-dubbo-api 72 | ${project.version} 73 | 74 | 75 | io.github.hexagonframework 76 | hexagonal-architecture-demo-adapter-dubbo-service 77 | ${project.version} 78 | 79 | 80 | io.github.hexagonframework 81 | hexagonal-architecture-demo-adapter-mybatis 82 | ${project.version} 83 | 84 | 85 | io.github.hexagonframework 86 | hexagonal-architecture-demo-main 87 | ${project.version} 88 | 89 | 90 | org.projectlombok 91 | lombok 92 | provided 93 | 1.16.18 94 | 95 | 96 | com.alibaba.spring.boot 97 | dubbo-spring-boot-starter 98 | ${dubbo-spring-boot.version} 99 | 100 | 101 | mysql 102 | mysql-connector-java 103 | ${mysql-connector-java.version} 104 | 105 | 106 | com.alibaba 107 | druid 108 | ${druid.version} 109 | 110 | 111 | io.shardingjdbc 112 | sharding-jdbc-core-spring-boot-starter 113 | ${sharding-jdbc.version} 114 | 115 | 116 | org.mybatis.spring.boot 117 | mybatis-spring-boot-starter 118 | ${mybatis-spring-boot.version} 119 | 120 | 121 | com.github.pagehelper 122 | pagehelper 123 | ${mybatis-pagehelper.version} 124 | 125 | 126 | 127 | 128 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------