SpringBoot构建微服务实战

Posted fengze

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot构建微服务实战相关的知识,希望对你有一定的参考价值。

整体架构:

 

 

服务提供方实现:

1. 创建一个Maven项目,

目录结构:

 

pom.xml文件内容如下:

 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 2     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 3     <modelVersion>4.0.0</modelVersion>
 4 
 5     <groupId>com.windy.mall.product</groupId>
 6     <artifactId>ms-mall</artifactId>
 7     <version>1.0.0-SNAPSHOT</version>
 8     <packaging>jar</packaging>
 9 
10     <parent>
11         <groupId>org.springframework.boot</groupId>
12         <artifactId>spring-boot-starter-parent</artifactId>
13         <version>1.4.0.RELEASE</version>
14     </parent>
15 
16     <name>ms-mall</name>
17     <url>http://maven.apache.org</url>
18 
19     <properties>
20         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
21         <maven.compiler.source>1.8</maven.compiler.source>
22         <maven.compiler.target>1.8</maven.compiler.target>
23     </properties>
24 
25     <dependencies>
26         <dependency>
27             <groupId>org.apache.curator</groupId>
28             <artifactId>curator-x-discovery-server</artifactId>
29             <version>2.11.0</version>
30         </dependency>
31         <dependency>
32             <groupId>org.mybatis</groupId>
33             <artifactId>mybatis</artifactId>
34             <version>3.4.1</version>
35         </dependency>
36         <dependency>
37             <groupId>mysql</groupId>
38             <artifactId>mysql-connector-java</artifactId>
39         </dependency>
40         <dependency>
41             <groupId>org.springframework.boot</groupId>
42             <artifactId>spring-boot-starter-jdbc</artifactId>
43         </dependency>
44         <dependency>
45             <groupId>org.springframework.boot</groupId>
46             <artifactId>spring-boot-starter-web</artifactId>
47         </dependency>
48         <dependency>
49             <groupId>org.mybatis.spring.boot</groupId>
50             <artifactId>mybatis-spring-boot-starter</artifactId>
51             <version>1.1.1</version>
52         </dependency>
53     </dependencies>
54 </project>
View Code

 

2. 创建数据库,sql如下:

1 create database db_products default charset utf8;
2 
3 create table products (pid int not null primary key auto_increment, pname varchar(200), type varchar(50), price double, createTime timestamp)
View Code

 

3. 创建resouces folder: src/main/resources,并创建application.properties文件

spring.datasource.url=jdbc:mysql:///db_products?useSSL=false
spring.datasource.username=***
spring.datasource.password=***
View Code

 

4. 创建实体类package: bean,并创建实体类Product.java

 1 package com.windy.mall.product.bean;
 2 
 3 import java.sql.Timestamp;
 4 
 5 public class Product {
 6 
 7     private Integer pid;
 8     private String pname;
 9     private String type;
10     private Double price;
11     private Timestamp createTime;
12     
13     public Integer getPid() {
14         return pid;
15     }
16     public void setPid(Integer pid) {
17         this.pid = pid;
18     }
19     public String getPname() {
20         return pname;
21     }
22     public void setPname(String pname) {
23         this.pname = pname;
24     }
25     public String getType() {
26         return type;
27     }
28     public void setType(String type) {
29         this.type = type;
30     }
31     public Double getPrice() {
32         return price;
33     }
34     public void setPrice(Double price) {
35         this.price = price;
36     }
37     public Timestamp getCreateTime() {
38         return createTime;
39     }
40     public void setCreateTime(Timestamp createTime) {
41         this.createTime = createTime;
42     }
43 
44     public String toString() {
45         return "Product [pid=" + pid + ", pname=" + pname + ", type=" + type + ", price=" + price + ", createTime="
46                 + createTime + "]";
47     }
48     
49     
50 }
View Code

 

5. 创建Mapper package:mapper,并创建一个Mapper接口:ProductMapper(整合Mybatis,基于注解的形式)

Mybatis会帮助我们自动创建实现类, 我们只需要声明接口即可.

问题: 如何使用Mybatis的逆向工程,自动生成实体类,并且Mapper接口都是以注解的形式存在,而并非XML形式?

可以考虑先使用Mybatis的逆向工程,生成实体类和Mapper的XML文件,然后再根据需要转换使用基于注解的方式.

 1 package com.windy.mall.product.mapper;
 2 
 3 import java.util.List;
 4 
 5 import org.apache.ibatis.annotations.Delete;
 6 import org.apache.ibatis.annotations.Insert;
 7 import org.apache.ibatis.annotations.Mapper;
 8 import org.apache.ibatis.annotations.Select;
 9 import org.apache.ibatis.annotations.Update;
10 
11 import com.windy.mall.product.bean.Product;
12 
13 @Mapper
14 public interface ProductMapper {
15 
16     @Insert("insert into products(pname,type,price)values(#{pname},#{type},#{price})")
17     public Integer add(Product product);
18     
19     @Delete("delete from products where pid=#{arg1}")
20     public Integer deleteById(Integer id);
21     
22     @Update("update products set pname=#{pname},type=#{type},price=#{price} where pid=#{pid} ")
23     public Integer update(Integer id);
24     
25     @Select("select * from products where pid=#{arg1}")
26     public Product getById(Integer id);
27     
28     @Select("select * from products order by pid desc")
29     public List<Product> queryByLists();
30     
31 }
View Code

 

