目录
微服务架构, 简单的说就是将单体应用进一步拆分,拆分成更小的服务,每个服务都是一个可以独 立运行的项目。
一旦采用微服务系统架构,就势必会遇到这样几个问题:
这么多小服务,如何管理他们?(服务治理 注册中心[服务注册 发现 剔除])
这么多小服务,他们之间如何通讯?(restful rpc)
这么多小服务,客户端怎么访问他们?(网关)
这么多小服务,一旦出现问题了,应该如何自处理?(容错)
这么多小服务,一旦出现问题了,应该如何排错? (链路追踪)
对于上面的问题,是任何一个微服务设计者都不能绕过去的,因此大部分的微服务产品都针对每一 个问题提供了相应的组件来解决它们。
实现项目之间的相互调用,使用的电商项目中的商品、订单、用户为案例
技术选型:
maven:3.5.4
数据库:MySQL 5.7
持久层: SpingData Jpa/Mybatis-plus
其他: SpringCloud Alibaba 技术栈
模块设计:
springcloud-shop父工程
shop-common 公共模块【实体类】
shop-user 用户微服务 【端口: 807x】
shop-product 商品微服务 【端口: 808x】
shop-order 订单微服务 【端口: 809x】
在微服务架构中,最常见的场景就是微服务之间的相互调用。我们以电商系统中常见的用户下单为 例来演示微服务的调用:客户向订单微服务发起一个下单的请求,在进行保存订单之前需要调用商品微 服务查询商品的信息。 我们一般把服务的主动调用方称为服务消费者,把服务的被调用方称为服务提供者。 在这种场景下,订单微服务就是一个服务消费者, 商品微服务就是一个服务提供者
创建一个maven工程
- "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.0modelVersion>
-
- <groupId>com.cdlgroupId>
- <artifactId>spcloud-shopartifactId>
- <version>1.0-SNAPSHOTversion>
- <modules>
- <module>shop-commonmodule>
- modules>
- <packaging>pompackaging>
-
-
- <properties>
- <java.version>1.8java.version>
- <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
- <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
- <spring-boot.version>2.3.2.RELEASEspring-boot.version>
- <spring-cloud.version>Hoxton.SR9spring-cloud.version>
- <spring-cloud-alibaba.version>2.2.6.RELEASEspring-cloud-alibaba.version>
- properties>
-
- <dependencyManagement>
- <dependencies>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-dependenciesartifactId>
- <version>${spring-boot.version}version>
- <type>pomtype>
- <scope>importscope>
- dependency>
- dependencies>
-
- dependencyManagement>
-
-
- project>
版本对应:
也是一个maven项目
创建成功之后
此时的父工程也发生了改变
- "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>spcloud-shopartifactId>
- <groupId>com.cdlgroupId>
- <version>1.0-SNAPSHOTversion>
- parent>
- <modelVersion>4.0.0modelVersion>
-
- <artifactId>shop-commonartifactId>
-
-
- <dependencies>
- <dependency>
- <groupId>org.projectlombokgroupId>
- <artifactId>lombokartifactId>
- dependency>
- <dependency>
- <groupId>com.alibabagroupId>
- <artifactId>fastjsonartifactId>
- <version>1.2.56version>
- dependency>
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- <version>5.1.44version>
- dependency>
- dependencies>
-
-
- project>
在基础类创建实体类
订单类 Order
- package com.cdl.model;
-
- import lombok.AllArgsConstructor;
- import lombok.Data;
- import lombok.NoArgsConstructor;
-
- @Data
- @AllArgsConstructor
- @NoArgsConstructor
-
- public class Order {
- private Long oid;//订单id
-
- //用户
- private Integer uid;//用户id
- private String username;//用户名
-
- //商品
- private Integer pid;//商品id
- private String pname;//商品名称
- private Double pprice;//商品单价
-
- //数量
- private Integer number;//购买数量
- }
- //订单
商品类 Product
- package com.cdl.model;
-
- import lombok.AllArgsConstructor;
- import lombok.Data;
- import lombok.NoArgsConstructor;
-
- //商品
- @Data
- @AllArgsConstructor
- @NoArgsConstructor
- public class Product {
- private Integer pid;//主键
-
- private String pname;//商品名称
- private Double pprice;//商品价格
- private Integer stock;//库存
- }
用户类 User
- package com.cdl.model;
-
- import lombok.AllArgsConstructor;
- import lombok.Data;
- import lombok.NoArgsConstructor;
-
- @Data//不再去写set和get方法
- @AllArgsConstructor
- @NoArgsConstructor
- public class User {
- private Integer uid;//主键
- private String username;//用户名
- private String password;//密码
- private String telephone;//手机号
- }
创建商品微服务和创建订单微服务同上一样的步骤
写方法和接口 实现跨服务调用
- "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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
-
- <parent>
- <artifactId>spcloud-shopartifactId>
- <groupId>com.cdlgroupId>
- <version>1.0-SNAPSHOTversion>
- parent>
-
- <modelVersion>4.0.0modelVersion>
- <artifactId>shop-userartifactId>
-
- <dependencies>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-webartifactId>
- dependency>
-
- <dependency>
- <groupId>com.cdlgroupId>
- <artifactId>shop-commonartifactId>
- <version>1.0-SNAPSHOTversion>
- dependency>
- dependencies>
-
- project>
- package com.cdl.shopuser.Ctroller;
-
- import com.cdl.model.User;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- /**
- * @author cdl
- * @site www.cdl.com
- * @create 2022-11-25 15:44
- */
- @RestController
- @RequestMapping("/user")
- public class UserCtroller {
- @RequestMapping("/get/{id}")
- public User get(@PathVariable("id")Integer id){
- return new User(id,"内马尔","123456","14567896543");
- }
- }
spring: application: name: shop-user server: port: 8070
- "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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
-
-
- <parent>
- <artifactId>spcloud-shopartifactId>
- <groupId>com.cdlgroupId>
- <version>1.0-SNAPSHOTversion>
- parent>
-
- <modelVersion>4.0.0modelVersion>
- <artifactId>shop-productartifactId>
-
- <dependencies>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-webartifactId>
- dependency>
-
- <dependency>
- <groupId>com.cdlgroupId>
- <artifactId>shop-commonartifactId>
- <version>1.0-SNAPSHOTversion>
- dependency>
- dependencies>
-
- project>
- package com.cdl.shopproduct.Controller;
-
- import com.cdl.model.Product;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- /**
- * @author cdl
- * @site www.cdl.com
- * @create 2022-11-25 15:51
- */
- @RestController
- @RequestMapping("/product")
- public class ProductCtroller {
-
- @RequestMapping("/get/{pid}")
- public Product get(@PathVariable("pid") Integer pid){
- return new Product(pid,"西游记",6d,20);
- }
-
- }
spring: application: name: shop-product server: port: 8080
- "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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
-
-
- <parent>
- <artifactId>spcloud-shopartifactId>
- <groupId>com.cdlgroupId>
- <version>1.0-SNAPSHOTversion>
- parent>
-
- <modelVersion>4.0.0modelVersion>
- <artifactId>shop-orderartifactId>
-
- <dependencies>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-webartifactId>
- dependency>
-
- <dependency>
- <groupId>com.cdlgroupId>
- <artifactId>shop-commonartifactId>
- <version>1.0-SNAPSHOTversion>
- dependency>
- dependencies>
-
- project>
- package com.cdl.shoporder.Controller;
-
- import com.cdl.model.Order;
- import com.cdl.model.Product;
- import com.cdl.model.User;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- import org.springframework.web.client.RestTemplate;
-
- /**
- * @author cdl
- * @site www.cdl.com
- * @create 2022-11-25 15:43
- */
- @RestController
- @RequestMapping("/order")
- public class OrderCtroller {
-
- @Autowired
- private RestTemplate restTemplate;
-
- @RequestMapping("/get/{uid}/{pid}")
- public Order get(@PathVariable("uid") Integer uid,
- @PathVariable("pid") Integer pid){
- //要在订单微服务中调用 用户微服务、商品微服务 跨项目调用
- User user = restTemplate.getForObject("http://localhost:8070/user/get/" + uid, User.class);
- Product product = restTemplate.getForObject("http://localhost:8080/product/get/" + pid, Product.class);
- Order order = new Order();
- order.setUsername(user.getUsername());
- order.setUid(user.getUid());
- order.setPprice(product.getPprice());
- order.setPname(product.getPname());
- order.setPid(product.getPid());
- order.setOid(System.currentTimeMillis());
- order.setNumber(product.getStock());
- return order;
- }
-
- }
- package com.cdl.shoporder;
-
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.context.annotation.Bean;
- import org.springframework.web.client.RestTemplate;
-
- @SpringBootApplication
- public class ShopOrderApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(ShopOrderApplication.class, args);
- }
-
- @Bean
- public RestTemplate restTemplate(){
- return new RestTemplate();
- }
-
- }
spring: application: name: shop-order server: port: 8090
访问用户
访问商品
跨服务访问
若出现以下报错 按途中删除test类就🆗了 注意不要删基础类的