微服务理论入门和手把手带你进行微服务环境搭建及支付订单业务编写
Posted 崇尚学技术的科班人
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了微服务理论入门和手把手带你进行微服务环境搭建及支付订单业务编写相关的知识,希望对你有一定的参考价值。
1、微服务架构概述
1. 微服务介绍
- 微服务架构是一种架构模式,他提倡将单一应用划分为一组小的服务,服务之间互相协调、互相配合,为用户提供最终价值。每个服务运行在其独立的进程中,服务与服务之间采用轻量级的通信机制互相协作(通常是基于
HTTP
协议的RESTful API
)。每个服务都围绕着其本业务进行构建,并且能够被独立的部署到生产环境、类生产环境等。另外,应当尽量避免统一的、集中式的服务管理机制,对具体的一个服务而言,应根据业务上下文,选择合适的语言、工具对其进行构建。
2. 技术说明
3. SpringCloud介绍
SpringBoot
开发的一个个功能模块只能属于一个个微服务。它只是一小部分。SpringCloud是由微服务以及一些技术栈组成的。
4. SpringCloud主要的技术栈
5. SpringBoot与SpringCloud之间的版本依赖
SpringBoot | SpringCloud |
---|---|
2.6.x | 2021.0.x aka Jubilee |
2.4.x, 2.5.x (Starting with 2020.0.3) | 2020.0.x aka Ilford |
2.2.x, 2.3.x (Starting with SR5) | Hoxton |
2.1.x | Greenwich |
2.0.x | Finchley |
1.5.x | Edgware |
1.5.x | Dalston |
6. 本博主SpringCloud的技术选型
技术名称 | 版本 |
---|---|
SpringCloud | Hoxton.SR1 |
SpringBoot | 2.2.2 RELEASE |
SpringCloud | 2.1.0RELEASE |
Java | 8 |
Maven | 3.5及以上 |
mysql | 5.7及以上 |
2、IDEA搭建工作空间
2.1、创建一个Project
1. 字符编码配置
2. 注解生效码激活
3. Java版本选择
4. 文件过滤
2.2、父工程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>
<groupId>com.xiao</groupId>
<artifactId>cloud02</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<!-- 统一管理jar包版本 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<junit.version>4.12</junit.version>
<log4j.version>1.2.17</log4j.version>
<lombok.version>1.18.20</lombok.version>
<mysql.version>5.1.47</mysql.version>
<druid.version>1.1.17</druid.version>
<mybatis.spring.boot.version>1.3.2</mybatis.spring.boot.version>
</properties>
<!-- 子模块继承之后,提供作用:锁定版本+子modlue不用写groupId和version -->
<dependencyManagement>
<dependencies>
<!--spring boot 2.2.2-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--spring cloud Hoxton.SR1-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--spring cloud alibaba 2.1.0.RELEASE-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.1.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>$mysql.version</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>$druid.version</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>$mybatis.spring.boot.version</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>$junit.version</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>$log4j.version</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>$lombok.version</version>
<optional>true</optional>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
<addResources>true</addResources>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.3、Maven工程内容说明
1. Maven中的dependencyManagement和dependencies
- 如果多个子项目都引入同一样依赖,则可以避免在每个子项目中都引入相关依赖;如果需要对依赖的版本进行修改和升级,只需要在父容器中进行更新,而不需要在一个个子项目中进行修改。
dependencyManagement
里面只是声明依赖,并不实现引入,因此子项目需要声明相关的依赖。- 如果子项目中指定了版本号,那么会使用子项目中指定的版本号。
2. Maven中跳过单元测试
3、Rest微服务工程构建
3.1、建Module
3.2、改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">
<parent>
<artifactId>cloud02</artifactId>
<groupId>com.xiao</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-provider-payment8001</artifactId>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-jdbc -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
3.3、改YML
server:
port: 8001
spring:
application:
name: cloud-payment-service
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: org.gjt.mm.mysql.Driver
url: jdbc:mysql://localhost:3306/db2019?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: 123456
mybatis:
mapperLocations: classpath:mapper/*.xml
type-aliases-package: com.atguigu.springcloud.entities
3.4、写主启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class PaymentMain8001
public static void main(String[] args)
SpringApplication.run(PaymentMain8001.class,args);
包结构说明
- 一定要将主启动类放置在一个包下。
3.5、支付模块业务类
3.5.1、建表
CREATE DATABASE db2019;
USE db2019;
CREATE TABLE `payment`(
`id` BIGINT(20) NOT NULL AUTO_INCREMENT COMMENT 'ID',
`serial` VARCHAR(200) DEFAULT '',
PRIMARY KEY (id)
)ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;
INSERT INTO payment VALUES(1,'aaaa01');
3.5.2、实体类
1. Payment
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Payment implements Serializable
private Long id;
private String serial;
2. CommonResult
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonResult<T>
private Integer code;
private String message;
private T data;
public CommonResult(Integer code,String message)
this(code,message,null);
- 用于
json
封装实体类
3.5.3、编写dao层
1. PaymentDao
import com.xiao.cloud.entities.Payment;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper
public interface PaymentDao
public int create(Payment payment);
public Payment getPaymentById(@Param("id") Long id);
2. PaymentMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xiao.cloud.dao.PaymentDao">
<insert id="create" parameterType="com.xiao.cloud.entities.Payment" useGeneratedKeys="true" keyProperty="id">
insert into db2019.payment(serial) value (#serial);
</insert>
<resultMap id="BaseResultMap" type="com.xiao.cloud.entities.Payment">
<!-- column表示的是数据库字段 property表示的是java实体类中字段 -->
<id column="id" property="id" jdbcType="BIGINT"手把手带你搭建SpringCloud, 从公共模块搭建一套完整微服务架构
微服务开发实战(spring-cloud/spring-cloud-alibaba/dubbo),一个案例,手把手带你入门