从零搭建微服务项目Pro(第0章——微服务项目脚手架搭建)

前言:

        在本专栏Base第0章曾介绍一种入门级的微服务项目搭建,尽管后续基于此框架上实现了Nacos、Eureka服务注册发现、配置管理、Feign调用、网关模块、OSS文件存储、JSR参数校验、LogBack日志配置,鉴权模块、定时任务模块等,但由于之前的博客更侧重于功能的实现而非整体架构的和谐统一,随着模块和功能的增多,原先框架设计层面上的不足也逐渐暴露出来,代码层面比如实体类存放位置、Exception处理方式难以统一,各层级耦合度高等,依赖方面如循环依赖、依赖冲突等问题也时有发生。因此,笔者通过参考实习公司代码,使用企业级的脚手架搭建方式,最终重构了原有项目,并实现了绝大部分功能。本章将结合代码以及一些个人见解具体介绍,并会介绍一些微服务相关知识帮助快速上手。

        最终实现完整项目代码链接如下,后续会逐步迭代并最终完成完整的后端代码,(可能还会包括Vue网页端和OpenHarmony手机端的前端代码),以及详尽的配置文档方便快速入手以及设计文档方便快速了解业务,欢迎Star和Follow。

wlf728050719/BitGoPlushttps://github.com/wlf728050719/BitGoPlus如果没有接触过微服务项目或者对上述功能模块感兴趣的小伙伴可收藏下面专栏目录链接,专栏主要内容为笔者从0接触微服务的全部学习心得和记录。以及建议按时间顺序看循序渐进。

从零搭建微服务项目(全)-CSDN博客https://blog.csdn.net/wlf2030/article/details/145799620?spm=1001.2014.3001.5501


一、前置知识:

为降低本章门槛还是做一些前置知识讲解。如果有一定微服务项目开发基础这部分就可以直接跳过。以及笔者有误的地方也欢迎评论区指出。

1.前端、后端、数据库

前端(客户端):B/S架构下前端主要指网页,C/S架构下主要指手机或电脑上的应用程序,当然也有微信小程序这种混合架构下的前端,前端主要用于和用户交互,并将用户指定通过http请求发送给后端。并展示后端返回数据

后端(服务器):接收前端请求,处理业务逻辑(如验证、计算),并与数据库交互,并将数据作为响应返回前端。

数据库:结构化存储数据(如用户信息、订单记录)。

这里拿BS架构举例,当搜索一次4399后按f12抓包能够看见其发起了一次http请求,请求地址是www.baidu.com以及将输入的值4399和编码格式utf8以及上次搜索的值43999一并发送(请求)给了对应域名的服务器,并成功获取了其返回的数据xml形式数据(响应)。

(注意不是有服务器就是后端,笔者大二学数据库时曾以为安装了数据库的云服务器就是后端,做的课设实际前端既负责处理远程服务器传来的数据又展示界面,当时信誓旦旦的和老师说是CS架构项目,现在看来可能叫CD结构更合适)

2.后端分层架构

(学习过Servlet开发或者Spring MVC/Spring Boot的小伙伴应该对这张图不会陌生。个人所有编程语言最喜欢Java很大程度上在于它把分层解耦的思想发挥到了极致。)

后端按照各自负责功能划分为五层,可以选择将Manager层合并到Service层,笔者更倾向五层架构。图中默认上层依赖于下层,箭头关系表示可直接依赖,如:开放接口层可以依赖于 Web 层,也可以直接依赖于 Service 层,依此类推:

开放接口层:可直接封装 Service 方法暴露成 RPC 接口;通过 Web 封装成 http 接口;网关控制层等。
终端显示层:各个端的模板渲染并执行显示的层。当前主要是 velocity 渲染,JS 渲染,JSP 渲染,移动端展示等。
Web 层:主要是对访问控制进行转发,各类基本参数校验,或者不复用的业务简单处理等。
Service 层:相对具体的业务逻辑服务层。
Manager 层:通用业务处理层,它有如下特征:
(1)对第三方平台封装的层,预处理返回结果及转化异常信息。
(2)对 Service 层通用能力的下沉,如缓存方案、中间件通用处理。
(3)与 DAO 层交互,对多个 DAO 的组合复用。
DAO 层:数据访问层,与底层 MySQL、Oracle、Hbase、OB 等进行数据交互。
外部接口或第三方平台:包括其它部门 RPC 开放接口,基础平台,其它公司的 HTTP 接口。
 
