Spring-Cloud-Alibaba(2)-搭建项目

微服务环境搭建

本次是使用的电商项目中的商品、订单、用户为案例进行学习。

环境准备

  • maven:3.6.x
  • 数据库:MySQL 5.8
  • 持久层: SpingData Jpa
  • 其他:​ SpringCloud Alibaba 技术栈

模块设计

  • springcloud-alibaba 父工程
  • shop-common 公共模块【实体类】
  • shop-user 用户微服务 【端口: 807x】
  • shop-product 商品微服务 【端口: 808x】
  • shop-order 订单微服务 【端口: 809x】

Snipaste_2021-04-08_16-06-55.png

微服务调用

在微服务架构中,最常见的场景就是微服务之间的相互调用。我们以电商系统中常见的用户下单为例来演示微服务的调用:客户向订单微服务发起一个下单的请求,在进行保存订单之前需要调用商品微服务查询商品的信息。

我们一般把服务的主动调用方称为服务消费者,把服务的被调用方称为服务提供者

父工程

创建一个maven工程,然后在pom.xml文件中添加下面内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
</parent>

<groupId>com.letcoding</groupId>
<artifactId>springCloud-alibaba</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>

<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
<spring-cloud-alibaba.version>2.1.0.RELEASE</spring-cloud-alibaba.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<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>
</project>

官方版本说明

common模块

这里的common模块,按照博猪个人习惯是不会放DTO/VO/ENTITY这些对象的,这个项目案例中目前也没有特别复杂的逻辑,所以也不存放util,这里博猪为了后续方便暂时在pom文件中放一些公共的依赖。

公共依赖:

  • JPA的启动器
  • lombok
  • 数据库连接驱动

pom依赖如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?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-alibaba</artifactId>
<groupId>com.letcoding</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>shop-common</artifactId>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.56</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
</dependencies>

</project>

用户模块

pom依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?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>
<groupId>com.letcoding</groupId>
<artifactId>springCloud-alibaba</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>

<modelVersion>4.0.0</modelVersion>
<artifactId>shop-user</artifactId>

<dependencies>
<dependency>
<groupId>com.letcoding</groupId>
<artifactId>shop-common</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

</dependencies>

</project>

项目配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
server:
port: 8081
spring:
application:
name: shop-product
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.56.120:3306/cloudAlibaba-demo?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: 123456
jpa:
properties:
hibernate:
hbm2ddl:
auto: update
dialect: org.hibernate.dialect.MySQL5InnoDBDialect

启动器

1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* @ClassName UserApplication
* @Description 用户启动类
* @Author will
* @Date 2021/4/8 17:19
* @Company 北京华焱坤泰科技有限公司
*/
@SpringBootApplication
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class, args);
}
}

实体类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
* @ClassName User
* @Description TODO
* @Author will
* @Date 2021/4/9 12:13
* @Company
*/
@Entity(name = "shop_user")
@Data
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
/** 主键 */
private Integer uId;
/** 用户名 */
private String userName;
/** 密码 */
private String password;
/** 手机号 */
private String telePhone;
}

数据仓储

1
2
3
4
5
6
7
8
/**
* @ClassName UserDao
* @Description UserDao
* @Author will
* @Date 2021/6/20 19:08
*/
public interface UserDao extends JpaRepository<User,Integer> {
}

产品模块

pom依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?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-alibaba</artifactId>
<groupId>com.letcoding</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>shop-product</artifactId>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>com.letcoding</groupId>
<artifactId>shop-common</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

</project>

项目配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
server:
port: 8081
spring:
application:
name: shop-product
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.56.120:3306/cloudAlibaba-demo?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: 123456
jpa:
properties:
hibernate:
hbm2ddl:
auto: update
dialect: org.hibernate.dialect.MySQL5InnoDBDialect

启动类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
* @ClassName ProductApplication
* @Description TODO
* @Author will
* @Date 2021/6/20 19:13
*/
@SpringBootApplication
@EnableDiscoveryClient
public class ProductApplication {

public static void main(String[] args) {
SpringApplication.run(ProductApplication.class, args);
}

}

实体类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
* @ClassName Product
* @Description TODO
* @Author will
* @Date 2021/6/20 19:16
*/
@Data
@Entity(name = "shop_product")
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
/** 主键 */
private Integer pId;
/** 商品名称 */
private String pName;
/** 商品价格 */
private Double pPrice;
/** 库存 */
private Integer stock;

}

数据仓储

1
2
3
4
5
6
7
8
/**
* @ClassName ProductDao
* @Description TODO
* @Author will
* @Date 2021/6/20 19:27
*/
public interface ProductDao extends JpaRepository<Product,Integer> {
}

Service接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
* @ClassName ProductService
* @Description TODO
* @Author will
* @Date 2021/6/20 19:28
*/
public interface ProductService {
/**
* 获取指定pId的产品对象
* @param pId
* @return
*/
Product queryById(Integer pId);
}

Service接口实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
* @ClassName ProductServiceImpl
* @Description TODO
* @Author will
* @Date 2021/6/20 19:29
*/
@Service
public class ProductServiceImpl implements ProductService {

@Autowired
private ProductDao productDao;

@Override
public Product queryById(Integer pId) {
Optional<Product> productOptional = productDao.findById(pId);
if (productOptional.isPresent()) {
return productOptional.get();
}
return null;
}
}

Controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
* @ClassName ProductController
* @Description TODO
* @Author will
* @Date 2021/6/20 19:43
*/
@RestController
@RequestMapping("/product")
@Slf4j
public class ProductController {

@Autowired
private ProductService productService;

@GetMapping("/info/{pId}")
public Product info(@PathVariable("pId") Integer pId) {
Product product = productService.queryById(pId);
log.info("查询到商品:" + JSON.toJSONString(product));
return product;
}
}

测试数据

1
2
3
4
INSERT INTO `shop_product`(`p_id`, `p_name`, `p_price`, `stock`) VALUES (1, '小米', 1000, 5000);
INSERT INTO `shop_product`(`p_id`, `p_name`, `p_price`, `stock`) VALUES (2, '华为', 2000, 5000);
INSERT INTO `shop_product`(`p_id`, `p_name`, `p_price`, `stock`) VALUES (3, '苹果', 3000, 5000);
INSERT INTO `shop_product`(`p_id`, `p_name`, `p_price`, `stock`) VALUES (4, 'OPPO', 4000, 5000);

测试

订单模块

pom依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?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-alibaba</artifactId>
<groupId>com.letcoding</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>shop-order</artifactId>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>com.letcoding</groupId>
<artifactId>shop-common</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.letcoding</groupId>
<artifactId>shop-product</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

</project>

项目配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
server:
port: 8091
spring:
application:
name: shop-order
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.56.120:3306/cloudAlibaba-demo?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: 123456
jpa:
properties:
hibernate:
hbm2ddl:
auto: update
dialect: org.hibernate.dialect.MySQL5InnoDBDialect

启动类

1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* @ClassName OrderApplication
* @Description TODO
* @Author will
* @Date 2021/6/20 19:32
*/
@SpringBootApplication
@EnableDiscoveryClient
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
}

实体类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/**
* @ClassName Order
* @Description TODO
* @Author will
* @Date 2021/6/20 19:33
*/
@Data
@Entity(name = "shop_order")
public class Order {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
/** 订单id */
private Long oId;
/** 用户id */
private Integer uId;
/** 用户名 */
private String userName;
/** 商品id */
private Integer pId;
/** 商品名称 */
private String pName;
/** 商品单价 */
private Double pPrice;
/** 购买数量 */
private Integer number;

}

数据仓储

1
2
3
4
5
6
7
8
/**
* @ClassName OrderDao
* @Description TODO
* @Author will
* @Date 2021/6/20 19:35
*/
public interface OrderDao extends JpaRepository<Order,Long> {
}

Service接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
* @ClassName OrderService
* @Description TODO
* @Author will
* @Date 2021/6/20 19:36
*/
public interface OrderService {
/**
* 保存订单对象
* @param order
*/
public void saveOrder(Order order);

}

Service接口实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* @ClassName OrderServiceImpl
* @Description TODO
* @Author will
* @Date 2021/6/20 19:36
*/
@Service
public class OrderServiceImpl implements OrderService {

@Autowired
private OrderDao orderDao;

@Override
public void saveOrder(Order order) {
orderDao.save(order);
}
}

实例配置

1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* @ClassName BeanConfiguration
* @Description TODO
* @Author will
* @Date 2021/6/20 20:34
*/
@Configuration
public class BeanConfiguration {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}

Controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* @ClassName OrderController
* @Description TODO
* @Author will
* @Date 2021/6/20 20:35
*/
@RestController
@Slf4j
@RequestMapping("/order")
public class OrderController {

private static final String REST_TEMPLATE = "restTemplate";

@Autowired
private OrderService orderService;
@Autowired
private RestTemplate restTemplate;
@Autowired
private DiscoveryClient discoveryClient;

/**
* 保存指定产品订单
* @param pId
* @return
*/
@GetMapping("/save/{pId}")
public Order order(@PathVariable("pId") Integer pId, @RequestParam(name = "queryType") String queryType) {
log.info(">>客户下单,这时候要调用商品微服务查询商品信息");
Product product = null;
// 通过restTemplate调用商品微服务
if (REST_TEMPLATE.equals(queryType)) {
product = queryProductByRestTemplate(pId);
}
log.info(">>商品信息,查询结果:" + JSON.toJSONString(product));
Order order = new Order();
order.setUId(1);
order.setUserName("测试用户");
order.setPId(product.getPId());
order.setPName(product.getPName());
order.setPPrice(product.getPPrice());
order.setNumber(1);
orderService.saveOrder(order);
return order;
}

/**
* RestTemplate风格查询
* @param pId 产品id
* @return
*/
private Product queryProductByRestTemplate(Integer pId) {
return restTemplate.getForObject("http://localhost:8081/product/info/" + pId, Product.class);
}
}

Spring-Cloud-Alibaba(2)-搭建项目
https://github.com/yangxiangnanwill/yangxiangnanwill.github.io/2024/01/03/好好码代码吖/JAVA/Spring/Spring-Cloud-Alibaba(2)-搭建项目/
作者
will
发布于
2024年1月3日
许可协议