{"id":13630909,"url":"https://github.com/chaokunyang/microservices-event-sourcing","last_synced_at":"2025-04-04T14:06:33.461Z","repository":{"id":59983806,"uuid":"84427866","full_name":"chaokunyang/microservices-event-sourcing","owner":"chaokunyang","description":"Microservices Event Sourcing 是一个微服务架构的在线购物网站，使用Spring Boot、Spring Cloud、Spring Reactor、OAuth2、CQRS 构建，实现了基于Event Sourcing的最终一致性，提供了构建端到端微服务的最佳实践","archived":false,"fork":false,"pushed_at":"2017-05-19T00:52:59.000Z","size":6465,"stargazers_count":725,"open_issues_count":12,"forks_count":331,"subscribers_count":108,"default_branch":"master","last_synced_at":"2025-03-28T13:07:19.458Z","etag":null,"topics":["angularjs","ecommerce","event-driven","event-sourcing","eventually-consistent","microservice","mongodb","neo4j","oauth2","spring-boot","springcloud"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/chaokunyang.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-03-09T10:13:00.000Z","updated_at":"2025-02-21T10:41:04.000Z","dependencies_parsed_at":"2022-09-23T14:39:39.983Z","dependency_job_id":null,"html_url":"https://github.com/chaokunyang/microservices-event-sourcing","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chaokunyang%2Fmicroservices-event-sourcing","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chaokunyang%2Fmicroservices-event-sourcing/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chaokunyang%2Fmicroservices-event-sourcing/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chaokunyang%2Fmicroservices-event-sourcing/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chaokunyang","download_url":"https://codeload.github.com/chaokunyang/microservices-event-sourcing/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247190231,"owners_count":20898700,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["angularjs","ecommerce","event-driven","event-sourcing","eventually-consistent","microservice","mongodb","neo4j","oauth2","spring-boot","springcloud"],"created_at":"2024-08-01T22:02:02.834Z","updated_at":"2025-04-04T14:06:33.445Z","avatar_url":"https://github.com/chaokunyang.png","language":"Java","funding_links":[],"categories":["Java","springcloud"],"sub_categories":[],"readme":"# Microservices Event Sourcing\n\n## 一、简介\n\nMicroservices Event Sourcing 是一个微服务架构的在线购物平台，使用Spring Boot、Spring Cloud、Spring Reactor、OAuth2、CQRS 构建，实现了基于Event Sourcing的最终一致性，提供了构建端到端微服务的最佳实践。\n\n#### 微服务优点\n微服务是最近几年很流行的一种架构模式，相比其他架构模式有着诸多优点。如：\n* 每个微服务很小\n    * 易于开发者理解，上手快，易开发、易重构\n    * IDE更快，使得开发更具生产力\n    * 启动速度更快，也使得开发更具生产力，同时加快应用部署\n* 每个服务可以独立部署，从而更容易频繁部署新版本的服务\n* 容易扩大开发规模。微服务使你能够跨团队开发。每个团队服务自己的一个或多个单一服务。每个团队可以独立于其他团队开发、部署、伸缩他们的服务\n* 改善故障隔离。比如，如果一个服务内存泄漏，那么只有这个服务被影响，其它服务仍然能够继续处理请求。而在单体架构中，一个不工作的组件可能拖垮整个系统\n* 每个服务可以独立开发和部署，易于持续集成/交付\n* 避免长期绑定到一个技术栈。当开发一个新的服务时，你可以使用一个新的技术栈。类似地，当对一个已有服务做出大的改动时，你可以使用一种新的技术栈轻松重写它。\n\n#### 微服务缺点\n微服务不是银弹，它也有着自己的缺点：\n* 一个分布式系统天然的复杂性\n    * 测试更加困难\n    * 开发者必须实现服务通信机制\n    * 数据的一致性问题\n* 部署复杂性。在生产环境中，还存在着部署和管理一个由很多不同类型服务组成的系统的运维复杂性\n* 中小规模下增加的资源消耗。在中小规模下，微服务比单体应用消耗更多资源。在大规模环境下，微服务凭借其独立部署的优势能够比单体应用有更好的资源利用率。\n\n#### 微服务模式\n业界对于微服务开发已经有了一套实践模式，如：\n* 服务注册\n* 服务发现\n* 负载均衡\n* 配置管理\n* 断路器\n* API网关\n* 每个服务一个数据库\n* 消息驱动的微服务\n* 事件源\n* CQRS\n* 访问令牌\n* 消费者驱动的契约测试\n* 日志聚合\n* 应用监控\n* 审计日志\n* 分布式追踪\n* 异常追踪\n* 健康监测\n* 每个服务一个容器\n\n这些微服务模式中的大多数都已经被Spring Cloud(套件)所实现，且开箱即用。但有一些需要经过一番努力才能够实现，如基于事件源实现最终一致性，命令查询职责分离(CQRS)，消息驱动的微服务。而这些模式对于微服务能否成功落地至关重要。因此，[Microservices Event Sourcing](https://github.com/chaokunyang/microservices-event-sourcing)项目在提供构建端到端微服务的最佳实践的同时，主要关注于基于事件源实现最终一致性。我将在接下来分别构建基于CQRS的微服务，和消息驱动的微服务。\n\n#### Microservices Event Sourcing架构\n整个系统分为三层：\n* web层：在线商店Web，负责请求和UI\n* backing层：服务发现、边缘服务、配置服务、用户服务、监控服务\n* backend层：目录服务、库存服务、账户服务、购物车服务、订单服务\n\n包含以下微服务.：\n* Config Service\n* Discovery Service\n* Edge Service\n* User Service\n* Catalog Service\n* Account Service\n* Order Service\n* Inventory Service\n* Shopping Cart Service\n* Online Store Web\n* Hystrix Dashboard\n\n#### 数据库\n微服务架构通常使用多个数据库。业务领域的资源分布在整个微服务架构中，每个微服务有它单独的数据库。开发团队通常根据数据库在解决特定问题时其优势选择合适类型的数据库。\n\nMicroservices Event Sourcing使用以下数据库类型.\n* MySQL - RDBMS\n* Neo4j - GraphDB\n* MongoDB - Document Store\n* Redis - Key/value Store\n\n即使是同种类型的数据库，每个服务也应该使用单独的数据库，这是微服务的基本模式之一，能够避免引起数据库层面的耦合，也易于维护、扩展、伸缩.\n\n## 二、Getting Started\n\n1、分别为user-service、account-service、catalog-service、shopping-cart-service创建单独的mysql数据库与用户\n``` sql\nCREATE  DATABASE time_store_user CHARACTER SET  utf8  COLLATE utf8_general_ci;\ngrant all privileges on time_store_user.* to 'time'@'localhost' identified by '123456';\ngrant all privileges on time_store_user.* to 'time'@'127.0.0.1' identified by '123456';\ngrant all privileges on time_store_user.* to 'time'@'::1' identified by '123456';\nflush privileges;\n```\n``` sql\nCREATE  DATABASE time_store_account CHARACTER SET  utf8  COLLATE utf8_general_ci;\ngrant all privileges on time_store_account.* to 'time'@'localhost' identified by '123456';\ngrant all privileges on time_store_account.* to 'time'@'127.0.0.1' identified by '123456';\ngrant all privileges on time_store_account.* to 'time'@'::1' identified by '123456';\nflush privileges;\n```\n``` sql\nCREATE  DATABASE time_store_catalog CHARACTER SET  utf8  COLLATE utf8_general_ci;\ngrant all privileges on time_store_catalog.* to 'time'@'localhost' identified by '123456';\ngrant all privileges on time_store_catalog.* to 'time'@'127.0.0.1' identified by '123456';\ngrant all privileges on time_store_catalog.* to 'time'@'::1' identified by '123456';\nflush privileges;\n```\n``` sql\nCREATE  DATABASE time_store_cart CHARACTER SET  utf8  COLLATE utf8_general_ci;\ngrant all privileges on time_store_cart.* to 'time'@'localhost' identified by '123456';\ngrant all privileges on time_store_cart.* to 'time'@'127.0.0.1' identified by '123456';\ngrant all privileges on time_store_cart.* to 'time'@'::1' identified by '123456';\nflush privileges;\n```\n\n2、为inventory-service安装neo4j 2.3，启动neo4j并修改密码为secret\n``` shell\ncurl -v -u neo4j:neo4j -X POST localhost:7474/user/neo4j/password -H \"Content-type:application/json\" -d \"{\\\"password\\\":\\\"secret\\\"}\"\n```\n3、为order-service安装mongo。在 localhost:27017上启动mongo\n\n4、依次启动以下微服务\n* Discovery Service\n* Edge Service\n* User Service\n* Catalog Service\n* Account Service\n* Order Service\n* Inventory Service\n* Shopping Cart Service\n* Online Store Web\n\n为了节省内存，以下服务可以不启动，它们对于业务开发没有很大影响\n* Hystrix Dashboard\n* Config Service\n\n当所有服务启动完成后，验证服务已经成功注册到[Eureka](http://localhost:8761)。\n\n如果每个服务成功加载，浏览[在线购物网站首页](http://localhost:8787)。点击登录，你将被重定向到Oauth2.0授权服务器，即[user-service](http://localhost:8181/auth/login)。用户名是 user，密码是 password。你将被认证和请求允许对在线购物网站进行令牌授权。在完成授权后，你将被重定向到在线购物网站，然后便可以访问来自边缘服务的受保护资源。\n\n## 三、预览\n* 首页未登录\n![首页未登录](https://raw.githubusercontent.com/chaokunyang/microservices-event-sourcing/master/docs/images/01-home-not-logged.png)\n* OAuth2.0登录\n![OAuth2.0登录](https://github.com/chaokunyang/microservices-event-sourcing/raw/master/docs/images/02-oauth2.0-login.png)\n* Oauth2.0授权\n![Oauth2.0授权](https://github.com/chaokunyang/microservices-event-sourcing/raw/master/docs/images/03-oauth2.0-grant.png)\n* 首页已登录\n![首页已登录](https://github.com/chaokunyang/microservices-event-sourcing/raw/master/docs/images/04-home-logged.png)\n* 商品详情\n![商品详情](https://github.com/chaokunyang/microservices-event-sourcing/raw/master/docs/images/05-product-detail.jpg)\n* 购物车\n![购物车](https://github.com/chaokunyang/microservices-event-sourcing/raw/master/docs/images/06-shopping-cart.png)\n* 订单列表\n![订单列表](https://github.com/chaokunyang/microservices-event-sourcing/raw/master/docs/images/07-orders.png)\n* 订单详情\n![订单详情](https://github.com/chaokunyang/microservices-event-sourcing/raw/master/docs/images/08-order.jpg)\n* 用户设置\n![订单详情](https://github.com/chaokunyang/microservices-event-sourcing/raw/master/docs/images/09-user-settings.jpg)\n\n## 四、License\n\nThis project is licensed under Apache License 2.0.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchaokunyang%2Fmicroservices-event-sourcing","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchaokunyang%2Fmicroservices-event-sourcing","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchaokunyang%2Fmicroservices-event-sourcing/lists"}