环境: jdk17
idea 2019
springboot:3.2.4
spring-cloud:2023.0.1
大概记录下关键配置
1 创建父工程
创建项目
删除src目录
因为是父子结构不需要。
引入依赖
XML
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.tuling</groupId>
<artifactId>spring-cloud-alibaba-demo</artifactId>
<packaging>pom</packaging>
<version>${revision}</version>
<modules>
<module>order01</module>
</modules>
<name>spring-cloud-alibaba-demo</name>
<description>demo</description>
<!-- 项目依赖版本 -->
<properties>
<revision>1.0.0</revision>
<java.version>17</java.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<skipTests>true</skipTests>
<spring.boot.version>3.2.4</spring.boot.version>
<spring-cloud.version>2023.0.1</spring-cloud.version>
<spring-cloud-alibaba.version>2023.0.1.0</spring-cloud-alibaba.version>
<spring.boot.test.version>3.2.4</spring.boot.test.version>
<lombok.version>1.18.30</lombok.version>
<flatten-maven-plugin.version>1.2.7</flatten-maven-plugin.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- 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>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Spring Cloud Alibaba -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- spring-boot-starter-test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring.boot.test.version}</version>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<optional>true</optional>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring.boot.version}</version>
</plugin>
</plugins>
</pluginManagement>
</build>
<!-- 阿里云的Maven镜像源 -->
<repositories>
<repository>
<id>aliyunmaven</id>
<name>aliyun</name>
<url>https://maven.aliyun.com/repository/public</url>
</repository>
<repository>
<id>central2</id>
<name>central2</name>
<url>https://repo1.maven.org/maven2/</url>
</repository>
</repositories>
</project>
2 创建子模块(服务提供者)
新建module
引入依赖
XML
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-cloud-alibaba-demo</artifactId>
<groupId>org.tuling</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>order01</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- Nacos注册中心 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<!-- <version>8.0.32</version> -->
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 打包插件 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<!-- 将源码中的xml文件打包到jar中 -->
<resources>
<resource>
<directory>src/main/java</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
</build>
</project>
3 添加配置文件
resources目录下新建 application.yml
配置文件,内容如下:
bash
server:
port: 8060
spring:
application:
name: tlmall-order01
cloud:
nacos:
discovery:
server-addr: tlmall-nacos-server:8848
#server-addr: nacos.tlmall.com:8848
#namespace: fox #指定为fox开发环境的命名空间
#namespace: dev # 指定注册到开发环境
#cluster-name: SH #上海机房
username: nacos
password: nacos
datasource:
url: jdbc:mysql://XXX:3306/tlmall_order?useSSL=false&characterEncoding=utf8&allowPublicKeyRetrieval=true
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 1234XXXXX
main:
allow-bean-definition-overriding: true
mybatis:
# 配置 mybatis 的xml文件的扫描路径
mapper-locations: classpath:mybatis/**/*.xml
# 配置实体类的扫描路径
type-aliases-package: org.tuling.tlmallorder01.entity
configuration:
# 开启驼峰命名转换
map-underscore-to-camel-case: true
# 开启日志
#log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl
management:
endpoints:
web:
exposure:
include: "*"
配置注意:数据源
4 添加启动类
5 添加测试业务代码
controller
service
mapper
/resources/mybatis/ 目录下新增 XXMapper.xml
文件
sql
CREATE TABLE `order` (
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
`user_id` varchar(255) DEFAULT NULL,
`commodity_code` varchar(255) DEFAULT NULL,
`count` int DEFAULT NULL,
`money` int DEFAULT '0',
`create_time` datetime DEFAULT NULL,
`update_time` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=90 DEFAULT CHARSET=utf8mb3;
6 服务的测试
启动nacos.参见:nacos 让服务之间互相认识-CSDN博客
启动应用,测试接口
如果启动了nacos.观察下服务列表
注意下EndPoint,http://localhost:8060/actuator/nacosdiscovery
至此,springBoot的项目基本架构搭建成功。可以陆续添加其它子模块。主要是pom里面的配置,版本不一致可能产生的问题。