拿项目代码举例:
PermissionMapper属于DAO层,没接触过mybatis的小伙伴也没关系,只需要知道这个类的这个方法是执行下面的sql并返回拿到的数据即可。

UserManagerImpl属于Manager层,其作用在于对插入数据库的主键进行处理,而非使用数据库生成主键。(个人倾向五层架构的原因也在于如果使用了mybatis plus,在遇到需要对与数据库直接交互数据特殊处理时,放dto中需要手写本来已经实现的crud操作费时费力,放service中与其他业务函数相比比如register、login忽然出现一个insert,update显得不伦不类)

UserServiceImpl属于Service层,能够看到其一个业务函数中使用了多个Manager层数据对象,实现了功能的复用。

UserController属于Web层,其将service层提供的功能封装为一个个接口,供前端访问。比如登录功能,前端只需要发送https://ip地址:port号/user/register并附带上注册所需要的数据即可在数据库中新增一个用户。

终端显示层负责将后端返回的数据(如JSON、XML)转换为用户可理解的界面(如表格、图表、文字),其实理解为前端也行,但一般都采用前后端分离的方式,所以一般不会直接在后端返回一个html页面。

3.微服务架构

服务调用:

微服务架构是一种 将单体应用拆分为多个小型、独立服务 的架构风格,每个服务负责不同的业务功能,比如A服务负责用户,B服务负责订单,各个服务内部可以相互调用,比如订单表中只有用户id,但订单服务需要返回用户完整信息,此时B服务就需要远程调用(RPC)A服务,传给它用户id,并拿取其返回信息拼装返回给前端。

服务调用可参考下面链接:从零搭建微服务项目Base(第1章——微服务模块间调用接口)_搭建一个微服务项目-CSDN博客

从零搭建微服务项目Base(第5章——SpringBoot项目LogBack日志配置+Feign使用)_springboot feign 日志-CSDN博客

从零搭建微服务项目Base(第6章——Feign性能优化以及模块抽取)-CSDN博客

服务注册、网关路由:

当服务从单体拆分成多个后,前端人员编写接口时需要把哪个服务对应哪个ip端口写入程序中,然后每次发请求均需要查询对应接口,这显然不切实际,一旦接口增加,岂不是用户还得升级以下前端配置,注册中心加网关即可很好解决这个问题,每个服务启动时,将自己的服务名和ip端口注册到注册中心,前端只需要记住网关服务的地址即可,所有请求都发向网关,网关中配置哪些请求路径匹配哪些服务,并从注册中心拿到这些服务的地址,网关进行转发即可。同时注册中心会告诉网关哪些服务实例挂了,或者哪些服务现在很忙,通过负载均衡决定到底路由到哪个服务。

服务注册可参考下面链接:

从零搭建微服务项目Base(第2章——Eureka服务注册和发现)_微服务开发实例 服务发现与注册-CSDN博客

从零搭建微服务项目Base(第3章——Nacos服务注册和发现)_nacos服务从零搭建-CSDN博客

网关路由可参考下面链接:

从零搭建微服务项目Base(第7章——微服务网关模块基础实现)-CSDN博客

鉴权:

但此时又出现一个问题,要是我知道你某个服务真实地址,我直接访问它不过你的网关,我直接访问敏感接口猛猛爬用户数据该怎么办,此时就需要为每个服务引入鉴权,你绕过我网关没关系,但你只要访问我的敏感接口就必须提供身份证明(jwt生成的token),同时为了让其他服务能够正常相互调用也需要进一步处理,这里不再细讲。

鉴权可参考下面链接:

从零搭建微服务项目Pro(第6-1章——Spring Security+JWT实现用户鉴权访问与token刷新)_微服务springboot+security+jwt实现刷新token-CSDN博客

配置管理:

既然服务名和ip对应关系已经存放在注册中心,不如多存点,将各服务配置也同样存储,这样就不用将配置也打包,否则一旦配置发生变化岂不是还得重新打包上线。因此nacos,eureka等(注册中心)顺便也做了配置管理。

配置管理可参考下面链接:

从零搭建微服务项目Base(第4章——Nacos环境隔离和配置拉取)_微服务项目搭建 nacos-CSDN博客

分库分表:

既然服务都拆分了,那自然数据库也得拆,否则相当于把热点从服务器转移到了数据库上。分库显然能够减轻单个数据库压力,那分表呢?垂直分表将热点数据集中减小每次数据吞吐量,水平分布则能通过主键id定位使用哪张表,如果分了100张表,则单表的数据量除以100,查询表的速率显然会涨一大截。当然由于分布式系统加分表后如何确保主键唯一以及具体如何使用也需要解决。

