目录
案例准备
技术选型
模块设计
微服务调用
创建父工程
创建基础模块
创建用户微服务
创建商品微服务
创建订单微服务
我们本次是使用的电商项目中的商品、订单、用户为案例进行讲解。
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工程,然后在pom.xml文件中添加下面内容
4.0.0 org.example springcloud-shops 1.0-SNAPSHOT shop-common pom 1.8 UTF-8 UTF-8 2.3.2.RELEASE Hoxton.SR9 2.2.6.RELEASE org.springframework.boot spring-boot-dependencies ${spring-boot.version} pom import
版本对应:
版本说明 · alibaba/spring-cloud-alibaba Wiki · GitHub
1 创建shop-common 模块,在pom.xml中添加依赖
springcloud-shops org.example 1.0-SNAPSHOT 4.0.0 shop-common org.projectlombok lombok com.alibaba fastjson 1.2.56 mysql mysql-connector-java 5.1.44
2 创建实体类
//用户
@Entity(name = "shop_user")//实体类跟数据表的对应
@Data//不再去写set和get方法
public class User {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)//数据库自增private Integer uid;//主键private String username;//用户名private String password;//密码private String telephone;//手机号
}
//商品
@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;//库存
}
//订单@Entity(name = "shop_order")@Datapublic 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;//购买数量}
步骤: 1 创建模块 导入依赖 2 创建SpringBoot主类 3 加入配置文件 4 创建必要的接口和实现类(controller service dao) 新建一个shop-user 模块,然后进行下面操作 1 创建pom.xml
springcloud-shops org.example 1.0-SNAPSHOT 4.0.0 shop-user 1.8 UTF-8 UTF-8 2.3.7.RELEASE org.springframework.boot spring-boot-starter-web org.example shop-common 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-web RELEASE compile org.springframework.boot spring-boot-starter-web RELEASE compile
2 编写主类
package com.zjy.shopuser;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class ShopUserApplication {public static void main(String[] args) {SpringApplication.run(ShopUserApplication.class, args);}}
3 创建配置文件(没有连接数据库的话server下面的删掉)
spring:application:name: shop-user
server:port: 8071datasource:driver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql:///shop?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=trueusername: rootpassword: sasajpa:properties:hibernate:hbm2ddl:auto: updatedialect: org.hibernate.dialect.MySQL5InnoDBDialect
4 编写控制层
package com.zjy.shopuser.controller;import com.zjy.model.User;import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** @author zjy* @site Bi8boYin* @company xxx公司* @create 2022-11-25 15:27*/
@RestController
@RequestMapping("/user")
public class UserController {@RequestMapping("/get/{id}")public User get(@PathVariable("id") Integer id){return new User(id,"小猪","xiaozhu111","17346958024");}
}
1 创建一个名为shop-product 的模块,并添加springboot依赖
springcloud-shops org.example 1.0-SNAPSHOT 4.0.0 shop-product 1.8 UTF-8 UTF-8 2.3.7.RELEASE org.springframework.boot spring-boot-starter-web org.example shop-common 1.0-SNAPSHOT
2 创建工程的主类
@SpringBootApplication
public class ProductApplication {public static void main(String[] args) {SpringApplication.run(ProductApplication.class);}
}
3 创建配置文件application.yml
spring:application:name: shop-product
server:port: 8081
datasource:driver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql:///shop?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=trueusername: rootpassword: sasajpa:properties:hibernate:hbm2ddl:auto: updatedialect: org.hibernate.dialect.MySQL5InnoDBDialect
4、创建controller
package com.zjy.shopproduct.controller;import com.zjy.model.Product;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** @author zjy* @site Bi8boYin* @company xxx公司* @create 2022-11-25 16:28*/@RestController
@RequestMapping("/product")
public class ProductController {@RequestMapping("/get/{pid}")public Product get(@PathVariable("pid") Integer pid){return new Product(pid,"小猪历险记",100d,23);}
}
1 创建一个名为shop-order 的模块,并添加springboot依赖
springcloud-shops org.example 1.0-SNAPSHOT 4.0.0 shop-order 1.8 UTF-8 UTF-8 2.3.7.RELEASE org.springframework.boot spring-boot-starter-web org.example shop-common 1.0-SNAPSHOT
2 创建工程的主类
package com.zjy.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);}@Beanpublic RestTemplate getRestTemplate() {return new RestTemplate();}
}
3、yml文件
server:port: 8091
spring:application:name: shop-orderdatasource:driver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql:///shop?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=trueusername: rootpassword: sasajpa:properties:hibernate:hbm2ddl:auto: updatedialect: org.hibernate.dialect.MySQL5InnoDBDialect
4编写controller层
package com.zjy.shoporder.controller;import com.zjy.model.Order;
import com.zjy.model.Product;
import com.zjy.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 zjy* @site Bi8boYin* @company xxx公司* @create 2022-11-25 16:54*/
@RestController
@RequestMapping("/order")
public class OrderController {@Autowiredprivate RestTemplate restTemplate;@RequestMapping("/get/{uid}/{pid}")public Order get(@PathVariable("uid") Integer uid,@PathVariable("pid") Integer pid){/*** 要在订单微服务中去调用 用户微服务 以及我们的 商品微服务 ,也就以为这跨项目调用httpClients*/User user = restTemplate.getForObject("http://localhost:8071/user/get/" + uid, User.class);Product product = restTemplate.getForObject("http://localhost:8081/product/get/" + pid, Product.class);Order order=new Order();order.setUsername(user.getUsername());order.setUid(user.getUid());order.setPid(product.getPid());order.setPname(product.getPname());order.setPprice(product.getPprice());order.setOid(System.currentTimeMillis());order.setNumber(product.getStock());return order;}}