版本对应
如果版本不正确 会出现各种奇怪错误
因此选择版本如下
- spring cloud alibaba 2.2.1.RELEASE
- spring boot 2.2.5.RELEASE
- spring cloud Hoxton.SR3
- nacos 1.2.1
- seata 1.2.0
- sentinel 1.7.1
- rockeMQ 4.4.0
整合记录
创建空项目
File -> Project ->Maven -> next
引入相关依赖
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring-cloud.version>Hoxton.SR3</spring-cloud.version>
<spring.cloud.alibaba.version>2.2.1.RELEASE</spring.cloud.alibaba.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring.cloud.alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--引入springcloud-->
<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>
跳过maven打包测试步骤
<!-- 跳过单元测试 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
下载 nacos 1.2.1
可在github右侧 release,跳转后点击Tag可查看历史版本,下载server.zip
配置nacos
如果使用数据库连接方式需要在数据库中建表
- 创建nacos数据库
- 将nacos->conf下nacos-mysql.sql导入数据库
- 修改当前目录下application.properties中
spring.datasource.platform=mysql
db.url.0=jdbc:mysql://127.0.0.1:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
db.user=root
db.password=root
- 启动nacos->bin下单机启动
cmd startup.cmd -m standalone
如果想在桌面启动可编写脚本
@echo off
start cmd /k "cd /d E:\studyWork\nacos\bin &&startup.cmd -m standalone"
- 登录控制台
localhost:8848
创建子项目
File->Module…->Maven->next 最后一步之前记得改模块名,否则会把中间的-给自动去掉
另一种方法:
复制一个子项目,将pom文件中改名,到父pom文件中添加改项目名
整合nacos
注册中心 配置中心
添加nacos依赖
<!--引入注册中心阿里巴巴-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
加注解
@EnableDiscoveryClient
加配置
spring:
cloud:
nacos:
discovery:
server-addr: localhost:8848
- 如果使用nacos的配置中心则加入依赖
- 加依赖
<!--引入配置中心阿里巴巴-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
- 创建bootstrap.yml
spring:
application:
name: server
profiles:
active: dev
cloud:
nacos:
config:
server-addr: localhost:8848
file-extension: yaml
- 在控制台中新建配置 命名规则为 a p p l i c a t i o n . n a m e − {application.name}- application.name−{profiles.active}.${file-extension}
- 使用配置中心配置
- 在application.yml中加入
user:
name: 1111
- 在java类中加注解查看获取的值是否正确
@Value("${user.name}")
此处与seata如果版本不配套,在nacos会修改配置成功,后台会报错,配置推送过来,但是无法获取最新配置
整合seata
分布式事务
下载seata 1.2.0
- 修改conf下的file.conf改到数据库中
store {
## store mode: file、db
mode = "db"
db {
## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp) etc.
datasource = "druid"
## mysql/oracle/postgresql/h2/oceanbase etc.
dbType = "mysql"
driverClassName = "com.mysql.jdbc.Driver"
url = "jdbc:mysql://localhost:3306/seata?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false"
user = "root"
password = "root"
minConn = 5
maxConn = 30
globalTable = "global_table"
branchTable = "branch_table"
lockTable = "lock_table"
queryLimit = 100
maxWait = 5000
}
}
- 修改从nacos拉去配置
registry {
type = "nacos"
nacos {
application = "seata-server"
serverAddr = "localhost:8848"
namespace = ""
cluster = "default"
username = ""
password = ""
}
}
config {
# file、nacos 、apollo、zk、consul、etcd3
type = "nacos"
nacos {
serverAddr = "localhost:8848"
namespace = ""
group = "SEATA_GROUP"
username = ""
password = ""
}
}
- 下载seata源码中script的sql 创建seata数据库
-- -------------------------------- The script used when storeMode is 'db' --------------------------------
-- the table to store GlobalSession data
CREATE TABLE IF NOT EXISTS `global_table`
(
`xid` VARCHAR(128) NOT NULL,
`transaction_id` BIGINT,
`status` TINYINT NOT NULL,
`application_id` VARCHAR(32),
`transaction_service_group` VARCHAR(32),
`transaction_name` VARCHAR(128),
`timeout` INT,
`begin_time` BIGINT,
`application_data` VARCHAR(2000),
`gmt_create` DATETIME,
`gmt_modified` DATETIME,
PRIMARY KEY (`xid`),
KEY `idx_gmt_modified_status` (`gmt_modified`, `status`),
KEY `idx_transaction_id` (`transaction_id`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8;
-- the table to store BranchSession data
CREATE TABLE IF NOT EXISTS `branch_table`
(
`branch_id` BIGINT NOT NULL,
`xid` VARCHAR(128) NOT NULL,
`transaction_id` BIGINT,
`resource_group_id` VARCHAR(32),
`resource_id` VARCHAR(256),
`branch_type` VARCHAR(8),
`status` TINYINT,
`client_id` VARCHAR(64),
`application_data` VARCHAR(2000),
`gmt_create` DATETIME(6),
`gmt_modified` DATETIME(6),
PRIMARY KEY (`branch_id`),
KEY `idx_xid` (`xid`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8;
-- the table to store lock data
CREATE TABLE IF NOT EXISTS `lock_table`
(
`row_key` VARCHAR(128) NOT NULL,
`xid` VARCHAR(96),
`transaction_id` BIGINT,
`branch_id` BIGINT NOT NULL,
`resource_id` VARCHAR(256),
`table_name` VARCHAR(32),
`pk` VARCHAR(36),
`gmt_create` DATETIME,
`gmt_modified` DATETIME,
PRIMARY KEY (`row_key`),
KEY `idx_branch_id` (`branch_id`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8;
- 每个业务表添加一张表
-- for AT mode you must to init this sql for you business database. the seata server not need it.
CREATE TABLE IF NOT EXISTS `undo_log`
(
`id` BIGINT(20) NOT NULL AUTO_INCREMENT COMMENT 'increment id',
`branch_id` BIGINT(20) NOT NULL COMMENT 'branch transaction id',
`xid` VARCHAR(100) NOT NULL COMMENT 'global transaction id',
`context` VARCHAR(128) NOT NULL COMMENT 'undo_log context,such as serialization',
`rollback_info` LONGBLOB NOT NULL COMMENT 'rollback info',
`log_status` INT(11) NOT NULL COMMENT '0:normal status,1:defense status',
`log_created` DATETIME NOT NULL COMMENT 'create datetime',
`log_modified` DATETIME NOT NULL COMMENT 'modify datetime',
PRIMARY KEY (`id`),
UNIQUE KEY `ux_undo_log` (`xid`, `branch_id`)
) ENGINE = InnoDB
AUTO_INCREMENT = 1
DEFAULT CHARSET = utf8 COMMENT ='AT transaction mode undo table';
- 从源码script中拉出config.txt修改
service.vgroupMapping.my_test_tx_group=default
注意vgroupMapping后的组名如果时多个则添加多条
store.mode=db
store.db.datasource=druid
store.db.dbType=mysql
store.db.driverClassName=com.mysql.jdbc.Driver
store.db.url=jdbc:mysql://127.0.0.1:3306/seata?useUnicode=true
store.db.user=root
store.db.password=root
- 将config.txt放在seata下
- 切换到seata/bin目录下 借助git bash 执行命令,将配置推到nacos
sh nacos-config.sh 127.0.0.1
加依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
<!-- 排除springbooot自带的的默认版本 使用自己seata版本对应的版本 -->
<exclusions>
<exclusion>
<artifactId>seata-all</artifactId>
<groupId>io.seata</groupId>
</exclusion>
<exclusion>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 这里版本要对应你seata的版本-->
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
<version>1.2.0</version>
</dependency>
需要去掉自带的seata版本加入配套的版本
加配置
spring:
cloud:
alibaba:
seata:
tx-service-group: jkpt-server-seata
从nacos拉取配置
seata:
tx-service-group: jkpt-server-seata
config:
type: nacos
nacos:
namespace:
server-addr: localhost:8848
group: SEATA_GROUP
registry:
type: nacos
nacos:
server-addr: localhost:8848
加注解
@GlobalTransactional(name = "addUser",rollbackFor = Exception.class)
运行seata脚本 端口9200
@echo off
start cmd /k "cd /d E:\studyWork\seata\bin &&seata-server.bat -p 9200 -h 127.0.0.1 -m db"
整合sentinel
流控模块
下载sentinel-dashboard
运行流控面板 端口9300
java -Dserver.port=9300 -Dproject.name=sentinel-dashboard -jar E:\studyWork\sentinel-dashboard-1.7.1.jar
pause
加依赖
<!-- sentinel -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
加配置
spring:
cloud:
sentinel:
transport:
dashboard: localhost:9300
整合RocketMQ
消息中间件
下载rocketMQ 4.4.0
下载rocketmq-console-ng
此包在原来的rocketmq-externals中有,现移至新的地址可在github中找
移动后的包用的rocketmq 4.9.0所以无法使用
以老包中的控制台为主
- 下载控制台源码
- 拖到本地运行
- 修改pom 文件为4.4.0
- 修改application.yml中rocketmq地址
- 跳过测试打包
运行rocketmq server
@echo off
start cmd /k "cd /d E:\RocketMQ\rocketmq-all-4.4.0-bin-release\bin &&mqnamesrv.cmd"
运行rocektmq borker 端口9876
@echo off
start cmd /k "cd /d E:\RocketMQ\rocketmq-all-4.4.0-bin-release\bin &&mqbroker.cmd -n localhost:9876"
运行rocketmq-console-ng
java -jar rocketmq-console-ng.jar
存在问题
rocket运行内存修改至512m
rocketmq的borker会在C:\Users\Administrator\store\commitlog 中创建文件
需要再E:\RocketMQ\rocketmq-all-4.4.0-bin-release\conf\2m-2s-async 中将每个节点添加一条diskMaxUsedSpaceRatio=98 表示磁盘占98以上再报警
加依赖
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-spring-boot-starter</artifactId>
<version>2.0.2</version>
</dependency>
加配置
rocketmq: name-server: 127.0.0.1:9876 producer: group: test-group
代码实现
生产者
@Resource
private RocketMQTemplate rocketMQTemplate;
@PostMapping("sendMsg")
public AjaxResult sendMsg(UserDTO userDTO){
rocketMQTemplate.convertAndSend(
"add-bonus",
UserDTO.builder()
.userId(1)
.message(userDTO.getMessage())
.build()
);
return AjaxResult.success();
}
消费者
- 配置
rocketmq:
name-server: 127.0.0.1:9876
- 代码实现
//rocketmq消费者使用
@Slf4j
@Service
@RocketMQMessageListener(topic = "add-bonus",consumerGroup = "consumer-group")
//@RequiredArgsConstructor(onConstructor = @_(@Autowired))
public class AddBonusListener implements RocketMQListener<UserDTO> {
@Override
public void onMessage(UserDTO userDTO) {
//收到消息执行的业务
int userId = userDTO.getUserId();
log.info("============消费消息:"+userDTO);
}
}
整合gateway
加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
- sentinel整合gateway
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
配置
spring:
application:
name: gateway-nacos
cloud:
nacos:
discovery:
server-addr: localhost:8848
gateway:
routes:
- id: jkpt-server
uri: lb://jkpt-server
predicates:
- Path=/jkpt-server/**
filters:
- StripPrefix=1
- id: jkpt-report
uri: lb://jkpt-report
predicates:
- Path=/jkpt-report/**
filters:
- StripPrefix=1
- id: jkpt-tunnel
uri: lb://jkpt-tunnel
predicates:
- Path=/jkpt-tunnel/**
filters:
- StripPrefix=1
#sentinel
sentinel:
transport:
dashboard: 127.0.0.1:9300
datasource:
ds:
nacos:
server-addr: localhost:8848
data-id: ${spring.application.name}.json
group-id: DEFAULT_GROUP
data-type: json
rule-type: flow
整合openfeign
加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
加配置
该配置主要在超时时用
feign:
client:
config:
default:
connectTimeout: 30000
readTimeout: 30000
加注解
@EnableFeignClients(basePackages = {"com.ysza.jkpt.modules.clients"})
代码
@FeignClient(value = "jkpt-report",fallbackFactory = ProjectFallbackFactory.class)
public interface ProjectClient {
@PostMapping("index")
String index();
}
失败方法
@Component
public class ProjectFallbackFactory implements FallbackFactory<ProjectClient> {
@Override
public ProjectClient create(Throwable throwable) {
return null;
}
}
整合SKYwalking
启动项目时添加jvm参数