分库分表可参考下面链接:

从零搭建微服务项目Pro(第7-1章——分布式雪花算法)_如果当前时间戳对于用例来说是异常的,那么深入到指标,找出时间戳中有高度异-CSDN博客

从零搭建微服务项目Pro(第8-1章——Sharding-Jdbc+MybatisPlus)_mybatis-plus 整合 sharding-jdbc-CSDN博客

缓存:

学习过计组的小伙伴都知道cache的速度远大于磁盘,当然此cache非彼cache,单拿最常用的缓存redis举例,其会将数据以key-value的形式存储,搜索效率远快于数据库逐条查询,因此一般常将不那么频繁变动的数据存在缓存,后端优先使用缓存数据。当然如果sql数据变动一定要及时更改缓存,否则就会出现经典面试题目,数据库和缓存不一致怎么办(延迟双删)。

缓存可参考下面链接:

(-----------------------占位符----------------------还没做------------------------后续更新------------------------)


二、代码约定:

对于代码的某种实现,不同时间可能会有不同的做法,为了统一代码,做了以下约定(其实只是单纯怕自己忘记变成左右手互博),该部分内容在项目readme.md中,会随着项目推进不断迭代(可能会吃书,仅供参考)

(1)表现层业务成功返回R.ok,可预见错误返回R.failed。用户异常操作时(如未授权访问/参数校验错误)均抛出异常(前后端统一)
(2)返回R中不带额外数据时,使用R<Boolean>,需要明确设置true,失败使用false(前后端统一)
(3)除了业务异常以及继承类msg使用中文,其余使用英文(用户友好)
(4)持久层类均与PO或DictItem结尾,对应同名Manager类(统一命名规范)
(5)Manager层负责可复用业务,Service层实现具体业务,原则上每个服务只能有一个service,且其函数与对应接口同名(分层解耦)
(6)所有接口传入对象只能为dto中定义对象,且只有dto中对象添加jsr校验(使参数校验部分统一由Controller负责)
(7)所有dto对象转po对象方法必须定义在dto对象中,且方法固定为newXX明确为两个不同对象(使po专注于与数据库交互)
(8)manager层不对mapper的异常进行特殊处理,抛出统一捕获(便于Service层回滚)
(9)每个服务RPC接口均命名为APIController且统一以/api路径开头(安全配置开放为内部端点,统一鉴权)
(10)通过@Cacheable注解value为命名空间,统一使用KeyGenerator进行管理。使用RedisTemplate则需要统一使用定义在RedisKey文件中的String.format/String形式(防止魔法值,且相同类型缓存使用类似键生成方式方便查询)
(11)所有service层涉及操作manager的方法均需要添加@Transactional(防止数据库脏数据)
(12)各模块内部exception定义在各自模块中,继承BizException或SysException(方便ExceptionHandler统一处理)
(13) 所有实体类必须定义在common-core模块中(否则容易出现循环依赖问题)


三、项目结构

.sql存储建库sql文件

.style存储代码风格规范文件(可参考下面链接)Java静态代码分析工具安装及使用(FindBugs、SpotBugs、SonarQube)以及使用CheckStyle规范阿里代码风格_spotbugs怎么用-CSDN博客

.yaml存储放在nacos上的配置文件,方便配置文件版本管理

auth-service鉴权服务模块

common公共模块定义

common-bom约定依赖版本

common-cache缓存相关模块

common-core存放所有模块pojo,异常声明处理,以及jsr参数校验(可参考下面链接)

从零搭建微服务项目Pro(第2-1章——JSR303自定义参数校验+异常处理)-CSDN博客

从零搭建微服务项目Pro(第2-2章——JSR303自定义文件校验+整合至微服务公共模块)-CSDN博客

common-data数据库模块,上文提到分库分表在此配置

common-feign服务调用模块,上文提到服务调用在此配置

common-security鉴权模块,上文提到鉴权在此配置

gateway网关模块,上文提到网关模块在此配置

order-service订单服务

task-service定时任务服务(可参考下面链接)

从零搭建微服务项目Pro(第1-1章——Quartz实现定时任务模块)_微服务如何处理定时任务-CSDN博客

从零搭建微服务项目Pro(第1-2章——Quartz实现定时任务模块优化)_springcloud quartz模块-CSDN博客