6. 创建一个响应类Response,把所有返回结果信息,封装在这个类中.

 1 package com.windy.mall.product.utils;
 2 
 3 public class Response {
 4     /*
 5      * code: 标志状态码
 6      * 200:表示成功
 7      * 404: 表示资源不存在
 8      * 500:表示失败
 9      */
10     
11     private String code;
12     private String msg;
13     private Object data;
14     
15     public Response() {
16         super();
17     }
18     
19     public Response(String code, String msg) {
20         super();
21         this.code = code;
22         this.msg = msg;
23     }
24     
25     public Response(String code, String msg, Object data) {
26         super();
27         this.code = code;
28         this.msg = msg;
29         this.data = data;
30     }
31     
32     public String getCode() {
33         return code;
34     }
35     
36     public void setCode(String code) {
37         this.code = code;
38     }
39     
40     public String getMsg() {
41         return msg;
42     }
43     
44     public void setMsg(String msg) {
45         this.msg = msg;
46     }
47     
48     public Object getData() {
49         return data;
50     }
51     
52     public void setData(Object data) {
53         this.data = data;
54     }
55     
56 }
View Code

 

7. 创建Controller类(由于只是简单的Demo,所以没有实现Service层)

 1 package com.windy.mall.product.controller;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.web.bind.annotation.DeleteMapping;
 5 import org.springframework.web.bind.annotation.GetMapping;
 6 import org.springframework.web.bind.annotation.PathVariable;
 7 import org.springframework.web.bind.annotation.PostMapping;
 8 import org.springframework.web.bind.annotation.PutMapping;
 9 import org.springframework.web.bind.annotation.RestController;
10 
11 import com.windy.mall.product.bean.Product;
12 import com.windy.mall.product.mapper.ProductMapper;
13 import com.windy.mall.product.utils.Response;
14 
15 @RestController
16 public class ProductController {
17 
18     @Autowired
19     private ProductMapper productMapper;
20     
21     @PostMapping("/ms/product/add")
22     public Object add(Product product){
23         Integer res = productMapper.add(product);
24         if(res == null){
25             return new Response("200", "Success!");
26         }
27         
28             return new Response("500","Fail!");
29     }
30     
31     @PutMapping("/ms/product/update")
32     public Object update(Product product ){
33         return new Response("200", "Success!", productMapper.update(product));
34     }
35     
36     @GetMapping("/ms/product/{id}")
37     public Object get(@PathVariable("id") Integer id ){
38         return new Response("200", "Success!", productMapper.getById(id));
39     }
40     
41     @GetMapping("/ms/products")
42     public Object list(){
43         return new Response("200","Success!", productMapper.queryByLists());
44     }
45     
46     @DeleteMapping("/ms/product/{id}")
47     public Object delete(@PathVariable("id") Integer id ){
48         Integer res = productMapper.deleteById(id);
49         return res==1? new Response("200","Success!") : new Response("500", "Fail!");
50         
51     }
52     
53 }
View Code

 

8. 针对Mapper的单元测试(待补充)

 

9. 编写服务注册类ServiceRegister.java

 

调用方:

1. 创建一个新的Maven工程,pom.xml文件如下:

 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 2     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 3     <modelVersion>4.0.0</modelVersion>
 4 
 5     <groupId>com.windy.mall.web</groupId>
 6     <artifactId>ms-mall-web</artifactId>
 7     <version>0.0.1-SNAPSHOT</version>
 8     <packaging>jar</packaging>
 9 
10     <name>ms-mall-web</name>
11     <url>http://maven.apache.org</url>
12 
13     <properties>
14         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15         <maven.compiler.source>1.8</maven.compiler.source>
16         <maven.compiler.target>1.8</maven.compiler.target>
17     </properties>
18 
19     <dependencies>
20         <dependency>
21             <groupId>org.apache.curator</groupId>
22             <artifactId>curator-x-discovery</artifactId>
23             <version>2.11.0</version>
24         </dependency>
25         <dependency>
26             <groupId>com.google.code.gson</groupId>
27             <artifactId>gson</artifactId>
28             <version>2.7</version>
29         </dependency>
30         <dependency>
31             <groupId>org.springframework</groupId>
32             <artifactId>spring-context</artifactId>
33             <version>4.3.2.RELEASE</version>
34         </dependency>
35         <dependency>
36             <groupId>org.springframework</groupId>
37             <artifactId>spring-web</artifactId>
38             <version>4.3.2.RELEASE</version>
39         </dependency>
40     </dependencies>
41 </project>
View Code

 

2. 

 

以上是关于SpringBoot构建微服务实战的主要内容,如果未能解决你的问题,请参考以下文章

Spring Cloud微服务实战-服务治理(Spring Cloud Eureka)

SpringBoot与Kubernetes云原生微服务实战

精通系列)创建 Springboot 项目+基于 ssm 多模块项目案例+微服务实战

SpringCloud微服务实战——第二章Springboot

Spring Cloud 微服务实战

SpringCloud微服务实战学习系列配置详解