这次项目是准备做一个电商项目,采用当前主流的微服务技术,技术栈是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:所有页面操作的工程
spring-cloud-alibaba的pom.xml
"1.0" encoding="UTF-8"?>"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"> 4.0.0 com.ghy spring-cloud-alibaba 0.0.1-SNAPSHOT spring-cloud-alibaba pom Demo project for Spring Boot 1.8 2.2.1.RELEASE spring-cloud-api spring-cloud-gateway spring-cloud-util spring-cloud-web spring-cloud-service org.springframework.boot spring-boot-starter-parent 2.2.10.RELEASE org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine org.springframework.boot spring-boot-devtools runtime true com.alibaba.cloud spring-cloud-alibaba-dependencies ${spring-cloud-alibaba.version} pom import org.apache.maven.plugins maven-compiler-plugin 3.8.1 1.8 UTF-8 org.springframework.boot spring-boot-maven-plugin 2.3.7.RELEASE com.ghy.spring_cloud_alibaba.SpringCloudAlibabaApplication repackage repackage
spring-cloud-api的pom.xml文件
"1.0" encoding="UTF-8"?>"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"> com.ghy spring-cloud-alibaba 0.0.1-SNAPSHOT 4.0.0 pom spring-cloud-goods-api spring-cloud-api 所有数据库表对应的Bean和Feign接口 com.baomidou mybatis-plus-boot-starter 3.3.2 provided
spring-cloud-gateway的pom.xml文件
"1.0" encoding="UTF-8"?>"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"> com.ghy spring-cloud-alibaba 0.0.1-SNAPSHOT 4.0.0 com.ghy spring-cloud-gateway 0.0.1-SNAPSHOT spring-cloud-gateway 这个是微服务的网关 1.8 UTF-8 UTF-8 2.3.7.RELEASE org.springframework.boot spring-boot-dependencies ${spring-boot.version} pom import org.apache.maven.plugins maven-compiler-plugin 3.8.1 1.8 UTF-8 org.springframework.boot spring-boot-maven-plugin 2.3.7.RELEASE com.ghy.springcloudgateway.SpringCloudGatewayApplication repackage repackage
spring-cloud-service的pom.xml
"1.0" encoding="UTF-8"?>"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"> com.ghy spring-cloud-alibaba 0.0.1-SNAPSHOT 4.0.0 pom spring-cloud-goods-service spring-cloud-service 所有业务类代码的父工程 spring-cloud-common com.ghy 0.0.1-SNAPSHOT spring-cloud-service-dependency com.ghy 0.0.1-SNAPSHOT
spring-cloud-util的pom.xml
"1.0" encoding="UTF-8"?>"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"> spring-cloud-alibaba com.ghy 0.0.1-SNAPSHOT 4.0.0 pom spring-cloud-util 所有工具工程管理 spring-cloud-util http://www.example.com spring-cloud-common spring-cloud-service-dependency UTF-8 1.7 1.7 junit junit 4.11 test
spring-cloud-web的pom.xml
"1.0" encoding="UTF-8"?>"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"> spring-cloud-alibaba com.ghy 0.0.1-SNAPSHOT 4.0.0 pom spring-cloud-web 管理涉及到页面渲染的工程 1.8 UTF-8 UTF-8 2.3.7.RELEASE
2.2、公共工程创建
spring-cloud-service-dependency是所有server微服务可能要用到的工具包,spring-cloud-service-dependency的pom.xml文件
"1.0" encoding="UTF-8"?>"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"> spring-cloud-util com.ghy 0.0.1-SNAPSHOT 4.0.0 spring-cloud-service-dependency 所有微服务工程公共依赖包、所有微服务工程公共配置类 UTF-8 1.7 1.7 org.springframework.boot spring-boot-starter-web com.baomidou mybatis-plus-boot-starter 3.3.2 mysql mysql-connector-java runtime org.springframework.boot spring-boot-starter-data-redis com.alibaba.cloud spring-cloud-starter-alibaba-nacos-config com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery
在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文件如下
"1.0" encoding="UTF-8"?>"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"> spring-cloud-util com.ghy 0.0.1-SNAPSHOT 4.0.0 spring-cloud-common http://www.example.com 工具工程 UTF-8 1.7 1.7 junit junit 4.11 test
在此工程中建立一个错误码类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 RespResultimplements 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; } }
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文件如下
"1.0" encoding="UTF-8"?>"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"> spring-cloud-api com.ghy 0.0.1-SNAPSHOT 4.0.0 spring-cloud-goods-api spring-cloud-goods对应的Api工程
然后创建他的实体类
@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文件如下
"1.0" encoding="UTF-8"?>"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"> com.ghy spring-cloud-service 0.0.1-SNAPSHOT 4.0.0 spring-cloud-goods-service 数据库的增删改查 com.ghy spring-cloud-goods-api 0.0.1-SNAPSHOT
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{ }
public interface BrandService extends IService{ /**** * 条件查询 * return List */ List queryList(Brand brand); /**** * 条件分页查询 * return Page */ Page queryPageList(Brand brand, Long currentPage, Long size); }
/**** * 条件查询 * return List*/ @Override public List queryList(Brand brand) { //条件包装对象 QueryWrapper queryWrapper = new QueryWrapper (); //根据name查询品牌 queryWrapper.like("name",brand.getName()); //根据initial查询 queryWrapper.eq("initial",brand.getInitial()); return brandMapper.selectList(queryWrapper); } /**** * 条件分页查询 * return Page */ @Override public Page queryPageList(Brand brand, Long currentPage, Long size) { //条件包装对象 QueryWrapper queryWrapper = new QueryWrapper (); //根据name查询品牌 queryWrapper.like("name",brand.getName()); return brandMapper.selectPage(new Page (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> queryList(@RequestBody Brand brand){ List
brands = brandService.queryList(brand); return RespResult.ok(brands); } /**** * 条件查询 */ @PostMapping(value = "/search/{page}/{size}") public RespResult > queryPageList( @PathVariable(value = "page")Long page, @PathVariable(value = "size")Long size, @RequestBody Brand brand){ Page pageInfo = brandService.queryPageList(brand,page,size); return RespResult.ok(pageInfo); } }
这样工程就创建完成了
git源码地址:https://gitee.com/TongHuaShuShuoWoDeJieJu/spring-cloud-alibaba.git