从零搭建微服务项目Pro(第1-3章——Quartz定时任务模块整合)_创建quarz服务和原有服务的交互逻辑-CSDN博客

user-service用户服务


四、Pom文件

虽然此处会展示,但仍然建议在项目文件中查看。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>cn.bit</groupId><artifactId>BitGoPlus</artifactId><version>1.0.0</version><name>${project.artifactId}</name><packaging>pom</packaging><properties><spring-boot.version>2.3.9.RELEASE</spring-boot.version><spring-cloud.version>2.2.5.RELEASE</spring-cloud.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target><maven.compiler.version>3.8.1</maven.compiler.version><checkstyle.skip>false</checkstyle.skip></properties><modules><module>user-service</module><module>order-service</module><module>common</module><module>gateway</module><module>task-service</module><module>auth-service</module></modules><dependencyManagement><dependencies><!-- 公共版本定义 --><dependency><groupId>cn.bit</groupId><artifactId>common-bom</artifactId><version>${project.version}</version><type>pom</type><scope>import</scope></dependency><!-- spring boot --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>${spring-boot.version}</version><type>pom</type><scope>import</scope></dependency><!--spring cloud--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><dependencies><!--lombok--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><scope>compile</scope><version>1.18.20</version><optional>true</optional></dependency></dependencies><build><resources><!--允许src/main/resources下配置文件读取pom中配置--><resource><directory>src/main/resources</directory><filtering>true</filtering></resource></resources><pluginManagement><plugins><!--checkstyle插件--><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-checkstyle-plugin</artifactId><version>3.6.0</version><dependencies><dependency><groupId>com.puppycrawl.tools</groupId><artifactId>checkstyle</artifactId><version>9.3</version></dependency></dependencies><configuration><outputEncoding>UTF-8</outputEncoding><skip>${checkstyle.skip}</skip><configLocation>.style/alibaba.xml</configLocation><consoleOutput>true</consoleOutput><failsOnError>false</failsOnError><linkXRef>false</linkXRef><includeTestSourceDirectory>false</includeTestSourceDirectory><sourceDirectories>${project.build.sourceDirectory}</sourceDirectories></configuration><executions><execution><id>validate</id><phase>validate</phase><goals><goal>check</goal></goals></execution></executions></plugin><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>${spring-boot.version}</version><executions><execution><goals><goal>repackage</goal></goals></execution></executions></plugin></plugins></pluginManagement><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>${maven.compiler.version}</version><configuration><target>${maven.compiler.target}</target><source>${maven.compiler.source}</source><encoding>UTF-8</encoding><skip>true</skip></configuration></plugin></plugins></build><!--多环境配置--><profiles><profile><id>dev</id><properties><profile.active>dev</profile.active><nacos.username>nacos</nacos.username><nacos.password>nacos</nacos.password><nacos.server-addr>192.168.200.1:8848</nacos.server-addr><nacos.cluster-name>BJ</nacos.cluster-name><nacos.namespace>5dadfe94-237d-4367-998e-b13d172ddcfc</nacos.namespace><profiles.auth-service.port>1232</profiles.auth-service.port><profiles.gateway.port>1233</profiles.gateway.port><profiles.user-service.port>1234</profiles.user-service.port><profiles.order-service.port>1235</profiles.order-service.port><profiles.quartz-service.port>1236</profiles.quartz-service.port></properties></profile><profile><id>test</id><properties><profile.active>test</profile.active><nacos.username>nacos</nacos.username><nacos.password>nacos</nacos.password><nacos.server-addr>192.168.200.1:8848</nacos.server-addr><nacos.cluster-name>BJ</nacos.cluster-name><nacos.namespace>5dadfe94-237d-4367-998e-b13d172ddcfc</nacos.namespace><profiles.auth-service.port>2232</profiles.auth-service.port><profiles.gateway.port>2233</profiles.gateway.port><profiles.user-service.port>2234</profiles.user-service.port><profiles.order-service.port>2235</profiles.order-service.port><profiles.quartz-service.port>2236</profiles.quartz-service.port></properties></profile><profile><id>prod</id><properties><profile.active>prod</profile.active><nacos.username>nacos</nacos.username><nacos.password>nacos</nacos.password><nacos.server-addr>192.168.200.1:8848</nacos.server-addr><nacos.cluster-name>BJ</nacos.cluster-name><nacos.namespace>5dadfe94-237d-4367-998e-b13d172ddcfc</nacos.namespace><profiles.auth-service.port>3232</profiles.auth-service.port><profiles.gateway.port>3233</profiles.gateway.port><profiles.user-service.port>3234</profiles.user-service.port><profiles.order-service.port>3235</profiles.order-service.port><profiles.quartz-service.port>3236</profiles.quartz-service.port></properties></profile></profiles></project>

