摘要:通过使用电商项目中的商品、订单、用户为案例进行实例演示。
maven:3.3.9 数据库:MySQL 5.7 持久层: SpingData Jpa 其他: SpringCloud Alibaba 技术栈
springcloud-alibaba 父工程 shop-common 公共模块【实体类】 shop-user 用户微服务 【端口: 807x】 shop-product 商品微服务 【端口: 808x】 shop-order 订单微服务 【端口: 809x】
在微服务架构中,最常见的场景就是微服务之间的相互调用。我们以电商系统中常见的用户下单为例来演示微服务的调用:客户向订单微服务发起一个下单的请求,在进行保存订单之前需要调用商品微服务查询商品的信息。
我们一般把服务的主动调用方称为服务消费者,把服务的被调用方称为服务提供者。
在这种场景下,订单微服务就是一个服务消费者, 商品微服务就是一个服务提供者。
创建一个maven工程,然后在pom.xml文件中添加下面内容
12345678910111213141516171819202122232425262728293031323334353637383940414243 | <?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.taiji</groupId> <artifactId>springcloud-alibaba</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <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> |
---|
版本对应:
1 创建shop-common
模块,在pom.xml中添加依赖
1234567891011121314151617181920212223242526272829303132333435 | <?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.taiji</groupId> <version>1.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> <version>2.3.0.RELEASE</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.2</version> </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> |
---|
2 创建实体类
123456789101112 | //User.java//用户@Entity(name = "shop_user")@Datapublic class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer uid;//主键 private String username;//用户名 private String password;//密码 private String telephone;//手机号} |
---|
1234567891011 | //Product.java//商品@Entity(name = "shop_product")@Datapublic class Product { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer pid;//主键 private String pname;//商品名称 private Double pprice;//商品价格 private Integer stock;//库存} |
---|
123456789101112131415 | //Order.java//订单@Entity(name = "shop_order")@Data //加上Data装饰器,无需指定get和set方法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;//购买数量} |
---|
步骤:
新建一个shop-user
模块,然后进行下面操作
1 创建pom.xml
1234567891011121314151617181920 | <?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.taiji</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>shop-user</artifactId> <dependencies> <dependency> <groupId>com.taiji</groupId> <artifactId>shop-common</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies></project> |
---|
2 编写主类UserApplication.java
123456 | @SpringBootApplicationpublic class UserApplication { public static void main(String[] args) { SpringApplication.run(UserApplication.class, args); }} |
---|
3 创建配置文件application.yml
12345678910111213141516 | server: port: 8071spring: application: name: service-user datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql:///shop?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true username: root password: root jpa: properties: hibernate: hbm2ddl: auto: update dialect: org.hibernate.dialect.MySQL5InnoDBDialect |
---|
1 创建一个名为shop_product
的模块,并添加springboot依赖
123456789101112131415161718192021222324 | <?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.taiji</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>shop-product</artifactId> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.taiji</groupId> <artifactId>shop-common</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies></project> |
---|
2 创建工程的主类ProductApplication.java
123456 | @SpringBootApplicationpublic class ProductApplication { public static void main(String[] args) { SpringApplication.run(ProductApplication.class, args); }} |
---|
3 创建配置文件application.yml
12345678910111213141516 | server: port: 8081spring: application: name: service-product datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql:///shop?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true username: root password: root jpa: properties: hibernate: hbm2ddl: auto: update dialect: org.hibernate.dialect.MySQL5InnoDBDialect |
---|
4 创建ProductDao
接口
12 | public interface ProductDao extends JpaRepository<Product,Integer> {} |
---|
5 创建ProductService接口和实现类
123 | public interface ProductService { Product findByPid(Integer pid);} |
---|
123456789 | @Servicepublic class ProductServiceImpl implements ProductService { @Autowired private ProductDao productDao; public Product findByPid(Integer pid) { return productDao.findById(pid).get(); }} |
---|
6 创建Controller
1234567891011121314151617 | @RestController@Slf4jpublic class ProductController { @Autowired private ProductService productService; @RequestMapping("/product/{pid}") public Product product(@PathVariable("pid")Integer pid){ log.info("接下来要进行{}号商品信息的查询",pid); Product product=productService.findByPid(pid); log.info("商品信息查询成功,内容为{}", JSON.toJSONString(product)); return product; }} |
---|
7 启动工程,等到数据库表创建完毕之后,加入测试数据
1234 | INSERT INTO shop_product VALUE(NULL,'小米','1000','5000');INSERT INTO shop_product VALUE(NULL,'华为','2000','5000');INSERT INTO shop_product VALUE(NULL,'苹果','3000','5000');INSERT INTO shop_product VALUE(NULL,'OPPO','4000','5000'); |
---|
8 通过浏览器访问服务
1 创建一个名为shop-order
的模块,并添加springboot依赖
12345678910111213141516171819202122232425 | <?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.taiji</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>shop-order</artifactId> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.taiji</groupId> <artifactId>shop-common</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies></project> |
---|
2 创建工程的主类OrderApplication.java
123456 | @SpringBootApplicationpublic class UserApplication { public static void main(String[] args) { SpringApplication.run(UserApplication.class, args); }} |
---|
3 创建配置文件application.yml
12345678910111213141516 | server: port: 8091spring: application: name: service-order datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql:///shop?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true username: root password: root jpa: properties: hibernate: hbm2ddl: auto: update dialect: org.hibernate.dialect.MySQL5InnoDBDialect |
---|
4 创建OrderDao
接口
12 | public interface OrderDao extends JpaRepository<Order,Integer> {} |
---|
5 创建OrderService接口和实现类
123 | public interface OrderService { void createOrder(Order order);} |
---|
123456789 | @Servicepublic class OrderServiceImpl implements OrderService { @Autowired private OrderDao orderDao; public void createOrder(Order order) { orderDao.save(order); }} |
---|
6 创建RestTemplate
1234567891011 | @SpringBootApplicationpublic class OrderApplication { public static void main(String[] args) { SpringApplication.run(OrderApplication.class, args); } @Bean public RestTemplate restTemplate(){ return new RestTemplate(); }} |
---|
7 创建Controller
123456789101112131415161718192021222324252627282930313233 | @RestController@Slf4jpublic class OrderController { @Autowired private RestTemplate restTemplate; @Autowired private OrderService orderService; @RequestMapping("/order/prod/{pid}") public Order order(@PathVariable("pid") Integer pid) { log.info("接收到{}号商品的下单请求,接下来调用商品微服务查询此商品信息", pid); ///1 一旦服务提供者的地址信息变化了,就得修改服务调用者的代码 ///2 一旦服务提供者做了集群,服务调用者一方无法实现负载均衡的去调用 ///3 一旦微服务变得越来越多,如何管理这个服务清单就成了问题 Product product = restTemplate.getForObject("http://localhost:8081/product/" + pid, Product.class); log.info("查询到{}号商品的信息,内容是{}", pid, 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.createOrder(order); log.info("创建订单成功,订单信息为{}", JSON.toJSONString(order)); return order; }} |
---|
8 启动工程,通过浏览器访问服务进行测试