SpringCloud Netflix介绍
SpringCloud 核心技术
SpringCloud Demo
新建maven项目springcloud-demo,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"> <modelVersion>4.0.0</modelVersion> <groupId>com.procl</groupId> <artifactId>springcloud-demo</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <!-- SpringBoot:parent --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.9.RELEASE</version> </parent> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <!-- Eureka --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!-- openfeign --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--swagger--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.8.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.8.0</version> </dependency> </dependencies> <dependencyManagement> <dependencies> <!-- springcloud版本管理 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.SR11</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> </project>
在springcloud-demo下新建module sp-commons
<?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>springcloud-demo</artifactId> <groupId>com.procl</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>sp-commons</artifactId> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <!-- lombok --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.26</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.9</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies> </project>
在sp-commos下新建类
com.procl.commos.pojo.User
package com.procl.commons.pojo; import lombok.Data; import lombok.experimental.Accessors; @Data @Accessors(chain = true) public class User { private Long userId; private String userName; private String passWord; private Integer score; private String salt; private Integer role; }
com.procl.commos.pojo.Product
package com.procl.commons.pojo; import lombok.Data; @Data public class Product { private Long productId; private String productName; private Long productStore; }
com.procl.commos.pojo.Order
package com.procl.commons.pojo; import lombok.Data; import lombok.experimental.Accessors; import java.util.Date; @Data @Accessors(chain = true) public class Order { private Long id; private String orderId; private Long userId; private Long productId; private Integer productNum; private Date created; }
com.procl.commos.pojo.ProductOrder
package com.procl.commons.pojo; import lombok.Data; @Data public class ProductOrder { private Long productId; private Integer productNum; }
com.procl.commons.config.SwaggerConfig
package com.procl.commons.config; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; @EnableSwagger2 @Configuration public class SwaggerConfig { /** * Swagger2的配置 * * @return docket */ public Docket createDocket() { ApiInfoBuilder apiInfoBuilder = new ApiInfoBuilder(); ApiInfo apiInfo = apiInfoBuilder.title("接口文档").build(); Docket docket = new Docket(DocumentationType.SWAGGER_2); docket.apiInfo(apiInfo); docket.select().apis(RequestHandlerSelectors.any()); return docket; } }
com.procl.commons.web.JsonResult
package com.procl.commons.web; import jdk.nashorn.internal.objects.annotations.Constructor; import lombok.Data; import org.springframework.boot.context.properties.ConstructorBinding; @Data public class JsonResult { private Integer state = 1; private String message; private Object data; public JsonResult(Object data){ this.data = data; } public JsonResult() { } public JsonResult(Exception e){ this.state = 0; this.message = e.getMessage(); } }
com.procl.commons.service.UserService
package com.procl.commons.service; import com.procl.commons.pojo.User; import java.util.List; public interface UserService { //获取用户 User getUserByUserId(Long userId); List<User> getAllUser(); //增加用户积分 int addScore(Long userId,Integer score); String register(String userName,String passWord); String login(String userName,String passWord); }
com.procl.commons.service.ProductService
package com.procl.commons.service; import com.procl.commons.pojo.Product; import com.procl.commons.pojo.ProductOrder; import java.util.List; public interface ProductService { //获取一个订单的商品列表 List<Product> getProducts(); //减少商品库存 int decreaseStore(ProductOrder productOrder); Integer getProductNum(Long productId); }
com.procl.commons.service.OrderService
package com.procl.commons.service; import com.procl.commons.pojo.Order; import com.procl.commons.pojo.ProductOrder; import java.util.List; public interface OrderService { //获取订单 List<Order> getOrder(String orderId); //添加订单 int insertOrder(List<ProductOrder> productOrderList); List<Order> getAllOrder(); }