本篇文章和大家分享一篇關(guān)于SpringCloud相關(guān)內(nèi)容的文章,內(nèi)容包括了SpringCloud的一些基本概念介紹以及帶大家初步體驗(yàn)一下SpringCloud框架的使用。
一、微服務(wù)簡(jiǎn)介
Ⅰ、我對(duì)微服務(wù)的理解
微服務(wù)是軟件開發(fā)的一種架構(gòu)方式,由單一的應(yīng)用小程序構(gòu)成的小服務(wù);一個(gè)軟件系統(tǒng)由多個(gè)服務(wù)組成;在微服務(wù)中,服務(wù)是細(xì)粒度的,協(xié)議是輕量級(jí)的(部署簡(jiǎn)單、性能開銷?。?/p>
Ⅱ、為什么要使用微服務(wù)?
隨著時(shí)代的發(fā)展,單體架構(gòu)(MVC三層模型)越來(lái)越不能滿足企業(yè)的要求;業(yè)務(wù)規(guī)模的不斷擴(kuò)大、團(tuán)隊(duì)開發(fā)人員的增多,使得單體架構(gòu)出現(xiàn)了以下幾個(gè)問(wèn)題:
(1)部署效率低(比如代碼量非常多,依賴的包非常多,那么每一次編譯打包、部署測(cè)試的時(shí)間就會(huì)很久)
(2)團(tuán)隊(duì)開發(fā)成本高(如果某個(gè)模塊出現(xiàn)問(wèn)題,可能就要多人修改)
(3)高可用性低(打成war包部署之后,如果某一塊出現(xiàn)了問(wèn)題,其他模塊都會(huì)受到影響)
(4)一旦代碼膨脹(簡(jiǎn)單了說(shuō)你寫的代碼量大,但功能沒(méi)實(shí)現(xiàn)幾個(gè),浪費(fèi)資源),上線就會(huì)變慢
SOA(面向服務(wù)編程)便出現(xiàn)了
SOA簡(jiǎn)介
SOA是一個(gè)組件模型,它將應(yīng)用程序的不同功能單元(稱為服務(wù))進(jìn)行拆分,并通過(guò)這些服務(wù)之間定義良好的接口和協(xié)議聯(lián)系起來(lái)。
雖然解決了服務(wù)的啟動(dòng)時(shí)間,但服務(wù)化也有很大的缺點(diǎn)
1、耦合度較高(一個(gè)依賴可能另一個(gè)模塊,另外一個(gè)模塊又可能依賴其他模塊)
2、公司成本高(每一個(gè)模塊都要由相應(yīng)的人員或者團(tuán)隊(duì)維護(hù))
2014年,隨著容器化技術(shù)的成熟以及 DevOps 文化的興起,微服務(wù)便應(yīng)運(yùn)而生了
微服務(wù)的特點(diǎn)
1.服務(wù)細(xì)粒度
2.協(xié)議輕量級(jí)
3.部署簡(jiǎn)單
4.服務(wù)的獨(dú)立維護(hù)性
二、spring cloud
Ⅰ、springcloud介紹
springcloud是分布式微服務(wù)架構(gòu)的一站式解決方案,是多種微服務(wù)架構(gòu)落地技術(shù)的集合體。
1.分布式/版本化配置
2.服務(wù)注冊(cè)和發(fā)現(xiàn)
3.路由
4.服務(wù)到服務(wù)的通話
5.負(fù)載均衡
6.斷路器
7.全局鎖
8.領(lǐng)導(dǎo)選舉和集群狀態(tài)
9.分布式消息傳遞
SpringCloud的官網(wǎng)地址:https://spring.io/projects/spring-cloud#overview
Ⅱ、boot和cloud的版本選型
1.進(jìn)入springcloud官方頁(yè)面:https://spring.io/projects/spring-cloud#learn
2. LEARN->Reference Doc.
3. 版本選擇
三、springcloud初級(jí)項(xiàng)目體驗(yàn)
最近發(fā)現(xiàn)網(wǎng)絡(luò)上很多springcloud入門體驗(yàn)都是關(guān)于消費(fèi)-訂單的小Demo,那我當(dāng)然不能落后了(菜是原罪??)
PRE: 模擬服務(wù)間調(diào)用之流程分析
1、創(chuàng)建一個(gè)父項(xiàng)目(統(tǒng)一管理子模塊依賴版本)
2、創(chuàng)建第一個(gè)子模塊-----》訂單服務(wù)模塊
3、創(chuàng)建第二個(gè)子模塊-----》客戶消費(fèi)模塊
3.1、訂單服務(wù)模塊
編寫代碼之前應(yīng)該先構(gòu)思好流程,如果直接就去碼代碼,碼了一會(huì)可能又要添添改改;還有可能做完項(xiàng)目之后又要對(duì)項(xiàng)目進(jìn)行重構(gòu),這就是沒(méi)有構(gòu)思好流程。
1、技術(shù)選型
2、業(yè)務(wù)流程
3、核心業(yè)務(wù)分析
4、代碼優(yōu)化
…
Ⅰ、數(shù)據(jù)庫(kù)表設(shè)計(jì)
Ⅱ、創(chuàng)建一個(gè)父工程
刪除多余的文件,留下一個(gè)pom.xml文件
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.cloud</groupId>
<artifactId>springcloud_all</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
## 創(chuàng)建了子模塊之后會(huì)自動(dòng)加入
## <module>payment-module</module>
</modules>
<packaging>pom</packaging>
<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.13</junit.version>
<log4j.version>1.2.17</log4j.version>
<lombok.version>1.16.18</lombok.version>
<mysql.version>8.0.19</mysql.version>
<druid.version>1.2.4</druid.version>
<mybatis.spring.boot.version>2.1.4</mybatis.spring.boot.version>
<druid.spring.boot.version>1.2.4</druid.spring.boot.version>
<springfox-swagger-ui.version>2.9.2</springfox-swagger-ui.version>
<springfox-swagger2.version>2.9.2</springfox-swagger2.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.4.3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- spring-cloud 2020.0.2-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2020.0.2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- spring-cloud alibaba-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2021.1</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>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.spring.boot.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${springfox-swagger-ui.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${springfox-swagger2.version}</version>
</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>
Ⅲ、創(chuàng)建子訂單模塊
右鍵->New->Module
PaymentApplication啟動(dòng)類
@SpringBootApplication
public class PaymentApplication {
public static void main(String[] args) {
SpringApplication.run(PaymentApplication.class,args);
}
}
Ⅲ、子模塊代碼編寫
Ⅲ.Ⅰ、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.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud_all</artifactId>
<groupId>com.cloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.pay</groupId>
<artifactId>payment-module</artifactId>
<dependencies>
<!-- web+監(jiān)控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
</dependency>
<!-- mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- jdbc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
</dependencies>
</project>
Ⅲ.Ⅱ、application.yml 配置文件編寫
server: port: 8081 spring: application: name: payment-module datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/cloud?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC username: root password: hao20001010 mybatis: mapper-locations: classpath:mapper/*.xml type-aliases-package: com.cloud.entity
Ⅲ.Ⅲ、Controller層編寫
package com.cloud.controller;
import com.cloud.Service.PaymentService;
import com.cloud.entity.Payment;
import com.cloud.entity.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* @author:抱著魚睡覺(jué)的喵喵
* @date:2021/4/18
* @description:
*/
@RestController
@Slf4j
@Api(value = "《----訂單模塊----》")
public class PaymentController {
private static final Logger logger = LoggerFactory.getLogger(PaymentController.class);
@Resource
private PaymentService paymentService;
/**
* 創(chuàng)建訂單模塊
* @api /payment/create
* @param payment Payment實(shí)體類
* @return Result<></>
*/
@ApiOperation(value = "創(chuàng)建訂單")
@PostMapping(value = "/payment/create")
public Result create(@RequestBody Payment payment) {
int result = paymentService.create(payment);
logger.info("插入結(jié)果========》" +result);
if (result > 0) {
return new Result(200,"插入成功", result);
} else {
return new Result(500, "插入失敗", null);
}
}
/**
* 訂單查詢模塊
* @api /payment/get/{id}
* @param id 編號(hào)
* @return Payment實(shí)體類
*/
@GetMapping(value = "/payment/get/{id}")
@ApiModelProperty(value = "訂單查詢")
public Result getPaymentById(@PathVariable(value = "id") Integer id) {
Payment payment = paymentService.getPaymentById(id);
logger.info("訂單查詢結(jié)果=========>"+payment);
if (payment != null) {
return new Result(200, "查詢成功", payment);
} else {
logger.error("訂單插敘失??!請(qǐng)排查原因");
return new Result(500, "查詢失敗,無(wú)該訂單ID:"+id, null);
}
}
}
Ⅲ.Ⅳ、service層
public interface PaymentService {
int create(Payment payment);
Payment getPaymentById(@Param("id") Integer id);
}
@Service
public class PaymentServiceImpl implements PaymentService {
@Resource
private PaymentMapper paymentMapper;
@Override
public int create(Payment payment) {
return paymentMapper.create(payment);
}
@Override
public Payment getPaymentById(Integer id) {
return paymentMapper.getPaymentById(id);
}
}
Ⅲ.Ⅴ、mapper層
@Mapper
public interface PaymentMapper {
int create(Payment payment);
Payment getPaymentById(@Param("id") Integer id);
}
Ⅲ.Ⅵ、mapper.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.cloud.mapper.PaymentMapper">
<insert id="create" parameterType="payment" keyProperty="id" useGeneratedKeys="true">
insert into payment(orders)values(#{orders})
</insert>
<select id="getPaymentById" parameterType="payment" resultMap="BaseResultMap" >
select * from payment where id=#{id}
</select>
<resultMap id="BaseResultMap" type="payment">
<id column="id" property="id" jdbcType="INTEGER"/>
<id column="orders" property="orders" jdbcType="VARCHAR"/>
</resultMap>
</mapper>
Ⅳ、統(tǒng)一結(jié)果封裝類及其實(shí)體類
@Data
public class Result<T> implements Serializable {
private Integer code;
private String message;
private T data;
public Result(){}
public Result(Integer code, String msg) {
this(code, msg, null);
}
public Result(Integer code, String msg, T data) {
this.code = code;
this.message = msg;
this.data = data;
}
}
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(value = "Payment實(shí)體類")
public class Payment {
@ApiModelProperty(value = "id")
private Integer id;
@ApiModelProperty(value = "訂單信息")
private String orders;
}
五、測(cè)試
3.2、消費(fèi)服務(wù)模塊
消費(fèi)模塊項(xiàng)目結(jié)構(gòu)圖
Payment、Result以及ConsumerApplication啟動(dòng)類和上面類似。
PRE:RestTemplate詳解
RestTemplate是spring封裝的一個(gè)服務(wù)端遠(yuǎn)程HTTP請(qǐng)求工具,支持常見的Rest請(qǐng)求(get、post等)
簡(jiǎn)單了說(shuō)就是用來(lái)遠(yuǎn)程調(diào)用的,因?yàn)槟阌唵文K是一個(gè)服務(wù),消費(fèi)模塊又是一個(gè)單獨(dú)的服務(wù),兩個(gè)服務(wù)端的端口號(hào)要進(jìn)行交互,所以就有了RestTemplate
RestTemplate是服務(wù)端HTTP調(diào)用的
Jsonp是客戶端調(diào)用的
Ⅰ、消費(fèi)模塊application.yml配置
server: port: 80
Ⅱ、配置類注入RestTemplate
@Configuration
public class ApplicationContextConfig {
@Bean
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
@Configuration : 該注解類似于beans
@Bean:當(dāng)然就類似于bean了
Ⅲ、ConsumerController
@RestController
@Api(value = "消費(fèi)模塊")
public class ConsumerController {
private static final String CONSUMER_URL = "http://localhost:8081";
@Resource
private RestTemplate restTemplate;
@GetMapping("/consumer/payment/create")
@ApiOperation(value = "模擬添加")
public Result<Payment> create(Payment payment) {
return restTemplate.postForObject(CONSUMER_URL + "/payment/create", payment, Result.class);
}
@ApiOperation(value = "模擬獲取")
@GetMapping("/consumer/payment/get/{id}")
public Result<Payment> getPayment(@PathVariable(value = "id") Integer id) {
return restTemplate.getForObject(CONSUMER_URL + "/payment/get/" + id, Result.class);
}
}
3.3、全局測(cè)試
到此結(jié)束!
到此這篇關(guān)于初次體驗(yàn)SpringCloud微服務(wù)框架的文章就介紹到這了,更多相關(guān)springcloud的內(nèi)容,請(qǐng)搜索W3Cschool以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,也希望大家以后多多支持!