一、簡介
這次專案是準備做一個電商專案,採用當前主流的微服務技術,技術棧是SpringCloud Alibaba,此次設計思路也很簡單,當前端服務請求過來時先經過四層負載LVS,用過LVS的朋友應該知道LVS的併發量相對來說還是比較高的,可以達到400W左右;經過LVS後,LVS會把服務請求傳送給七層負載Nginx叢集服務,Nginx在後面文章也有說明,可以載入靜態資源什麼的,Nginx的抗壓能力也是非常高的,記得他併發量好像是可以達到5到6W左右吧。Nginx收到請求後會把請求路由到後端服務Gatway閘道器路由,閘道器雖然是做分發服務的,但其實在閘道器這層可以做的東西是很多的,比喻:鑑權、限流、負載分發、攔截....;經過閘道器後,服務請求就會路由到具體的業務服務程式碼;這就是整體的大體設計;
二、專案搭建
spring-cloud-api:存放所有與資料庫對應的實體類,以及遠端呼叫的feign介面
spring-cloud-goods-api:與spring-cloud-server對應的API工程
spring-cloud-gateway:所有的微服務閘道器工程,存放不同服務的微服務閘道器或者微服務閘道器叢集
spring-cloud-service:所有微服務的業務模組工程
spring-cloud-util:所有工具工程,這裡面又可以拆分兩塊,一塊是微服務依賴包;另一塊是普通工具包
spring-cloud-web:所有頁面操作的工程
2.1、父工程搭建
spring-cloud-alibaba的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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.ghy</groupId> <artifactId>spring-cloud-alibaba</artifactId> <version>0.0.1-SNAPSHOT</version> <name>spring-cloud-alibaba</name> <packaging>pom</packaging> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud-alibaba.version>2.2.1.RELEASE</spring-cloud-alibaba.version> </properties> <modules> <module>spring-cloud-api</module> <module>spring-cloud-gateway</module> <module>spring-cloud-util</module> <module>spring-cloud-web</module> <module>spring-cloud-service</module> </modules> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.10.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <!--lombok,方便建立javaBean的一些方法--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <!--測試--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <!--熱部署外掛--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> </dependencies> <dependencyManagement> <dependencies> <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> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.3.7.RELEASE</version> <configuration> <mainClass>com.ghy.spring_cloud_alibaba.SpringCloudAlibabaApplication</mainClass> </configuration> <executions> <execution> <id>repackage</id> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
spring-cloud-api的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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <groupId>com.ghy</groupId> <artifactId>spring-cloud-alibaba</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <packaging>pom</packaging> <modules> <module>spring-cloud-goods-api</module> </modules> <artifactId>spring-cloud-api</artifactId> <description>所有資料庫表對應的Bean和Feign介面</description> <dependencies> <!--MyBatis Plus--> <!--編寫JavaBean會用到MyBatis Plus的相關注解,引入依賴防止程式編譯 不通過--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.3.2</version> <scope>provided</scope> </dependency> </dependencies> </project>
spring-cloud-gateway的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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <groupId>com.ghy</groupId> <artifactId>spring-cloud-alibaba</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <groupId>com.ghy</groupId> <artifactId>spring-cloud-gateway</artifactId> <version>0.0.1-SNAPSHOT</version> <name>spring-cloud-gateway</name> <description>這個是微服務的閘道器</description> <properties> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <spring-boot.version>2.3.7.RELEASE</spring-boot.version> </properties> <dependencies> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.3.7.RELEASE</version> <configuration> <mainClass>com.ghy.springcloudgateway.SpringCloudGatewayApplication</mainClass> </configuration> <executions> <execution> <id>repackage</id> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
spring-cloud-service的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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <groupId>com.ghy</groupId> <artifactId>spring-cloud-alibaba</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <packaging>pom</packaging> <modules> <module>spring-cloud-goods-service</module> </modules> <artifactId>spring-cloud-service</artifactId> <description>所有業務類程式碼的父工程</description> <dependencies> <!--jar包依賴--> <dependency> <artifactId>spring-cloud-common</artifactId> <groupId>com.ghy</groupId> <version>0.0.1-SNAPSHOT</version> </dependency> <!--工具包依賴--> <dependency> <artifactId>spring-cloud-service-dependency</artifactId> <groupId>com.ghy</groupId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> </project>
spring-cloud-util的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>spring-cloud-alibaba</artifactId> <groupId>com.ghy</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <packaging>pom</packaging> <artifactId>spring-cloud-util</artifactId> <description> 所有工具工程管理 </description> <name>spring-cloud-util</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <modules> <module>spring-cloud-common</module> <module>spring-cloud-service-dependency</module> </modules> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> </dependencies> </project>
spring-cloud-web的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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>spring-cloud-alibaba</artifactId> <groupId>com.ghy</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <packaging>pom</packaging> <artifactId>spring-cloud-web</artifactId> <description>管理涉及到頁面渲染的工程</description> <properties> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <spring-boot.version>2.3.7.RELEASE</spring-boot.version> </properties> <dependencies> </dependencies> </project>
2.2、公共工程建立
spring-cloud-service-dependency是所有server微服務可能要用到的工具包,spring-cloud-service-dependency的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>spring-cloud-util</artifactId> <groupId>com.ghy</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>spring-cloud-service-dependency</artifactId> <description> 所有微服務工程公共依賴包、所有微服務工程公共配置類 </description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <!--web包--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--MyBatis Plus--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.3.2</version> </dependency> <!--MySQL--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!--Redis--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!--Nacos--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> </dependencies> </project>
在spring-cloud-service-dependency工程中建立一個分頁攔截器StartConfig類
@Configuration public class StartConfig { /**** * 分頁攔截器 */ @Bean public PaginationInterceptor paginationInterceptor(){ PaginationInterceptor pageInterceptor = new PaginationInterceptor(); // 設定資料型別 pageInterceptor.setDbType(DbType.MYSQL); return pageInterceptor; } }
spring-cloud-common是所有應用的基礎包,它的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>spring-cloud-util</artifactId> <groupId>com.ghy</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>spring-cloud-common</artifactId> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <description> 工具工程 </description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> </dependencies> </project>
在此工程中建立一個錯誤碼類RespCode類
public enum RespCode { SUCCESS(20000, "操作成功"), ERROR(50000, "操作失敗"), SYSTEM_ERROR(50001, "系統錯誤"); private Integer code; private String message; RespCode(Integer code, String message) { this.code = code; this.message = message; } RespCode() { } public Integer getCode() { return code; } public void setCode(Integer code) { this.code = code; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
RespResult類
public class RespResult<T> implements Serializable { //響應資料結果集 private T data; /** * 狀態碼 * 20000 操作成功 * 50000 操作失敗 */ private Integer code; /***S * 響應資訊 */ private String message; public RespResult() { } public RespResult(RespCode resultCode) { this.code = resultCode.getCode(); this.message = resultCode.getMessage(); } public RespResult(T data, RespCode resultCode) { this.data = data; this.code = resultCode.getCode(); this.message = resultCode.getMessage(); } public static RespResult ok() { return new RespResult(null, RespCode.SUCCESS); } public static RespResult ok(Object data) { return new RespResult(data, RespCode.SUCCESS); } public static RespResult error() { return new RespResult(null, RespCode.ERROR); } public static RespResult error(String message) { return secByError(RespCode.ERROR.getCode(),message); } //自定義異常 public static RespResult secByError(Integer code,String message) { RespResult err = new RespResult(); err.setCode(code); err.setMessage(message); return err; } public static RespResult error(RespCode resultCode) { return new RespResult(resultCode); } public T getData() { return data; } public void setData(T data) { this.data = data; } public Integer getCode() { return code; } public void setCode(Integer code) { this.code = code; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
3、品牌管理工能的建立
CREATE TABLE `brand` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '品牌id',
`name` varchar(100) NOT NULL COMMENT '品牌名稱',
`image` varchar(1000) DEFAULT '' COMMENT '品牌圖片地址',
`initial` varchar(1) DEFAULT '' COMMENT '品牌的首字母',
`sort` int(11) DEFAULT NULL COMMENT '排序',
PRIMARY KEY (`id`) )
ENGINE=InnoDB AUTO_INCREMENT=325475 DEFAULT CHARSET=utf8 COMMENT='品牌表';
商品表設計完後,就是要寫品牌的工程及建立他的實體類,下面建立spring-cloud-goods-api工程,他的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>spring-cloud-api</artifactId> <groupId>com.ghy</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>spring-cloud-goods-api</artifactId> <description> spring-cloud-goods對應的Api工程 </description> </project>
然後建立他的實體類
@Data @NoArgsConstructor @AllArgsConstructor @TableName(value = "brand") public class Brand { // ID(主鍵) @TableId:標記當前屬性為主鍵列對應的屬性 @TableId(type = IdType.AUTO) private Integer id; // 品牌名字 private String name; // 品牌圖片 private String image; // 品牌首字母 private String initial; // 排序 private Integer sort; }
然後上建立他的業務工程spring-cloud-goods-service工程,他的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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <groupId>com.ghy</groupId> <artifactId>spring-cloud-service</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>spring-cloud-goods-service</artifactId> <description> 資料庫的增刪改查 </description> <dependencies> <!--goods-api--> <dependency> <groupId>com.ghy</groupId> <artifactId>spring-cloud-goods-api</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> </project>
yml檔案
server: port: 8081 spring: application: name: spring-cloud-goods-service datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://192.168.32.32:3306/shop_goods?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC username: root password: root cloud: nacos: config: file-extension: yaml server-addr: 192.168.32.32:8848 discovery: #Nacos的註冊地址 server-addr: 192.168.32.32:8848 # ====================MybatisPlus==================== mybatis-plus: mapper-locations: mapper/*.xml type-aliases-package: com.ghy.springcloudgoodsapi.*.entity configuration: map-underscore-to-camel-case: true #駝峰轉換 log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #日誌檢視 #日誌配置 logging: pattern: console: "%msg%n"
BrandMapper類
public interface BrandMapper extends BaseMapper<Brand> { }
public interface BrandService extends IService<Brand> { /**** * 條件查詢 * return List<Brand> */ List<Brand> queryList(Brand brand); /**** * 條件分頁查詢 * return Page<Brand> */ Page<Brand> queryPageList(Brand brand, Long currentPage, Long size); }
/**** * 條件查詢 * return List<Brand> */ @Override public List<Brand> queryList(Brand brand) { //條件包裝物件 QueryWrapper<Brand> queryWrapper = new QueryWrapper<Brand>(); //根據name查詢品牌 queryWrapper.like("name",brand.getName()); //根據initial查詢 queryWrapper.eq("initial",brand.getInitial()); return brandMapper.selectList(queryWrapper); } /**** * 條件分頁查詢 * return Page<Brand> */ @Override public Page<Brand> queryPageList(Brand brand, Long currentPage, Long size) { //條件包裝物件 QueryWrapper<Brand> queryWrapper = new QueryWrapper<Brand>(); //根據name查詢品牌 queryWrapper.like("name",brand.getName()); return brandMapper.selectPage(new Page<Brand>(currentPage,size),queryWrapper); } }
@RestController @RequestMapping(value = "/brand") public class BrandController { @Autowired private BrandService brandService; /**** * 增加方法 */ @PostMapping public RespResult add(@RequestBody Brand brand){ brandService.save(brand); return RespResult.ok(); } /**** * 修改方法 */ @PutMapping public RespResult update(@RequestBody Brand brand){ brandService.updateById(brand); return RespResult.ok(); } /**** * 刪除方法 */ @DeleteMapping("/{id}") public RespResult delete(@PathVariable(value = "id")String id){ brandService.removeById(id); return RespResult.ok(); } /**** * 條件查詢 */ @PostMapping(value = "/search") public RespResult<List<Brand>> queryList(@RequestBody Brand brand){ List<Brand> brands = brandService.queryList(brand); return RespResult.ok(brands); } /**** * 條件查詢 */ @PostMapping(value = "/search/{page}/{size}") public RespResult<Page<Brand>> queryPageList( @PathVariable(value = "page")Long page, @PathVariable(value = "size")Long size, @RequestBody Brand brand){ Page<Brand> pageInfo = brandService.queryPageList(brand,page,size); return RespResult.ok(pageInfo); } }
這樣工程就建立完成了
git原始碼地址:https://gitee.com/TongHuaShuShuoWoDeJieJu/spring-cloud-alibaba.git