微服務環境搭建
我們本次是使用的電商專案中的商品、訂單、使用者為案例進行講解。
案例準備1.1 技術選型
maven:3.3.9
資料庫:MySQL 5.7
持久層: SpingData Jpa
其他: SpringCloud Alibaba 技術棧
1.2 模組設計
springcloud-alibaba 父工程
shop-common 公共模組【實體類】
shop-user 使用者微服務 【埠: 807x】
shop-product 商品微服務 【埠: 808x】
shop-order 訂單微服務 【埠: 809x】
2.1.3 微服務呼叫
在微服務架構中,最常見的場景就是微服務之間的相互呼叫。我們以電商系統中常見的使用者下單為例來演示微服務的呼叫:客戶向訂單微服務發起一個下單的請求,在進行儲存訂單之前需要呼叫商品微服務查詢商品的資訊。
我們一般把服務的主動呼叫方稱為服務消費者,把服務的被呼叫方稱為服務提供者。
在這種場景下,訂單微服務就是一個服務消費者, 商品微服務就是一個服務提供者。
2 建立父工程建立一個maven工程,然後在pom.xml檔案中新增下面內容
<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.0http://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.itheima</groupId><artifactId>springcloud-alibaba</artifactId><version>1.0-SNAPSHOT</version><packaging>pom</packaging><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>
版本對應:
3 建立基礎模組1 建立 shop-common 模組,在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.0http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>springcloud-alibaba</artifactId><groupId>com.itheima</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></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></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 建立實體類
//使用者@Entity(name = "shop_user")@Datapublic class User {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Integer uid;//主鍵private String username;//使用者名稱private String password;//密碼private String telephone;//手機號}//商品@Entity(name = "shop_product")@Datapublic class Product {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)
4 建立使用者微服務
步驟:
建立模組 匯入依賴 建立SpringBoot主類加入配置檔案 建立必要的介面和實現類(controller service dao)新建一個 shop-user 模組,然後進行下面操作
1 建立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.0http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>springcloud-alibaba</artifactId><groupId>com.itheima</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>shop-user</artifactId><dependencies><dependency><groupId>com.itheima</groupId><artifactId>shop-common</artifactId><version>1.0-SNAPSHOT</version></dependency></dependencies></project>
2 編寫主類
@SpringBootApplication@EnableDiscoveryClientpublic class UserApplication {public static void main(String[] args) {SpringApplication.run(UserApplication.class, args);}}
3 建立配置檔案
server:port: 8071spring:application:name: service-productdatasource:driver-class-name: com.mysql.jdbc.Driver
5 建立商品微服務
1 建立一個名為 shop_product 的模組,並新增springboot依賴
<?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.0http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>springcloud-alibaba</artifactId><groupId>com.itheima</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.itheima</groupId><artifactId>shop-common</artifactId><version>1.0-SNAPSHOT</version></dependency></dependencies></project>
2 建立工程的主類
package com.itheima;@SpringBootApplicationpublic class ProductApplication {public static void main(String[] args) {SpringApplication.run(ProductApplication.class, args);}}
3 建立配置檔案application.yml
server:port: 8081spring: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
4 建立ProductDao介面
package com.itheima.dao;public interface ProductDao extends JpaRepository<Product,Integer> {}
5 建立ProductService介面和實現類
package com.itheima.service.impl;@Servicepublic class ProductServiceImpl implements ProductService {@Autowiredprivate ProductDao productDao;@Overridepublic Product findByPid(Integer pid) {return productDao.findById(pid).get();}}
6 建立Controller
@RestController@Slf4jpublic class ProductController {@Autowiredprivate ProductService productService;@GetMapping("/product/{pid}")public Product product(@PathVariable("pid") Integer pid) {Product product = productService.findByPid(pid);log.info("查詢到商品:" + JSON.toJSONString(product));return product;}}
7 啟動工程,等到資料庫表建立完畢之後,加入測試資料
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 透過瀏覽器訪問服務
6 建立訂單微服務1 建立一個名為 shop-order 的模組,並新增springboot依賴
<?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.0http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>springcloud-alibaba</artifactId><groupId>com.itheima</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.itheima</groupId><artifactId>shop-common</artifactId><version>1.0-SNAPSHOT</version></dependency></dependencies></project>
2 建立工程的主類
package com.itheima;@SpringBootApplicationpublic class OrderApplication {public static void main(String[] args) {SpringApplication.run(OrderApplication.class, args);}}
3 建立配置檔案application.yml
server:port: 8091spring: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
4 建立OrderDao介面
package com.itheima.dao;public interface OrderDao extends JpaRepository<Order,Long> {}
5 建立OrderService介面和實現類
@Servicepublic class OrderServiceImpl implements OrderService {@Autowiredprivate OrderDao orderDao;@Overridepublic void save(Order order) {orderDao.save(order);}}
6 建立RestTemplate
@SpringBootApplicationpublic class OrderApplication {public static void main(String[] args) {SpringApplication.run(OrderApplication.class, args);}@Beanpublic RestTemplate getRestTemplate() {return new RestTemplate();}}
7 建立Controller
package com.itheima.controller;@RestController@Slf4jpublic 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);
8 啟動工程,透過瀏覽器訪問服務進行測試