「这是我参与11月更文挑战的第3天,活动详情查看:2021最后一次更文挑战」
背景
\
项目代码
\
版本依赖
组件版本关系
毕业版本依赖关系
依赖管理
Spring Cloud Alibaba BOM 包含了它所使用的所有依赖的版本。
\
RELEASE 版本
Spring Cloud Hoxton
如果需要使用 Spring Cloud Hoxton 版本,请在 dependencyManagement 中添加如下内容
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.5.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Spring Cloud Greenwich
如果需要使用 Spring Cloud Greenwich 版本,请在 dependencyManagement 中添加如下内容
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.1.4.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Spring Cloud Finchley
如果需要使用 Spring Cloud Finchley 版本,请在 dependencyManagement 中添加如下内容
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.0.4.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Spring Cloud Edgware
如果需要使用 Spring Cloud Edgware 版本,请在 dependencyManagement 中添加如下内容
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>1.5.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
孵化器版本依赖关系(不推荐使用)
\
组件
nacos
官网
安装
linux
tar -xvf nacos-server-2.0.3.tar.gz
windows
spring.datasource.platform=mysql
db.num=1
db.url.0=jdbc:mysql://localhost:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
db.user=root
db.password=123456
升级安装MySQL8.0+打包
https://nacos.io/zh-cn/docs/system-configurations.html
待验证是否1.4+直接支持mysql8.0
另外看到一个介绍升级MySQL8.0的方法
下载的v2.0.3在公司验证了,通过rar解压缩打开修改里面的MySQL数据库URL配置可以实现兼容8.0+
默认是使用Derby内嵌数据库
\
参考(1.3版本之前)
1、下载代码github.com/alibaba/nac…
2、解压,获取
\
???直接支持???
3、
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/jifen?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false
username: root
password: ...
driver-class-name: com.mysql.cj.jdbc.Driver
4、
mvn clean compile package -Prelease-nacos -Dmaven.test.skip=true clean install -U
mvn package -Prelease-nacos -Dmaven.test.skip=true clean install -U
\
使用步骤
POM
启动类
yml配置
Gateway
官网
使用步骤
新建一个api-gateway模块,导入依赖
POM
<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.chenpt</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>api-gateway</artifactId>
<dependencies>
<!--nacos客户端-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--gateway网关-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!--gateway网关限流-->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-spring-cloud-gateway-adapter</artifactId>
</dependency>
</dependencies>
</project>
启动类
@SpringBootApplication
@EnableDiscoveryClient
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
System.out.println("会员网关服务启动成功");
}
}
yml配置
server:
port: 7001
spring:
application:
name: gateway
cloud:
nacos:
discovery:
server-addr: localhost:8848
gateway:
routes: # 路由数组[路由 就是指定当请求满足什么条件的时候转到哪个微服务]
- id: member_route # 当前路由的标识, 要求唯一
uri: lb://member # lb指的是从nacos中按照名称获取微服务,并遵循负载均衡策略
predicates: # 断言(就是路由转发要满足的条件)
- Path=/service-member/member/** # 当请求路径满足Path指定的规则时,才进行路由转发
filters: # 过滤器,请求在传递过程中可以通过过滤器对其进行一定的修改
- StripPrefix=1 # 转发之前去掉1层路径
# discovery:
# locator:
# lower-case-service-id: off
测试接口
member模块工程的接口 application:name = member
@RestController
public class MemberController {
@Autowired
private Environment env;
@Resource
private SysUserMapper sysUserMapper;
@Resource
private IMemberService memberService;
@GetMapping(value = "/member/common/{uid}")
public CommonInfoVo selectCommonInfo(@PathVariable(name = "uid") Long uid) {
System.out.println(env.getProperty("info"));
//sysUserMapper.insert(new SysUserEntity(11L, System.currentTimeMillis()+"", System.currentTimeMillis()+""));
/*sysUserMapper.selectList(new LambdaQueryWrapper<SysUserEntity>())
.forEach(sysUserEntity -> System.out.println("==>" + sysUserEntity.toString()));*/
return new CommonInfoVo(10000000000L, "张三");
}
}
测试效果
浏览器测试通过gateway转发到member服务
浏览器测试直接访问member服务
工作流程
核心模块
filter和predict
GateWay的Filter约大概有32种。路由规则配置详细的看SpringCloud Gateway官网文档
使用举例
Sentinel
\
官网
\
Dasboard
#启动命令参考
java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard.jar
Dasboard通用配置
基础配置项
| Item | Description | Type | Default Value | Required | Notes |
|---|---|---|---|---|---|
| project.name | The name of your microservice | String | null | no | It's recommended to provide the name. |
| csp.sentinel.app.type | The type of your microservice | int | 0 (APP_TYPE_COMMON) | no | introduced since 1.6.0 |
| csp.sentinel.metric.file.single.size | The max size of single metric log file | long | 52428800 (50MB) | no | |
| csp.sentinel.metric.file.total.count | The max amount of metric log files | int | 6 | no | |
| csp.sentinel.statistic.max.rt | Maximum allowed response time (in ms). If exceeding this value, it will be recorded as this value. | int | 4900 | no | introduced since 1.4.1 |
| csp.sentinel.spi.classloader | The SPI classloader mechanism | String | default | no | If the value is context, then Sentinel will use the thread context classloader as the SPI classloader. |
日志配置项
| Item | Description | Type | Default Value | Required | Notes |
|---|---|---|---|---|---|
| csp.sentinel.log.dir | The log directory | String | ${user.home}/logs/csp/ | no | introduced since 1.3.0 |
| csp.sentinel.log.use.pid | Whether the log files include the process ID | boolean | false | no | introduced since 1.3.0 |
| csp.sentinel.log.output.type | The output destination of the record logs (console for the terminal, file for the file) | String | file | no | introduced since 1.6.2 |
规则持久化配置
目前了解到是结合Nacos,持久化到Nacos,简书上面有提到还有其他方案。
版本问题
持久化到Nacos
参考文章:blog.csdn.net/star1210644…
- 在Gateway模块工程引入sentinel持久化pom POM
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
2.gateway工程配置yml
Seata
官网
下载地址
\
配置Nacos
tar -xvf seata-server-0.9.0.tar.gz
cd seata
ls
有4个文件 bin conf lib LICENSE
修改registry.conf文件
cd conf
vi registry.conf
修改后的内容如下: (本文使用nacos作为注册中心)
\
registry {
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
type = "nacos" # 这里修改为nacos
nacos {
serverAddr = "192.168.0.199:8848" # 这里填你的nacos地址
namespace = ""
cluster = "default"
}
}
config {
# file、nacos 、apollo、zk、consul、etcd3
type = "file"
file {
name = "file.conf"
}
}
cd ../bin
nohup sh seata-server.sh -p 8091 -h 192.168.0.199 -m file &> seata.log &
-p 指定启动seata server的端口号。
-h 指定seata server所绑定的主机。
-m 事务日志、事务执行信息存储的方式,目前支持file(文件方式)、db(数据库方式,建表语句请查看config/db_store.sql、config/db_undo_log.sql)
6.查看启动日志
\
tail -1000f seata.log
当我们看到
-Server started
时并未发现其他错误信息,我们的
seata server
已经启动成功。
Seata
\
官网
\
Seata术语
TC(Transaction Coordinator)-事务协调者
维护全局和分支事务的状态,驱动全局事务提交或回滚。
TM(Transaction Manager)-事务管理器
定义全局事务的范围:开始全局事务、提交或回滚全局事务。
RM(Resource Manager)-资源管理器
管理分支事务处理的资源、与TC交谈以注册分支事务和报告分支事务的状态
\
典型的分布式事务处理过程
- TM向TC申请开启一个全局事务,全局事务创建成功并生成一个全局事务的XID
- XID在微服务调用链路的上下文中传递
- RM向TC注册分支事务,将其纳入XID对应全局事务的管辖
- TM向TC发起针对XID的全局提交或回滚决议
- TC调度XID下管辖的全部分支事务完成提交或回滚请求
\
(面试提问:你怎么解决分布式事务? 用的阿里巴巴的Seata组件。那你给我说说你的理解?)分布式事务处理过程的1 ID 和三组件模型
SEATA 的分布式交易解决方案
\
\
使用步骤
这个文章写得挺好的【blog.csdn.net/hancoder/ar…
1.下载seata-server
2.解压缩,修改file.conf,调整事务日志的存储模式为为DB存储
## transaction log store, only used in seata-server
store {
## store mode: file、db、redis
mode = "db"
## rsa decryption public key
publicKey = ""
## file store property
file {
## store location dir
dir = "sessionStore"
# branch session size , if exceeded first try compress lockkey, still exceeded throws exceptions
maxBranchSessionSize = 16384
# globe session size , if exceeded throws exceptions
maxGlobalSessionSize = 512
# file buffer size , if exceeded allocate new buffer
fileWriteBufferCacheSize = 16384
# when recover batch read size
sessionReloadReadSize = 100
# async, sync
flushDiskMode = async
}
## database store property
db {
## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp)/HikariDataSource(hikari) etc.
datasource = "druid"
## mysql/oracle/postgresql/h2/oceanbase etc.
dbType = "mysql"
driverClassName = "com.mysql.jdbc.Driver"
## if using mysql to store the data, recommend add rewriteBatchedStatements=true in jdbc connection param
url = "jdbc:mysql://127.0.0.1:3306/seata?rewriteBatchedStatements=true"
user = "mysql"
password = "mysql"
minConn = 5
maxConn = 100
globalTable = "global_table"
branchTable = "branch_table"
lockTable = "lock_table"
queryLimit = 100
maxWait = 5000
}
## redis store property
redis {
## redis mode: single、sentinel
mode = "single"
## single mode property
single {
host = "127.0.0.1"
port = "6379"
}
## sentinel mode property
sentinel {
masterName = ""
## such as "10.28.235.65:26379,10.28.235.65:26380,10.28.235.65:26381"
sentinelHosts = ""
}
password = ""
database = "0"
minConn = 1
maxConn = 10
maxTotal = 100
queryLimit = 100
}
}
3.修改register.conf调整Seata的注册中心
4.启动
Usage: sh seata-server.sh(for linux and mac) or cmd seata-server.bat(for windows) [options]
Options:
--host, -h The host to bind. Default: 0.0.0.0
--port, -p The port to listen. Default: 8091
--storeMode, -m log store mode : file、db Default: file
--help e.g.
sh seata-server.sh -p 8091 -h 127.0.0.1 -m file
5、工程
5.1 工程pom
\
5.2 工程配置文件
\
\
工作原理
\