common

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>cn.bit</groupId><artifactId>BitGoPlus</artifactId><version>1.0.0</version></parent><artifactId>common</artifactId><packaging>pom</packaging><name>common</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><modules><module>common-bom</module><module>common-core</module><module>common-cache</module><module>common-feign</module><module>common-data</module><module>common-security</module></modules>
</project>

common-bom

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>cn.bit</groupId><artifactId>common-bom</artifactId><version>1.0.0</version><packaging>pom</packaging><name>common-bom</name><url>http://maven.apache.org</url><properties><bitgo.version>1.0.0</bitgo.version><mybatis-plus.version>3.5.1</mybatis-plus.version><sharding-jdbc.version>4.1.1</sharding-jdbc.version><jjwt.version>0.9.1</jjwt.version><jaxb.version>2.3.1</jaxb.version><mysql.connector.version>8.0.28</mysql.connector.version><openfeign.version>2.2.5.RELEASE</openfeign.version><gateway.version>2.2.5.RELEASE</gateway.version></properties><dependencyManagement><dependencies><!-- jwt --><dependency><groupId>io.jsonwebtoken</groupId><artifactId>jjwt</artifactId><version>${jjwt.version}</version></dependency><dependency><groupId>javax.xml.bind</groupId><artifactId>jaxb-api</artifactId><version>${jaxb.version}</version></dependency><dependency><groupId>org.glassfish.jaxb</groupId><artifactId>jaxb-runtime</artifactId><version>${jaxb.version}</version></dependency><!-- sharding jdbc --><dependency><groupId>org.apache.shardingsphere</groupId><artifactId>sharding-jdbc-spring-boot-starter</artifactId><version>${sharding-jdbc.version}</version></dependency><!-- mybatis plus --><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>${mybatis-plus.version}</version></dependency><!-- mysql --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>${mysql.connector.version}</version></dependency><!-- open feign --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId><version>${openfeign.version}</version></dependency><!-- gateway --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId><version>${gateway.version}</version></dependency><!-- =============内部依赖版本号============== --><dependency><groupId>cn.bit</groupId><artifactId>common-core</artifactId><version>${bitgo.version}</version></dependency><dependency><groupId>cn.bit</groupId><artifactId>common-cache</artifactId><version>${bitgo.version}</version></dependency><dependency><groupId>cn.bit</groupId><artifactId>common-feign</artifactId><version>${bitgo.version}</version></dependency><dependency><groupId>cn.bit</groupId><artifactId>common-data</artifactId><version>${bitgo.version}</version></dependency><dependency><groupId>cn.bit</groupId><artifactId>common-security</artifactId><version>${bitgo.version}</version></dependency></dependencies></dependencyManagement>
</project>

common-cache

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>cn.bit</groupId><artifactId>common</artifactId><version>1.0.0</version></parent><artifactId>common-cache</artifactId><packaging>jar</packaging><name>common-redis</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><!-- redis --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><!-- cache --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId></dependency></dependencies>
</project>

common-core

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>cn.bit</groupId><artifactId>common</artifactId><version>1.0.0</version></parent><artifactId>common-core</artifactId><packaging>jar</packaging><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><!-- validation --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-validation</artifactId></dependency><!-- web --><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId></dependency><!-- mybatis plus --><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId></dependency><!-- security --><dependency><groupId>org.springframework.security</groupId><artifactId>spring-security-core</artifactId></dependency></dependencies>
</project>

common-data

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>cn.bit</groupId><artifactId>common</artifactId><version>1.0.0</version></parent><artifactId>common-data</artifactId><packaging>jar</packaging><name>common-mybatis</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><!-- mybatis plus --><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId></dependency><!-- =======内部依赖======= --><!-- common cache --><dependency><groupId>cn.bit</groupId><artifactId>common-cache</artifactId></dependency></dependencies>
</project>

common-feign

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>cn.bit</groupId><artifactId>common</artifactId><version>1.0.0</version></parent><artifactId>common-feign</artifactId><packaging>jar</packaging><name>common-feign</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><!-- web --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- open feign --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><!-- =======内部依赖======= --><!-- common-core --><dependency><groupId>cn.bit</groupId><artifactId>common-core</artifactId></dependency></dependencies>
</project>

