文章推荐
从0到1构建微服务之服务注册-nacos(二)
从0到1构建微服务之服务通信-feign(三)
从0到1构建微服务之服务网关-gateway(四)
文章目录
简介
本章内容主要讲述基础工程目录的创建以及各工程的功能、各工程之间的关系
注:目前所有的代码构建肯定不是最优的,比如版本依赖的版本号抽取管理等等,后续都会一步一步去做。
1、基础工程目录的创建
这里打算创建的项目名为share,也是父工程。包含子工程如下
1.1工程结构
1.2实际的工程目录结构
1.3各工程的作用及目录
-
share-biz 主要是业务工程,下面可以根据各业务创建不同的biz模块服务,这里暂时先创建一个share-biz-admin,外部调用都先经过biz
-
share-gateway 主要为网关工程,这里暂时不实现,后续再来实现网关功能
-
share-user 这个为用户服务
-
share-common 这个公共服务包
-
share-service-client 这个为各基础服务暴露的接口工程
1.4系统架构图
前面的创建了工程目录,但是调用关系大家可能还比较模糊,下面画出系统架构图。目前看起来是比较简陋,后面会一步一步去完善
2、启动项目工程
2.1、父工程
2.1.1、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.cys.share</groupId>
<artifactId>share</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>share-common</module>
<module>share-biz</module>
<module>share-service-client</module>
<module>share-gateway</module>
<module>share-user</module>
<module>share-biz/share-biz-admin</module>
</modules>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.7.13</version>
<exclusions><!-- 去掉springboot默认配置 -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.7.13</version>
<exclusions><!-- 去掉springboot默认配置 -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.83</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
</dependencies>
</project>
2.2、adminbiz服务
2.2.1、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>share</artifactId>
<groupId>com.cys.share</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>share-biz-admin</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<poi.version>4.1.2</poi.version>
</properties>
<dependencies>
</dependencies>
</project>
2.2.2、application.yml
server:
port: 8001
servlet:
context-path: /adminbiz
spring:
application:
name: adminbiz-server
2.2.3、创建启动类AdminBizApplication
package com.cys.share.biz.admin;
@SpringBootApplication
@ComponentScan({"com.cys.share.biz.admin"})
public class AdminBizApplication {
public static void main(String[] args) {
SpringApplication.run(AdminBizApplication.class, args);
}
}
2.3、gateway服务
2.3.1、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>share</artifactId>
<groupId>com.cys.share</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>share-gateway</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
</dependencies>
</project>
2.3.2、application.yml
server:
port: 8003
spring:
main:
web-application-type: reactive
application:
name: gateway-server
2.3.3、创建启动类GatewayApplication
package com.cys.share.gateway;
@SpringBootApplication()
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}
2.4、user服务
2.4.1、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>share</artifactId>
<groupId>com.cys.share</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>share-user</artifactId>
<dependencies>
</dependencies>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>
2.4.2、application.yml
server:
port: 8002
servlet:
context-path: /user
# context-path: /${spring.application.name}
spring:
application:
name: user-server
2.4.3、创建启动类GatewayApplication
package com.cys.share.user;
@SpringBootApplication
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class, args);
}
}