一、简介
这次项目是准备做一个电商项目,采用当前主流的微服务技术,技术栈是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