1 Star 0 Fork 7

phatom/monolithic_arch_springboot

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

Fenix's BookStore后端:以单体架构实现

logo

Travis-CI Coverage Status License Document License About Author

如果你此时并不曾了解过什么是“The Fenix Project”,建议先阅读这部分内容

单体架构是Fenix's Bookstore'第一个版本的服务端实现,它与此后基于微服务(Spring Cloud、Kubernetes)、服务网格(Istio)、无服务(Serverless)架构风格实现的其他版本,在业务功能上的表现是完全一致的。如果你不是针对性地带着解决某个具体问题、了解某项具体工具、技术的目的而来,而是时间充裕,希望了解软件架构的全貌与发展的话,笔者推荐以此工程入手来了解现代软件架构,因为单体架构的结构是相对直观的,易于理解的架构,对后面接触的其他架构风格也起良好的铺垫作用。此外,笔者在对应的文档中详细分析了作为一个架构设计者,会考虑哪些的通用问题,希望把抽象的“架构”一词具象化出来。

运行程序

以下几种途径,可以运行程序,浏览最终的效果:

  • 通过Docker容器方式运行:
$ docker run -d -p 8080:8080 --name bookstore icyfenix/bookstore:monolithic 

然后在浏览器访问:http://localhost:8080,系统预置了一个用户(user:icyfenix,pw:123456),也可以注册新用户来测试。

默认会使用HSQLDB的内存模式作为数据库,并在系统启动时自动初始化好了Schema,完全开箱即用。但这同时也意味着当程序运行结束时,所有的数据都将不会被保留。

如果希望使用HSQLDB的文件模式,或者其他非嵌入式的独立的数据库支持的话,也是很简单的。以常用的MySQL/MariaDB为例,程序中也已内置了MySQL的表结构初始化脚本,你可以使用环境变量“PROFILES”来激活SpringBoot中针对MySQL所提供的配置,命令如下所示:

$ docker run -d -p 8080:8080 --name bookstore icyfenix/bookstore:monolithic -e PROFILES=mysql

此时你需要通过Docker link、Docker Compose或者直接在主机的Host文件中提供一个名为“mysql_lan”的DNS映射,使程序能顺利链接到数据库,关于数据库的更多配置,可参考源码中的application-mysql.yml

  • 通过Git上的源码,以Maven运行:
# 克隆获取源码
$ git clone https://github.com/fenixsoft/monolithic_arch_springboot.git

# 进入工程根目录
$ cd monolithic_arch_springboot

# 编译打包
# 采用Maven Wrapper,此方式只需要机器安装有JDK 8或以上版本即可,无需包括Maven在内的其他任何依赖
# 如在Windows下应使用mvnw.cmd package代替以下命令
$ ./mvnw package

# 运行程序,地址为localhost:8080
$ java -jar target/bookstore-1.0.0-Monolithic-SNAPSHOT.jar

然后在浏览器访问:http://localhost:8080,系统预置了一个用户(user:icyfenix,pw:123456),也可以注册新用户来测试。

  • 通过Git上的源码,在IDE环境中运行:
  • 以IntelliJ IDEA为例,Git克隆本项目后,在File -> Open菜单选择本项目所在的目录,或者pom.xml文件,以Maven方式导入工程。

  • IDEA将自动识别出这是一个SpringBoot工程,并定位启动入口为BookstoreApplication,待IDEA内置的Maven自动下载完所有的依赖包后,运行该类即可启动。

  • 如你使用其他的IDE,没有对SpringBoot的直接支持,亦可自行定位到BookstoreApplication,这是一个带有main()方法的Java类,运行即可。

  • 可通过IDEA的Maven面板中Lifecycle里面的package来对项目进行打包、发布。

  • 在IDE环境中修改配置(如数据库等)会更加简单,具体可以参考工程中application.yml和application-mysql.ylm中的内容。

技术组件