common-security

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>cn.bit</groupId><artifactId>common</artifactId><version>1.0.0</version></parent><artifactId>common-security</artifactId><packaging>jar</packaging><name>common-security</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><!-- security --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency><!-- web --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- jwt --><dependency><groupId>io.jsonwebtoken</groupId><artifactId>jjwt</artifactId></dependency><dependency><groupId>javax.xml.bind</groupId><artifactId>jaxb-api</artifactId></dependency><dependency><groupId>org.glassfish.jaxb</groupId><artifactId>jaxb-runtime</artifactId></dependency><!-- =======内部依赖======= --><!-- common-core --><dependency><groupId>cn.bit</groupId><artifactId>common-core</artifactId></dependency><!-- common-feign --><dependency><groupId>cn.bit</groupId><artifactId>common-feign</artifactId></dependency><!-- =======内部依赖======= --></dependencies>
</project>

gateway

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>cn.bit</groupId><artifactId>BitGoPlus</artifactId><version>1.0.0</version></parent><artifactId>gateway</artifactId><name>gateway</name><description>gateway</description><dependencies><!-- gateway --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId></dependency><!-- =======公共依赖======= --><!-- nacos discovery --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><!-- nacos config --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId></dependency><!-- =======内部依赖======= --></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

order-service

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>cn.bit</groupId><artifactId>BitGoPlus</artifactId><version>1.0.0</version></parent><artifactId>order-service</artifactId><name>order-service</name><description>order-service</description><dependencies><!-- =======公共依赖======= --><!-- web --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- nacos discovery --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><!-- nacos config --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId></dependency><!-- open feign --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><!-- sharding jdbc --><dependency><groupId>org.apache.shardingsphere</groupId><artifactId>sharding-jdbc-spring-boot-starter</artifactId></dependency><!-- mysql --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><!-- mybatis plus --><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId></dependency><!-- =======内部依赖======= --><!-- common-core --><dependency><groupId>cn.bit</groupId><artifactId>common-core</artifactId></dependency><!-- common-data --><dependency><groupId>cn.bit</groupId><artifactId>common-data</artifactId></dependency><!-- common-cache --><dependency><groupId>cn.bit</groupId><artifactId>common-cache</artifactId></dependency><!-- common-feign --><dependency><groupId>cn.bit</groupId><artifactId>common-feign</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

task-service

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>cn.bit</groupId><artifactId>BitGoPlus</artifactId><version>1.0.0</version></parent><artifactId>task-service</artifactId><name>task-service</name><description>task-service</description><dependencies><!-- quartz--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-quartz</artifactId></dependency><!-- =======公共依赖======= --><!-- web --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- nacos discovery --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><!-- nacos config --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId></dependency><!-- open feign  --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><!-- sharding jdbc --><dependency><groupId>org.apache.shardingsphere</groupId><artifactId>sharding-jdbc-spring-boot-starter</artifactId></dependency><!-- mysql --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><!-- mybatis plus --><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId></dependency><!-- =======内部依赖======= --><!-- common-core --><dependency><groupId>cn.bit</groupId><artifactId>common-core</artifactId></dependency><!-- common-data --><dependency><groupId>cn.bit</groupId><artifactId>common-data</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

user-service

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>cn.bit</groupId><artifactId>BitGoPlus</artifactId><version>1.0.0</version></parent><artifactId>user-service</artifactId><name>user-service</name><description>user-service</description><dependencies><!-- =======公共依赖======= --><!-- web --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- nacos discovery --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><!-- nacos config --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId></dependency><!-- open feign --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><!-- sharding jdbc --><dependency><groupId>org.apache.shardingsphere</groupId><artifactId>sharding-jdbc-spring-boot-starter</artifactId></dependency><!-- mysql --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><!-- mybatis plus --><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId></dependency><!-- =======内部依赖======= --><!-- common-core --><dependency><groupId>cn.bit</groupId><artifactId>common-core</artifactId></dependency><!-- common-data --><dependency><groupId>cn.bit</groupId><artifactId>common-data</artifactId></dependency><!-- common-security --><dependency><groupId>cn.bit</groupId><artifactId>common-security</artifactId></dependency><!-- common-cache --><dependency><groupId>cn.bit</groupId><artifactId>common-cache</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

核心为将配置文件定义在根pom中更具不同开发环境选择不同配置,比如nacos地址和各服务端口,并用common-bom统一管理用到所有依赖以及内部common模块的版本信息,再由根pom将其导入通过<dependencyManagement>进而实现对整个项目所用依赖统一管理。


