微服务项目搭建
先了解一下单体、集群、分布式和微服务概念
单体、集群、分布式和微服务概念
1.单体:就是把项目都部署到同一机器上
优点:
- 项目架构简单,小型项目的话, 开发成本低
- 项目部署在一个节点上, 维护方便
缺点:
- 全部功能集成在一个工程中,对于大型项目来讲不易开发和维护
- 项目模块之间紧密耦合,单点容错率低
- 无法针对不同模块进行针对性优化和水平扩展
2.集群:把同样的服务部署到多台机器形成多节点的集群,多节点提供相同的服务,从而使系统的处理能力得到更大的提升
优点:
- 强扩展能力
- 更容易实现
- 高可用性
- 容易管理
缺点:
- 当应用出现故障需要重新修复运转时,其他服务器会接管该应用的数据区,而这个接管过程却需要消耗一些时间,应用越大,接管时间越长,会造成一定的延误。
3.分布式: 分布式是从部署的角度去讲的, 我们一个完整项目部署的时候部署到了多态机器当中,那么我们的架构就可以称之为分布式的架构
优点:
- 抽取公共的功能为服务层,提高代码复用性
缺点:
- 系统间耦合度变高,调用关系错综复杂,难以维护
4.微服务架构: 从业务拆分的角度去讲, 我们一个大的项目有很多业务, 我们可以按照业务进行拆分, 拆分成若干个小的服务,这个小的服务连接着各自的数据库, 每一个服务独立运行, 服务与服务之间互不干扰, 这个就称之为微服务架构
优点:
- 服务原子化拆分,独立打包、部署和升级,保证每个微服务清晰的任务划分,利于扩展
- 微服务之间采用RESTful等轻量级Http协议相互调用
缺点:
- 分布式系统开发的技术成本高(容错、分布式事务等)
接下来开始项目的搭建~
1. 创建父工程
创建一个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>
<groupId>cn.test</groupId>
<artifactId>shop-parent</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<!--父工程-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
</parent>
<!--依赖版本的锁定-->
<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>Hoxton.SR8</spring-cloud.version>
<spring-cloud-alibaba.version>2.2.3.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>
2. 创建商品微服务
1.创建shop-product-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">
<parent>
<artifactId>shop-parent</artifactId>
<groupId>cn.tset</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>shop-product-api</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>
</dependencies>
</project>
2 创建实体类
//商品
@Entity(name = "t_shop_product")
@Data
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long pid;//主键
private String pname;//商品名称
private Double pprice;//商品价格
private Integer stock;//库存
}
3.创建shop-product-server项目,然后在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">
<parent>
<artifactId>shop-parent</artifactId>
<groupId>cn.test</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>shop-product-server</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.56</version>
</dependency>
<dependency>
<groupId>cn.test</groupId>
<artifactId>shop-product-api</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>
4.编写启动类ProductServer.java
package cn.test;
@SpringBootApplication
public class ProductServer {
public static void main(String[] args) {
SpringApplication.run(ProductServer.class,args);
}
}
5.编写配置文件application.yml
server:
port: 8081
spring:
application:
name: product-service
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql:///shop-product?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
username: root
password: admin
jpa:
properties:
hibernate:
hbm2ddl:
auto: update
dialect: org.hibernate.dialect.MySQL5InnoDBDialect
6.在数据库中创建shop-product的数据库
7.创建ProductDao
package cn.test.dao;
public interface ProductDao extends JpaRepository<Product, Long> {
}
8.创建ProductService接口和实现类
package cn.test.service.impl;
@Service
public class ProductServiceImpl implements ProductService {
@Autowired
private ProductDao productDao;
@Override
public Product findByPid(Long pid) {
return productDao.findById(pid).get();
}
}
9.创建Controller
package cn.test.controller;
@RestController
@Slf4j
public class ProductController {
@Autowired
private ProductService productService;
//商品信息查询
@RequestMapping("/product")
public Product findByPid(@RequestParam("pid") Long pid) {
log.info("接下来要进行{}号商品信息的查询", pid);
Product product = productService.findByPid(pid);
log.info("商品信息查询成功,内容为{}", JSON.toJSONString(product));
return product;
}
}
10.启动工程,等到数据库表创建完毕之后,加入测试数据
INSERT INTO t_shop_product VALUE(NULL,'小米','1000','5000');
INSERT INTO t_shop_product VALUE(NULL,'华为','2000','5000');
INSERT INTO t_shop_product VALUE(NULL,'苹果','3000','5000');
INSERT INTO t_shop_product VALUE(NULL,'OPPO','4000','5000');
11.通过浏览器访问服务
3. 创建订单微服务
1.创建shop-order-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">
<parent>
<artifactId>shop-parent</artifactId>
<groupId>cn.test</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>shop-order-api</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>
</dependencies>
</project>
2 创建实体类
//订单
@Entity(name = "t_shop_order")
@Data
public class Order {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long oid;//订单id
//用户
private Long uid;//用户id
private String username;//用户名
//商品
private Long pid;//商品id
private String pname;//商品名称
private Double pprice;//商品单价
//数量
private Integer number;//购买数量
}
3.创建shop-order-server项目,然后在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">
<parent>
<artifactId>shop-parent</artifactId>
<groupId>cn.test</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>shop-order-server</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.56</version>
</dependency>
<dependency>
<groupId>cn.test</groupId>
<artifactId>shop-order-api</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>
4.编写启动类OrderServer.java
package cn.test;
@SpringBootApplication
public class OrderServer {
public static void main(String[] args) {
SpringApplication.run(OrderServer.class,args);
}
}
5.编写配置文件application.yml
server:
port: 8082
spring:
application:
name: order-service
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql:///shop-order?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
username: root
password: admin
jpa:
properties:
hibernate:
hbm2ddl:
auto: update
dialect: org.hibernate.dialect.MySQL5InnoDBDialect
6.在数据库中创建shop-order的数据库
7.创建OrderDao
package cn.test.dao;
public interface OrderDao extends JpaRepository<Order, Long> {
}
8.创建OrderService接口和实现类
package cn.test.service.impl;
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
@Autowired
private OrderDao orderDao;
@Override
public Order createOrder(Long productId,Long userId) {
// TODO: 2021/5/6 后面需要通过productId查询真正Product数据
//通过Feign实现远程调用
//Product product = productFeignApi.findByPid(productId);
Order order = new Order();
order.setUid(userId);
order.setUsername("James");
order.setPid(1);
order.setPname("小米data from port:8081");
order.setPprice("1000");
order.setNumber(1);
orderDao.save(order);
log.info("创建订单成功,订单信息为{}", JSON.toJSONString(order));
return order;
}
}
9.创建Controller
package cn.test.controller;
@RestController
@Slf4j
public class OrderController {
@Autowired
private OrderService orderService;
@RequestMapping("/save")
public Order order(Long pid,Long uid) {
return orderService.createOrder(pid,uid);
}
}
到这里我们已经把一个最简单的微服务项目搭建好了,可以看到上面我们把商品服务和订单服务各自搭建SpringBoot项目,在真正的开发中,每个微服务都是部署到各自一台机器上的,但由于只是学习用的,也只有一台电脑,所以在这里只是模拟部署到两台机器上了。分别部署到了两个Tomcat上,端口分别是8081和8082。
上面案例并没有真正通过远程调用product-service微服务去获取真正的Product数据,后面分享Ribbon和Feign组件时会实现远程调用微服务。
springCloud Alibaba之Nacos组件