搭建聚合项目(横向拆分、springboot项目、maven项目、整合dubbo、redis集群、nginx部署)
文章目录
一、准备工作
因为本篇文章重点讲解聚合项目的搭建,所以我们之前拿来上文的项目,进行拆分改造讲解(上文点这里!)
1、版本环境
- JDK:JDK9版本
- idea的maven-complice-version:9版本
<maven.compiler.source>9</maven.compiler.source>
<maven.compiler.target>9</maven.compiler.target>
- dubbo-admin可视化:2.6.0版本
2、主要ip和端口声明:
- zookeeper集群配置
zookeeper配置在linux虚拟机上(因为不想开太多个linux虚拟机,所以配置在 一台linux虚拟机上了,差别只在于修改ip地址)
包名称 | ip地址 | 端口号 |
---|---|---|
zookeeper-2181 | 172.16.248.201 | 2181 |
zookeeper-2182 | 172.16.248.201 | 2182 |
zookeeper-2183 | 172.16.248.201 | 2183 |
- dubbo集群配置
- 消费者集群
配置在我的windows系统上了
- 消费者集群
应用名 | ip地址 | 端口号 |
---|---|---|
dubbodemo-consumer_1 | localhost | 2011 |
dubbodemo-consumer_1 | localhost | 2012 |
dubbodemo-consumer_1 | localhost | 2013 |
dubbodemo-consumer_1 | localhost | 2014 |
- 服务者集群
配置在我的windows系统上了
应用名 | ip地址 | 端口号 |
---|---|---|
dubbodemo_provider_1 | localhost | 8081 |
dubbodemo_provider_1 | localhost | 8082 |
3、预览项目效果目录
二、创建一个空项目
- IDEA左上角->File->new->Project
- next
- 点击finish结束
三、创建Parent项目(管理jar包)
创建项目
- IDEA左上角->File->new->new Module
- Next
- 选择合适的路径和包名
- finish
pom.xml
1、由于parent项目主管理jar包的版本信息,所以需要制定版本,引入jar包;
2、其次parent的父工程是spring-boot-starter-parent,这是因为虽然parent项目不是一个springboot项目,但是后面web项目的父级工程从正常的spring-boot-starter-parent变为parent项目,理所应当parent项目要继承spring-boot-starter-parent;
3、 然后指定maven的编译版本也是需要的
4、在dependencies
标签外面嵌套一个dependencyManagement
标签可以使继承该项目的子类不自动导入全部parent所具有的jar包,便于管理项目!
5、打包类型为pom
<?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>cn.ebuy</groupId>
<artifactId>ebuyShop-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<!--打包类型-->
<packaging>pom</packaging>
<!-- properties:统一管理版本号 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>9</maven.compiler.source>
<maven.compiler.target>9</maven.compiler.target>
<oracle.version>11.2.0.3</oracle.version>
<dubbo.version>2.7.6</dubbo.version>
<zookeeper.version>2.7.6</zookeeper.version>
<springboot.mybatis.version>2.1.2</springboot.mybatis.version>
<validator.version>6.1.1.Final</validator.version>
<pagehelper.version>1.3.0</pagehelper.version>
<curator.version>4.2.0</curator.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.3</version>
<relativePath/>
</parent>
<dependencyManagement>
<!-- 引用父级工程所需的JAR包-->
<dependencies>
<!-- 1、 引入 SpringBoot 模块相关 开始 -->
<!-- 1.1 引入SpringBoot web模块 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--1.2 引入SpringBoot 单元测试模块 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--1.3 引入SpringBoot thymeleaf模块 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--1、 引入 SpringBoot 模块相关 结束 -->
<!--2、 引入 dubbo 模块 开始 -->
<!--2.1 引入 SpringBoot dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo.version}</version>
</dependency>
<!--2.2 引入 zookeepero 开始 -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-zookeeper</artifactId>
<version>${zookeeper.version}</version>
<exclusions>
<exclusion>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>${curator.version}</version>
</dependency>
<!--2、 引入 dubbo 模块 结束 -->
<!--3、 SpringBoot整合mybatis 启动器-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${springboot.mybatis.version}</version>
</dependency>
<!--4、Oracle驱动类-->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>${oracle.version}</version>
</dependency>
<!--6、数据校验的jar-->
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>${validator.version}</version>
</dependency>
<!--7、分页组件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>${pagehelper.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
- 然后点击编译、install一下,开始接下来的manager项目了!(注意我使用的jdk9版本,导入的ojdbc是6,亲测ojdbc10是不能被jdk9兼容的!)
四、创建manager项目
model层,主管数据访问层和业务逻辑层,管理项目之间的关系
创建项目
- IDEA左上角->File->new->new Module
- next
- finish(注意parent虽然是manager的父级,但是文件夹设置为同级!)
pom.xml
1、指定打包为pom类型
2、 然后指定maven的编译版本也是需要的
3、manager的version、groupId可加可不加
<?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>ebuyShop-parent</artifactId>
<groupId>cn.ebuy</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../ebuyShop-parent/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>ebuyShop-manager</artifactId>
<version>1.0-SNAPSHOT</version>
<groupId>cn.ebuy</groupId>
<!--打包类型-->
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>9</maven.compiler.source>
<maven.compiler.target>9</maven.compiler.target>
</properties>
</project>
- 开始创建pojo项目
五、创建pojo项目
创建项目
- IDEA左上角->File->new->new Module
- Next
- Finish
pom.xml
1、指定打包为jar类型
2、 然后指定maven的编译版本也是需要的
3、pojo的version、groupId可加可不加
<?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>ebuyShop-manager</artifactId>
<groupId>cn.ebuy</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>ebuyShop-pojo</artifactId>
<groupId>cn.ebuy</groupId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>9</maven.compiler.source>
<maven.compiler.target>9</maven.compiler.target>
</properties>
<!--数据校验-->
<dependencies>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
</dependencies>
</project>
导入我们的实体
- 创建包并导入实体
- 接下来创建interface层
五、创建mapper项目(数据访问层)
创建项目
- IDEA左上角->File->new->new Module
- Next
- finish
pom.xml
1、指定打包为jar类型
2、 然后指定maven的编译版本也是需要的
3、mapper的version、groupId可加可不加
4、mapper依赖于pojo项目,导入对应的jar
6、因为需要mybatis,所以引入mybatis的启动器jar
pom.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>ebuyShop-manager</artifactId>
<groupId>cn.ebuy</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>ebuyShop-mapper</artifactId>
<groupId>cn.ebuy</groupId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>9</maven.compiler.source>
<maven.compiler.target>9</maven.compiler.target>
</properties>
<dependencies>
<!--mapper层对pojo层依赖-->
<dependency>
<groupId>cn.ebuy</groupId>
<artifactId>ebuyShop-pojo</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--引入mybatis的jar-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>
导入对应的接口和xml文件
五、创建interface项目
创建项目
- IDEA左上角->File->new->new Module
- Next
- finish
pom.xml
1、指定打包为jar类型
2、 然后指定maven的编译版本也是需要的
3、interface的version、groupId可加可不加
4、interface依赖于pojo项目,导入对应的jar
<?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>ebuyShop-manager</artifactId>
<groupId>cn.ebuy</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>ebuyShop-interface</artifactId>
<groupId>cn.ebuy</groupId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>9</maven.compiler.source>
<maven.compiler.target>9</maven.compiler.target>
</properties>
<dependencies>
<!--业务逻辑层接口需要pojo层-->
<dependency>
<groupId>cn.ebuy</groupId>
<artifactId>ebuyShop-pojo</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>
导入我们的接口(仅接口、无实现类)
六、创建service项目(业务逻辑层、提供者)
创建项目
- IDEA左上角->File->new->new Module(注意是springboot项目了)
- Next
- finish
pom.xml
1、指定打包为jar类型
2、 然后指定maven的编译版本也是需要的
3、service的version、groupId可加可不加
4、service依赖于pojo、mapper、interface项目,导入对应的jar
5、注意parent是manager,不是springboot-parent(因为manager继承自parent,而parent继承自springboot-parent)
<?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>
<artifactId>ebuyShop-manager</artifactId>
<groupId>cn.ebuy</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>cn.ebuy</groupId>
<artifactId>ebuyShop-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ebuyShop-service</name>
<packaging>jar</packaging>
<description>ebuyShop-service</description>
<properties>
<maven.compiler.source>9</maven.compiler.source>
<maven.compiler.target>9</maven.compiler.target>
</properties>
<dependencies>
<!--需要引用的模块 开始-->
<dependency>
<groupId>cn.ebuy</groupId>
<artifactId>ebuyShop-pojo</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>cn.ebuy</groupId>
<artifactId>ebuyShop-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>cn.ebuy</groupId>
<artifactId>ebuyShop-mapper</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--需要引用的模块 结束-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.5.3</version>
</dependency>
<!--1.3 引入SpringBoot thymeleaf模块 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.5.3</version>
</dependency>
<!--1、 引入 SpringBoot 模块相关 结束 -->
<!--2、 引入 dubbo 模块 开始 -->
<!--2.1 引入 SpringBoot dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<!--2.2 引入 zookeepero 开始 -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-zookeeper</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
</dependency>
<!--2、 引入 dubbo 模块 结束 -->
<!--3、 SpringBoot整合mybatis 启动器-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<!--4、Oracle驱动类-->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
导入我们的实现类(仅实现类)
- 扫描mapper包
配置主配置文件
1、配置该服务占用的端口
2、dubbo相关的配置
3、数据库配置
4、redis配置
5、mybatis配置
6、日志配置
- application.yml
server:
port: 8081
dubbo:
application:
name: ebuy_provider
registry:
address: xxx.xx.x.xxx:xxxx,xxx.xx.x.xxx:xxxx,xxx.xx.x.xxx:xxxx
protocol: zookeeper
timeout: 3000000
protocol:
port: 20882
name: dubbo
scan:
base-packages: cn.ebuy.service.impl
config-center:
timeout: 10000
spring:
datasource:
url: jdbc:oracle:thin:@localhost:1521:orcl1
driver-class-name: oracle.jdbc.OracleDriver
username: c##ebuy
password: www123
tomcat:
max-active: 20
max-wait: 60000
min-idle: 2
initial-size: 1
redis:
cluster:
nodes: 127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381,127.0.0.1:6382,127.0.0.1:6383,127.0.0.1:6384
# password: 123456
jedis:
pool:
max-active: 10
min-idle: 5
max-wait: 6000
mybatis:
type-aliases-package: cn.ebuy.pojo
mapper-locations: cn/ebuy/mapper/*.xml
logging:
level:
cn.ebuy: DEBUG
启动测试一下
- idea启动
- 服务注册成功
六、创建web项目(业务逻辑层、提供者)
创建项目
- IDEA左上角->File->new->new Module(注意是springboot项目了)
- Next
- finish
pom.xml
1、指定打包为jar类型
2、 然后指定maven的编译版本也是需要的
4、web依赖于pojo、interface项目,导入对应的jar
5、注意parent是parent(我们创建的父级项目),不是springboot-parent(因为parent继承自springboot-parent,所以继承parent间接了springboot-parent)
<?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>
<artifactId>ebuyShop-parent</artifactId>
<groupId>cn.ebuy</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>cn.ebuy</groupId>
<artifactId>ebuyShop-web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ebuyShop-web</name>
<description>ebuyShop-web</description>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>9</maven.compiler.source>
<maven.compiler.target>9</maven.compiler.target>
<springboot.version>2.5.3</springboot.version>
</properties>
<dependencies>
<!--引入模块 开始-->
<dependency>
<groupId>cn.ebuy</groupId>
<artifactId>ebuyShop-pojo</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>cn.ebuy</groupId>
<artifactId>ebuyShop-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--引入模块 结束-->
<!-- 1.1 引入SpringBoot web模块 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${springboot.version}</version>
</dependency>
<!--1.2 引入SpringBoot 单元测试模块 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${springboot.version}</version>
<scope>test</scope>
</dependency>
<!--1.3 引入SpringBoot thymeleaf模块 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>${springboot.version}</version>
</dependency>
<!--1、 引入 SpringBoot 模块相关 结束 -->
<!--2、 引入 dubbo 模块 开始 -->
<!--2.1 引入 SpringBoot dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<!--2.2 引入 zookeepero 开始 -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-zookeeper</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
</dependency>
<!--2、 引入 dubbo 模块 结束 -->
<!-- 实现对 Spring Session 使用 Redis 作为数据源的自动化配置 -->
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
<!-- 实现对 Spring Data Redis 的自动化配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<exclusions>
<!-- 去掉对 Lettuce 的依赖,因为 Spring Boot 优先使用 Lettuce 作为 Redis 客户端 -->
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 引入 Jedis 的依赖,这样 Spring Boot 实现对 Jedis 的自动化配置 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
导入我们的controller
导入静态资源以及模板html
配置主配置文件
1、配置该服务占用的端口
2、模板引擎配置
3、redis配置(这里的redis是为了解决集群带来的session共享问题)
4、dubbo相关的配置
- application.yml
server:
port: 2011
spring:
thymeleaf:
prefix: classpath:/templates/
suffix: .html
encoding: UTF-8
cache: false
mode: LEGACYHTML5
redis:
cluster:
nodes: 127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381,127.0.0.1:6382,127.0.0.1:6383,127.0.0.1:6384
password: 123456
jedis:
pool:
max-active: 10
min-idle: 5
max-wait: 6000
dubbo:
application:
name: ebuy_consumer
registry:
address: xxx.xx.x.xxx:xxxx,xxx.xx.x.xxx:xxxx,xxx.xx.x.xxx:xxx
protocol: zookeeper
check: false
timeout: 3000000
配置springboot启动注解
@SpringBootApplication(exclude = {
DataSourceAutoConfiguration.class,
TransactionAutoConfiguration.class,
HibernateJpaAutoConfiguration.class})
- 因为使用了redis,springboot会自动装配数据库配置,但是由于web层不涉及数据库连接,所以根本不需要配置数据库,所以加上该配置可以让springboot不对数据库进行装配!!!(不然会报错)
测试运行
- idea运行
- 查询消费者情况
这样聚合项目就构建完成了!!!