微服務環境搭建
我們這次是使用的電商專案的商品、訂單、使用者為案例進行講解
2.1 案例準備
2.1.1 技術選型
- maven :3.3.9
- 資料庫:mysql
- 持久層:SpringData JPA
- SpringCloud Alibaba技術棧
2.1.2 模組設計
- springcloud-alibaba 父工程
- shop-common 公共模組
- shop-user 使用者模組
- shop-product 商品模組
- shop-order 訂單模組
2.1.3 微服務呼叫
在微服務架構中,最常見的場景就是微服務之間的相互呼叫。我們以電商系統中常見的使用者下單為例來 演示微服務的呼叫:客戶向訂單微服務發起一下單的請求,在進行儲存訂單之前需要呼叫商品微服務 查詢商品的資訊
我們一般把服務的主動呼叫方稱為服務消費者,把服務的被呼叫方稱為服務提供者.
2.2建立父工程
<?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.ityml</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>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</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>
</dependencies>
</dependencyManagement>
</project>
對應版本:
https://www.cnblogs.com/onehm/p/13931711.html
2.3建立基礎模組
2.3.1 shop-common模組
-
在pom中新增依賴
<?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>com.ityml</groupId> <artifactId>springcloud-alibaba</artifactId> <version>1.0-SNAPSHOT</version> </parent> <groupId>com.ityml</groupId> <artifactId>shop-common</artifactId> <version>1.0-SNAPSHOT</version> <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.79</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.49</version> </dependency> </dependencies> </project>
-
建立實體類
/** * ----------------------------- * PackageName: com.ityml.entity * ClassName:User * Description:使用者 * * @author:it-yml CreateTime:2022-03-22 * ----------------------------- */ @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; }
/** * ----------------------------- * PackageName: com.ityml.entity * ClassName:Product * Description:商品 * * @author:it-yml CreateTime:2022-03-22 * ----------------------------- */ @Entity(name = "shop-product") @Data public class Product { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private String pname; private Double price; private Integer stock; }
/** * ----------------------------- * PackageName: com.ityml.entity * ClassName:Order * Description:訂單 * * @author:it-yml CreateTime:2022-03-22 * ----------------------------- */ @Entity(name = "shop-order") @Data public class Order { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long oid; private Integer uid; private String username; }
2.4 建立使用者微服務
步驟:
- 建立模組
- 建立SpringBoot主類
- 加入配置檔案
- 建立必要的介面和實現類
建立一個shop-user模組,然後進行下面操作
-
建立pom
<?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>com.ityml</groupId> <artifactId>springcloud-alibaba</artifactId> <version>1.0-SNAPSHOT</version> </parent> <groupId>com.ityml</groupId> <artifactId>shop-user</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>com.ityml</groupId> <artifactId>shop-common</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> </project>
-
編寫主類
package com.ityml; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * ----------------------------- * PackageName: com.ityml * ClassName:UserApplication * Description:啟動類 * * @author:it-yml CreateTime:2022-03-22 * ----------------------------- */ @SpringBootApplication public class UserApplication { public static void main(String[] args) { SpringApplication.run(UserApplication.class,args); } }
-
建立配置檔案
server: port: 8080 spring: application: name: service-user datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:// username: data-password: jpa: properties: hibernate: hbm2ddl: auto: update dialect: org.hibernate.dialect.MySQL.5InnoDDialect
2.5 建立商品微服務
-
建立一個名為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.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.ityml</groupId> <artifactId>springcloud-alibaba</artifactId> <version>1.0-SNAPSHOT</version> </parent> <groupId>com.ityml</groupId> <artifactId>shop-product</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.ityml</groupId> <artifactId>shop-common</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> </project>
-
建立工程主類
package com.ityml; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * ----------------------------- * PackageName: com.ityml * ClassName:ProductApplication * Description:商品主類 * * @author:it-yml CreateTime:2022-03-23 * ----------------------------- */ @SpringBootApplication public class ProductApplication { public static void main(String[] args) { SpringApplication.run(ProductApplication.class,args); } }
-
建立配置檔案application.yml
server: port: 8080 spring: application: name: service-product datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:// username: data-password: jpa: properties: hibernate: hbm2ddl: auto: update dialect: org.hibernate.dialect.MySQL.5InnoDDialect
-
建立ProductDao介面
package com.ityml.dao; import com.ityml.entity.Product; import org.springframework.data.jpa.repository.JpaRepository; /** * ----------------------------- * PackageName: com.ityml.dao * ClassName:ProductDao * Description: * * @author:it-yml CreateTime:2022-03-23 * ----------------------------- */ public interface ProductDao extends JpaRepository<Product,Integer> { }
-
建立ProductService介面和實現類
package com.ityml.service; import com.ityml.entity.Product; /** * ----------------------------- * PackageName: com.ityml.service * ClassName:ProductService * Description: * * @author:it-yml CreateTime:2022-03-23 * ----------------------------- */ public interface ProductService { Product findById(Integer pid); }
package com.ityml.impl; import com.ityml.dao.ProductDao; import com.ityml.entity.Product; import com.ityml.service.ProductService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** * ----------------------------- * PackageName: com.ityml.impl * ClassName:ProductServiceImpl * Description:實現類 * * @author:it-yml CreateTime:2022-03-23 * ----------------------------- */ @Service public class ProductServiceImpl implements ProductService { @Autowired private ProductDao productDao; @Override public Product findById(Integer pid) { return productDao.findById(pid).get(); } }
-
建立Controller
package com.ityml.controller; import com.ityml.entity.Product; import com.ityml.service.ProductService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * ----------------------------- * PackageName: com.ityml.controller * ClassName:ProductController * Description:主類 * * @author:it-yml CreateTime:2022-03-23 * ----------------------------- */ @RestController @Slf4j public class ProductController { @Autowired private ProductService productService; @GetMapping("/product/{pid}") public Product product(@PathVariable("pid") Integer pid){ Product product = productService.findById(pid); return product; } }
-
啟動工程
2.6 建立訂單微服務
-
建立一個名為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.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.ityml</groupId> <artifactId>springcloud-alibaba</artifactId> <version>1.0-SNAPSHOT</version> </parent> <groupId>com.ityml</groupId> <artifactId>shop-order</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.ityml</groupId> <artifactId>shop-common</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> </project>
-
建立工程主類
package com.ityml; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * ----------------------------- * PackageName: com.ityml * ClassName:OrderApplication * Description:主類 * * @author:it-yml CreateTime:2022-03-23 * ----------------------------- */ @SpringBootApplication public class OrderApplication { public static void main(String[] args) { SpringApplication.run(OrderApplication.class,args); } }
-
建立配置檔案
server: port: 8080 spring: application: name: service-order datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:// username: data-password: jpa: properties: hibernate: hbm2ddl: auto: update dialect: org.hibernate.dialect.MySQL.5InnoDDialect
-
建立OrderDao介面
package com.ityml.dao; import com.ityml.entity.Order; import org.springframework.data.jpa.repository.JpaRepository; /** * ----------------------------- * PackageName: com.ityml.dao * ClassName:ProductDao * Description: * * @author:it-yml CreateTime:2022-03-23 * ----------------------------- */ public interface OrderDao extends JpaRepository<Order,Integer> { }
-
建立OrderService介面和實現類
package com.ityml.service; import com.ityml.entity.Order; /** * ----------------------------- * PackageName: com.ityml.service * ClassName:ProductService * Description: * * @author:it-yml CreateTime:2022-03-23 * ----------------------------- */ public interface OrderService { void save(Order order); }
-
建立RestTemplate
package com.ityml; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; /** * ----------------------------- * PackageName: com.ityml * ClassName:OrderApplication * Description:主類 * * @author:it-yml CreateTime:2022-03-23 * ----------------------------- */ @SpringBootApplication public class OrderApplication { public static void main(String[] args) { SpringApplication.run(OrderApplication.class,args); } @Bean public RestTemplate getRestTemplate(){ return new RestTemplate(); } }
-
建立Controller
package com.ityml.controller; import com.ityml.entity.Order; import com.ityml.entity.Product; import com.ityml.service.OrderService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; /** * ----------------------------- * PackageName: com.ityml.controller * ClassName:OrderController * Description:主類 * * @author:it-yml CreateTime:2022-03-23 * ----------------------------- */ @RestController @Slf4j public class OrderController { @Autowired private OrderService orderService; @Autowired private RestTemplate restTemplate; /** * 準備買一件商品 * @param pid */ @GetMapping("/order/prod/{pid}") public void Order(@PathVariable("pid") Integer pid) { Product product = restTemplate.getForObject("http:/localhost:8081/product" + pid, Product.class); Order order = new Order(); orderService.save(order); } }
-
啟動工程,通過瀏覽器訪問服務進行測試