前言
有小伙伴说自己搭建Spring Cloud项目时,总会出现很多问题,项目无法正常启动,就算启动成功了也会出现一些难以预料的异常。
项目无法正常启动或者项目搭建不起来的原因大多是各个组件间的依赖和版本问题,这里就提供一个可以正常运行的Spring Cloud环境,以及环境搭建过程中的各个组件之间的依赖。
开发环境
操作系统:win10 JDK版本:1.8 Maven:3.8.6 IDEA工具:2022.2.2 MySQL:5.7 Nacos:nacos-server-1.2.0 SpringBoot:2.1.8.RELEASE spring-cloud:Greenwich.SR3 spring-cloud-alibaba-dependencies:2.1.0.RELEASE mybatis-plus:3.2
项目目录结构
SpringCloud(父模块:删除没用的src目录)
common(子模块:每个微服务都依赖此模块)coupon(子模块:优惠券服务)member(子模块:会员服务)gateway(子模块:API网管)order(子模块:订单服务)product(子模块:商品服务)
Spring Cloud(父模块:删除没用的src目录)依赖
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">
common(子模块:每个微服务都依赖此模块)
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">
coupon(子模块:优惠券服务)
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">
配置文件(application.yml)
spring:
datasource:
username: username
password: password
url: jdbc:mysql://127.0.0.1:3306/cloud?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
driver-class-name: com.mysql.cj.jdbc.Driver
# 配置nacos端口信息
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
# 为当前服务起名字,便于在nacos注册中心查看
application:
name: coupon
# 配置SQL的映射文件
mybatis-plus:
mapper-locations: classpath:/mapper/**/*.xml
# id自增
global-config:
db-config:
id-type: auto
# 服务端口号
server:
port: 7000
bootstrap.properties 依赖中引入了Nacos做配置中心的依赖,没有该配置文件项目启动时会抛出异常
# 配置服务的名称
spring.application.name=coupon
# nacos的地址和端口
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
member(子模块:会员服务)
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">
配置文件(application.yml)
#MySQL连接配置
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/cloud?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
driver-class-name: com.mysql.cj.jdbc.Driver
username: username
password: password
# Nacos配置
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
application:
name: member
# MyBatis—Plus配置
mybatis-plus:
mapper-locations: classpath:/mapper/**/*.xml
# id自增
global-config:
db-config:
id-type: auto
# 服务的端口配置
server:
port: 8000
bootstrap.properties 依赖中引入了Nacos做配置中心的依赖,没有该配置文件项目启动时会抛出异常
# 配置服务的名称
spring.application.name=coupon
# nacos的地址和端口
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
。。。。。。其他子模块依赖和配置也是一样,需要的其他依赖可以自行引入。。。。。。
注意:项目环境搭建时一定要注意各个组件之间的版本号问题,否则项目可能会出现一些意料外的错误信息。
相关链接
发表评论