Fenix's BookStore单体架构后端尽可能采用标准的技术组件进行构建,不依赖与具体的实现,包括:

  • JSR 370:Java API for RESTful Web Services 2.1(JAX-RS 2.1)
    RESTFul服务方面,采用的实现为Jersey 2,亦可替换为Apache CXF、RESTeasy、WebSphere、WebLogic等

  • JSR 330:Dependency Injection for Java 1.0
    依赖注入方面,采用的的实现为SpringBoot 2中内置的Spring Framework 5。虽然在多数场合中尽可能地使用了JSR 330的标准注解,但仍有少量地方由于Spring在对@Named、@Inject等注解的支持表现上与本身提供的注解差异,使用了Spring的私有注解。如替换成其他的CDI实现,如HK2,需要较大的改动

  • JSR 338:Java Persistence 2.2
    持久化方面,采用的实现为Spring Data JPA。可替换为Batoo JPA、EclipseLink、OpenJPA等实现,只需将使用CrudRepository所省略的代码手动补全回来即可,无需其他改动。

  • JSR 380:Bean Validation 2.0
    数据验证方面,采用的实现为Hibernate Validator 6,可替换为Apache BVal等其他验证框架

  • JSR 315:Java Servlet 3.0
    Web访问方面,采用的实现为SpringBoot 2中默认的Tomcat 9 Embed,可替换为Jetty、Undertow等其他Web服务器

有以下组件仍然依赖了非标准化的技术实现,包括:

  • JSR 375:Java EE Security API specification 1.0
    认证/授权方面,在2017年才发布的JSR 375中仍然没有直接包含OAuth2和JWT的直接支持,因后续实现微服务架构时对比的需要,单体架构中选择了Spring Security 5作为认证服务,Spring Security OAuth 2.3作为授权服务,Spring Security JWT作为JWT令牌支持,并未采用标准的JSR 375实现,如Soteria。

  • JSR 353/367:Java API for JSON Processing/Binding
    在JSON序列化/反序列化方面,由于Spring Security OAuth的限制(使用JSON-B作为反序列化器时的结果与Jackson等有差异),采用了Spring Security OAuth默认的Jackson,并未采用标准的JSR 353/367实现,如Apache Johnzon、Eclipse Yasson等。

工程结构

Fenix's BookStore单体架构后端参考(并未完全遵循)了DDD的分层模式和设计原则,整体分为以下四层:

  1. Resource:对应DDD中的User Interface层,负责向用户显示信息或者解释用户发出的命令。请注意,这里指的“用户”不一定是使用用户界面的人,可以是位于另一个进程或计算机的服务。由于本工程采用了MVVM前后端分离模式,这里所指的用户实际上是前端的服务消费者,所以这里以RESTFul中的核心概念”资源“(Resource)来命名。
  2. Application:对应DDD中的Application层,负责定义软件本身对外暴露的能力,即软件本身可以完成哪些任务,并负责对内协调领域对象来解决问题。根据DDD的原则,应用层要尽量简单,不包含任何业务规则或者知识,而只为下一层中的领域对象协调任务,分配工作,使它们互相协作,这一点在代码上表现为Application层中一般不会存在任何的条件判断语句。在许多项目中,Application层都会被选为包裹事务(代码进入此层事务开始,退出此层事务提交或者回滚)的载体。
  3. Domain:对应DDD中的Domain层,负责实现业务逻辑,即表达业务概念,处理业务状态信息以及业务规则这些行为,此层是整个项目的重点。
  4. Infrastructure:对应DDD中的Infrastructure层,向其他层提供通用的技术能力,譬如持久化能力、远程服务通讯、工具集,等等。

协议

  • 本文档代码部分采用Apache 2.0协议进行许可。遵循许可的前提下,你可以自由地对代码进行修改,再发布,可以将代码用作商业用途。但要求你:

    • 署名:在原有代码和衍生代码中,保留原作者署名及代码来源信息。
    • 保留许可证:在原有代码和衍生代码中,保留Apache 2.0协议文件。
  • 本作品文档部分采用知识共享署名 4.0 国际许可协议进行许可。 遵循许可的前提下,你可以自由地共享,包括在任何媒介上以任何形式复制、发行本作品,亦可以自由地演绎、修改、转换或以本作品为基础进行二次创作。但要求你:

    • 署名:应在使用本文档的全部或部分内容时候,注明原作者及来源信息。
    • 非商业性使用:不得用于商业出版或其他任何带有商业性质的行为。如需商业使用,请联系作者。
    • 相同方式共享的条件:在本文档基础上演绎、修改的作品,应当继续以知识共享署名 4.0国际许可协议进行许可。
Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

周志明凤凰架构源码-单体架构 展开 收起
README
Apache-2.0
取消

发行版

暂无发行版

贡献者 (3)

全部

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/phatom/monolithic_arch_springboot.git
git@gitee.com:phatom/monolithic_arch_springboot.git
phatom
monolithic_arch_springboot
monolithic_arch_springboot
master

搜索帮助