Idea 2023.2.7构建SpringCloud多模块项目
本文介绍如何使用idea 2023.2.7构建基于SpringCloud alibaba微服务项目,基于Nacos注册中心。
环境准备:
JDK版本:jdk17
SpringBoot:3.3.4
SpringCloud:2023.0.3
Nacos服务端:2.4.3
1、创建父工程,选择Spring Initalizr
2、选择父项目组件
3、删除src
4、修改父模块pom.xml配置文件
默认的打包方式是jar,父工程不参与打包,增加
<packaging>pom</packaging>
并引入alibaba依赖整合
<!--整合springcloud alibaba-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2021.0.5.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
引入公共依赖如orm框架、注册中心、数据库连接池等
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2023.0.1.2</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
<version>4.1.4</version>
</dependency>
<!--引入sentinel依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>2023.0.1.2</version>
</dependency>
5、创建子模块user-center
(1)选中项目,右击,New->Module
(2)输入Name:user-center用户中心信息
(3)选择需要的组件,父工程中已包含依赖可不选
(4)创建完成,结构如下
(5)修改user-center的pom.xml文件,修改parent里的groupId为父工程groupId
修改前:
修改后:
<parent>
<groupId>com.xu</groupId>
<artifactId>psyRestRoom</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
(6)修改父工程pom.xml,加modules
<modules>
<module>user-center</module>
</modules>
(7)application.yml配置
spring:
application:
name: user-center
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: ENC(w1Teu1o/5A3viCdkQ3VkC6++hMwBrsGip37LnjTi0h0Xqa3MoQ3ilOxSbg9otQTuoTm/Z3bTPPFqsBlYFo+6aiNdyOe25oON5AqF0mycFxMreo3VVrkn8ZkpXETkUBYt)
username: ENC(jSszHsa26QY5M/aI7aM6pNtQGcyEnPU0NJIHG5C4oN5uu00PGgkyh+Up/gHIIlah)
password: ENC(VhlpQvPt9FUqy4sbpweQ5Q5PQ/mspFmOUV+RdjI4VGfekp/gK/2swL6b5cgnPWDa)
cloud:
nacos:
discovery:
server-addr: http://192.168.1.226:8848
username: nacos
password: nacos
config:
import-check:
enabled: false
file-extension: yml
sentinel:
transport:
dashboard: localhost:8080 #配置sentinel服务端
http-method-specify: true #开启请求方式前缀
nacos:
core:
auth:
enabled:true
jasypt:
encryptor:
password: f0d15ddc
server:
port: 8081
feign:
sentinel:
enabled: true
(8)增加bootstrap.yaml
spring:
cloud:
nacos:
config:
server-addr: http://192.168.1.226:8848
name: user-center
file-extension: yaml
group: DEFAULT_GROUP
username: nacos
password: nacos
再根据以上方法创建子模块
启动子模块如下:
查看注册中心: