前面讲了微服务的许多概念和简单性对阶段代码。我们本次就系统化的实现一个小型微服务项目,使用电商项目中的商品、订单、用户为案例来实现。
springcloud-alibaba
:父工程shop-common
:公共模块【实体类】shop-user
:用户微服务 【端口: 807x】shop-product
:商品微服务 【端口: 808x】shop-order
:订单微服务 【端口: 809x】在微服务架构中,最常见的场景就是微服务之间的相互调用。我们以电商系统中常见的用户下单为例来 演示微服务的调用:
客户向订单微服务发起一个下单的请求,在进行保存订单之前需要调用商品微服务查询商品的信息。
我们一般把服务的主动调用方称为服务消费者,把服务的被调用方称为服务提供者。
在这种场景下,订单微服务就是一个服务消费者, 商品微服务就是一个服务提供者。
创建一个maven工程,然后在pom.xml文件中添加下面内容。
4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.3.RELEASE com.itheima springcloud-alibaba 1.0-SNAPSHOT pom 1.8 UTF-8 UTF-8 Greenwich.RELEASE 2.1.0.RELEASE org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import
创建 shop-common 模块,在pom.xml中添加依赖:
springcloud-alibaba com.itheima 1.0-SNAPSHOT 4.0.0 shop-common org.springframework.boot spring-boot-starter-data-jpa org.projectlombok lombok com.alibaba fastjson 1.2.56 mysql mysql-connector-java 5.1.6
用户实体类:
//用户
@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;//手机号
}
商品实体类:
//商品
@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")
@Data
public class Order {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long oid;//订单idprivate Integer uid;//用户idprivate String username;//用户名
}
步骤:
新建一个 shop-user
模块,然后进行下面操作:
依赖于 shop-cmmon
模块
springcloud-alibaba com.itheima 1.0-SNAPSHOT 4.0.0 shop-user com.itheima shop-common 1.0-SNAPSHOT
@SpringBootApplication
@EnableDiscoveryClient
public class UserApplication {
public static void main(String[] args) { SpringApplication.run(UserApplication.class, args); }
}
server:port: 8071
spring:application:name: service-productdatasource:driver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql:///shop?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=trueusername: rootpassword: rootjpa:properties:hibernate:hbm2ddl:auto: updatedialect: org.hibernate.dialect.MySQL5InnoDBDialect
创建一个名为 shop_product
的模块,并添加springboot依赖,同样依赖于shop-common
:
springcloud-alibaba com.itheima 1.0-SNAPSHOT 4.0.0 shop-product org.springframework.boot spring-boot-starter-web com.itheima shop-common 1.0-SNAPSHOT
package com.itheima;
@SpringBootApplication
public class ProductApplication {
public static void main(String[] args) { SpringApplication.run(ProductApplication.class, args); }
}
server:
port: 8081
spring:
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
package com.itheima.dao;
public interface ProductDao extends JpaRepository {
}
package com.itheima.service.impl;
@Service
public class ProductServiceImpl implements ProductService {
@Autowired
private ProductDao productDao;
@Override
public Product findByPid(Integer pid) {
return productDao.findById(pid).get();
}
}
@RestController
@Slf4j
public class ProductController {
@Autowired
private ProductService productService;
@GetMapping("/product/{pid}")
public Product product(@PathVariable("pid") Integer pid) {
Product product = productService.findByPid(pid); log.info("查询到商品:" + JSON.toJSONString(product)); return product;
}
}
启动商品微服务工程,等到数据库表创建完毕之后,加入测试数据:
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');
通过浏览器访问服务:
表明,商品微服务已完成,可用。
创建一个名为 shop-order
的模块,并添加springboot依赖:
springcloud-alibaba com.itheima 1.0-SNAPSHOT 4.0.0 shop-order org.springframework.boot spring-boot-starter-web com.itheima shop-common 1.0-SNAPSHOT
package com.itheima;
@SpringBootApplication
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
}
server:
port: 8091
spring:
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
package com.itheima.dao;
public interface OrderDao extends JpaRepository {
}
@Service
public class OrderServiceImpl implements OrderService {
@Autowired
private OrderDao orderDao;
@Override
public void save(Order order) {
orderDao.save(order);
}
}
@SpringBootApplication
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
@Bean
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
package com.itheima.controller;
@RestController
@Slf4j
public class OrderController {@Autowiredprivate RestTemplate restTemplate;@Autowiredprivate 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);
}
至此,用户微服务、商品微服务、订单微服务都已经简单的搭好了,也能互相调用。但相信大家发现其中的一个点,就是我们把服务提供者的网络地址(ip,端 口)等硬编码到了代码中。
例如:
通过RestTemplate,把服务提供者的网络地址(ip,端 口)等硬编码到了代码中,这样会有什么问题呢?该怎么进行改进呢?大家可以想一下,我们在下篇文章中会解决这个问题。
从这篇文章中,大家可以简单的了解到微服务的架构是怎样的,其中的工程原理以及服务之间的调用等。