五、服务bootstrap配置文件

server:port: @profiles.order-service.port@spring:profiles:active: @profile.active@application:name: @project.artifactId@cloud:nacos:server-addr: @nacos.server-addr@username: @nacos.username@password: @nacos.password@discovery:cluster-name: @nacos.cluster-name@namespace: @nacos.namespace@group: ${spring.profiles.active}config:cluster-name: ${spring.cloud.nacos.discovery.cluster-name}namespace: ${spring.cloud.nacos.discovery.namespace}group: ${spring.cloud.nacos.discovery.group}file-extension: yamlshared-configs:- data-id: application.yamlgroup: ${spring.profiles.active}refresh: true
snowflake:service-name: ${spring.application.name}

所有服务均使用此bootstrap.yaml文件,不同的配置使用在nacos服务器上的yaml文件,同时各服务公共配置文件为application.yaml。


总结:

理论上使用项目提供的所有配置文件和sql以及代码是能够运行所有服务的,不过由于本章重点在于微服务脚手架的搭建,所以不再具体讲解如何正确配置,后续发布release版本代码会同步更新博客,感兴趣可以start或者follow,基本每周都会有较大新增和改动,当然能自己正确配置的是这个(大拇指)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/75552.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

VS Code下开发FPGA——FPGA开发体验提升__下

上一篇&#xff1a;IntelliJ IDEA下开发FPGA-CSDN博客 Type&#xff1a;Quartus 一、安装插件 在应用商店先安装Digtal IDE插件 安装后&#xff0c;把其他相关的Verilog插件禁用&#xff0c;避免可能的冲突。重启后&#xff0c;可能会弹出下面提示 这是插件默认要求的工具链&a…

使用Python从零开始构建端到端文本到图像 Transformer大模型

简介&#xff1a;通过特征向量从文本生成图像 回顾&#xff1a;多模态 Transformer 在使用Python从零实现一个端到端多模态 Transformer大模型中&#xff0c;我们调整了字符级 Transformer 以处理图像&#xff08;通过 ResNet 特征&#xff09;和文本提示&#xff0c;用于视觉…

Webpack中的文件指纹:给资源戴上个“名牌”

你是否想过&#xff0c;当你修改代码后&#xff0c;浏览器为什么仍然拿着旧版资源不放&#xff1f;秘密就在于——文件指纹&#xff01;简单来说&#xff0c;文件指纹就像给每个构建出来的文件贴上独一无二的“姓名牌”&#xff0c;告诉浏览器&#xff1a;“嘿&#xff0c;我更…

python可变对象与不可变对象

文章目录 Python 中的可变对象与不可变对象不可变对象(Immutable Objects)可变对象(Mutable Objects)重要区别 Python 中的可变对象与不可变对象 在 Python 中&#xff0c;对象可以分为可变对象(mutable)和不可变对象(immutable)&#xff0c;这是 Python 中非常重要的概念&…

DeepSeek私有化部署性能怎么样?企业级AI落地实战解析!

1. 私有化部署是什么&#xff1f;为什么企业需要它&#xff1f; 很多公司在考虑用AI时都会问&#xff1a;“DeepSeek私有化部署性能怎么样&#xff1f;能不能在我们自己的服务器上跑&#xff1f;” 私有化部署的意思就是把AI模型装在你自己的机房或者云服务器上&#xff0c;而…

SQL学习--基础语法学习

SQL和excle对比 学习目标 单表查询 项目背景 SQL 练习环境 SQL Online Compiler - Next gen SQL Editor 商品信息表&#xff1a;https://study-zhibo.oss-cn-shanghai.aliyuncs.com/test/%E5%95%86%E5%93%81%E4%BF%A1%E6%81%AF%E8%A1%A8.csv 订单明细表&#xff1a;https://…

【Docker基础-网络】--查阅笔记4

目录 Docker 网络网络类型none 网络host 网络bridge 网络自定义网络 容器间通信IP 通信Docker DNS Serverjoined 容器 容器与外部通信容器访问外部外部访问容器 Docker 网络 学习Docker提供的几种原生网络如何创建自定义网络容器间通信&#xff0c;容器于外界交互 Docker 安装…

GPT模型架构与文本生成技术深度解析

