本次是使用的电商项目中的商品、订单、用户为案例进行讲解。本章只是搭建基本环境,并不过多讲解.
第一章:微服务的架构介绍发展
第二章:微服务环境搭建
第三章:Nacos Discovery--服务治理
第四章:Sentinel--服务容错
第五章:Gateway--服务网关
第六章:Sleuth--链路追踪
第七章:Rocketmq--消息驱动
2.1 案例准备
2.1.1 技术选型
maven:3.6.0
数据库:MySQL 5.7
持久层:Sping Data Jpa
其他:Spring Cloud Alibaba 技术栈
2.1.2 模块设计
下面是项目端口号说明及项目结构:
1587028347.png
- micro-mall
- mall-commons 【公共组件】
- common-bom 【】
- common-core 【公共核心】
- mall-dependencies 【spring-cloud-alibaba 父工程】
- mall-modules
- order-service-parent
- order-service 【用户微服务,端口: 807x】
- order-service-api
- product-service-parent
- product-service 【商品微服务,端口: 808x】
- product-service-api
- user-service-parent
- user-service 【订单微服务,端口: 809x】
- user-service-api
2.1.3 微服务调用
在微服务架构中,最常见的场景就是微服务之间的相互调用。我们以电商系统中常见的用户下单为例来演示微服务的调用:客户向订单微服务发起一个下单的请求,在进行保存订单之前需要调用商品微服务查询商品的信息。
我们一般把服务的主动调用方称为服务消费者,把服务的被调用方称为服务提供者。
image在这种场景下,订单微服务就是一个服务消费者, 商品微服务就是一个服务提供者。
2.2 创建父工程
创建一个maven工程,然后在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>
<packaging>pom</packaging>
<groupId>com.bxs</groupId>
<artifactId>mall-dependencies</artifactId>
<version>${revision}</version>
<name>micro-mall</name>
<description>项目依赖</description>
<!--依赖版本的锁定-->
<properties>
<!-- 版本号 -->
<revision>1.0.0</revision>
<!-- 编译 -->
<java.version>1.8</java.version>
<!-- spring boot、spring cloud -->
<spring-boot.version>2.2.5.RELEASE</spring-boot.version>
<spring-cloud.version>Hoxton.SR3</spring-cloud.version>
<spring-cloud-alibaba.version>2.2.0.RELEASE</spring-cloud-alibaba.version>
<springfox.version>2.9.2</springfox.version>
<swagger.version>1.6.1</swagger.version>
<mysql.version>5.1.6</mysql.version>
<fastjson.version>1.2.68</fastjson.version>
</properties>
<!--公用依赖-->
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!--选项 SpringCloud 和 SpringCloudAlibaba d的版本对应-->
<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>
<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>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<!-- swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${springfox.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${springfox.version}</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>${swagger.version}</version>
</dependency>
<!--MySQL-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- mybatis -->
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
截自 2020 年 4月 18日 版本对应:
Spring Cloud Version | Spring Cloud Alibaba Version | Spring Boot Version |
---|---|---|
Spring Cloud Hoxton | 2.2.x.RELEASE | 2.2.x.RELEASE |
Spring Cloud Greenwich | 2.1.x.RELEASE | 2.1.x.RELEASE |
Spring Cloud Finchley | 2.0.x.RELEASE | 2.0.x.RELEASE |
Spring Cloud Edgware | 1.5.x.RELEASE | 1.5.x.RELEASE |
2.3 创建用户微服务API
步骤:
- 创建模块 导入依赖
- 创建必要的接口和实体类(entity dto)
2.3.1 新建一个 user-service-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">
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<parent>
<artifactId>user-service-parent</artifactId>
<groupId>com.bxs</groupId>
<version>${revision}</version>
</parent>
<artifactId>user-service-api</artifactId>
<name>${project.artifactId}</name>
<description>用户服务api</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
</dependencies>
</project>
2.3.2 创建实体类
import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@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;//手机号
}
2.4 创建用户微服务
步骤:
- 创建模块 导入依赖
- 创建SpringBoot主类
- 加入配置文件
- 创建必要的接口和实现类(controller service dao)
2.4.1 新建一个 user-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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<parent>
<artifactId>user-service-parent</artifactId>
<groupId>com.bxs</groupId>
<version>1.0.0</version>
</parent>
<artifactId>user-service</artifactId>
<name>${project.artifactId}</name>
<description>用户服务</description>
<dependencies>
<dependency>
<groupId>com.bxs</groupId>
<artifactId>user-service-api</artifactId>
</dependency>
</dependencies>
</project>
2.4.2 编写主类
@SpringBootApplication
@EnableDiscoveryClient
public class UserServiceApplication {
public static void main(String[] args) {
SpringApplication.run( UserServiceApplication.class, args );
}
}
2.4.3. 编写配置文件
server:
port: 8071
spring:
application:
name: user-service
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://39.105.167.131:3306/smile_boot?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
username: admin
password: admin
jpa:
properties:
hibernate:
hbm2ddl:
#auto: create
auto: update
dialect: org.hibernate.dialect.MySQL5InnoDBDialect
format_sql: true
show-sql: true
2.5 创建商品微服务API
步骤:
- 创建模块 导入依赖
- 创建必要的接口和实体类(entity dto)
2.5.1 新建一个 product-service-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">
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<parent>
<artifactId>product-service-parent</artifactId>
<groupId>com.bxs</groupId>
<version>1.0.0</version>
</parent>
<artifactId>product-service-api</artifactId>
<name>${project.artifactId}</name>
<description>商品服务API</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>
2.5.2 创建实体类
import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity(name = "shop_product")
@Data
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer pid;//主键
private String pname;//商品名称
private Double pprice;//商品价格
private Integer stock;//库存
}
2.6 创建商品微服务
步骤:
- 创建模块 导入依赖
- 创建SpringBoot主类
- 加入配置文件
- 创建必要的接口和实现类(controller service dao)
2.6.1 新建一个 product-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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<parent>
<artifactId>product-service-parent</artifactId>
<groupId>com.bxs</groupId>
<version>1.0.0</version>
</parent>
<artifactId>product-service</artifactId>
<name>${project.artifactId}</name>
<description>商品服务</description>
<dependencies>
<dependency>
<groupId>com.bxs</groupId>
<artifactId>product-service-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
2.6.2 创建工程的主类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class ProductServiceApplication {
public static void main(String[] args) {
SpringApplication.run( ProductServiceApplication.class, args );
}
}
2.6.3编写配置文件
server:
port: 8081
spring:
application:
name: product-service
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://39.105.167.131:3306/smile_boot?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
username: admin
password: admin
jpa:
properties:
hibernate:
hbm2ddl:
#auto: create
auto: update
dialect: org.hibernate.dialect.MySQL5InnoDBDialect
format_sql: true
show-sql: true
2.6.4 创建ProductDao接口
public interface ProductDao extends JpaRepository<Product, Integer> {
}
2.6.5 创建ProductService接口和实现类
@Service
public class ProductServiceImpl implements ProductService {
@Autowired
private ProductDao productDao;
@Override
public Product findByPid(Integer pid) {
return productDao.findById(pid).get();
}
}
2.6.6 创建Controller
@RestController
@Slf4j
public class ProductController {
@Autowired
private ProductService productService;
// 商品信息查询
@RequestMapping(path = "/product/{pid}")
public Product product(@PathVariable(name = "pid") Integer pid){
log.info("接下来要进行{}号商品信息的查询", pid);
Product product = productService.findByPid(pid);
return product;
}
}
2.6.7 启动工程,等到数据库表创建完毕之后,加入测试数据
image2.6.8 通过浏览器访问服务
image2.7 创建订单微服务API
步骤
- 创建模块 导入依赖
- 创建必要的接口和实体类(entity dto)
2.7.1 新建一个 order-service-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">
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<parent>
<artifactId>order-service-parent</artifactId>
<groupId>com.bxs</groupId>
<version>1.0.0</version>
</parent>
<artifactId>order-service-api</artifactId>
<name>${project.artifactId}</name>
<description>订单服务</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
</dependencies>
</project>
2.7.2 创建实体类
import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity(name = "shop_order")
@Data
public class Order {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long oid;//订单id
//用户
private Integer uid;//用户id
private String username;//用户名
//商品
private Integer pid;//商品id
private String pname;//商品名称
private Double pprice;//商品单价
//数量
private Integer number;//购买数量
}
2.8 创建订单微服务
步骤:
- 创建模块 导入依赖
- 创建SpringBoot主类
- 加入配置文件
- 创建必要的接口和实现类(controller service dao)
2.8.1 新建一个 user-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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<parent>
<artifactId>order-service-parent</artifactId>
<groupId>com.bxs</groupId>
<version>1.0.0</version>
</parent>
<artifactId>order-service</artifactId>
<name>${project.artifactId}</name>
<description>订单服务</description>
<dependencies>
<dependency>
<groupId>com.bxs</groupId>
<artifactId>order-service-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
2.8.2 编写主类
@SpringBootApplication
public class OrderServiceApplication {
public static void main(String[] args) {
SpringApplication.run( OrderServiceApplication.class, args );
}
}
2.8.3 创建配置文件
server:
port: 8091
spring:
application:
name: order-service
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://39.105.167.131:3306/smile_boot?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
username: admin
password: admin
jpa:
properties:
hibernate:
hbm2ddl:
#auto: create
auto: update
dialect: org.hibernate.dialect.MySQL5InnoDBDialect
format_sql: true
show-sql: true
2.8.4 创建 OrderDao 接口
public interface OrderDao extends JpaRepository<Order,Long> {
}
2.8.5 创建 OrderService 接口和实现类
@Service
public class OrderServiceImpl implements OrderService {
@Autowired
private OrderDao orderDao;
@Override
public void save(Order order) {
orderDao.save( order );
}
}
2.8.6 创建RestTemplate
@SpringBootApplication
public class OrderServiceApplication {
public static void main(String[] args) {
SpringApplication.run( OrderServiceApplication.class, args );
}
@Bean
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
2.8.7 创建Controller
@RestController
@Slf4j
public class OrderController {
@Autowired
private RestTemplate restTemplate;
@Autowired
private OrderService orderService;
//准备买1件商品
@GetMapping("/order/prod/{pid}")
public Order order(@PathVariable("pid") Integer pid) {
log.info(">>客户下单,这时候要调用商品微服务查询商品信息");
//通过restTemplate调用商品微服务
Product product = restTemplate.getForObject("http://localhost:8081/product/" + pid, Product.class);
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.save(order);
return order;
}
}
网友评论