SpringCloudAlibaba Series 1: Microservices concepts and SpringCloudAlibaba introduction

This time, we will explain the goods, orders and users in the e-commerce project.

2.1 Case Preparation

2.1.1 Technical selection

Maven: 3.3.9

Database: MySQL 5.7

Persistence layer: SpingData Jpa

Others: SpringCloud Alibaba Technology stack

2.1.2 Module design

Springcloud – alibaba, the parent project

Shop-common Public module

Shop-user user microservices [port: 807x]

Shop-product micro service [port: 808x]

Shop-order microservice [port: 809x]

2.1.3 Microservice invocation

In microservices architecture, the most common scenario is that microservices call each other. We take the common user order in the e-commerce system as an example to demonstrate the invocation of microservice: the customer initiates an order request to the order microservice, and before saving the order, the product microservice needs to be called to query the information of the product.

We generally call the active invokers of services as service consumers, and the invoked parties of services as service providers.

In this scenario, the order microservice is a service consumer and the commodity microservice is a service provider.

2.2 Creating a Parent Project

Create a Maven project and add the following to the POM.xml file

<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.itheima</groupId> 

<artifactId>springcloud-alibaba</artifactId> 

<version>1.0 the 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>
Copy the code

Version:

2.3 Creating basic Modules

1 Create the shop-common module and add the dependency to 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.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 

<parent> 

<artifactId>springcloud-alibaba</artifactId> 

<groupId>com.itheima</groupId> 

<version>1.0 the 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>
Copy the code

2 Create an entity class

/ / user

@Entity(name = "shop_user") 

@Data 

public class User { 

@Id 

@GeneratedValue(strategy = GenerationType.IDENTITY) 

private Integer uid;/ / the primary key

private String username;/ / user name

private String password;/ / password

private String telephone;/ / cell phone number

}

/ / goods

@Entity(name = "shop_product") 

@Data 

public class Product { 

@Id 

@GeneratedValue(strategy = GenerationType.IDENTITY) 

private Integer pid;/ / the primary key

private String pname;// Product name

private Double pprice;// Commodity prices

private Integer stock;/ / inventory

}

/ / order

@Entity(name = "shop_order") 

@Data 

public class Order { 

@Id 

@GeneratedValue(strategy = GenerationType.IDENTITY) 

private Long oid;/ / order id

private Integer uid;/ / user id

private String username;/ / user name
Copy the code

2.4 Creating user Microservices

Steps:

  1. Create module import dependencies

  2. Create the SpringBoot main class

  3. Adding a Configuration File

  4. Create the necessary interfaces and implementation classes (Controller Service DAO)

Create a new shop-user module and do the following

1 create 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.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 

<parent> 

<artifactId>springcloud-alibaba</artifactId> 

<groupId>com.itheima</groupId> 

<version>1.0 the 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 the SNAPSHOT</version> 

</dependency> 

</dependencies> 

</project>
Copy the code

2 Write the main class

@SpringBootApplication 

@EnableDiscoveryClient 

public class UserApplication { 

public static void main(String[] args) {SpringApplication. Run (UserApplication. Class, args); }}Copy the code

3 Create a configuration file

server: 

port: 8071 

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
Copy the code

2.5 Create commodity microservices

1 Create a module named shop_product and add springBoot dependencies


       

<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.itheima</groupId> 

<version>1.0 the 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 the SNAPSHOT</version> 

</dependency> 

</dependencies> 

</project>
Copy the code

2 Create the main class of the project

package com.itheima; 

@SpringBootApplication 

public class ProductApplication { 

public static void main(String[] args) {SpringApplication. Run (ProductApplication. Class, args); }}Copy the code

3 Create the configuration file application.yml

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
Copy the code

4 Create the ProductDao interface

package com.itheima.dao; 

public interface ProductDao extends JpaRepository<Product.Integer> {}Copy the code

5 Create the ProductService interface and implementation class

package com.itheima.service.impl; 

@Service 

public class ProductServiceImpl implements ProductService { 

@Autowired 

private ProductDao productDao; 

@Override 

public Product findByPid(Integer pid) { 

returnproductDao.findById(pid).get(); }}Copy the code

6 to create the Controller

@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("Found product :" + JSON.toJSONString(product)); 

returnproduct; }}Copy the code

7 Start the project and add the test data after the database table is created

INSERT INTO shop_product VALUE(NULL.'millet'.'1000'.'5000'); 

INSERT INTO shop_product VALUE(NULL.'huawei'.'2000'.'5000'); 

INSERT INTO shop_product VALUE(NULL.'apple'.'3000'.'5000'); 

INSERT INTO shop_product VALUE(NULL.'OPPO'.'4000'.'5000');
Copy the code

8 Access the service using a browser

2.6 Creating an order microservice

1 Create a module named shop-order and add the SpringBoot dependency


       

<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.itheima</groupId> 

<version>1.0 the 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 the SNAPSHOT</version> 

</dependency> 

</dependencies> 

</project>
Copy the code

2 Create the main class of the project

package com.itheima; 

@SpringBootApplication 

public class OrderApplication { 

public static void main(String[] args) { SpringApplication.run(OrderApplication.class, args); }}Copy the code

3 Create the configuration file application.yml

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
Copy the code

4 Create the OrderDao interface

package com.itheima.dao; 

public interface OrderDao extends JpaRepository<Order.Long> {}Copy the code

Create the OrderService interface and implementation class

@Service 

public class OrderServiceImpl implements OrderService { 

@Autowired 

private OrderDao orderDao; 

@Override 

public void save(Order order) { orderDao.save(order); }}Copy the code

6 create RestTemplate

@SpringBootApplication 

public class OrderApplication { 

public static void main(String[] args) { 

SpringApplication.run(OrderApplication.class, args); 

}

@Bean 

public RestTemplate getRestTemplate(a) { 

return newRestTemplate(); }}Copy the code

7 create Controller

package com.itheima.controller;
@RestController @Slf4j public class OrderController {
	@Autowired private RestTemplate restTemplate;
	@Autowired private OrderService orderService;
	// Ready to buy 1 item
	@GetMapping("/order/prod/{pid}") 
	public Order order(@PathVariable("pid") Integer pid) {
		log.info(">> Customer orders, at this time to call the product micro service to query the product information");
		// Invoke the commodity microservice from restTemplate
		Product product = restTemplate.getForObject( 
		"http://localhost:8081/product/" + pid, Product.class);
		log.info(">> Product information, query result :" + JSON.toJSONString(product));
		Order order = new Order();
		order.setUid(1);
		order.setUsername("Test user");
		order.setPid(product.getPid());
		order.setPname(product.getPname());
		order.setPprice(product.getPprice());
		order.setNumber(1);
		orderService.save(order);
		returnorder; }}Copy the code

7 Start the project and use a browser to access the service for testing

Articles continue to be updated with an Amway backend interview guide:Github.com/ThinkingHan…Welcome Star.