3.项目搭建

    xiaoxiao2025-05-27  92

    3.项目搭建

    3.4.创建父工程

    创建统一的父工程:leyou,用来管理依赖及其版本,注意是创建project,而不是module

    填写项目信息:

    填写保存的位置信息:

    然后将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>com.leyou.parent</groupId> <artifactId>leyou</artifactId> <version>1.0.0-SNAPSHOT</version> <packaging>pom</packaging> <name>leyou</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.7.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Finchley.SR2</spring-cloud.version> <mybatis.starter.version>1.3.2</mybatis.starter.version> <mapper.starter.version>2.0.2</mapper.starter.version> <druid.starter.version>1.1.9</druid.starter.version> <mysql.version>5.1.32</mysql.version> <pageHelper.starter.version>1.2.3</pageHelper.starter.version> <leyou.latest.version>1.0.0-SNAPSHOT</leyou.latest.version> <fastDFS.client.version>1.26.1-RELEASE</fastDFS.client.version> </properties> <dependencyManagement> <dependencies> <!-- springCloud --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!-- mybatis启动器 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis.starter.version}</version> </dependency> <!-- 通用Mapper启动器 --> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>${mapper.starter.version}</version> </dependency> <!-- 分页助手启动器 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>${pageHelper.starter.version}</version> </dependency> <!-- mysql驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <!--FastDFS客户端--> <dependency> <groupId>com.github.tobato</groupId> <artifactId>fastdfs-client</artifactId> <version>${fastDFS.client.version}</version> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>

    可以发现,我们在父工程中引入了SpringCloud等,很多以后需要用到的依赖,以后创建的子工程就不需要自己引入了。

    可以删除src目录,工程结构如下:

    3.5.创建EurekaServer

    3.5.1.创建工程

    我们的注册中心,起名为:leyou-registry

    选择新建module:

    不要选择骨架:

    然后填写项目坐标,我们的项目名称为leyou-registry:

    选择安装目录,因为是聚合项目,目录应该是在父工程leyou的下面:

    3.5.2.添加依赖

    添加EurekaServer的依赖:

    <?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>leyou</artifactId> <groupId>com.leyou.parent</groupId> <version>1.0.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <groupId>com.leyou.common</groupId> <artifactId>leyou-registry</artifactId> <version>1.0.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies> </project>

    3.5.3.编写启动类

    @SpringBootApplication @EnableEurekaServer public class LeyouRegistryApplication { public static void main(String[] args) { SpringApplication.run(LeyouRegistryApplication.class, args); } }

    3.5.4.配置文件

    server: port: 10086 spring: application: name: leyou-registry eureka: client: service-url: defaultZone: http://127.0.0.1:${server.port}/eureka register-with-eureka: false # 把自己注册到eureka服务列表 fetch-registry: false # 拉取eureka服务信息 server: enable-self-preservation: false # 关闭自我保护 eviction-interval-timer-in-ms: 5000 # 每隔5秒钟,进行一次服务列表的清理

    3.5.5.项目的结构

    目前,整个项目的结构如图:

    3.6.创建Zuul网关

    3.6.1.创建工程

    与上面类似,选择maven方式创建Module,然后填写项目名称,我们命名为:leyou-gateway

    填写保存的目录:

    3.6.2.添加依赖

    这里我们需要添加Zuul和EurekaClient的依赖:

    <?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>leyou</artifactId> <groupId>com.leyou.parent</groupId> <version>1.0.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <groupId>com.leyou.common</groupId> <artifactId>leyou-gateway</artifactId> <version>1.0.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!-- springboot提供微服务检测接口,默认对外提供几个接口 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies> </project>

    3.6.3.编写启动类

    @SpringBootApplication @EnableDiscoveryClient @EnableZuulProxy public class LeyouGatewayApplication { public static void main(String[] args) { SpringApplication.run(LeyouGatewayApplication.class, args); } }

    3.6.4.配置文件

    server: port: 10010 spring: application: name: leyou-gateway eureka: client: registry-fetch-interval-seconds: 5 service-url: defaultZone: http://127.0.0.1:10086/eureka zuul: prefix: /api # 路由路径前缀

    3.6.5.项目结构

    目前,leyou下有两个子模块:

    leyou-registry:服务的注册中心(EurekaServer)leyou-gateway:服务网关(Zuul)

    目前,服务的结构如图所示:

    截止到这里,我们已经把基础服务搭建完毕,为了便于开发,统一配置中心(ConfigServer)我们留待以后添加。

    3.7.创建商品微服务

    既然是一个全品类的电商购物平台,那么核心自然就是商品。因此我们要搭建的第一个服务,就是商品微服务。其中会包含对于商品相关的一系列内容的管理,包括:

    商品分类管理品牌管理商品规格参数管理商品管理库存管理

    3.7.1.微服务的结构

    因为与商品的品类相关,我们的工程命名为leyou-item.

    需要注意的是,我们的leyou-item是一个微服务,那么将来肯定会有其它系统需要来调用服务中提供的接口,获取的接口数据,也需要对应的实体类来封装,因此肯定也会使用到接口中关联的实体类。

    因此这里我们需要使用聚合工程,将要提供的接口及相关实体类放到独立子工程中,以后别人引用的时候,只需要知道坐标即可。

    我们会在leyou-item中创建两个子工程:

    leyou-item-interface:主要是对外暴露的接口及相关实体类leyou-item-service:所有业务逻辑及内部使用接口

    调用关系如图所示:

    3.7.2.leyou-item

    依然是使用maven构建:

    保存的位置:

    因为是聚合工程,所以把项目打包方式设置为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"> <parent> <artifactId>leyou</artifactId> <groupId>com.leyou.parent</groupId> <version>1.0.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <groupId>com.leyou.item</groupId> <artifactId>leyou-item</artifactId> <version>1.0.0-SNAPSHOT</version> <!-- 打包方式为pom --> <packaging>pom</packaging> </project>

    3.7.3.leyou-item-interface

    在leyou-item工程上点击右键,选择new --> module:

    依然是使用maven构建,注意父工程是leyou-item:

    注意:目录结构,保存到leyou-item下的leyou-item-interface目录中:

    点击Finish完成。

    此时的项目结构:

    3.7.4.leyou-item-service

    与leyou-item-interface类似,我们选择在leyou-item上右键,新建module,然后填写项目信息:

    填写存储位置

    点击Finish完成。

    3.7.5.整个微服务结构

    如图所示:

    我们打开leyou-item的pom查看,会发现leyou-item-interface和leyou-item-service都已经成为module了:

    可以删除leyou-item工程的src目录

    3.7.6.添加依赖

    接下来我们给leyou-item-service中添加依赖:

    思考一下我们需要什么?

    Eureka客户端web启动器mybatis启动器通用mapper启动器分页助手启动器连接池,我们用默认的Hykiramysql驱动千万不能忘了,我们自己也需要ly-item-interface中的实体类

    这些依赖,我们在顶级父工程:leyou中已经添加好了。所以直接引入即可:

    <?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>leyou-item</artifactId> <groupId>com.leyou.item</groupId> <version>1.0.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <groupId>com.leyou.item</groupId> <artifactId>leyou-item-service</artifactId> <version>1.0.0-SNAPSHOT</version> <dependencies> <!-- web启动器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- eureka客户端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!-- mybatis的启动器 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <!-- 通用mapper启动器 --> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> </dependency> <!-- 分页助手启动器 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> </dependency> <!-- jdbc启动器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!-- mysql驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>com.leyou.item</groupId> <artifactId>leyou-item-interface</artifactId> <version>1.0.0-SNAPSHOT</version> </dependency> <!-- springboot检测服务启动器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies> </project>

    leyou-item-interface中需要什么我们暂时不清楚,所以先不管。以后需要什么依赖,再引入。

    3.7.7.编写启动和配置

    在整个leyou-item工程中,只有leyou-item-service是需要启动的。因此在其中编写启动类即可:

    @SpringBootApplication @EnableDiscoveryClient public class LeyouItemServiceApplication { public static void main(String[] args) { SpringApplication.run(LeyouItemServiceApplication.class, args); } }

    然后是全局属性文件:

    server: port: 8081 spring: application: name: item-service datasource: url: jdbc:mysql://localhost:3306/leyou username: root password: root hikari: max-lifetime: 28830000 # 一个连接的生命时长(毫秒),超时而且没被使用则被释放(retired),缺省:30分钟,建议设置比数据库超时时长少30秒,参考MySQL wait_timeout参数(show variables like '%timeout%';) maximum-pool-size: 9 # 连接池中允许的最大连接数。缺省值:10;推荐的公式:((core_count * 2) + effective_spindle_count) eureka: client: service-url: defaultZone: http://127.0.0.1:10086/eureka instance: lease-renewal-interval-in-seconds: 5 # 5秒钟发送一次心跳 lease-expiration-duration-in-seconds: 10 # 10秒不发送就过期

    3.8.添加商品微服务的路由规则

    既然商品微服务已经创建,接下来肯定要添加路由规则到Zuul中,我们不使用默认的路由规则。

    修改leyou-gateway工程的application.yml配置文件:

    zuul: prefix: /api # 路由路径前缀 routes: item-service: /item/** # 商品微服务的映射路径

    3.9.启动测试

    我们分别启动:leyou-registry,leyou-gateway,leyou-item-service

    查看Eureka面板:

    3.10.测试路由规则

    为了测试路由规则是否畅通,我们是不是需要在item-service中编写一个controller接口呢?

    其实不需要,SpringBoot提供了一个依赖:actuator

    只要我们添加了actuator的依赖,它就会为我们生成一系列的访问接口:

    /info/health/refresh…

    添加依赖:

    <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>

    重启后访问Eureka控制台:

    鼠标悬停在item-service上,会显示一个地址:

    这就是actuator提供的接口,我们点击访问:

    因为我们没有添加信息,所以是一个空的json,但是可以肯定的是:我们能够访问到item-service了。

    接下来我们通过路由访问试试,根据路由规则,我们需要访问的地址是:

    http://127.0.0.1:10010/api/item/actuator/info

    3.11.通用工具模块

    有些工具或通用的约定内容,我们希望各个服务共享,因此需要创建一个工具模块:leyou-common

    右键leyou工程,使用maven来构建module:

    位置信息:

    结构:

    目前还不需要编码。

    最新回复(0)