核心发现概述 本文通过系统分析OpenAI的GPT系列模型架构&#xff0c;揭示其基于Transformer解码器的核心设计原理与文本生成机制。研究显示&#xff0c;GPT模型通过自回归机制实现上下文感知的序列生成&#xff0c;其堆叠式解码器结构配合创新的位置编码方案&#xff0c;可有效…

AWTK-MVVM 如何让多个View复用一个Model记录+关于app_conf的踩坑

前言 有这么一个业务&#xff0c;主界面点击应用窗口进入声纳显示界面&#xff0c;声纳显示界面再通过按钮进入菜单界面&#xff0c;菜单界面有很多关于该声纳显示界面的设置项&#xff0c;比如量程&#xff0c;增益&#xff0c;时间显示&#xff0c;亮度&#xff0c;对比度等…

CrystalDiskInfo电脑硬盘监控工具 v9.6.0中文绿色便携版

前言 CrystalDiskInfo是一个不用花钱的硬盘小帮手软件&#xff0c;它可以帮你看看你的电脑硬盘工作得怎么样&#xff0c;健不健康。这个软件能显示硬盘的温度高不高、还有多少地方没用、传输东西快不快等等好多信息。用了它&#xff0c;你就能很容易地知道硬盘现在是什么情况&…

数据分析-数据预处理

数据分析-数据预处理 处理重复值 duplicated( )查找重复值 import pandas as pd apd.DataFrame(data[[A,19],[B,19],[C,20],[A,19],[C,20]],columns[name,age]) print(a) print(--------------------------) aa.duplicated() print(a)只判断全局不判断每个 any() import p…

如何用海伦公式快速判断点在直线的哪一侧

一、海伦公式的定义与推导 1. 海伦公式的定义 海伦公式&#xff08;Heron’s Formula&#xff09;是用于计算三角形面积的一种方法&#xff0c;适用于已知三角形三边长度的情况。公式如下&#xff1a; S s ( s − a ) ( s − b ) ( s − c ) S \sqrt{s(s - a)(s - b)(s - c…

python推箱子游戏

,--^----------,--------,-----,-------^--,-------- 作者 yty---------------------------^----------_,-------, _________________________XXXXXX XXXXXX XXXXXX ______(XXXXXXXXXXXX(________(------ 0 [[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], [1,0,0,0,0,0,0,0,0,0,0,0,…

使用Python建模量子隧穿

引言 量子隧穿是量子力学中的一个非常有趣且令人神往的现象。在经典物理学中,我们通常认为粒子必须克服一个势垒才能通过它。但是,在量子力学中,粒子有时可以“穿越”一个势垒,即使它的能量不足以克服这个势垒。这种现象被称为“量子隧穿”。今天,我们将通过 Python 来建…

Vuex Actions 多参数传递的解决方案及介绍

Vuex Actions 多参数传递的解决方案及介绍 引言 在Vuex状态管理模式中&#xff0c;Actions 扮演着至关重要的角色。它主要用于处理异步操作&#xff0c;并且可以提交 Mutations 来修改全局状态。然而&#xff0c;在实际开发中&#xff0c;我们常常会遇到需要向 Actions 传递多…

设计模式 --- 策略模式

​策略模式&#xff08;Strategy Pattern&#xff09;是一种 ​​行为型设计模式​​&#xff0c;用于动态切换算法或策略​​&#xff0c;使得算法可以独立于客户端变化。它通过封装算法策略并使其可互换&#xff0c;提升了系统的灵活性和扩展性&#xff0c;尤其适用于需要多种…

【论文阅读】RMA: Rapid Motor Adaptation for Legged Robots

Paper: https://arxiv.org/abs/2107.04034Project: https://ashish-kmr.github.io/rma-legged-robots/Code: https://github.com/antonilo/rl_locomotion训练环境&#xff1a;Raisim 1.方法 RMA&#xff08;Rapid Motor Adaptation&#xff09;算法通过两阶段训练实现四足机器…

QQ风格客服聊天窗口

QQ风格客服聊天窗口 展示引入方式 展示 引入方式 <!DOCTYPE html> <html lang"zh-CN"> <head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0"><title&g…

【家政平台开发(37)】家政平台蜕变记:性能优化与代码重构揭秘

本【家政平台开发】专栏聚焦家政平台从 0 到 1 的全流程打造。从前期需求分析,剖析家政行业现状、挖掘用户需求与梳理功能要点,到系统设计阶段的架构选型、数据库构建,再到开发阶段各模块逐一实现。涵盖移动与 PC 端设计、接口开发及性能优化,测试阶段多维度保障平台质量,…