mirror of
https://github.com/jeecgboot/JeecgBoot.git
synced 2025-12-31 01:01:27 +08:00
后台目录结构大调整,让结构更清晰
This commit is contained in:
@ -0,0 +1,2 @@
|
||||
http://localhost:9111
|
||||
账号密码:admin/admin
|
||||
65
jeecg-server-cloud/jeecg-visual/jeecg-cloud-monitor/pom.xml
Normal file
65
jeecg-server-cloud/jeecg-visual/jeecg-cloud-monitor/pom.xml
Normal file
@ -0,0 +1,65 @@
|
||||
<?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>jeecg-visual</artifactId>
|
||||
<groupId>org.jeecgframework.boot</groupId>
|
||||
<version>3.4.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>jeecg-cloud-monitor</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<!--Spring Boot Admin Server监控服务端-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>de.codecentric</groupId>
|
||||
<artifactId>spring-boot-admin-starter-server</artifactId>
|
||||
<version>2.3.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!--安全模块-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-security</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-tomcat</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<!--undertow容器-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-undertow</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
</build>
|
||||
</project>
|
||||
@ -0,0 +1,18 @@
|
||||
package org.jeecg.monitor;
|
||||
|
||||
import de.codecentric.boot.admin.server.config.EnableAdminServer;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
* 监控服务
|
||||
* @author zyf
|
||||
* @date: 2022/4/21 10:55
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@EnableAdminServer
|
||||
public class JeecgMonitorApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(JeecgMonitorApplication.class);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,52 @@
|
||||
package org.jeecg.monitor.config;
|
||||
|
||||
import de.codecentric.boot.admin.server.config.AdminServerProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
|
||||
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
|
||||
|
||||
/**
|
||||
* @author scott
|
||||
*/
|
||||
@Configuration
|
||||
public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
|
||||
|
||||
private final String adminContextPath;
|
||||
|
||||
public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
|
||||
this.adminContextPath = adminServerProperties.getContextPath();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void configure(HttpSecurity http) throws Exception {
|
||||
// 登录成功处理类
|
||||
SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
|
||||
successHandler.setTargetUrlParameter("redirectTo");
|
||||
successHandler.setDefaultTargetUrl(adminContextPath + "/");
|
||||
|
||||
http.authorizeRequests()
|
||||
//静态文件允许访问
|
||||
.antMatchers(adminContextPath + "/assets/**").permitAll()
|
||||
//登录页面允许访问
|
||||
.antMatchers(adminContextPath + "/login", "/css/**", "/js/**", "/image/*").permitAll()
|
||||
//其他所有请求需要登录
|
||||
.anyRequest().authenticated()
|
||||
.and()
|
||||
//登录页面配置,用于替换security默认页面
|
||||
.formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()
|
||||
//登出页面配置,用于替换security默认页面
|
||||
.logout().logoutUrl(adminContextPath + "/logout").and()
|
||||
.httpBasic().and()
|
||||
.csrf()
|
||||
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
|
||||
.ignoringAntMatchers(
|
||||
"/instances",
|
||||
"/actuator/**"
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,37 @@
|
||||
server:
|
||||
port: 9111
|
||||
spring:
|
||||
boot:
|
||||
admin:
|
||||
ui:
|
||||
title: JeecgCloud监控中心
|
||||
client:
|
||||
instance:
|
||||
metadata:
|
||||
tags:
|
||||
environment: local
|
||||
security:
|
||||
user:
|
||||
name: "admin"
|
||||
password: "admin"
|
||||
application:
|
||||
name: jeecg-monitor
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
server-addr: @config.server-addr@
|
||||
metadata:
|
||||
user.name: ${spring.security.user.name}
|
||||
user.password: ${spring.security.user.password}
|
||||
# 服务端点检查
|
||||
management:
|
||||
trace:
|
||||
http:
|
||||
enabled: true
|
||||
endpoints:
|
||||
web:
|
||||
exposure:
|
||||
include: "*"
|
||||
endpoint:
|
||||
health:
|
||||
show-details: always
|
||||
@ -0,0 +1,15 @@
|
||||
FROM anapsix/alpine-java:8_server-jre_unlimited
|
||||
|
||||
MAINTAINER jeecgos@163.com
|
||||
|
||||
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
|
||||
|
||||
RUN mkdir -p /jeecg-cloud-sentinel
|
||||
|
||||
WORKDIR /jeecg-cloud-sentinel
|
||||
|
||||
EXPOSE 8848
|
||||
|
||||
ADD ./target/jeecg-cloud-sentinel-3.4.0.jar ./
|
||||
|
||||
CMD sleep 5;java -Dfile.encoding=utf-8 -Djava.security.egd=file:/dev/./urandom -jar jeecg-cloud-sentinel-3.4.0.jar
|
||||
@ -0,0 +1,9 @@
|
||||
访问地址: http://localhost:9000
|
||||
账号密码:sentinel/sentinel
|
||||
|
||||
|
||||
# 使用方法
|
||||
|
||||
- 1、第一次登录sentinel内容是空的,必须访问了微服务实例的请求才会出现配置
|
||||
- 2、sentinel做了深度改造,支持持久化到nacos中
|
||||
- 3、目前只针对gateway做的控制,其他服务不需要
|
||||
147
jeecg-server-cloud/jeecg-visual/jeecg-cloud-sentinel/pom.xml
Normal file
147
jeecg-server-cloud/jeecg-visual/jeecg-cloud-sentinel/pom.xml
Normal file
@ -0,0 +1,147 @@
|
||||
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<artifactId>jeecg-visual</artifactId>
|
||||
<groupId>org.jeecgframework.boot</groupId>
|
||||
<version>3.4.0</version>
|
||||
</parent>
|
||||
<artifactId>jeecg-cloud-sentinel</artifactId>
|
||||
<name>jeecg-cloud-sentinel</name>
|
||||
<description>sentinel启动模块</description>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.jeecgframework.cloud</groupId>
|
||||
<artifactId>sentinel-dashboard</artifactId>
|
||||
<version>1.8.3</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>sentinel-web-servlet</artifactId>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<artifactId>sentinel-transport-simple-http</artifactId>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<artifactId>sentinel-parameter-flow-control</artifactId>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<artifactId>sentinel-core</artifactId>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<artifactId>sentinel-api-gateway-adapter-common</artifactId>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
<artifactId>sentinel-datasource-nacos</artifactId>
|
||||
<version>1.8.3</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>sentinel-core</artifactId>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
<artifactId>sentinel-core</artifactId>
|
||||
<version>1.8.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
<artifactId>sentinel-web-servlet</artifactId>
|
||||
<version>1.8.3</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>sentinel-core</artifactId>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
<artifactId>sentinel-transport-simple-http</artifactId>
|
||||
<version>1.8.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
<artifactId>sentinel-parameter-flow-control</artifactId>
|
||||
<version>1.8.3</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>sentinel-core</artifactId>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
<artifactId>sentinel-api-gateway-adapter-common</artifactId>
|
||||
<version>1.8.3</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>sentinel-parameter-flow-control</artifactId>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<artifactId>sentinel-core</artifactId>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<!--undertow容器-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-undertow</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>commons-lang</groupId>
|
||||
<artifactId>commons-lang</artifactId>
|
||||
<version>2.6</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
<version>4.5.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpcore</artifactId>
|
||||
<version>4.4.5</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpasyncclient</artifactId>
|
||||
<version>4.1.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpcore-nio</artifactId>
|
||||
<version>4.4.6</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright 1999-2018 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.csp.sentinel.dashboard;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.core.env.Environment;
|
||||
import com.alibaba.csp.sentinel.init.InitExecutor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* Sentinel dashboard application.
|
||||
*
|
||||
* @author Carpenter Lee
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@Slf4j
|
||||
public class JeecgSentinelApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.setProperty("csp.sentinel.app.type", "1");
|
||||
triggerSentinelInit();
|
||||
ConfigurableApplicationContext application = SpringApplication.run(JeecgSentinelApplication.class, args);
|
||||
Environment env = application.getEnvironment();
|
||||
String port = env.getProperty("server.port");
|
||||
log.info("\n----------------------------------------------------------\n\t" +
|
||||
"Application SentinelDashboard is running! Access URLs:\n\t" +
|
||||
"Local: \t\thttp://localhost:" + port + "/\n\t" +
|
||||
"----------------------------------------------------------");
|
||||
}
|
||||
|
||||
private static void triggerSentinelInit() {
|
||||
new Thread(() -> InitExecutor.doInit()).start();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,39 @@
|
||||
package com.alibaba.csp.sentinel.dashboard.constants;
|
||||
|
||||
/**
|
||||
* sentinel常量配置
|
||||
* @author zyf
|
||||
*/
|
||||
public class SentinelConStants {
|
||||
public static final String GROUP_ID = "SENTINEL_GROUP";
|
||||
|
||||
/**
|
||||
* 流控规则
|
||||
*/
|
||||
public static final String FLOW_DATA_ID_POSTFIX = "-flow-rules";
|
||||
/**
|
||||
* 热点参数
|
||||
*/
|
||||
public static final String PARAM_FLOW_DATA_ID_POSTFIX = "-param-rules";
|
||||
/**
|
||||
* 降级规则
|
||||
*/
|
||||
public static final String DEGRADE_DATA_ID_POSTFIX = "-degrade-rules";
|
||||
/**
|
||||
* 系统规则
|
||||
*/
|
||||
public static final String SYSTEM_DATA_ID_POSTFIX = "-system-rules";
|
||||
/**
|
||||
* 授权规则
|
||||
*/
|
||||
public static final String AUTHORITY_DATA_ID_POSTFIX = "-authority-rules";
|
||||
|
||||
/**
|
||||
* 网关API
|
||||
*/
|
||||
public static final String GETEWAY_API_DATA_ID_POSTFIX = "-api-rules";
|
||||
/**
|
||||
* 网关流控规则
|
||||
*/
|
||||
public static final String GETEWAY_FLOW_DATA_ID_POSTFIX = "-flow-rules";
|
||||
}
|
||||
@ -0,0 +1,181 @@
|
||||
package com.alibaba.csp.sentinel.dashboard.controller;
|
||||
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.auth.AuthAction;
|
||||
import com.alibaba.csp.sentinel.dashboard.auth.AuthService.PrivilegeType;
|
||||
import com.alibaba.csp.sentinel.dashboard.controller.base.BaseRuleController;
|
||||
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRuleProvider;
|
||||
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRulePublisher;
|
||||
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
|
||||
import com.alibaba.csp.sentinel.util.StringUtil;
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.AuthorityRuleEntity;
|
||||
import com.alibaba.csp.sentinel.dashboard.domain.Result;
|
||||
import com.alibaba.csp.sentinel.dashboard.repository.rule.RuleRepository;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
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.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* 授权规则控制器
|
||||
*
|
||||
* @author zyf
|
||||
* @date 2022-04-13
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping(value = "/authority")
|
||||
public class AuthorityRuleController extends BaseRuleController {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(AuthorityRuleController.class);
|
||||
|
||||
@Autowired
|
||||
private RuleRepository<AuthorityRuleEntity, Long> repository;
|
||||
@Autowired
|
||||
@Qualifier("authorityRuleNacosProvider")
|
||||
private DynamicRuleProvider<List<AuthorityRuleEntity>> ruleProvider;
|
||||
@Autowired
|
||||
@Qualifier("authorityRuleNacosPublisher")
|
||||
private DynamicRulePublisher<List<AuthorityRuleEntity>> rulePublisher;
|
||||
|
||||
@GetMapping("/rules")
|
||||
@AuthAction(PrivilegeType.READ_RULE)
|
||||
public Result<List<AuthorityRuleEntity>> apiQueryAllRulesForMachine(@RequestParam String app,
|
||||
@RequestParam String ip,
|
||||
@RequestParam Integer port) {
|
||||
if (StringUtil.isEmpty(app)) {
|
||||
return Result.ofFail(-1, "app cannot be null or empty");
|
||||
}
|
||||
if (StringUtil.isEmpty(ip)) {
|
||||
return Result.ofFail(-1, "ip cannot be null or empty");
|
||||
}
|
||||
if (port == null || port <= 0) {
|
||||
return Result.ofFail(-1, "Invalid parameter: port");
|
||||
}
|
||||
try {
|
||||
List<AuthorityRuleEntity> rules = ruleProvider.getRules(app);
|
||||
rules = repository.saveAll(rules);
|
||||
return Result.ofSuccess(rules);
|
||||
} catch (Throwable throwable) {
|
||||
logger.error("Error when querying authority rules", throwable);
|
||||
return Result.ofFail(-1, throwable.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private <R> Result<R> checkEntityInternal(AuthorityRuleEntity entity) {
|
||||
if (entity == null) {
|
||||
return Result.ofFail(-1, "bad rule body");
|
||||
}
|
||||
if (StringUtil.isBlank(entity.getApp())) {
|
||||
return Result.ofFail(-1, "app can't be null or empty");
|
||||
}
|
||||
if (StringUtil.isBlank(entity.getIp())) {
|
||||
return Result.ofFail(-1, "ip can't be null or empty");
|
||||
}
|
||||
if (entity.getPort() == null || entity.getPort() <= 0) {
|
||||
return Result.ofFail(-1, "port can't be null");
|
||||
}
|
||||
if (entity.getRule() == null) {
|
||||
return Result.ofFail(-1, "rule can't be null");
|
||||
}
|
||||
if (StringUtil.isBlank(entity.getResource())) {
|
||||
return Result.ofFail(-1, "resource name cannot be null or empty");
|
||||
}
|
||||
if (StringUtil.isBlank(entity.getLimitApp())) {
|
||||
return Result.ofFail(-1, "limitApp should be valid");
|
||||
}
|
||||
if (entity.getStrategy() != RuleConstant.AUTHORITY_WHITE
|
||||
&& entity.getStrategy() != RuleConstant.AUTHORITY_BLACK) {
|
||||
return Result.ofFail(-1, "Unknown strategy (must be blacklist or whitelist)");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@PostMapping("/rule")
|
||||
@AuthAction(PrivilegeType.WRITE_RULE)
|
||||
public Result<AuthorityRuleEntity> apiAddAuthorityRule(@RequestBody AuthorityRuleEntity entity) {
|
||||
Result<AuthorityRuleEntity> checkResult = checkEntityInternal(entity);
|
||||
if (checkResult != null) {
|
||||
return checkResult;
|
||||
}
|
||||
entity.setId(null);
|
||||
Date date = new Date();
|
||||
entity.setGmtCreate(date);
|
||||
entity.setGmtModified(date);
|
||||
try {
|
||||
entity = repository.save(entity);
|
||||
publishRules(entity.getApp());
|
||||
} catch (Throwable throwable) {
|
||||
logger.error("Failed to add authority rule", throwable);
|
||||
return Result.ofThrowable(-1, throwable);
|
||||
}
|
||||
return Result.ofSuccess(entity);
|
||||
}
|
||||
|
||||
@PutMapping("/rule/{id}")
|
||||
@AuthAction(PrivilegeType.WRITE_RULE)
|
||||
public Result<AuthorityRuleEntity> apiUpdateParamFlowRule(@PathVariable("id") Long id,
|
||||
@RequestBody AuthorityRuleEntity entity) {
|
||||
if (id == null || id <= 0) {
|
||||
return Result.ofFail(-1, "Invalid id");
|
||||
}
|
||||
Result<AuthorityRuleEntity> checkResult = checkEntityInternal(entity);
|
||||
if (checkResult != null) {
|
||||
return checkResult;
|
||||
}
|
||||
entity.setId(id);
|
||||
Date date = new Date();
|
||||
entity.setGmtCreate(null);
|
||||
entity.setGmtModified(date);
|
||||
try {
|
||||
entity = repository.save(entity);
|
||||
if (entity == null) {
|
||||
return Result.ofFail(-1, "Failed to save authority rule");
|
||||
}
|
||||
publishRules(entity.getApp());
|
||||
} catch (Throwable throwable) {
|
||||
logger.error("Failed to save authority rule", throwable);
|
||||
return Result.ofThrowable(-1, throwable);
|
||||
}
|
||||
return Result.ofSuccess(entity);
|
||||
}
|
||||
|
||||
@DeleteMapping("/rule/{id}")
|
||||
@AuthAction(PrivilegeType.DELETE_RULE)
|
||||
public Result<Long> apiDeleteRule(@PathVariable("id") Long id) {
|
||||
if (id == null) {
|
||||
return Result.ofFail(-1, "id cannot be null");
|
||||
}
|
||||
AuthorityRuleEntity oldEntity = repository.findById(id);
|
||||
if (oldEntity == null) {
|
||||
return Result.ofSuccess(null);
|
||||
}
|
||||
try {
|
||||
repository.delete(id);
|
||||
publishRules(oldEntity.getApp());
|
||||
} catch (Exception e) {
|
||||
return Result.ofFail(-1, e.getMessage());
|
||||
}
|
||||
return Result.ofSuccess(id);
|
||||
}
|
||||
|
||||
private void publishRules(String app) throws Exception {
|
||||
List<AuthorityRuleEntity> rules = repository.findAllByApp(app);
|
||||
rulePublisher.publish(app, rules);
|
||||
//延迟加载
|
||||
delayTime();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,209 @@
|
||||
package com.alibaba.csp.sentinel.dashboard.controller;
|
||||
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.auth.AuthAction;
|
||||
import com.alibaba.csp.sentinel.dashboard.auth.AuthService.PrivilegeType;
|
||||
import com.alibaba.csp.sentinel.dashboard.controller.base.BaseRuleController;
|
||||
import com.alibaba.csp.sentinel.dashboard.repository.rule.RuleRepository;
|
||||
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRuleProvider;
|
||||
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRulePublisher;
|
||||
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
|
||||
import com.alibaba.csp.sentinel.slots.block.degrade.circuitbreaker.CircuitBreakerStrategy;
|
||||
import com.alibaba.csp.sentinel.util.StringUtil;
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.DegradeRuleEntity;
|
||||
import com.alibaba.csp.sentinel.dashboard.domain.Result;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
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.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* 降级规则控制器
|
||||
*
|
||||
* @author zyf
|
||||
* @date 2022-04-13
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/degrade")
|
||||
public class DegradeController extends BaseRuleController {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(DegradeController.class);
|
||||
|
||||
@Autowired
|
||||
private RuleRepository<DegradeRuleEntity, Long> repository;
|
||||
@Autowired
|
||||
@Qualifier("degradeRuleNacosProvider")
|
||||
private DynamicRuleProvider<List<DegradeRuleEntity>> ruleProvider;
|
||||
@Autowired
|
||||
@Qualifier("degradeRuleNacosPublisher")
|
||||
private DynamicRulePublisher<List<DegradeRuleEntity>> rulePublisher;
|
||||
|
||||
@GetMapping("/rules.json")
|
||||
@AuthAction(PrivilegeType.READ_RULE)
|
||||
public Result<List<DegradeRuleEntity>> apiQueryMachineRules(String app, String ip, Integer port) {
|
||||
if (StringUtil.isEmpty(app)) {
|
||||
return Result.ofFail(-1, "app can't be null or empty");
|
||||
}
|
||||
if (StringUtil.isEmpty(ip)) {
|
||||
return Result.ofFail(-1, "ip can't be null or empty");
|
||||
}
|
||||
if (port == null) {
|
||||
return Result.ofFail(-1, "port can't be null");
|
||||
}
|
||||
try {
|
||||
List<DegradeRuleEntity> rules = ruleProvider.getRules(app);
|
||||
rules = repository.saveAll(rules);
|
||||
return Result.ofSuccess(rules);
|
||||
} catch (Throwable throwable) {
|
||||
logger.error("queryApps error:", throwable);
|
||||
return Result.ofThrowable(-1, throwable);
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping("/rule")
|
||||
@AuthAction(PrivilegeType.WRITE_RULE)
|
||||
public Result<DegradeRuleEntity> apiAddRule(@RequestBody DegradeRuleEntity entity) {
|
||||
Result<DegradeRuleEntity> checkResult = checkEntityInternal(entity);
|
||||
if (checkResult != null) {
|
||||
return checkResult;
|
||||
}
|
||||
Date date = new Date();
|
||||
entity.setGmtCreate(date);
|
||||
entity.setGmtModified(date);
|
||||
try {
|
||||
entity = repository.save(entity);
|
||||
publishRules(entity.getApp());
|
||||
} catch (Throwable t) {
|
||||
logger.error("Failed to add new degrade rule, app={}, ip={}", entity.getApp(), entity.getIp(), t);
|
||||
return Result.ofThrowable(-1, t);
|
||||
}
|
||||
return Result.ofSuccess(entity);
|
||||
}
|
||||
|
||||
@PutMapping("/rule/{id}")
|
||||
@AuthAction(PrivilegeType.WRITE_RULE)
|
||||
public Result<DegradeRuleEntity> apiUpdateRule(@PathVariable("id") Long id,
|
||||
@RequestBody DegradeRuleEntity entity) {
|
||||
if (id == null || id <= 0) {
|
||||
return Result.ofFail(-1, "id can't be null or negative");
|
||||
}
|
||||
DegradeRuleEntity oldEntity = repository.findById(id);
|
||||
if (oldEntity == null) {
|
||||
return Result.ofFail(-1, "Degrade rule does not exist, id=" + id);
|
||||
}
|
||||
entity.setApp(oldEntity.getApp());
|
||||
entity.setIp(oldEntity.getIp());
|
||||
entity.setPort(oldEntity.getPort());
|
||||
entity.setId(oldEntity.getId());
|
||||
Result<DegradeRuleEntity> checkResult = checkEntityInternal(entity);
|
||||
if (checkResult != null) {
|
||||
return checkResult;
|
||||
}
|
||||
|
||||
entity.setGmtCreate(oldEntity.getGmtCreate());
|
||||
entity.setGmtModified(new Date());
|
||||
try {
|
||||
entity = repository.save(entity);
|
||||
publishRules(entity.getApp());
|
||||
} catch (Throwable t) {
|
||||
logger.error("Failed to save degrade rule, id={}, rule={}", id, entity, t);
|
||||
return Result.ofThrowable(-1, t);
|
||||
}
|
||||
return Result.ofSuccess(entity);
|
||||
}
|
||||
|
||||
@DeleteMapping("/rule/{id}")
|
||||
@AuthAction(PrivilegeType.DELETE_RULE)
|
||||
public Result<Long> delete(@PathVariable("id") Long id) {
|
||||
if (id == null) {
|
||||
return Result.ofFail(-1, "id can't be null");
|
||||
}
|
||||
|
||||
DegradeRuleEntity oldEntity = repository.findById(id);
|
||||
if (oldEntity == null) {
|
||||
return Result.ofSuccess(null);
|
||||
}
|
||||
|
||||
try {
|
||||
repository.delete(id);
|
||||
publishRules(oldEntity.getApp());
|
||||
} catch (Throwable throwable) {
|
||||
logger.error("Failed to delete degrade rule, id={}", id, throwable);
|
||||
return Result.ofThrowable(-1, throwable);
|
||||
}
|
||||
return Result.ofSuccess(id);
|
||||
}
|
||||
|
||||
private void publishRules(/*@NonNull*/ String app) throws Exception {
|
||||
List<DegradeRuleEntity> rules = repository.findAllByApp(app);
|
||||
rulePublisher.publish(app, rules);
|
||||
//延迟加载
|
||||
delayTime();
|
||||
}
|
||||
|
||||
private <R> Result<R> checkEntityInternal(DegradeRuleEntity entity) {
|
||||
if (StringUtil.isBlank(entity.getApp())) {
|
||||
return Result.ofFail(-1, "app can't be blank");
|
||||
}
|
||||
if (StringUtil.isBlank(entity.getIp())) {
|
||||
return Result.ofFail(-1, "ip can't be null or empty");
|
||||
}
|
||||
if (entity.getPort() == null || entity.getPort() <= 0) {
|
||||
return Result.ofFail(-1, "invalid port: " + entity.getPort());
|
||||
}
|
||||
if (StringUtil.isBlank(entity.getLimitApp())) {
|
||||
return Result.ofFail(-1, "limitApp can't be null or empty");
|
||||
}
|
||||
if (StringUtil.isBlank(entity.getResource())) {
|
||||
return Result.ofFail(-1, "resource can't be null or empty");
|
||||
}
|
||||
Double threshold = entity.getCount();
|
||||
if (threshold == null || threshold < 0) {
|
||||
return Result.ofFail(-1, "invalid threshold: " + threshold);
|
||||
}
|
||||
Integer recoveryTimeoutSec = entity.getTimeWindow();
|
||||
if (recoveryTimeoutSec == null || recoveryTimeoutSec <= 0) {
|
||||
return Result.ofFail(-1, "recoveryTimeout should be positive");
|
||||
}
|
||||
Integer strategy = entity.getGrade();
|
||||
if (strategy == null) {
|
||||
return Result.ofFail(-1, "circuit breaker strategy cannot be null");
|
||||
}
|
||||
if (strategy < CircuitBreakerStrategy.SLOW_REQUEST_RATIO.getType()
|
||||
|| strategy > RuleConstant.DEGRADE_GRADE_EXCEPTION_COUNT) {
|
||||
return Result.ofFail(-1, "Invalid circuit breaker strategy: " + strategy);
|
||||
}
|
||||
if (entity.getMinRequestAmount() == null || entity.getMinRequestAmount() <= 0) {
|
||||
return Result.ofFail(-1, "Invalid minRequestAmount");
|
||||
}
|
||||
if (entity.getStatIntervalMs() == null || entity.getStatIntervalMs() <= 0) {
|
||||
return Result.ofFail(-1, "Invalid statInterval");
|
||||
}
|
||||
if (strategy == RuleConstant.DEGRADE_GRADE_RT) {
|
||||
Double slowRatio = entity.getSlowRatioThreshold();
|
||||
if (slowRatio == null) {
|
||||
return Result.ofFail(-1, "SlowRatioThreshold is required for slow request ratio strategy");
|
||||
} else if (slowRatio < 0 || slowRatio > 1) {
|
||||
return Result.ofFail(-1, "SlowRatioThreshold should be in range: [0.0, 1.0]");
|
||||
}
|
||||
} else if (strategy == RuleConstant.DEGRADE_GRADE_EXCEPTION_RATIO) {
|
||||
if (threshold > 1) {
|
||||
return Result.ofFail(-1, "Ratio threshold should be in range: [0.0, 1.0]");
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,253 @@
|
||||
package com.alibaba.csp.sentinel.dashboard.controller;
|
||||
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.auth.AuthAction;
|
||||
import com.alibaba.csp.sentinel.dashboard.auth.AuthService;
|
||||
import com.alibaba.csp.sentinel.dashboard.auth.AuthService.PrivilegeType;
|
||||
import com.alibaba.csp.sentinel.dashboard.client.CommandNotFoundException;
|
||||
import com.alibaba.csp.sentinel.dashboard.controller.base.BaseRuleController;
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.SentinelVersion;
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.ParamFlowRuleEntity;
|
||||
import com.alibaba.csp.sentinel.dashboard.discovery.AppManagement;
|
||||
import com.alibaba.csp.sentinel.dashboard.domain.Result;
|
||||
import com.alibaba.csp.sentinel.dashboard.repository.rule.RuleRepository;
|
||||
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRuleProvider;
|
||||
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRulePublisher;
|
||||
import com.alibaba.csp.sentinel.dashboard.util.VersionUtils;
|
||||
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
|
||||
import com.alibaba.csp.sentinel.util.StringUtil;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
/**
|
||||
* 热点参数规则控制器
|
||||
*
|
||||
* @author zyf
|
||||
* @date 2022-04-13
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping(value = "/paramFlow")
|
||||
public class ParamFlowRuleController extends BaseRuleController {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(ParamFlowRuleController.class);
|
||||
|
||||
@Autowired
|
||||
private AppManagement appManagement;
|
||||
@Autowired
|
||||
private RuleRepository<ParamFlowRuleEntity, Long> repository;
|
||||
@Autowired
|
||||
@Qualifier("paramFlowRuleNacosProvider")
|
||||
private DynamicRuleProvider<List<ParamFlowRuleEntity>> ruleProvider;
|
||||
@Autowired
|
||||
@Qualifier("paramFlowRuleNacosPublisher")
|
||||
private DynamicRulePublisher<List<ParamFlowRuleEntity>> rulePublisher;
|
||||
|
||||
private boolean checkIfSupported(String app, String ip, int port) {
|
||||
try {
|
||||
return Optional.ofNullable(appManagement.getDetailApp(app))
|
||||
.flatMap(e -> e.getMachine(ip, port))
|
||||
.flatMap(m -> VersionUtils.parseVersion(m.getVersion())
|
||||
.map(v -> v.greaterOrEqual(version020)))
|
||||
.orElse(true);
|
||||
// If error occurred or cannot retrieve machine info, return true.
|
||||
} catch (Exception ex) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("/rules")
|
||||
@AuthAction(PrivilegeType.READ_RULE)
|
||||
public Result<List<ParamFlowRuleEntity>> apiQueryAllRulesForMachine(@RequestParam String app,
|
||||
@RequestParam String ip,
|
||||
@RequestParam Integer port) {
|
||||
if (StringUtil.isEmpty(app)) {
|
||||
return Result.ofFail(-1, "app cannot be null or empty");
|
||||
}
|
||||
if (StringUtil.isEmpty(ip)) {
|
||||
return Result.ofFail(-1, "ip cannot be null or empty");
|
||||
}
|
||||
if (port == null || port <= 0) {
|
||||
return Result.ofFail(-1, "Invalid parameter: port");
|
||||
}
|
||||
if (!checkIfSupported(app, ip, port)) {
|
||||
return unsupportedVersion();
|
||||
}
|
||||
try {
|
||||
List<ParamFlowRuleEntity> rules = ruleProvider.getRules(app);
|
||||
rules = repository.saveAll(rules);
|
||||
return Result.ofSuccess(rules);
|
||||
} catch (ExecutionException ex) {
|
||||
logger.error("Error when querying parameter flow rules", ex.getCause());
|
||||
if (isNotSupported(ex.getCause())) {
|
||||
return unsupportedVersion();
|
||||
} else {
|
||||
return Result.ofThrowable(-1, ex.getCause());
|
||||
}
|
||||
} catch (Throwable throwable) {
|
||||
logger.error("Error when querying parameter flow rules", throwable);
|
||||
return Result.ofFail(-1, throwable.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isNotSupported(Throwable ex) {
|
||||
return ex instanceof CommandNotFoundException;
|
||||
}
|
||||
|
||||
@PostMapping("/rule")
|
||||
@AuthAction(AuthService.PrivilegeType.WRITE_RULE)
|
||||
public Result<ParamFlowRuleEntity> apiAddParamFlowRule(@RequestBody ParamFlowRuleEntity entity) {
|
||||
Result<ParamFlowRuleEntity> checkResult = checkEntityInternal(entity);
|
||||
if (checkResult != null) {
|
||||
return checkResult;
|
||||
}
|
||||
if (!checkIfSupported(entity.getApp(), entity.getIp(), entity.getPort())) {
|
||||
return unsupportedVersion();
|
||||
}
|
||||
entity.setId(null);
|
||||
entity.getRule().setResource(entity.getResource().trim());
|
||||
Date date = new Date();
|
||||
entity.setGmtCreate(date);
|
||||
entity.setGmtModified(date);
|
||||
try {
|
||||
entity = repository.save(entity);
|
||||
publishRules(entity.getApp());
|
||||
return Result.ofSuccess(entity);
|
||||
} catch (ExecutionException ex) {
|
||||
logger.error("Error when adding new parameter flow rules", ex.getCause());
|
||||
if (isNotSupported(ex.getCause())) {
|
||||
return unsupportedVersion();
|
||||
} else {
|
||||
return Result.ofThrowable(-1, ex.getCause());
|
||||
}
|
||||
} catch (Throwable throwable) {
|
||||
logger.error("Error when adding new parameter flow rules", throwable);
|
||||
return Result.ofFail(-1, throwable.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private <R> Result<R> checkEntityInternal(ParamFlowRuleEntity entity) {
|
||||
if (entity == null) {
|
||||
return Result.ofFail(-1, "bad rule body");
|
||||
}
|
||||
if (StringUtil.isBlank(entity.getApp())) {
|
||||
return Result.ofFail(-1, "app can't be null or empty");
|
||||
}
|
||||
if (StringUtil.isBlank(entity.getIp())) {
|
||||
return Result.ofFail(-1, "ip can't be null or empty");
|
||||
}
|
||||
if (entity.getPort() == null || entity.getPort() <= 0) {
|
||||
return Result.ofFail(-1, "port can't be null");
|
||||
}
|
||||
if (entity.getRule() == null) {
|
||||
return Result.ofFail(-1, "rule can't be null");
|
||||
}
|
||||
if (StringUtil.isBlank(entity.getResource())) {
|
||||
return Result.ofFail(-1, "resource name cannot be null or empty");
|
||||
}
|
||||
if (entity.getCount() < 0) {
|
||||
return Result.ofFail(-1, "count should be valid");
|
||||
}
|
||||
if (entity.getGrade() != RuleConstant.FLOW_GRADE_QPS) {
|
||||
return Result.ofFail(-1, "Unknown mode (blockGrade) for parameter flow control");
|
||||
}
|
||||
if (entity.getParamIdx() == null || entity.getParamIdx() < 0) {
|
||||
return Result.ofFail(-1, "paramIdx should be valid");
|
||||
}
|
||||
if (entity.getDurationInSec() <= 0) {
|
||||
return Result.ofFail(-1, "durationInSec should be valid");
|
||||
}
|
||||
if (entity.getControlBehavior() < 0) {
|
||||
return Result.ofFail(-1, "controlBehavior should be valid");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@PutMapping("/rule/{id}")
|
||||
@AuthAction(AuthService.PrivilegeType.WRITE_RULE)
|
||||
public Result<ParamFlowRuleEntity> apiUpdateParamFlowRule(@PathVariable("id") Long id,
|
||||
@RequestBody ParamFlowRuleEntity entity) {
|
||||
if (id == null || id <= 0) {
|
||||
return Result.ofFail(-1, "Invalid id");
|
||||
}
|
||||
ParamFlowRuleEntity oldEntity = repository.findById(id);
|
||||
if (oldEntity == null) {
|
||||
return Result.ofFail(-1, "id " + id + " does not exist");
|
||||
}
|
||||
|
||||
Result<ParamFlowRuleEntity> checkResult = checkEntityInternal(entity);
|
||||
if (checkResult != null) {
|
||||
return checkResult;
|
||||
}
|
||||
if (!checkIfSupported(entity.getApp(), entity.getIp(), entity.getPort())) {
|
||||
return unsupportedVersion();
|
||||
}
|
||||
entity.setId(id);
|
||||
Date date = new Date();
|
||||
entity.setGmtCreate(oldEntity.getGmtCreate());
|
||||
entity.setGmtModified(date);
|
||||
try {
|
||||
entity = repository.save(entity);
|
||||
publishRules(entity.getApp());
|
||||
return Result.ofSuccess(entity);
|
||||
} catch (ExecutionException ex) {
|
||||
logger.error("Error when updating parameter flow rules, id=" + id, ex.getCause());
|
||||
if (isNotSupported(ex.getCause())) {
|
||||
return unsupportedVersion();
|
||||
} else {
|
||||
return Result.ofThrowable(-1, ex.getCause());
|
||||
}
|
||||
} catch (Throwable throwable) {
|
||||
logger.error("Error when updating parameter flow rules, id=" + id, throwable);
|
||||
return Result.ofFail(-1, throwable.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@DeleteMapping("/rule/{id}")
|
||||
@AuthAction(PrivilegeType.DELETE_RULE)
|
||||
public Result<Long> apiDeleteRule(@PathVariable("id") Long id) {
|
||||
if (id == null) {
|
||||
return Result.ofFail(-1, "id cannot be null");
|
||||
}
|
||||
ParamFlowRuleEntity oldEntity = repository.findById(id);
|
||||
if (oldEntity == null) {
|
||||
return Result.ofSuccess(null);
|
||||
}
|
||||
|
||||
try {
|
||||
repository.delete(id);
|
||||
publishRules(oldEntity.getApp());
|
||||
return Result.ofSuccess(id);
|
||||
} catch (ExecutionException ex) {
|
||||
logger.error("Error when deleting parameter flow rules", ex.getCause());
|
||||
if (isNotSupported(ex.getCause())) {
|
||||
return unsupportedVersion();
|
||||
} else {
|
||||
return Result.ofThrowable(-1, ex.getCause());
|
||||
}
|
||||
} catch (Throwable throwable) {
|
||||
logger.error("Error when deleting parameter flow rules", throwable);
|
||||
return Result.ofFail(-1, throwable.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private void publishRules(String app) throws Exception {
|
||||
List<ParamFlowRuleEntity> rules = repository.findAllByApp(app);
|
||||
rulePublisher.publish(app, rules);
|
||||
//延迟加载
|
||||
delayTime();
|
||||
}
|
||||
|
||||
private <R> Result<R> unsupportedVersion() {
|
||||
return Result.ofFail(4041,
|
||||
"Sentinel client not supported for parameter flow control (unsupported version or dependency absent)");
|
||||
}
|
||||
|
||||
private final SentinelVersion version020 = new SentinelVersion().setMinorVersion(2);
|
||||
}
|
||||
@ -0,0 +1,242 @@
|
||||
package com.alibaba.csp.sentinel.dashboard.controller;
|
||||
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.auth.AuthAction;
|
||||
import com.alibaba.csp.sentinel.dashboard.auth.AuthService.PrivilegeType;
|
||||
import com.alibaba.csp.sentinel.dashboard.controller.base.BaseRuleController;
|
||||
import com.alibaba.csp.sentinel.dashboard.repository.rule.RuleRepository;
|
||||
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRuleProvider;
|
||||
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRulePublisher;
|
||||
import com.alibaba.csp.sentinel.util.StringUtil;
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.SystemRuleEntity;
|
||||
import com.alibaba.csp.sentinel.dashboard.domain.Result;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
|
||||
/**
|
||||
* 系统规则控制器
|
||||
*
|
||||
* @author zyf
|
||||
* @date 2022-04-13
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/system")
|
||||
public class SystemController extends BaseRuleController {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(SystemController.class);
|
||||
|
||||
@Autowired
|
||||
private RuleRepository<SystemRuleEntity, Long> repository;
|
||||
@Autowired
|
||||
@Qualifier("systemRuleNacosProvider")
|
||||
private DynamicRuleProvider<List<SystemRuleEntity>> ruleProvider;
|
||||
@Autowired
|
||||
@Qualifier("systemRuleNacosPublisher")
|
||||
private DynamicRulePublisher<List<SystemRuleEntity>> rulePublisher;
|
||||
|
||||
private <R> Result<R> checkBasicParams(String app, String ip, Integer port) {
|
||||
if (StringUtil.isEmpty(app)) {
|
||||
return Result.ofFail(-1, "app can't be null or empty");
|
||||
}
|
||||
if (StringUtil.isEmpty(ip)) {
|
||||
return Result.ofFail(-1, "ip can't be null or empty");
|
||||
}
|
||||
if (port == null) {
|
||||
return Result.ofFail(-1, "port can't be null");
|
||||
}
|
||||
if (port <= 0 || port > 65535) {
|
||||
return Result.ofFail(-1, "port should be in (0, 65535)");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@GetMapping("/rules.json")
|
||||
@AuthAction(PrivilegeType.READ_RULE)
|
||||
public Result<List<SystemRuleEntity>> apiQueryMachineRules(String app, String ip,
|
||||
Integer port) {
|
||||
Result<List<SystemRuleEntity>> checkResult = checkBasicParams(app, ip, port);
|
||||
if (checkResult != null) {
|
||||
return checkResult;
|
||||
}
|
||||
try {
|
||||
List<SystemRuleEntity> rules = ruleProvider.getRules(app);
|
||||
rules = repository.saveAll(rules);
|
||||
return Result.ofSuccess(rules);
|
||||
} catch (Throwable throwable) {
|
||||
logger.error("Query machine system rules error", throwable);
|
||||
return Result.ofThrowable(-1, throwable);
|
||||
}
|
||||
}
|
||||
|
||||
private int countNotNullAndNotNegative(Number... values) {
|
||||
int notNullCount = 0;
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
if (values[i] != null && values[i].doubleValue() >= 0) {
|
||||
notNullCount++;
|
||||
}
|
||||
}
|
||||
return notNullCount;
|
||||
}
|
||||
|
||||
@RequestMapping("/new.json")
|
||||
@AuthAction(PrivilegeType.WRITE_RULE)
|
||||
public Result<SystemRuleEntity> apiAdd(String app, String ip, Integer port,
|
||||
Double highestSystemLoad, Double highestCpuUsage, Long avgRt,
|
||||
Long maxThread, Double qps) {
|
||||
|
||||
Result<SystemRuleEntity> checkResult = checkBasicParams(app, ip, port);
|
||||
if (checkResult != null) {
|
||||
return checkResult;
|
||||
}
|
||||
|
||||
int notNullCount = countNotNullAndNotNegative(highestSystemLoad, avgRt, maxThread, qps, highestCpuUsage);
|
||||
if (notNullCount != 1) {
|
||||
return Result.ofFail(-1, "only one of [highestSystemLoad, avgRt, maxThread, qps,highestCpuUsage] "
|
||||
+ "value must be set > 0, but " + notNullCount + " values get");
|
||||
}
|
||||
if (null != highestCpuUsage && highestCpuUsage > 1) {
|
||||
return Result.ofFail(-1, "highestCpuUsage must between [0.0, 1.0]");
|
||||
}
|
||||
SystemRuleEntity entity = new SystemRuleEntity();
|
||||
entity.setApp(app.trim());
|
||||
entity.setIp(ip.trim());
|
||||
entity.setPort(port);
|
||||
// -1 is a fake value
|
||||
if (null != highestSystemLoad) {
|
||||
entity.setHighestSystemLoad(highestSystemLoad);
|
||||
} else {
|
||||
entity.setHighestSystemLoad(-1D);
|
||||
}
|
||||
|
||||
if (null != highestCpuUsage) {
|
||||
entity.setHighestCpuUsage(highestCpuUsage);
|
||||
} else {
|
||||
entity.setHighestCpuUsage(-1D);
|
||||
}
|
||||
|
||||
if (avgRt != null) {
|
||||
entity.setAvgRt(avgRt);
|
||||
} else {
|
||||
entity.setAvgRt(-1L);
|
||||
}
|
||||
if (maxThread != null) {
|
||||
entity.setMaxThread(maxThread);
|
||||
} else {
|
||||
entity.setMaxThread(-1L);
|
||||
}
|
||||
if (qps != null) {
|
||||
entity.setQps(qps);
|
||||
} else {
|
||||
entity.setQps(-1D);
|
||||
}
|
||||
Date date = new Date();
|
||||
entity.setGmtCreate(date);
|
||||
entity.setGmtModified(date);
|
||||
try {
|
||||
entity = repository.save(entity);
|
||||
publishRules(app);
|
||||
} catch (Throwable throwable) {
|
||||
logger.error("Add SystemRule error", throwable);
|
||||
return Result.ofThrowable(-1, throwable);
|
||||
}
|
||||
return Result.ofSuccess(entity);
|
||||
}
|
||||
|
||||
@GetMapping("/save.json")
|
||||
@AuthAction(PrivilegeType.WRITE_RULE)
|
||||
public Result<SystemRuleEntity> apiUpdateIfNotNull(Long id, String app, Double highestSystemLoad,
|
||||
Double highestCpuUsage, Long avgRt, Long maxThread, Double qps) {
|
||||
if (id == null) {
|
||||
return Result.ofFail(-1, "id can't be null");
|
||||
}
|
||||
SystemRuleEntity entity = repository.findById(id);
|
||||
if (entity == null) {
|
||||
return Result.ofFail(-1, "id " + id + " dose not exist");
|
||||
}
|
||||
|
||||
if (StringUtil.isNotBlank(app)) {
|
||||
entity.setApp(app.trim());
|
||||
}
|
||||
if (highestSystemLoad != null) {
|
||||
if (highestSystemLoad < 0) {
|
||||
return Result.ofFail(-1, "highestSystemLoad must >= 0");
|
||||
}
|
||||
entity.setHighestSystemLoad(highestSystemLoad);
|
||||
}
|
||||
if (highestCpuUsage != null) {
|
||||
if (highestCpuUsage < 0) {
|
||||
return Result.ofFail(-1, "highestCpuUsage must >= 0");
|
||||
}
|
||||
if (highestCpuUsage > 1) {
|
||||
return Result.ofFail(-1, "highestCpuUsage must <= 1");
|
||||
}
|
||||
entity.setHighestCpuUsage(highestCpuUsage);
|
||||
}
|
||||
if (avgRt != null) {
|
||||
if (avgRt < 0) {
|
||||
return Result.ofFail(-1, "avgRt must >= 0");
|
||||
}
|
||||
entity.setAvgRt(avgRt);
|
||||
}
|
||||
if (maxThread != null) {
|
||||
if (maxThread < 0) {
|
||||
return Result.ofFail(-1, "maxThread must >= 0");
|
||||
}
|
||||
entity.setMaxThread(maxThread);
|
||||
}
|
||||
if (qps != null) {
|
||||
if (qps < 0) {
|
||||
return Result.ofFail(-1, "qps must >= 0");
|
||||
}
|
||||
entity.setQps(qps);
|
||||
}
|
||||
Date date = new Date();
|
||||
entity.setGmtModified(date);
|
||||
try {
|
||||
entity = repository.save(entity);
|
||||
publishRules(entity.getApp());
|
||||
} catch (Throwable throwable) {
|
||||
logger.error("save error:", throwable);
|
||||
return Result.ofThrowable(-1, throwable);
|
||||
}
|
||||
return Result.ofSuccess(entity);
|
||||
}
|
||||
|
||||
@RequestMapping("/delete.json")
|
||||
@AuthAction(PrivilegeType.DELETE_RULE)
|
||||
public Result<?> delete(Long id) {
|
||||
if (id == null) {
|
||||
return Result.ofFail(-1, "id can't be null");
|
||||
}
|
||||
SystemRuleEntity oldEntity = repository.findById(id);
|
||||
if (oldEntity == null) {
|
||||
return Result.ofSuccess(null);
|
||||
}
|
||||
try {
|
||||
repository.delete(id);
|
||||
publishRules(oldEntity.getApp());
|
||||
} catch (Throwable throwable) {
|
||||
logger.error("delete error:", throwable);
|
||||
return Result.ofThrowable(-1, throwable);
|
||||
}
|
||||
return Result.ofSuccess(id);
|
||||
}
|
||||
|
||||
private void publishRules(String app) throws Exception {
|
||||
List<SystemRuleEntity> rules = repository.findAllByApp(app);
|
||||
rulePublisher.publish(app, rules);
|
||||
//延迟加载
|
||||
delayTime();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,26 @@
|
||||
package com.alibaba.csp.sentinel.dashboard.controller.base;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
|
||||
/**
|
||||
* Nacos持久化通用处理类
|
||||
*
|
||||
* @author zyf
|
||||
* @date 2022-04-13
|
||||
*/
|
||||
public class BaseRuleController {
|
||||
/**
|
||||
* 延迟一下
|
||||
*
|
||||
* 解释:列表加载数据的时候,Nacos持久化还没做完,导致加载数据不对
|
||||
*/
|
||||
public static void delayTime(){
|
||||
try {
|
||||
TimeUnit.MILLISECONDS.sleep(100);
|
||||
System.out.println("-------------睡100毫秒-----------");
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,260 @@
|
||||
package com.alibaba.csp.sentinel.dashboard.controller.gateway;
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.auth.AuthAction;
|
||||
import com.alibaba.csp.sentinel.dashboard.auth.AuthService;
|
||||
import com.alibaba.csp.sentinel.dashboard.controller.base.BaseRuleController;
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.ApiDefinitionEntity;
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.ApiPredicateItemEntity;
|
||||
import com.alibaba.csp.sentinel.dashboard.discovery.MachineInfo;
|
||||
import com.alibaba.csp.sentinel.dashboard.domain.Result;
|
||||
import com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.api.AddApiReqVo;
|
||||
import com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.api.ApiPredicateItemVo;
|
||||
import com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.api.UpdateApiReqVo;
|
||||
import com.alibaba.csp.sentinel.dashboard.repository.gateway.InMemApiDefinitionStore;
|
||||
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRuleProvider;
|
||||
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRulePublisher;
|
||||
import com.alibaba.csp.sentinel.util.StringUtil;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.*;
|
||||
|
||||
import static com.alibaba.csp.sentinel.adapter.gateway.common.SentinelGatewayConstants.*;
|
||||
|
||||
/**
|
||||
* 网关API规则控制器
|
||||
*
|
||||
* @author zyf
|
||||
* @date 2022-04-13
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping(value = "/gateway/api")
|
||||
public class GatewayApiController extends BaseRuleController {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(GatewayApiController.class);
|
||||
|
||||
@Autowired
|
||||
private InMemApiDefinitionStore repository;
|
||||
|
||||
|
||||
@Autowired
|
||||
@Qualifier("gateWayApiNacosProvider")
|
||||
private DynamicRuleProvider<List<ApiDefinitionEntity>> apiProvider;
|
||||
|
||||
@Autowired
|
||||
@Qualifier("gateWayApiNacosPublisher")
|
||||
private DynamicRulePublisher<List<ApiDefinitionEntity>> apiPublisher;
|
||||
|
||||
@GetMapping("/list.json")
|
||||
@AuthAction(AuthService.PrivilegeType.READ_RULE)
|
||||
public Result<List<ApiDefinitionEntity>> queryApis(String app, String ip, Integer port) {
|
||||
if (StringUtil.isEmpty(app)) {
|
||||
return Result.ofFail(-1, "app can't be null or empty");
|
||||
}
|
||||
if (StringUtil.isEmpty(ip)) {
|
||||
return Result.ofFail(-1, "ip can't be null or empty");
|
||||
}
|
||||
if (port == null) {
|
||||
return Result.ofFail(-1, "port can't be null");
|
||||
}
|
||||
|
||||
try {
|
||||
List<ApiDefinitionEntity> apis = apiProvider.getRules(app);
|
||||
repository.saveAll(apis);
|
||||
return Result.ofSuccess(apis);
|
||||
} catch (Throwable throwable) {
|
||||
logger.error("queryApis error:", throwable);
|
||||
return Result.ofThrowable(-1, throwable);
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping("/new.json")
|
||||
@AuthAction(AuthService.PrivilegeType.WRITE_RULE)
|
||||
public Result<ApiDefinitionEntity> addApi(HttpServletRequest request, @RequestBody AddApiReqVo reqVo) {
|
||||
|
||||
String app = reqVo.getApp();
|
||||
if (StringUtil.isBlank(app)) {
|
||||
return Result.ofFail(-1, "app can't be null or empty");
|
||||
}
|
||||
|
||||
ApiDefinitionEntity entity = new ApiDefinitionEntity();
|
||||
entity.setApp(app.trim());
|
||||
|
||||
String ip = reqVo.getIp();
|
||||
if (StringUtil.isBlank(ip)) {
|
||||
return Result.ofFail(-1, "ip can't be null or empty");
|
||||
}
|
||||
entity.setIp(ip.trim());
|
||||
|
||||
Integer port = reqVo.getPort();
|
||||
if (port == null) {
|
||||
return Result.ofFail(-1, "port can't be null");
|
||||
}
|
||||
entity.setPort(port);
|
||||
|
||||
// API名称
|
||||
String apiName = reqVo.getApiName();
|
||||
if (StringUtil.isBlank(apiName)) {
|
||||
return Result.ofFail(-1, "apiName can't be null or empty");
|
||||
}
|
||||
entity.setApiName(apiName.trim());
|
||||
|
||||
// 匹配规则列表
|
||||
List<ApiPredicateItemVo> predicateItems = reqVo.getPredicateItems();
|
||||
if (CollectionUtils.isEmpty(predicateItems)) {
|
||||
return Result.ofFail(-1, "predicateItems can't empty");
|
||||
}
|
||||
|
||||
List<ApiPredicateItemEntity> predicateItemEntities = new ArrayList<>();
|
||||
for (ApiPredicateItemVo predicateItem : predicateItems) {
|
||||
ApiPredicateItemEntity predicateItemEntity = new ApiPredicateItemEntity();
|
||||
|
||||
// 匹配模式
|
||||
Integer matchStrategy = predicateItem.getMatchStrategy();
|
||||
if (!Arrays.asList(URL_MATCH_STRATEGY_EXACT, URL_MATCH_STRATEGY_PREFIX, URL_MATCH_STRATEGY_REGEX).contains(matchStrategy)) {
|
||||
return Result.ofFail(-1, "invalid matchStrategy: " + matchStrategy);
|
||||
}
|
||||
predicateItemEntity.setMatchStrategy(matchStrategy);
|
||||
|
||||
// 匹配串
|
||||
String pattern = predicateItem.getPattern();
|
||||
if (StringUtil.isBlank(pattern)) {
|
||||
return Result.ofFail(-1, "pattern can't be null or empty");
|
||||
}
|
||||
predicateItemEntity.setPattern(pattern);
|
||||
|
||||
predicateItemEntities.add(predicateItemEntity);
|
||||
}
|
||||
entity.setPredicateItems(new LinkedHashSet<>(predicateItemEntities));
|
||||
|
||||
// 检查API名称不能重复
|
||||
List<ApiDefinitionEntity> allApis = repository.findAllByMachine(MachineInfo.of(app.trim(), ip.trim(), port));
|
||||
if (allApis.stream().map(o -> o.getApiName()).anyMatch(o -> o.equals(apiName.trim()))) {
|
||||
return Result.ofFail(-1, "apiName exists: " + apiName);
|
||||
}
|
||||
|
||||
Date date = new Date();
|
||||
entity.setGmtCreate(date);
|
||||
entity.setGmtModified(date);
|
||||
|
||||
try {
|
||||
entity = repository.save(entity);
|
||||
} catch (Throwable throwable) {
|
||||
logger.error("add gateway api error:", throwable);
|
||||
return Result.ofThrowable(-1, throwable);
|
||||
}
|
||||
|
||||
if (!publishApis(app, ip, port)) {
|
||||
logger.warn("publish gateway apis fail after add");
|
||||
}
|
||||
|
||||
return Result.ofSuccess(entity);
|
||||
}
|
||||
|
||||
@PostMapping("/save.json")
|
||||
@AuthAction(AuthService.PrivilegeType.WRITE_RULE)
|
||||
public Result<ApiDefinitionEntity> updateApi(@RequestBody UpdateApiReqVo reqVo) {
|
||||
String app = reqVo.getApp();
|
||||
if (StringUtil.isBlank(app)) {
|
||||
return Result.ofFail(-1, "app can't be null or empty");
|
||||
}
|
||||
|
||||
Long id = reqVo.getId();
|
||||
if (id == null) {
|
||||
return Result.ofFail(-1, "id can't be null");
|
||||
}
|
||||
|
||||
ApiDefinitionEntity entity = repository.findById(id);
|
||||
if (entity == null) {
|
||||
return Result.ofFail(-1, "api does not exist, id=" + id);
|
||||
}
|
||||
|
||||
// 匹配规则列表
|
||||
List<ApiPredicateItemVo> predicateItems = reqVo.getPredicateItems();
|
||||
if (CollectionUtils.isEmpty(predicateItems)) {
|
||||
return Result.ofFail(-1, "predicateItems can't empty");
|
||||
}
|
||||
|
||||
List<ApiPredicateItemEntity> predicateItemEntities = new ArrayList<>();
|
||||
for (ApiPredicateItemVo predicateItem : predicateItems) {
|
||||
ApiPredicateItemEntity predicateItemEntity = new ApiPredicateItemEntity();
|
||||
|
||||
// 匹配模式
|
||||
int matchStrategy = predicateItem.getMatchStrategy();
|
||||
if (!Arrays.asList(URL_MATCH_STRATEGY_EXACT, URL_MATCH_STRATEGY_PREFIX, URL_MATCH_STRATEGY_REGEX).contains(matchStrategy)) {
|
||||
return Result.ofFail(-1, "Invalid matchStrategy: " + matchStrategy);
|
||||
}
|
||||
predicateItemEntity.setMatchStrategy(matchStrategy);
|
||||
|
||||
// 匹配串
|
||||
String pattern = predicateItem.getPattern();
|
||||
if (StringUtil.isBlank(pattern)) {
|
||||
return Result.ofFail(-1, "pattern can't be null or empty");
|
||||
}
|
||||
predicateItemEntity.setPattern(pattern);
|
||||
|
||||
predicateItemEntities.add(predicateItemEntity);
|
||||
}
|
||||
entity.setPredicateItems(new LinkedHashSet<>(predicateItemEntities));
|
||||
|
||||
Date date = new Date();
|
||||
entity.setGmtModified(date);
|
||||
|
||||
try {
|
||||
entity = repository.save(entity);
|
||||
} catch (Throwable throwable) {
|
||||
logger.error("update gateway api error:", throwable);
|
||||
return Result.ofThrowable(-1, throwable);
|
||||
}
|
||||
|
||||
if (!publishApis(app, entity.getIp(), entity.getPort())) {
|
||||
logger.warn("publish gateway apis fail after update");
|
||||
}
|
||||
|
||||
return Result.ofSuccess(entity);
|
||||
}
|
||||
|
||||
@PostMapping("/delete.json")
|
||||
@AuthAction(AuthService.PrivilegeType.DELETE_RULE)
|
||||
public Result<Long> deleteApi(Long id) {
|
||||
if (id == null) {
|
||||
return Result.ofFail(-1, "id can't be null");
|
||||
}
|
||||
|
||||
ApiDefinitionEntity oldEntity = repository.findById(id);
|
||||
if (oldEntity == null) {
|
||||
return Result.ofSuccess(null);
|
||||
}
|
||||
|
||||
try {
|
||||
repository.delete(id);
|
||||
} catch (Throwable throwable) {
|
||||
logger.error("delete gateway api error:", throwable);
|
||||
return Result.ofThrowable(-1, throwable);
|
||||
}
|
||||
|
||||
if (!publishApis(oldEntity.getApp(), oldEntity.getIp(), oldEntity.getPort())) {
|
||||
logger.warn("publish gateway apis fail after delete");
|
||||
}
|
||||
return Result.ofSuccess(id);
|
||||
}
|
||||
|
||||
private boolean publishApis(String app, String ip, Integer port) {
|
||||
List<ApiDefinitionEntity> apis = repository.findAllByApp(app);
|
||||
try {
|
||||
apiPublisher.publish(app, apis);
|
||||
//延迟加载
|
||||
delayTime();
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
logger.error("publish api error!");
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,431 @@
|
||||
package com.alibaba.csp.sentinel.dashboard.controller.gateway;
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.auth.AuthAction;
|
||||
import com.alibaba.csp.sentinel.dashboard.auth.AuthService;
|
||||
import com.alibaba.csp.sentinel.dashboard.controller.base.BaseRuleController;
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.GatewayFlowRuleEntity;
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.GatewayParamFlowItemEntity;
|
||||
import com.alibaba.csp.sentinel.dashboard.domain.Result;
|
||||
import com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.rule.AddFlowRuleReqVo;
|
||||
import com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.rule.GatewayParamFlowItemVo;
|
||||
import com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.rule.UpdateFlowRuleReqVo;
|
||||
import com.alibaba.csp.sentinel.dashboard.repository.gateway.InMemGatewayFlowRuleStore;
|
||||
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRuleProvider;
|
||||
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRulePublisher;
|
||||
import com.alibaba.csp.sentinel.util.StringUtil;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import static com.alibaba.csp.sentinel.slots.block.RuleConstant.*;
|
||||
import static com.alibaba.csp.sentinel.adapter.gateway.common.SentinelGatewayConstants.*;
|
||||
import static com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.GatewayFlowRuleEntity.*;
|
||||
|
||||
/**
|
||||
* 网关限流规则控制器
|
||||
*
|
||||
* @author zyf
|
||||
* @date 2022-04-13
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping(value = "/gateway/flow")
|
||||
public class GatewayFlowRuleController extends BaseRuleController {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(GatewayFlowRuleController.class);
|
||||
|
||||
@Autowired
|
||||
private InMemGatewayFlowRuleStore repository;
|
||||
|
||||
@Autowired
|
||||
@Qualifier("gateWayFlowRulesNacosProvider")
|
||||
private DynamicRuleProvider<List<GatewayFlowRuleEntity>> ruleProvider;
|
||||
|
||||
@Autowired
|
||||
@Qualifier("gateWayFlowRulesNacosPublisher")
|
||||
private DynamicRulePublisher<List<GatewayFlowRuleEntity>> rulePublisher;
|
||||
|
||||
@GetMapping("/list.json")
|
||||
@AuthAction(AuthService.PrivilegeType.READ_RULE)
|
||||
public Result<List<GatewayFlowRuleEntity>> queryFlowRules(String app, String ip, Integer port) {
|
||||
|
||||
if (StringUtil.isEmpty(app)) {
|
||||
return Result.ofFail(-1, "app can't be null or empty");
|
||||
}
|
||||
if (StringUtil.isEmpty(ip)) {
|
||||
return Result.ofFail(-1, "ip can't be null or empty");
|
||||
}
|
||||
if (port == null) {
|
||||
return Result.ofFail(-1, "port can't be null");
|
||||
}
|
||||
|
||||
try {
|
||||
List<GatewayFlowRuleEntity> rules = ruleProvider.getRules(app);
|
||||
repository.saveAll(rules);
|
||||
return Result.ofSuccess(rules);
|
||||
} catch (Throwable throwable) {
|
||||
logger.error("query gateway flow rules error:", throwable);
|
||||
return Result.ofThrowable(-1, throwable);
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping("/new.json")
|
||||
@AuthAction(AuthService.PrivilegeType.WRITE_RULE)
|
||||
public Result<GatewayFlowRuleEntity> addFlowRule(@RequestBody AddFlowRuleReqVo reqVo) {
|
||||
|
||||
String app = reqVo.getApp();
|
||||
if (StringUtil.isBlank(app)) {
|
||||
return Result.ofFail(-1, "app can't be null or empty");
|
||||
}
|
||||
|
||||
GatewayFlowRuleEntity entity = new GatewayFlowRuleEntity();
|
||||
entity.setApp(app.trim());
|
||||
|
||||
String ip = reqVo.getIp();
|
||||
if (StringUtil.isBlank(ip)) {
|
||||
return Result.ofFail(-1, "ip can't be null or empty");
|
||||
}
|
||||
entity.setIp(ip.trim());
|
||||
|
||||
Integer port = reqVo.getPort();
|
||||
if (port == null) {
|
||||
return Result.ofFail(-1, "port can't be null");
|
||||
}
|
||||
entity.setPort(port);
|
||||
|
||||
// API类型, Route ID或API分组
|
||||
Integer resourceMode = reqVo.getResourceMode();
|
||||
if (resourceMode == null) {
|
||||
return Result.ofFail(-1, "resourceMode can't be null");
|
||||
}
|
||||
if (!Arrays.asList(RESOURCE_MODE_ROUTE_ID, RESOURCE_MODE_CUSTOM_API_NAME).contains(resourceMode)) {
|
||||
return Result.ofFail(-1, "invalid resourceMode: " + resourceMode);
|
||||
}
|
||||
entity.setResourceMode(resourceMode);
|
||||
|
||||
// API名称
|
||||
String resource = reqVo.getResource();
|
||||
if (StringUtil.isBlank(resource)) {
|
||||
return Result.ofFail(-1, "resource can't be null or empty");
|
||||
}
|
||||
entity.setResource(resource.trim());
|
||||
|
||||
// 针对请求属性
|
||||
GatewayParamFlowItemVo paramItem = reqVo.getParamItem();
|
||||
if (paramItem != null) {
|
||||
GatewayParamFlowItemEntity itemEntity = new GatewayParamFlowItemEntity();
|
||||
entity.setParamItem(itemEntity);
|
||||
|
||||
// 参数属性 0-ClientIP 1-Remote Host 2-Header 3-URL参数 4-Cookie
|
||||
Integer parseStrategy = paramItem.getParseStrategy();
|
||||
if (!Arrays.asList(PARAM_PARSE_STRATEGY_CLIENT_IP, PARAM_PARSE_STRATEGY_HOST, PARAM_PARSE_STRATEGY_HEADER
|
||||
, PARAM_PARSE_STRATEGY_URL_PARAM, PARAM_PARSE_STRATEGY_COOKIE).contains(parseStrategy)) {
|
||||
return Result.ofFail(-1, "invalid parseStrategy: " + parseStrategy);
|
||||
}
|
||||
itemEntity.setParseStrategy(paramItem.getParseStrategy());
|
||||
|
||||
// 当参数属性为2-Header 3-URL参数 4-Cookie时,参数名称必填
|
||||
if (Arrays.asList(PARAM_PARSE_STRATEGY_HEADER, PARAM_PARSE_STRATEGY_URL_PARAM, PARAM_PARSE_STRATEGY_COOKIE).contains(parseStrategy)) {
|
||||
// 参数名称
|
||||
String fieldName = paramItem.getFieldName();
|
||||
if (StringUtil.isBlank(fieldName)) {
|
||||
return Result.ofFail(-1, "fieldName can't be null or empty");
|
||||
}
|
||||
itemEntity.setFieldName(paramItem.getFieldName());
|
||||
}
|
||||
|
||||
String pattern = paramItem.getPattern();
|
||||
// 如果匹配串不为空,验证匹配模式
|
||||
if (StringUtil.isNotEmpty(pattern)) {
|
||||
itemEntity.setPattern(pattern);
|
||||
Integer matchStrategy = paramItem.getMatchStrategy();
|
||||
if (!Arrays.asList(PARAM_MATCH_STRATEGY_EXACT, PARAM_MATCH_STRATEGY_CONTAINS, PARAM_MATCH_STRATEGY_REGEX).contains(matchStrategy)) {
|
||||
return Result.ofFail(-1, "invalid matchStrategy: " + matchStrategy);
|
||||
}
|
||||
itemEntity.setMatchStrategy(matchStrategy);
|
||||
}
|
||||
}
|
||||
|
||||
// 阈值类型 0-线程数 1-QPS
|
||||
Integer grade = reqVo.getGrade();
|
||||
if (grade == null) {
|
||||
return Result.ofFail(-1, "grade can't be null");
|
||||
}
|
||||
if (!Arrays.asList(FLOW_GRADE_THREAD, FLOW_GRADE_QPS).contains(grade)) {
|
||||
return Result.ofFail(-1, "invalid grade: " + grade);
|
||||
}
|
||||
entity.setGrade(grade);
|
||||
|
||||
// QPS阈值
|
||||
Double count = reqVo.getCount();
|
||||
if (count == null) {
|
||||
return Result.ofFail(-1, "count can't be null");
|
||||
}
|
||||
if (count < 0) {
|
||||
return Result.ofFail(-1, "count should be at lease zero");
|
||||
}
|
||||
entity.setCount(count);
|
||||
|
||||
// 间隔
|
||||
Long interval = reqVo.getInterval();
|
||||
if (interval == null) {
|
||||
return Result.ofFail(-1, "interval can't be null");
|
||||
}
|
||||
if (interval <= 0) {
|
||||
return Result.ofFail(-1, "interval should be greater than zero");
|
||||
}
|
||||
entity.setInterval(interval);
|
||||
|
||||
// 间隔单位
|
||||
Integer intervalUnit = reqVo.getIntervalUnit();
|
||||
if (intervalUnit == null) {
|
||||
return Result.ofFail(-1, "intervalUnit can't be null");
|
||||
}
|
||||
if (!Arrays.asList(INTERVAL_UNIT_SECOND, INTERVAL_UNIT_MINUTE, INTERVAL_UNIT_HOUR, INTERVAL_UNIT_DAY).contains(intervalUnit)) {
|
||||
return Result.ofFail(-1, "Invalid intervalUnit: " + intervalUnit);
|
||||
}
|
||||
entity.setIntervalUnit(intervalUnit);
|
||||
|
||||
// 流控方式 0-快速失败 2-匀速排队
|
||||
Integer controlBehavior = reqVo.getControlBehavior();
|
||||
if (controlBehavior == null) {
|
||||
return Result.ofFail(-1, "controlBehavior can't be null");
|
||||
}
|
||||
if (!Arrays.asList(CONTROL_BEHAVIOR_DEFAULT, CONTROL_BEHAVIOR_RATE_LIMITER).contains(controlBehavior)) {
|
||||
return Result.ofFail(-1, "invalid controlBehavior: " + controlBehavior);
|
||||
}
|
||||
entity.setControlBehavior(controlBehavior);
|
||||
|
||||
if (CONTROL_BEHAVIOR_DEFAULT == controlBehavior) {
|
||||
// 0-快速失败, 则Burst size必填
|
||||
Integer burst = reqVo.getBurst();
|
||||
if (burst == null) {
|
||||
return Result.ofFail(-1, "burst can't be null");
|
||||
}
|
||||
if (burst < 0) {
|
||||
return Result.ofFail(-1, "invalid burst: " + burst);
|
||||
}
|
||||
entity.setBurst(burst);
|
||||
} else if (CONTROL_BEHAVIOR_RATE_LIMITER == controlBehavior) {
|
||||
// 1-匀速排队, 则超时时间必填
|
||||
Integer maxQueueingTimeoutMs = reqVo.getMaxQueueingTimeoutMs();
|
||||
if (maxQueueingTimeoutMs == null) {
|
||||
return Result.ofFail(-1, "maxQueueingTimeoutMs can't be null");
|
||||
}
|
||||
if (maxQueueingTimeoutMs < 0) {
|
||||
return Result.ofFail(-1, "invalid maxQueueingTimeoutMs: " + maxQueueingTimeoutMs);
|
||||
}
|
||||
entity.setMaxQueueingTimeoutMs(maxQueueingTimeoutMs);
|
||||
}
|
||||
|
||||
Date date = new Date();
|
||||
entity.setGmtCreate(date);
|
||||
entity.setGmtModified(date);
|
||||
|
||||
try {
|
||||
entity = repository.save(entity);
|
||||
} catch (Throwable throwable) {
|
||||
logger.error("add gateway flow rule error:", throwable);
|
||||
return Result.ofThrowable(-1, throwable);
|
||||
}
|
||||
|
||||
if (!publishRules(app, ip, port)) {
|
||||
logger.warn("publish gateway flow rules fail after add");
|
||||
}
|
||||
|
||||
return Result.ofSuccess(entity);
|
||||
}
|
||||
|
||||
@PostMapping("/save.json")
|
||||
@AuthAction(AuthService.PrivilegeType.WRITE_RULE)
|
||||
public Result<GatewayFlowRuleEntity> updateFlowRule(@RequestBody UpdateFlowRuleReqVo reqVo) {
|
||||
|
||||
String app = reqVo.getApp();
|
||||
if (StringUtil.isBlank(app)) {
|
||||
return Result.ofFail(-1, "app can't be null or empty");
|
||||
}
|
||||
|
||||
Long id = reqVo.getId();
|
||||
if (id == null) {
|
||||
return Result.ofFail(-1, "id can't be null");
|
||||
}
|
||||
|
||||
GatewayFlowRuleEntity entity = repository.findById(id);
|
||||
if (entity == null) {
|
||||
return Result.ofFail(-1, "gateway flow rule does not exist, id=" + id);
|
||||
}
|
||||
|
||||
// 针对请求属性
|
||||
GatewayParamFlowItemVo paramItem = reqVo.getParamItem();
|
||||
if (paramItem != null) {
|
||||
GatewayParamFlowItemEntity itemEntity = new GatewayParamFlowItemEntity();
|
||||
entity.setParamItem(itemEntity);
|
||||
|
||||
// 参数属性 0-ClientIP 1-Remote Host 2-Header 3-URL参数 4-Cookie
|
||||
Integer parseStrategy = paramItem.getParseStrategy();
|
||||
if (!Arrays.asList(PARAM_PARSE_STRATEGY_CLIENT_IP, PARAM_PARSE_STRATEGY_HOST, PARAM_PARSE_STRATEGY_HEADER
|
||||
, PARAM_PARSE_STRATEGY_URL_PARAM, PARAM_PARSE_STRATEGY_COOKIE).contains(parseStrategy)) {
|
||||
return Result.ofFail(-1, "invalid parseStrategy: " + parseStrategy);
|
||||
}
|
||||
itemEntity.setParseStrategy(paramItem.getParseStrategy());
|
||||
|
||||
// 当参数属性为2-Header 3-URL参数 4-Cookie时,参数名称必填
|
||||
if (Arrays.asList(PARAM_PARSE_STRATEGY_HEADER, PARAM_PARSE_STRATEGY_URL_PARAM, PARAM_PARSE_STRATEGY_COOKIE).contains(parseStrategy)) {
|
||||
// 参数名称
|
||||
String fieldName = paramItem.getFieldName();
|
||||
if (StringUtil.isBlank(fieldName)) {
|
||||
return Result.ofFail(-1, "fieldName can't be null or empty");
|
||||
}
|
||||
itemEntity.setFieldName(paramItem.getFieldName());
|
||||
}
|
||||
|
||||
String pattern = paramItem.getPattern();
|
||||
// 如果匹配串不为空,验证匹配模式
|
||||
if (StringUtil.isNotEmpty(pattern)) {
|
||||
itemEntity.setPattern(pattern);
|
||||
Integer matchStrategy = paramItem.getMatchStrategy();
|
||||
if (!Arrays.asList(PARAM_MATCH_STRATEGY_EXACT, PARAM_MATCH_STRATEGY_CONTAINS, PARAM_MATCH_STRATEGY_REGEX).contains(matchStrategy)) {
|
||||
return Result.ofFail(-1, "invalid matchStrategy: " + matchStrategy);
|
||||
}
|
||||
itemEntity.setMatchStrategy(matchStrategy);
|
||||
}
|
||||
} else {
|
||||
entity.setParamItem(null);
|
||||
}
|
||||
|
||||
// 阈值类型 0-线程数 1-QPS
|
||||
Integer grade = reqVo.getGrade();
|
||||
if (grade == null) {
|
||||
return Result.ofFail(-1, "grade can't be null");
|
||||
}
|
||||
if (!Arrays.asList(FLOW_GRADE_THREAD, FLOW_GRADE_QPS).contains(grade)) {
|
||||
return Result.ofFail(-1, "invalid grade: " + grade);
|
||||
}
|
||||
entity.setGrade(grade);
|
||||
|
||||
// QPS阈值
|
||||
Double count = reqVo.getCount();
|
||||
if (count == null) {
|
||||
return Result.ofFail(-1, "count can't be null");
|
||||
}
|
||||
if (count < 0) {
|
||||
return Result.ofFail(-1, "count should be at lease zero");
|
||||
}
|
||||
entity.setCount(count);
|
||||
|
||||
// 间隔
|
||||
Long interval = reqVo.getInterval();
|
||||
if (interval == null) {
|
||||
return Result.ofFail(-1, "interval can't be null");
|
||||
}
|
||||
if (interval <= 0) {
|
||||
return Result.ofFail(-1, "interval should be greater than zero");
|
||||
}
|
||||
entity.setInterval(interval);
|
||||
|
||||
// 间隔单位
|
||||
Integer intervalUnit = reqVo.getIntervalUnit();
|
||||
if (intervalUnit == null) {
|
||||
return Result.ofFail(-1, "intervalUnit can't be null");
|
||||
}
|
||||
if (!Arrays.asList(INTERVAL_UNIT_SECOND, INTERVAL_UNIT_MINUTE, INTERVAL_UNIT_HOUR, INTERVAL_UNIT_DAY).contains(intervalUnit)) {
|
||||
return Result.ofFail(-1, "Invalid intervalUnit: " + intervalUnit);
|
||||
}
|
||||
entity.setIntervalUnit(intervalUnit);
|
||||
|
||||
// 流控方式 0-快速失败 2-匀速排队
|
||||
Integer controlBehavior = reqVo.getControlBehavior();
|
||||
if (controlBehavior == null) {
|
||||
return Result.ofFail(-1, "controlBehavior can't be null");
|
||||
}
|
||||
if (!Arrays.asList(CONTROL_BEHAVIOR_DEFAULT, CONTROL_BEHAVIOR_RATE_LIMITER).contains(controlBehavior)) {
|
||||
return Result.ofFail(-1, "invalid controlBehavior: " + controlBehavior);
|
||||
}
|
||||
entity.setControlBehavior(controlBehavior);
|
||||
|
||||
if (CONTROL_BEHAVIOR_DEFAULT == controlBehavior) {
|
||||
// 0-快速失败, 则Burst size必填
|
||||
Integer burst = reqVo.getBurst();
|
||||
if (burst == null) {
|
||||
return Result.ofFail(-1, "burst can't be null");
|
||||
}
|
||||
if (burst < 0) {
|
||||
return Result.ofFail(-1, "invalid burst: " + burst);
|
||||
}
|
||||
entity.setBurst(burst);
|
||||
} else if (CONTROL_BEHAVIOR_RATE_LIMITER == controlBehavior) {
|
||||
// 2-匀速排队, 则超时时间必填
|
||||
Integer maxQueueingTimeoutMs = reqVo.getMaxQueueingTimeoutMs();
|
||||
if (maxQueueingTimeoutMs == null) {
|
||||
return Result.ofFail(-1, "maxQueueingTimeoutMs can't be null");
|
||||
}
|
||||
if (maxQueueingTimeoutMs < 0) {
|
||||
return Result.ofFail(-1, "invalid maxQueueingTimeoutMs: " + maxQueueingTimeoutMs);
|
||||
}
|
||||
entity.setMaxQueueingTimeoutMs(maxQueueingTimeoutMs);
|
||||
}
|
||||
|
||||
Date date = new Date();
|
||||
entity.setGmtModified(date);
|
||||
|
||||
try {
|
||||
entity = repository.save(entity);
|
||||
} catch (Throwable throwable) {
|
||||
logger.error("update gateway flow rule error:", throwable);
|
||||
return Result.ofThrowable(-1, throwable);
|
||||
}
|
||||
|
||||
if (!publishRules(app, entity.getIp(), entity.getPort())) {
|
||||
logger.warn("publish gateway flow rules fail after update");
|
||||
}
|
||||
|
||||
return Result.ofSuccess(entity);
|
||||
}
|
||||
|
||||
|
||||
@PostMapping("/delete.json")
|
||||
@AuthAction(AuthService.PrivilegeType.DELETE_RULE)
|
||||
public Result<Long> deleteFlowRule(Long id) {
|
||||
|
||||
if (id == null) {
|
||||
return Result.ofFail(-1, "id can't be null");
|
||||
}
|
||||
|
||||
GatewayFlowRuleEntity oldEntity = repository.findById(id);
|
||||
if (oldEntity == null) {
|
||||
return Result.ofSuccess(null);
|
||||
}
|
||||
|
||||
try {
|
||||
repository.delete(id);
|
||||
} catch (Throwable throwable) {
|
||||
logger.error("delete gateway flow rule error:", throwable);
|
||||
return Result.ofThrowable(-1, throwable);
|
||||
}
|
||||
|
||||
if (!publishRules(oldEntity.getApp(), oldEntity.getIp(), oldEntity.getPort())) {
|
||||
logger.warn("publish gateway flow rules fail after delete");
|
||||
}
|
||||
|
||||
return Result.ofSuccess(id);
|
||||
}
|
||||
|
||||
private boolean publishRules(String app, String ip, Integer port) {
|
||||
List<GatewayFlowRuleEntity> rules = repository.findAllByApp(app);
|
||||
try {
|
||||
rulePublisher.publish(app, rules);
|
||||
//延迟加载
|
||||
delayTime();
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
logger.error("publish rules error!");
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,230 @@
|
||||
/*
|
||||
* Copyright 1999-2018 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.csp.sentinel.dashboard.controller.v2;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.auth.AuthAction;
|
||||
import com.alibaba.csp.sentinel.dashboard.auth.AuthService;
|
||||
import com.alibaba.csp.sentinel.dashboard.auth.AuthService.PrivilegeType;
|
||||
import com.alibaba.csp.sentinel.dashboard.controller.base.BaseRuleController;
|
||||
import com.alibaba.csp.sentinel.util.StringUtil;
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity;
|
||||
import com.alibaba.csp.sentinel.dashboard.repository.rule.InMemoryRuleRepositoryAdapter;
|
||||
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRuleProvider;
|
||||
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRulePublisher;
|
||||
import com.alibaba.csp.sentinel.dashboard.domain.Result;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
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.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* 流控规则控制器
|
||||
*
|
||||
* @author zyf
|
||||
* @date 2022-04-13
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping(value = "/v2/flow")
|
||||
public class FlowControllerV2 extends BaseRuleController {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(FlowControllerV2.class);
|
||||
|
||||
@Autowired
|
||||
private InMemoryRuleRepositoryAdapter<FlowRuleEntity> repository;
|
||||
|
||||
@Autowired
|
||||
@Qualifier("flowRuleNacosProvider")
|
||||
private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider;
|
||||
@Autowired
|
||||
@Qualifier("flowRuleNacosPublisher")
|
||||
private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher;
|
||||
|
||||
@GetMapping("/rules")
|
||||
@AuthAction(PrivilegeType.READ_RULE)
|
||||
public Result<List<FlowRuleEntity>> apiQueryMachineRules(@RequestParam String app) {
|
||||
|
||||
if (StringUtil.isEmpty(app)) {
|
||||
return Result.ofFail(-1, "app can't be null or empty");
|
||||
}
|
||||
try {
|
||||
List<FlowRuleEntity> rules = ruleProvider.getRules(app);
|
||||
if (rules != null && !rules.isEmpty()) {
|
||||
for (FlowRuleEntity entity : rules) {
|
||||
entity.setApp(app);
|
||||
if (entity.getClusterConfig() != null && entity.getClusterConfig().getFlowId() != null) {
|
||||
entity.setId(entity.getClusterConfig().getFlowId());
|
||||
}
|
||||
}
|
||||
}
|
||||
rules = repository.saveAll(rules);
|
||||
return Result.ofSuccess(rules);
|
||||
} catch (Throwable throwable) {
|
||||
logger.error("Error when querying flow rules", throwable);
|
||||
return Result.ofThrowable(-1, throwable);
|
||||
}
|
||||
}
|
||||
|
||||
private <R> Result<R> checkEntityInternal(FlowRuleEntity entity) {
|
||||
if (entity == null) {
|
||||
return Result.ofFail(-1, "invalid body");
|
||||
}
|
||||
if (StringUtil.isBlank(entity.getApp())) {
|
||||
return Result.ofFail(-1, "app can't be null or empty");
|
||||
}
|
||||
if (StringUtil.isBlank(entity.getLimitApp())) {
|
||||
return Result.ofFail(-1, "limitApp can't be null or empty");
|
||||
}
|
||||
if (StringUtil.isBlank(entity.getResource())) {
|
||||
return Result.ofFail(-1, "resource can't be null or empty");
|
||||
}
|
||||
if (entity.getGrade() == null) {
|
||||
return Result.ofFail(-1, "grade can't be null");
|
||||
}
|
||||
if (entity.getGrade() != 0 && entity.getGrade() != 1) {
|
||||
return Result.ofFail(-1, "grade must be 0 or 1, but " + entity.getGrade() + " got");
|
||||
}
|
||||
if (entity.getCount() == null || entity.getCount() < 0) {
|
||||
return Result.ofFail(-1, "count should be at lease zero");
|
||||
}
|
||||
if (entity.getStrategy() == null) {
|
||||
return Result.ofFail(-1, "strategy can't be null");
|
||||
}
|
||||
if (entity.getStrategy() != 0 && StringUtil.isBlank(entity.getRefResource())) {
|
||||
return Result.ofFail(-1, "refResource can't be null or empty when strategy!=0");
|
||||
}
|
||||
if (entity.getControlBehavior() == null) {
|
||||
return Result.ofFail(-1, "controlBehavior can't be null");
|
||||
}
|
||||
int controlBehavior = entity.getControlBehavior();
|
||||
if (controlBehavior == 1 && entity.getWarmUpPeriodSec() == null) {
|
||||
return Result.ofFail(-1, "warmUpPeriodSec can't be null when controlBehavior==1");
|
||||
}
|
||||
if (controlBehavior == 2 && entity.getMaxQueueingTimeMs() == null) {
|
||||
return Result.ofFail(-1, "maxQueueingTimeMs can't be null when controlBehavior==2");
|
||||
}
|
||||
if (entity.isClusterMode() && entity.getClusterConfig() == null) {
|
||||
return Result.ofFail(-1, "cluster config should be valid");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@PostMapping("/rule")
|
||||
@AuthAction(value = AuthService.PrivilegeType.WRITE_RULE)
|
||||
public Result<FlowRuleEntity> apiAddFlowRule(@RequestBody FlowRuleEntity entity) {
|
||||
|
||||
Result<FlowRuleEntity> checkResult = checkEntityInternal(entity);
|
||||
if (checkResult != null) {
|
||||
return checkResult;
|
||||
}
|
||||
entity.setId(null);
|
||||
Date date = new Date();
|
||||
entity.setGmtCreate(date);
|
||||
entity.setGmtModified(date);
|
||||
entity.setLimitApp(entity.getLimitApp().trim());
|
||||
entity.setResource(entity.getResource().trim());
|
||||
try {
|
||||
entity = repository.save(entity);
|
||||
publishRules(entity.getApp());
|
||||
} catch (Throwable throwable) {
|
||||
logger.error("Failed to add flow rule", throwable);
|
||||
return Result.ofThrowable(-1, throwable);
|
||||
}
|
||||
return Result.ofSuccess(entity);
|
||||
}
|
||||
|
||||
@PutMapping("/rule/{id}")
|
||||
@AuthAction(AuthService.PrivilegeType.WRITE_RULE)
|
||||
|
||||
public Result<FlowRuleEntity> apiUpdateFlowRule(@PathVariable("id") Long id,
|
||||
@RequestBody FlowRuleEntity entity) {
|
||||
if (id == null || id <= 0) {
|
||||
return Result.ofFail(-1, "Invalid id");
|
||||
}
|
||||
FlowRuleEntity oldEntity = repository.findById(id);
|
||||
if (oldEntity == null) {
|
||||
return Result.ofFail(-1, "id " + id + " does not exist");
|
||||
}
|
||||
if (entity == null) {
|
||||
return Result.ofFail(-1, "invalid body");
|
||||
}
|
||||
|
||||
entity.setApp(oldEntity.getApp());
|
||||
entity.setIp(oldEntity.getIp());
|
||||
entity.setPort(oldEntity.getPort());
|
||||
Result<FlowRuleEntity> checkResult = checkEntityInternal(entity);
|
||||
if (checkResult != null) {
|
||||
return checkResult;
|
||||
}
|
||||
|
||||
entity.setId(id);
|
||||
Date date = new Date();
|
||||
entity.setGmtCreate(oldEntity.getGmtCreate());
|
||||
entity.setGmtModified(date);
|
||||
try {
|
||||
entity = repository.save(entity);
|
||||
if (entity == null) {
|
||||
return Result.ofFail(-1, "save entity fail");
|
||||
}
|
||||
publishRules(oldEntity.getApp());
|
||||
} catch (Throwable throwable) {
|
||||
logger.error("Failed to update flow rule", throwable);
|
||||
return Result.ofThrowable(-1, throwable);
|
||||
}
|
||||
return Result.ofSuccess(entity);
|
||||
}
|
||||
|
||||
@DeleteMapping("/rule/{id}")
|
||||
@AuthAction(PrivilegeType.DELETE_RULE)
|
||||
public Result<Long> apiDeleteRule(@PathVariable("id") Long id) {
|
||||
if (id == null || id <= 0) {
|
||||
return Result.ofFail(-1, "Invalid id");
|
||||
}
|
||||
FlowRuleEntity oldEntity = repository.findById(id);
|
||||
if (ObjectUtils.isEmpty(oldEntity)) {
|
||||
return Result.ofSuccess(null);
|
||||
}
|
||||
|
||||
try {
|
||||
repository.delete(id);
|
||||
publishRules(oldEntity.getApp());
|
||||
} catch (Exception e) {
|
||||
return Result.ofFail(-1, e.getMessage());
|
||||
}
|
||||
return Result.ofSuccess(id);
|
||||
}
|
||||
|
||||
private void publishRules(/*@NonNull*/ String app) throws Exception {
|
||||
List<FlowRuleEntity> rules = repository.findAllByApp(app);
|
||||
rulePublisher.publish(app, rules);
|
||||
//延迟加载
|
||||
delayTime();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,32 @@
|
||||
package com.alibaba.csp.sentinel.dashboard.rule.nacos;
|
||||
|
||||
/**
|
||||
* @Description: nacos配置
|
||||
* @author: zyf
|
||||
* @date: 2022/03/01$
|
||||
* @version: V1.0
|
||||
*/
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "nacos.server")
|
||||
@Data
|
||||
public class NacosConfigProperties {
|
||||
|
||||
private String ip;
|
||||
|
||||
private String namespace;
|
||||
|
||||
private String username;
|
||||
|
||||
private String password;
|
||||
|
||||
private String groupId;
|
||||
|
||||
public String getServerAddr() {
|
||||
return this.getIp();
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,162 @@
|
||||
/*
|
||||
* Copyright 1999-2018 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.csp.sentinel.dashboard.rule.nacos;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.ApiDefinitionEntity;
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.GatewayFlowRuleEntity;
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.*;
|
||||
import com.alibaba.csp.sentinel.dashboard.rule.nacos.entity.AuthorityRuleCorrectEntity;
|
||||
import com.alibaba.csp.sentinel.dashboard.rule.nacos.entity.ParamFlowRuleCorrectEntity;
|
||||
import com.alibaba.nacos.api.PropertyKeyConst;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import com.alibaba.csp.sentinel.datasource.Converter;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.nacos.api.config.ConfigFactory;
|
||||
import com.alibaba.nacos.api.config.ConfigService;
|
||||
|
||||
/**
|
||||
* sentinel配置类
|
||||
*
|
||||
* @author zyf
|
||||
* @date 2022-04-13
|
||||
*/
|
||||
@Configuration
|
||||
public class SentinelConfig {
|
||||
|
||||
@Autowired
|
||||
private NacosConfigProperties nacosConfigProperties;
|
||||
|
||||
|
||||
/**
|
||||
* 流控规则
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
public Converter<List<FlowRuleEntity>, String> flowRuleEntityEncoder() {
|
||||
return JSON::toJSONString;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Converter<String, List<FlowRuleEntity>> flowRuleEntityDecoder() {
|
||||
return s -> JSON.parseArray(s, FlowRuleEntity.class);
|
||||
}
|
||||
/**
|
||||
* 降级规则
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
public Converter<List<DegradeRuleEntity>, String> degradeRuleEntityEncoder() {
|
||||
return JSON::toJSONString;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Converter<String, List<DegradeRuleEntity>> degradeRuleEntityDecoder() {
|
||||
return s -> JSON.parseArray(s, DegradeRuleEntity.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 热点参数 规则
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
public Converter<List<ParamFlowRuleCorrectEntity>, String> paramFlowRuleEntityEncoder() {
|
||||
return JSON::toJSONString;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Converter<String, List<ParamFlowRuleCorrectEntity>> paramFlowRuleEntityDecoder() {
|
||||
return s -> JSON.parseArray(s, ParamFlowRuleCorrectEntity.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 系统规则
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
public Converter<List<SystemRuleEntity>, String> systemRuleRuleEntityEncoder() {
|
||||
return JSON::toJSONString;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Converter<String, List<SystemRuleEntity>> systemRuleRuleEntityDecoder() {
|
||||
return s -> JSON.parseArray(s, SystemRuleEntity.class);
|
||||
}
|
||||
/**
|
||||
* 授权规则
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
public Converter<List<AuthorityRuleCorrectEntity>, String> authorityRuleRuleEntityEncoder() {
|
||||
return JSON::toJSONString;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Converter<String, List<AuthorityRuleCorrectEntity>> authorityRuleRuleEntityDecoder() {
|
||||
return s -> JSON.parseArray(s, AuthorityRuleCorrectEntity.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 网关API
|
||||
*
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
@Bean
|
||||
public Converter<List<ApiDefinitionEntity>, String> apiDefinitionEntityEncoder() {
|
||||
return JSON::toJSONString;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Converter<String, List<ApiDefinitionEntity>> apiDefinitionEntityDecoder() {
|
||||
return s -> JSON.parseArray(s, ApiDefinitionEntity.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 网关flowRule
|
||||
*
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
@Bean
|
||||
public Converter<List<GatewayFlowRuleEntity>, String> gatewayFlowRuleEntityEncoder() {
|
||||
return JSON::toJSONString;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Converter<String, List<GatewayFlowRuleEntity>> gatewayFlowRuleEntityDecoder() {
|
||||
return s -> JSON.parseArray(s, GatewayFlowRuleEntity.class);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ConfigService nacosConfigService() throws Exception {
|
||||
Properties properties=new Properties();
|
||||
properties.put(PropertyKeyConst.SERVER_ADDR,nacosConfigProperties.getServerAddr());
|
||||
if(StringUtils.isNotBlank(nacosConfigProperties.getUsername())){
|
||||
properties.put(PropertyKeyConst.USERNAME,nacosConfigProperties.getUsername());
|
||||
}
|
||||
if(StringUtils.isNotBlank(nacosConfigProperties.getPassword())){
|
||||
properties.put(PropertyKeyConst.PASSWORD,nacosConfigProperties.getPassword());
|
||||
}
|
||||
return ConfigFactory.createConfigService(properties);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,50 @@
|
||||
package com.alibaba.csp.sentinel.dashboard.rule.nacos.authority;
|
||||
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.constants.SentinelConStants;
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.AuthorityRuleEntity;
|
||||
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRuleProvider;
|
||||
import com.alibaba.csp.sentinel.dashboard.rule.nacos.entity.AuthorityRuleCorrectEntity;
|
||||
import com.alibaba.csp.sentinel.datasource.Converter;
|
||||
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule;
|
||||
import com.alibaba.csp.sentinel.util.StringUtil;
|
||||
import com.alibaba.nacos.api.config.ConfigService;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 授权规则拉取(黑名单白名单)
|
||||
*
|
||||
* @author zyf
|
||||
* @date 2022-04-13
|
||||
*/
|
||||
@Component("authorityRuleNacosProvider")
|
||||
public class AuthorityRuleNacosProvider implements DynamicRuleProvider<List<AuthorityRuleEntity>> {
|
||||
@Autowired
|
||||
private ConfigService configService;
|
||||
@Autowired
|
||||
private Converter<String, List<AuthorityRuleCorrectEntity>> converter;
|
||||
|
||||
@Override
|
||||
public List<AuthorityRuleEntity> getRules(String appName) throws Exception {
|
||||
String rules = configService.getConfig(appName + SentinelConStants.AUTHORITY_DATA_ID_POSTFIX,
|
||||
SentinelConStants.GROUP_ID, 3000);
|
||||
if (StringUtil.isEmpty(rules)) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
List<AuthorityRuleCorrectEntity> entityList = converter.convert(rules);
|
||||
return entityList.stream().map(rule -> {
|
||||
AuthorityRule authorityRule = new AuthorityRule();
|
||||
BeanUtils.copyProperties(rule, authorityRule);
|
||||
AuthorityRuleEntity entity = AuthorityRuleEntity.fromAuthorityRule(rule.getApp(), rule.getIp(), rule.getPort(), authorityRule);
|
||||
entity.setId(rule.getId());
|
||||
entity.setGmtCreate(rule.getGmtCreate());
|
||||
return entity;
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,47 @@
|
||||
package com.alibaba.csp.sentinel.dashboard.rule.nacos.authority;
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.constants.SentinelConStants;
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.AuthorityRuleEntity;
|
||||
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRulePublisher;
|
||||
import com.alibaba.csp.sentinel.dashboard.rule.nacos.entity.AuthorityRuleCorrectEntity;
|
||||
import com.alibaba.csp.sentinel.datasource.Converter;
|
||||
import com.alibaba.csp.sentinel.util.AssertUtil;
|
||||
import com.alibaba.nacos.api.config.ConfigService;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 授权规则持久化(黑名单白名单)
|
||||
*
|
||||
* @author zyf
|
||||
* @date 2022-04-13
|
||||
*/
|
||||
@Component("authorityRuleNacosPublisher")
|
||||
public class AuthorityRuleNacosPublisher implements DynamicRulePublisher<List<AuthorityRuleEntity>> {
|
||||
@Autowired
|
||||
private ConfigService configService;
|
||||
@Autowired
|
||||
private Converter<List<AuthorityRuleCorrectEntity>, String> converter;
|
||||
|
||||
@Override
|
||||
public void publish(String app, List<AuthorityRuleEntity> rules) throws Exception {
|
||||
AssertUtil.notEmpty(app, "app name cannot be empty");
|
||||
if (rules == null) {
|
||||
return;
|
||||
}
|
||||
// 转换
|
||||
List<AuthorityRuleCorrectEntity> list = rules.stream().map(rule -> {
|
||||
AuthorityRuleCorrectEntity entity = new AuthorityRuleCorrectEntity();
|
||||
BeanUtils.copyProperties(rule, entity);
|
||||
return entity;
|
||||
}).collect(Collectors.toList());
|
||||
|
||||
configService.publishConfig(app + SentinelConStants.AUTHORITY_DATA_ID_POSTFIX,
|
||||
SentinelConStants.GROUP_ID, converter.convert(list));
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,39 @@
|
||||
package com.alibaba.csp.sentinel.dashboard.rule.nacos.degrade;
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.constants.SentinelConStants;
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.DegradeRuleEntity;
|
||||
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRuleProvider;
|
||||
import com.alibaba.csp.sentinel.dashboard.rule.nacos.SentinelConfig;
|
||||
import com.alibaba.csp.sentinel.datasource.Converter;
|
||||
import com.alibaba.csp.sentinel.util.StringUtil;
|
||||
import com.alibaba.nacos.api.config.ConfigService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 降级规则拉取
|
||||
*
|
||||
* @author zyf
|
||||
* @date 2022-04-13
|
||||
*/
|
||||
@Component("degradeRuleNacosProvider")
|
||||
public class DegradeRuleNacosProvider implements DynamicRuleProvider<List<DegradeRuleEntity>> {
|
||||
|
||||
@Autowired
|
||||
private ConfigService configService;
|
||||
@Autowired
|
||||
private Converter<String, List<DegradeRuleEntity>> converter;
|
||||
|
||||
@Override
|
||||
public List<DegradeRuleEntity> getRules(String appName) throws Exception {
|
||||
String rules = configService.getConfig(appName + SentinelConStants.DEGRADE_DATA_ID_POSTFIX,
|
||||
SentinelConStants.GROUP_ID, 3000);
|
||||
if (StringUtil.isEmpty(rules)) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
return converter.convert(rules);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,38 @@
|
||||
package com.alibaba.csp.sentinel.dashboard.rule.nacos.degrade;
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.constants.SentinelConStants;
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.DegradeRuleEntity;
|
||||
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRulePublisher;
|
||||
import com.alibaba.csp.sentinel.dashboard.rule.nacos.SentinelConfig;
|
||||
import com.alibaba.csp.sentinel.datasource.Converter;
|
||||
import com.alibaba.csp.sentinel.util.AssertUtil;
|
||||
import com.alibaba.nacos.api.config.ConfigService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 降级规则推送
|
||||
*
|
||||
* @author zyf
|
||||
* @date 2022-04-13
|
||||
*/
|
||||
@Component("degradeRuleNacosPublisher")
|
||||
public class DegradeRuleNacosPublisher implements DynamicRulePublisher<List<DegradeRuleEntity>> {
|
||||
|
||||
@Autowired
|
||||
private ConfigService configService;
|
||||
@Autowired
|
||||
private Converter<List<DegradeRuleEntity>, String> converter;
|
||||
|
||||
@Override
|
||||
public void publish(String app, List<DegradeRuleEntity> rules) throws Exception {
|
||||
AssertUtil.notEmpty(app, "app name cannot be empty");
|
||||
if (rules == null) {
|
||||
return;
|
||||
}
|
||||
configService.publishConfig(app + SentinelConStants.DEGRADE_DATA_ID_POSTFIX,
|
||||
SentinelConStants.GROUP_ID, converter.convert(rules));
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,110 @@
|
||||
package com.alibaba.csp.sentinel.dashboard.rule.nacos.entity;
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.RuleEntity;
|
||||
import com.alibaba.csp.sentinel.slots.block.Rule;
|
||||
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @author zyf
|
||||
* @description 重写授权规则实体类,原因同热点规则
|
||||
* @date 2022-04-13
|
||||
*/
|
||||
public class AuthorityRuleCorrectEntity implements RuleEntity {
|
||||
|
||||
private Long id;
|
||||
private String app;
|
||||
private String ip;
|
||||
private Integer port;
|
||||
private String limitApp;
|
||||
private String resource;
|
||||
private Date gmtCreate;
|
||||
private Date gmtModified;
|
||||
|
||||
private int strategy;
|
||||
|
||||
@Override
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getApp() {
|
||||
return app;
|
||||
}
|
||||
|
||||
public void setApp(String app) {
|
||||
this.app = app;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getIp() {
|
||||
return ip;
|
||||
}
|
||||
|
||||
public void setIp(String ip) {
|
||||
this.ip = ip;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public void setPort(Integer port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public String getLimitApp() {
|
||||
return limitApp;
|
||||
}
|
||||
|
||||
public void setLimitApp(String limitApp) {
|
||||
this.limitApp = limitApp;
|
||||
}
|
||||
|
||||
public String getResource() {
|
||||
return resource;
|
||||
}
|
||||
|
||||
public void setResource(String resource) {
|
||||
this.resource = resource;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Date getGmtCreate() {
|
||||
return gmtCreate;
|
||||
}
|
||||
|
||||
public void setGmtCreate(Date gmtCreate) {
|
||||
this.gmtCreate = gmtCreate;
|
||||
}
|
||||
|
||||
public Date getGmtModified() {
|
||||
return gmtModified;
|
||||
}
|
||||
|
||||
public void setGmtModified(Date gmtModified) {
|
||||
this.gmtModified = gmtModified;
|
||||
}
|
||||
|
||||
public int getStrategy() {
|
||||
return strategy;
|
||||
}
|
||||
|
||||
public void setStrategy(int strategy) {
|
||||
this.strategy = strategy;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Rule toRule(){
|
||||
AuthorityRule rule=new AuthorityRule();
|
||||
return rule;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,194 @@
|
||||
package com.alibaba.csp.sentinel.dashboard.rule.nacos.entity;
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.RuleEntity;
|
||||
import com.alibaba.csp.sentinel.slots.block.Rule;
|
||||
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowClusterConfig;
|
||||
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowItem;
|
||||
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author zyf
|
||||
* @description 重写热点规则实体类,。查看sentinel-dashboard在自定义ParamFlowRuleNacosPublisher时候 推送的数据是ParamFlowRuleEntity。 客户端接收的ParamFlowRule类
|
||||
* @date 2022-04-13
|
||||
*/
|
||||
public class ParamFlowRuleCorrectEntity implements RuleEntity {
|
||||
|
||||
private Long id;
|
||||
private String app;
|
||||
private String ip;
|
||||
private Integer port;
|
||||
private String limitApp;
|
||||
private String resource;
|
||||
private Date gmtCreate;
|
||||
|
||||
private int grade = 1;
|
||||
private Integer paramIdx;
|
||||
private double count;
|
||||
private int controlBehavior = 0;
|
||||
private int maxQueueingTimeMs = 0;
|
||||
private int burstCount = 0;
|
||||
private long durationInSec = 1L;
|
||||
private List<ParamFlowItem> paramFlowItemList = new ArrayList();
|
||||
private Map<Object, Integer> hotItems = new HashMap();
|
||||
private boolean clusterMode = false;
|
||||
private ParamFlowClusterConfig clusterConfig;
|
||||
|
||||
public int getGrade() {
|
||||
return grade;
|
||||
}
|
||||
|
||||
public void setGrade(int grade) {
|
||||
this.grade = grade;
|
||||
}
|
||||
|
||||
public Integer getParamIdx() {
|
||||
return paramIdx;
|
||||
}
|
||||
|
||||
public void setParamIdx(Integer paramIdx) {
|
||||
this.paramIdx = paramIdx;
|
||||
}
|
||||
|
||||
public double getCount() {
|
||||
return count;
|
||||
}
|
||||
|
||||
public void setCount(double count) {
|
||||
this.count = count;
|
||||
}
|
||||
|
||||
public int getControlBehavior() {
|
||||
return controlBehavior;
|
||||
}
|
||||
|
||||
public void setControlBehavior(int controlBehavior) {
|
||||
this.controlBehavior = controlBehavior;
|
||||
}
|
||||
|
||||
public int getMaxQueueingTimeMs() {
|
||||
return maxQueueingTimeMs;
|
||||
}
|
||||
|
||||
public void setMaxQueueingTimeMs(int maxQueueingTimeMs) {
|
||||
this.maxQueueingTimeMs = maxQueueingTimeMs;
|
||||
}
|
||||
|
||||
public int getBurstCount() {
|
||||
return burstCount;
|
||||
}
|
||||
|
||||
public void setBurstCount(int burstCount) {
|
||||
this.burstCount = burstCount;
|
||||
}
|
||||
|
||||
public long getDurationInSec() {
|
||||
return durationInSec;
|
||||
}
|
||||
|
||||
public void setDurationInSec(long durationInSec) {
|
||||
this.durationInSec = durationInSec;
|
||||
}
|
||||
|
||||
public List<ParamFlowItem> getParamFlowItemList() {
|
||||
return paramFlowItemList;
|
||||
}
|
||||
|
||||
public void setParamFlowItemList(List<ParamFlowItem> paramFlowItemList) {
|
||||
this.paramFlowItemList = paramFlowItemList;
|
||||
}
|
||||
|
||||
public Map<Object, Integer> getHotItems() {
|
||||
return hotItems;
|
||||
}
|
||||
|
||||
public void setHotItems(Map<Object, Integer> hotItems) {
|
||||
this.hotItems = hotItems;
|
||||
}
|
||||
|
||||
public boolean isClusterMode() {
|
||||
return clusterMode;
|
||||
}
|
||||
|
||||
public void setClusterMode(boolean clusterMode) {
|
||||
this.clusterMode = clusterMode;
|
||||
}
|
||||
|
||||
public ParamFlowClusterConfig getClusterConfig() {
|
||||
return clusterConfig;
|
||||
}
|
||||
|
||||
public void setClusterConfig(ParamFlowClusterConfig clusterConfig) {
|
||||
this.clusterConfig = clusterConfig;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Date getGmtCreate() {
|
||||
return gmtCreate;
|
||||
}
|
||||
|
||||
public void setGmtCreate(Date gmtCreate) {
|
||||
this.gmtCreate = gmtCreate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getApp() {
|
||||
return app;
|
||||
}
|
||||
|
||||
public void setApp(String app) {
|
||||
this.app = app;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getIp() {
|
||||
return ip;
|
||||
}
|
||||
|
||||
public void setIp(String ip) {
|
||||
this.ip = ip;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public void setPort(Integer port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public String getLimitApp() {
|
||||
return limitApp;
|
||||
}
|
||||
|
||||
public void setLimitApp(String limitApp) {
|
||||
this.limitApp = limitApp;
|
||||
}
|
||||
|
||||
public String getResource() {
|
||||
return resource;
|
||||
}
|
||||
|
||||
public void setResource(String resource) {
|
||||
this.resource = resource;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Rule toRule() {
|
||||
ParamFlowRule rule = new ParamFlowRule();
|
||||
return rule;
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright 1999-2018 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.csp.sentinel.dashboard.rule.nacos.flow;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.constants.SentinelConStants;
|
||||
import com.alibaba.csp.sentinel.dashboard.rule.nacos.SentinelConfig;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity;
|
||||
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRuleProvider;
|
||||
import com.alibaba.csp.sentinel.datasource.Converter;
|
||||
import com.alibaba.csp.sentinel.util.StringUtil;
|
||||
import com.alibaba.nacos.api.config.ConfigService;
|
||||
|
||||
/**
|
||||
* 流控规则拉取
|
||||
*
|
||||
* @author zyf
|
||||
* @date 2022-04-13
|
||||
*/
|
||||
@Component("flowRuleNacosProvider")
|
||||
public class FlowRuleNacosProvider implements DynamicRuleProvider<List<FlowRuleEntity>> {
|
||||
|
||||
@Autowired
|
||||
private ConfigService configService;
|
||||
@Autowired
|
||||
private Converter<String, List<FlowRuleEntity>> converter;
|
||||
|
||||
@Override
|
||||
public List<FlowRuleEntity> getRules(String appName) throws Exception {
|
||||
String rules = configService.getConfig(appName + SentinelConStants.FLOW_DATA_ID_POSTFIX,
|
||||
SentinelConStants.GROUP_ID, 3000);
|
||||
if (StringUtil.isEmpty(rules)) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
return converter.convert(rules);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright 1999-2018 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.csp.sentinel.dashboard.rule.nacos.flow;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.constants.SentinelConStants;
|
||||
import com.alibaba.csp.sentinel.dashboard.rule.nacos.SentinelConfig;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity;
|
||||
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRulePublisher;
|
||||
import com.alibaba.csp.sentinel.datasource.Converter;
|
||||
import com.alibaba.csp.sentinel.util.AssertUtil;
|
||||
import com.alibaba.nacos.api.config.ConfigService;
|
||||
|
||||
/**
|
||||
* 流控规则推送
|
||||
*
|
||||
* @author zyf
|
||||
* @date 2022-04-13
|
||||
*/
|
||||
@Component("flowRuleNacosPublisher")
|
||||
public class FlowRuleNacosPublisher implements DynamicRulePublisher<List<FlowRuleEntity>> {
|
||||
|
||||
@Autowired
|
||||
private ConfigService configService;
|
||||
@Autowired
|
||||
private Converter<List<FlowRuleEntity>, String> converter;
|
||||
|
||||
@Override
|
||||
public void publish(String app, List<FlowRuleEntity> rules) throws Exception {
|
||||
AssertUtil.notEmpty(app, "app name cannot be empty");
|
||||
if (rules == null) {
|
||||
return;
|
||||
}
|
||||
configService.publishConfig(app + SentinelConStants.FLOW_DATA_ID_POSTFIX,
|
||||
SentinelConStants.GROUP_ID, converter.convert(rules));
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,35 @@
|
||||
package com.alibaba.csp.sentinel.dashboard.rule.nacos.gateway;
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.constants.SentinelConStants;
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.ApiDefinitionEntity;
|
||||
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRuleProvider;
|
||||
import com.alibaba.csp.sentinel.datasource.Converter;
|
||||
import com.alibaba.csp.sentinel.util.StringUtil;
|
||||
import com.alibaba.nacos.api.config.ConfigService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
/**
|
||||
* 网关API规则拉取
|
||||
*
|
||||
* @author zyf
|
||||
* @date 2022-04-13
|
||||
*/
|
||||
@Component("gateWayApiNacosProvider")
|
||||
public class GateWayApiNacosProvider implements DynamicRuleProvider<List<ApiDefinitionEntity>> {
|
||||
@Autowired
|
||||
private ConfigService configService;
|
||||
@Autowired
|
||||
private Converter<String , List<ApiDefinitionEntity>> converter;
|
||||
@Override
|
||||
public List<ApiDefinitionEntity> getRules(String appName) throws Exception {
|
||||
String rules = configService.getConfig(appName+ SentinelConStants.GETEWAY_API_DATA_ID_POSTFIX
|
||||
, SentinelConStants.GROUP_ID,3000);
|
||||
if(StringUtil.isEmpty(rules)){
|
||||
return new ArrayList<>();
|
||||
}
|
||||
return converter.convert(rules);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,35 @@
|
||||
package com.alibaba.csp.sentinel.dashboard.rule.nacos.gateway;
|
||||
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.constants.SentinelConStants;
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.ApiDefinitionEntity;
|
||||
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRulePublisher;
|
||||
import com.alibaba.csp.sentinel.datasource.Converter;
|
||||
import com.alibaba.csp.sentinel.util.AssertUtil;
|
||||
import com.alibaba.nacos.api.config.ConfigService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
/**
|
||||
* 网关API规则推送
|
||||
*
|
||||
* @author zyf
|
||||
* @date 2022-04-13
|
||||
*/
|
||||
@Component("gateWayApiNacosPublisher")
|
||||
public class GateWayApiNacosPublisher implements DynamicRulePublisher<List<ApiDefinitionEntity>> {
|
||||
@Autowired
|
||||
private ConfigService configService;
|
||||
@Autowired
|
||||
private Converter<List<ApiDefinitionEntity>, String> converter;
|
||||
@Override
|
||||
public void publish(String app, List<ApiDefinitionEntity> rules) throws Exception {
|
||||
AssertUtil.notEmpty(app, "app name cannot be empty");
|
||||
if (rules == null) {
|
||||
return;
|
||||
}
|
||||
configService.publishConfig(app+ SentinelConStants.GETEWAY_API_DATA_ID_POSTFIX,
|
||||
SentinelConStants.GROUP_ID,converter.convert(rules));
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,40 @@
|
||||
package com.alibaba.csp.sentinel.dashboard.rule.nacos.gateway;
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.constants.SentinelConStants;
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.GatewayFlowRuleEntity;
|
||||
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRuleProvider;
|
||||
import com.alibaba.csp.sentinel.dashboard.rule.nacos.SentinelConfig;
|
||||
import com.alibaba.csp.sentinel.datasource.Converter;
|
||||
import com.alibaba.csp.sentinel.util.StringUtil;
|
||||
import com.alibaba.nacos.api.config.ConfigService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 网关流控规则拉取
|
||||
*
|
||||
* @author zyf
|
||||
* @date 2022-04-13
|
||||
*/
|
||||
@Component("gateWayFlowRulesNacosProvider")
|
||||
public class GateWayFlowRulesNacosProvider implements DynamicRuleProvider<List<GatewayFlowRuleEntity>> {
|
||||
|
||||
@Autowired
|
||||
private ConfigService configService;
|
||||
@Autowired
|
||||
private Converter<String, List<GatewayFlowRuleEntity>> converter;
|
||||
|
||||
@Override
|
||||
public List<GatewayFlowRuleEntity> getRules(String appName) throws Exception {
|
||||
String rules = configService.getConfig(appName + SentinelConStants.GETEWAY_FLOW_DATA_ID_POSTFIX,
|
||||
SentinelConStants.GROUP_ID, 3000);
|
||||
if (StringUtil.isEmpty(rules)) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
return converter.convert(rules);
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,41 @@
|
||||
package com.alibaba.csp.sentinel.dashboard.rule.nacos.gateway;
|
||||
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.constants.SentinelConStants;
|
||||
import com.alibaba.csp.sentinel.dashboard.rule.nacos.SentinelConfig;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.GatewayFlowRuleEntity;
|
||||
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRulePublisher;
|
||||
import com.alibaba.csp.sentinel.datasource.Converter;
|
||||
import com.alibaba.csp.sentinel.util.AssertUtil;
|
||||
import com.alibaba.nacos.api.config.ConfigService;
|
||||
|
||||
/**
|
||||
* 网关流控规则推送
|
||||
*
|
||||
* @author zyf
|
||||
* @date 2022-04-13
|
||||
*/
|
||||
@Component("gateWayFlowRulesNacosPublisher")
|
||||
public class GateWayFlowRulesNacosPublisher implements DynamicRulePublisher<List<GatewayFlowRuleEntity>> {
|
||||
|
||||
@Autowired
|
||||
private ConfigService configService;
|
||||
@Autowired
|
||||
private Converter<List<GatewayFlowRuleEntity>, String> converter;
|
||||
|
||||
|
||||
@Override
|
||||
public void publish(String app, List<GatewayFlowRuleEntity> rules) throws Exception {
|
||||
AssertUtil.notEmpty(app, "app name cannot be empty");
|
||||
if (rules == null) {
|
||||
return;
|
||||
}
|
||||
configService.publishConfig(app + SentinelConStants.GETEWAY_FLOW_DATA_ID_POSTFIX,
|
||||
SentinelConStants.GROUP_ID, converter.convert(rules));
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,52 @@
|
||||
package com.alibaba.csp.sentinel.dashboard.rule.nacos.paramflow;
|
||||
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.constants.SentinelConStants;
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.ParamFlowRuleEntity;
|
||||
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRuleProvider;
|
||||
import com.alibaba.csp.sentinel.dashboard.rule.nacos.SentinelConfig;
|
||||
import com.alibaba.csp.sentinel.dashboard.rule.nacos.entity.ParamFlowRuleCorrectEntity;
|
||||
import com.alibaba.csp.sentinel.datasource.Converter;
|
||||
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule;
|
||||
import com.alibaba.csp.sentinel.util.StringUtil;
|
||||
import com.alibaba.nacos.api.config.ConfigService;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 加载热点参数规则
|
||||
*
|
||||
* @author zyf
|
||||
* @date 2022-04-13
|
||||
*/
|
||||
@Component("paramFlowRuleNacosProvider")
|
||||
public class ParamFlowRuleNacosProvider implements DynamicRuleProvider<List<ParamFlowRuleEntity>> {
|
||||
|
||||
@Autowired
|
||||
private ConfigService configService;
|
||||
@Autowired
|
||||
private Converter<String, List<ParamFlowRuleCorrectEntity>> converter;
|
||||
|
||||
@Override
|
||||
public List<ParamFlowRuleEntity> getRules(String appName) throws Exception {
|
||||
String rules = configService.getConfig(appName + SentinelConStants.PARAM_FLOW_DATA_ID_POSTFIX,
|
||||
SentinelConStants.GROUP_ID, 3000);
|
||||
if (StringUtil.isEmpty(rules)) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
List<ParamFlowRuleCorrectEntity> entityList = converter.convert(rules);
|
||||
return entityList.stream().map(rule -> {
|
||||
ParamFlowRule paramFlowRule = new ParamFlowRule();
|
||||
BeanUtils.copyProperties(rule, paramFlowRule);
|
||||
ParamFlowRuleEntity entity = ParamFlowRuleEntity.fromParamFlowRule(rule.getApp(), rule.getIp(), rule.getPort(), paramFlowRule);
|
||||
entity.setId(rule.getId());
|
||||
entity.setGmtCreate(rule.getGmtCreate());
|
||||
return entity;
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,51 @@
|
||||
package com.alibaba.csp.sentinel.dashboard.rule.nacos.paramflow;
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.constants.SentinelConStants;
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.ParamFlowRuleEntity;
|
||||
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRulePublisher;
|
||||
import com.alibaba.csp.sentinel.dashboard.rule.nacos.SentinelConfig;
|
||||
import com.alibaba.csp.sentinel.dashboard.rule.nacos.entity.ParamFlowRuleCorrectEntity;
|
||||
import com.alibaba.csp.sentinel.datasource.Converter;
|
||||
import com.alibaba.csp.sentinel.util.AssertUtil;
|
||||
import com.alibaba.nacos.api.config.ConfigService;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 持久化热点参数规则
|
||||
*
|
||||
* @author zyf
|
||||
* @date 2022-04-13
|
||||
*/
|
||||
@Component("paramFlowRuleNacosPublisher")
|
||||
public class ParamFlowRuleNacosPublisher implements DynamicRulePublisher<List<ParamFlowRuleEntity>> {
|
||||
|
||||
@Autowired
|
||||
private ConfigService configService;
|
||||
@Autowired
|
||||
private Converter<List<ParamFlowRuleCorrectEntity>, String> converter;
|
||||
|
||||
@Override
|
||||
public void publish(String app, List<ParamFlowRuleEntity> rules) throws Exception {
|
||||
AssertUtil.notEmpty(app, "app name cannot be empty");
|
||||
if (rules == null) {
|
||||
return;
|
||||
}
|
||||
rules.forEach(e -> e.setApp(app));
|
||||
|
||||
// 转换
|
||||
List<ParamFlowRuleCorrectEntity> list = rules.stream().map(rule -> {
|
||||
ParamFlowRuleCorrectEntity entity = new ParamFlowRuleCorrectEntity();
|
||||
BeanUtils.copyProperties(rule, entity);
|
||||
return entity;
|
||||
}).collect(Collectors.toList());
|
||||
|
||||
configService.publishConfig(app + SentinelConStants.PARAM_FLOW_DATA_ID_POSTFIX,
|
||||
SentinelConStants.GROUP_ID, converter.convert(list));
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,37 @@
|
||||
package com.alibaba.csp.sentinel.dashboard.rule.nacos.system;
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.constants.SentinelConStants;
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.SystemRuleEntity;
|
||||
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRuleProvider;
|
||||
import com.alibaba.csp.sentinel.datasource.Converter;
|
||||
import com.alibaba.csp.sentinel.util.StringUtil;
|
||||
import com.alibaba.nacos.api.config.ConfigService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 加载系统规则
|
||||
*
|
||||
* @author zyf
|
||||
* @date 2022-04-13
|
||||
*/
|
||||
@Component("systemRuleNacosProvider")
|
||||
public class SystemRuleNacosProvider implements DynamicRuleProvider<List<SystemRuleEntity>> {
|
||||
@Autowired
|
||||
private ConfigService configService;
|
||||
@Autowired
|
||||
private Converter<String, List<SystemRuleEntity>> converter;
|
||||
|
||||
@Override
|
||||
public List<SystemRuleEntity> getRules(String appName) throws Exception {
|
||||
String rules = configService.getConfig(appName + SentinelConStants.SYSTEM_DATA_ID_POSTFIX,
|
||||
SentinelConStants.GROUP_ID, 3000);
|
||||
if (StringUtil.isEmpty(rules)) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
return converter.convert(rules);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,37 @@
|
||||
package com.alibaba.csp.sentinel.dashboard.rule.nacos.system;
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.constants.SentinelConStants;
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.SystemRuleEntity;
|
||||
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRulePublisher;
|
||||
import com.alibaba.csp.sentinel.datasource.Converter;
|
||||
import com.alibaba.csp.sentinel.util.AssertUtil;
|
||||
import com.alibaba.nacos.api.config.ConfigService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 持久化系统规则
|
||||
*
|
||||
* @author zyf
|
||||
* @date 2022-04-13
|
||||
*/
|
||||
@Component("systemRuleNacosPublisher")
|
||||
public class SystemRuleNacosPublisher implements DynamicRulePublisher<List<SystemRuleEntity>> {
|
||||
|
||||
@Autowired
|
||||
private ConfigService configService;
|
||||
@Autowired
|
||||
private Converter<List<SystemRuleEntity>, String> converter;
|
||||
|
||||
@Override
|
||||
public void publish(String app, List<SystemRuleEntity> rules) throws Exception {
|
||||
AssertUtil.notEmpty(app, "app name cannot be empty");
|
||||
if (rules == null) {
|
||||
return;
|
||||
}
|
||||
configService.publishConfig(app + SentinelConStants.SYSTEM_DATA_ID_POSTFIX,
|
||||
SentinelConStants.GROUP_ID, converter.convert(rules));
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,37 @@
|
||||
server:
|
||||
port: 9000
|
||||
servlet:
|
||||
session:
|
||||
cookie:
|
||||
name: sentinel_dashboard_cookie
|
||||
encoding:
|
||||
charset: UTF-8
|
||||
enabled: true
|
||||
force: true
|
||||
spring:
|
||||
mvc:
|
||||
#Spring Boot 2.6+\u540E\u6620\u5C04\u5339\u914D\u7684\u9ED8\u8BA4\u7B56\u7565\u5DF2\u4ECEAntPathMatcher\u66F4\u6539\u4E3APathPatternParser,\u9700\u8981\u624B\u52A8\u6307\u5B9A\u4E3Aant-path-matcher
|
||||
pathmatch:
|
||||
matching-strategy: ant-path-matcher
|
||||
#auth settings
|
||||
auth:
|
||||
filter:
|
||||
exclude-url-suffixes: htm,html,js,css,map,ico,ttf,woff,png
|
||||
exclude-urls: /,/auth/login,/auth/logout,/registry/machine,/version
|
||||
password: sentinel
|
||||
username: sentinel
|
||||
logging:
|
||||
level:
|
||||
org:
|
||||
springframework:
|
||||
web: INFO
|
||||
pattern:
|
||||
file: '%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n'
|
||||
file:
|
||||
name: ${user.home}/logs/csp/sentinel-dashboard.log
|
||||
nacos:
|
||||
server:
|
||||
ip: @config.server-addr@
|
||||
sentinel:
|
||||
dashboard:
|
||||
version: 1.8.2
|
||||
@ -0,0 +1,44 @@
|
||||
<?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>jeecg-cloud-test</artifactId>
|
||||
<groupId>org.jeecgframework.boot</groupId>
|
||||
<version>3.4.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<description>公共测试模块</description>
|
||||
<artifactId>jeecg-cloud-test-more</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<!-- 引入jeecg-boot-starter-cloud依赖 -->
|
||||
<dependency>
|
||||
<groupId>org.jeecgframework.boot</groupId>
|
||||
<artifactId>jeecg-boot-starter-cloud</artifactId>
|
||||
<!--system模块需要排除jeecg-system-cloud-api-->
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.jeecgframework.boot</groupId>
|
||||
<artifactId>jeecg-system-cloud-api</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<!--定时任务-->
|
||||
<dependency>
|
||||
<groupId>org.jeecgframework.boot</groupId>
|
||||
<artifactId>jeecg-boot-starter-job</artifactId>
|
||||
</dependency>
|
||||
<!--rabbitmq消息队列-->
|
||||
<dependency>
|
||||
<groupId>org.jeecgframework.boot</groupId>
|
||||
<artifactId>jeecg-boot-starter-rabbitmq</artifactId>
|
||||
</dependency>
|
||||
<!-- 分布式锁依赖 -->
|
||||
<dependency>
|
||||
<groupId>org.jeecgframework.boot</groupId>
|
||||
<artifactId>jeecg-boot-starter-lock</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@ -0,0 +1,26 @@
|
||||
package org.jeecg.modules.test.constant;
|
||||
|
||||
/**
|
||||
* 微服务单元测试常量定义
|
||||
* @author: zyf
|
||||
* @date: 2022/04/21
|
||||
*/
|
||||
public interface CloudConstant {
|
||||
|
||||
/**
|
||||
* MQ测试队列名字
|
||||
*/
|
||||
public final static String MQ_JEECG_PLACE_ORDER = "jeecg_place_order";
|
||||
|
||||
/**
|
||||
* MQ测试消息总线
|
||||
*/
|
||||
public final static String MQ_DEMO_BUS_EVENT = "demoBusEvent";
|
||||
|
||||
/**
|
||||
* 分布式锁lock key
|
||||
*/
|
||||
public final static String REDISSON_DEMO_LOCK_KEY1 = "demoLockKey1";
|
||||
public final static String REDISSON_DEMO_LOCK_KEY2 = "demoLockKey2";
|
||||
|
||||
}
|
||||
@ -0,0 +1,30 @@
|
||||
package org.jeecg.modules.test.feign.client;
|
||||
|
||||
import org.jeecg.common.api.vo.Result;
|
||||
|
||||
import org.jeecg.common.constant.ServiceNameConstants;
|
||||
import org.jeecg.config.FeignConfig;
|
||||
import org.jeecg.modules.test.constant.CloudConstant;
|
||||
import org.jeecg.modules.test.feign.factory.JeecgTestClientFactory;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
/**
|
||||
* 常规feign接口定义
|
||||
* @author: zyf
|
||||
* @date: 2022/04/21
|
||||
*/
|
||||
@FeignClient(value = ServiceNameConstants.SERVICE_DEMO, configuration = FeignConfig.class,fallbackFactory = JeecgTestClientFactory.class)
|
||||
@Component
|
||||
public interface JeecgTestClient {
|
||||
|
||||
/**
|
||||
* feign测试方法
|
||||
* @param name
|
||||
* @return
|
||||
*/
|
||||
@GetMapping(value = "/test/getMessage")
|
||||
String getMessage(@RequestParam(value = "name",required = false) String name);
|
||||
}
|
||||
@ -0,0 +1,15 @@
|
||||
//package org.jeecg.modules.test.feign.client;
|
||||
//
|
||||
//import org.jeecg.common.api.vo.Result;
|
||||
//import org.springframework.web.bind.annotation.GetMapping;
|
||||
//import org.springframework.web.bind.annotation.PostMapping;
|
||||
//import org.springframework.web.bind.annotation.RequestParam;
|
||||
//
|
||||
///**
|
||||
// * 动态feign接口定义
|
||||
// */
|
||||
//public interface JeecgTestClientDyn {
|
||||
//
|
||||
// @GetMapping(value = "/test/getMessage")
|
||||
// Result<String> getMessage(@RequestParam(value = "name",required = false) String name);
|
||||
//}
|
||||
@ -0,0 +1,78 @@
|
||||
package org.jeecg.modules.test.feign.controller;
|
||||
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.jeecg.common.api.vo.Result;
|
||||
import org.jeecg.modules.test.feign.client.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import com.alibaba.csp.sentinel.annotation.SentinelResource;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
|
||||
/**
|
||||
* 微服务单元测试
|
||||
* @author: zyf
|
||||
* @date: 2022/04/21
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/sys/test")
|
||||
@Api(tags = "【微服务】单元测试")
|
||||
public class JeecgTestFeignController {
|
||||
|
||||
@Autowired
|
||||
private JeecgTestClient jeecgTestClient;
|
||||
|
||||
/**
|
||||
* 熔断: fallbackFactory优先于 @SentinelResource
|
||||
*
|
||||
* @param name
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/getMessage")
|
||||
@ApiOperation(value = "测试feign调用demo服务1", notes = "测试feign @SentinelResource熔断写法 | 测试熔断关闭jeecg-demo服务")
|
||||
@SentinelResource(value = "test_more_getMessage", fallback = "getDefaultUser")
|
||||
public Result<String> getMessage(@RequestParam(value = "name", required = false) String name) {
|
||||
log.info("---------Feign fallbackFactory优先级高于@SentinelResource-----------------");
|
||||
String resultMsg = jeecgTestClient.getMessage(" I am jeecg-system 服务节点,呼叫 jeecg-demo!");
|
||||
return Result.OK(null, resultMsg);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试方法:关闭demo服务,访问请求 http://127.0.0.1:9999/sys/test/getMessage
|
||||
*
|
||||
* @param name
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/getMessage2")
|
||||
@ApiOperation(value = "测试feign调用demo服务2", notes = "测试feign fallbackFactory熔断写法 | 测试熔断关闭jeecg-demo服务")
|
||||
public Result<String> getMessage2(@RequestParam(value = "name", required = false) String name) {
|
||||
log.info("---------测试 Feign fallbackFactory-----------------");
|
||||
String resultMsg = jeecgTestClient.getMessage(" I am jeecg-system 服务节点,呼叫 jeecg-demo!");
|
||||
return Result.OK(null, resultMsg);
|
||||
}
|
||||
|
||||
|
||||
@GetMapping("/fallback")
|
||||
@ApiOperation(value = "测试熔断", notes = "测试熔断")
|
||||
@SentinelResource(value = "test_more_fallback", fallback = "getDefaultUser")
|
||||
public Result<Object> test(@RequestParam(value = "name", required = false) String name) {
|
||||
if (StringUtils.isEmpty(name)) {
|
||||
throw new IllegalArgumentException("name param is empty");
|
||||
}
|
||||
return Result.OK();
|
||||
}
|
||||
|
||||
/**
|
||||
* 熔断,默认回调函数
|
||||
*
|
||||
* @param name
|
||||
* @return
|
||||
*/
|
||||
public Result<Object> getDefaultUser(String name) {
|
||||
log.info("熔断,默认回调函数");
|
||||
return Result.error(null, "访问超时, 自定义 @SentinelResource Fallback");
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,23 @@
|
||||
package org.jeecg.modules.test.feign.factory;
|
||||
|
||||
|
||||
|
||||
|
||||
import org.springframework.cloud.openfeign.FallbackFactory;
|
||||
import org.jeecg.modules.test.feign.client.JeecgTestClient;
|
||||
import org.jeecg.modules.test.feign.fallback.JeecgTestFallback;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author qinfeng
|
||||
*/
|
||||
@Component
|
||||
public class JeecgTestClientFactory implements FallbackFactory<JeecgTestClient> {
|
||||
|
||||
@Override
|
||||
public JeecgTestClient create(Throwable throwable) {
|
||||
JeecgTestFallback fallback = new JeecgTestFallback();
|
||||
fallback.setCause(throwable);
|
||||
return fallback;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,25 @@
|
||||
package org.jeecg.modules.test.feign.fallback;
|
||||
|
||||
import org.jeecg.common.api.vo.Result;
|
||||
|
||||
import lombok.Setter;
|
||||
import org.jeecg.modules.test.feign.client.JeecgTestClient;
|
||||
|
||||
|
||||
/**
|
||||
* 接口fallback实现
|
||||
*
|
||||
* @author: scott
|
||||
* @date: 2022/4/11 19:41
|
||||
*/
|
||||
public class JeecgTestFallback implements JeecgTestClient {
|
||||
|
||||
@Setter
|
||||
private Throwable cause;
|
||||
|
||||
|
||||
@Override
|
||||
public String getMessage(String name) {
|
||||
return "访问超时, 自定义FallbackFactory";
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,71 @@
|
||||
package org.jeecg.modules.test.lock;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jeecg.boot.starter.lock.annotation.JLock;
|
||||
import org.jeecg.boot.starter.lock.client.RedissonLockClient;
|
||||
import org.jeecg.boot.starter.rabbitmq.client.RabbitMqClient;
|
||||
import org.jeecg.common.base.BaseMap;
|
||||
import org.jeecg.modules.test.constant.CloudConstant;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 分布式锁测试demo
|
||||
* @author: zyf
|
||||
* @date: 2022/04/21
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class DemoLockTest {
|
||||
@Autowired
|
||||
RedissonLockClient redissonLock;
|
||||
// @Autowired
|
||||
// RabbitMqClient rabbitMqClient;
|
||||
|
||||
/**
|
||||
* 测试方法:
|
||||
* @Scheduled(cron = "0/5 * * * * ?") 表示每5秒执行一次
|
||||
* @JLock(lockKey = CloudConstant.REDISSON_DEMO_LOCK_KEY1)分布式锁,10秒钟才释放
|
||||
* 结果:每10秒钟输出一次 “执行 分布式锁 业务逻辑1” 就说明锁成功了
|
||||
*
|
||||
* 测试分布式锁【注解方式】
|
||||
*/
|
||||
@Scheduled(cron = "0/5 * * * * ?")
|
||||
@JLock(lockKey = CloudConstant.REDISSON_DEMO_LOCK_KEY1)
|
||||
public void execute() throws InterruptedException {
|
||||
log.info("执行execute任务开始,休眠十秒开始,当前系统时间戳(秒):"+ System.currentTimeMillis()/1000);
|
||||
Thread.sleep(10000);
|
||||
log.info("========执行 分布式锁 业务逻辑1=============");
|
||||
// Map map = new BaseMap();
|
||||
// map.put("orderId", "BJ0001");
|
||||
// rabbitMqClient.sendMessage(CloudConstant.MQ_JEECG_PLACE_ORDER, map);
|
||||
// //延迟10秒发送
|
||||
// map.put("orderId", "NJ0002");
|
||||
// rabbitMqClient.sendMessage(CloudConstant.MQ_JEECG_PLACE_ORDER, map, 10000);
|
||||
|
||||
log.info("execute任务结束,休眠十秒完成,当前系统时间戳(秒):"+ System.currentTimeMillis()/1000);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 测试分布式锁【编码方式】
|
||||
* @Scheduled(cron = "0/5 * * * * ?")
|
||||
*/
|
||||
public void execute2() throws InterruptedException {
|
||||
int expireSeconds=6000;
|
||||
if (redissonLock.tryLock(CloudConstant.REDISSON_DEMO_LOCK_KEY2, -1, expireSeconds)) {
|
||||
log.info("执行任务execute2开始,休眠十秒");
|
||||
Thread.sleep(10000);
|
||||
log.info("=============业务逻辑2===================");
|
||||
log.info("定时execute2结束,休眠十秒");
|
||||
|
||||
redissonLock.unlock(CloudConstant.REDISSON_DEMO_LOCK_KEY2);
|
||||
} else {
|
||||
log.info("execute2获取锁失败");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,235 @@
|
||||
|
||||
package org.jeecg.modules.test.xxljob;
|
||||
|
||||
|
||||
import com.xxl.job.core.biz.model.ReturnT;
|
||||
import com.xxl.job.core.handler.IJobHandler;
|
||||
import com.xxl.job.core.handler.annotation.XxlJob;
|
||||
import com.xxl.job.core.log.XxlJobLogger;
|
||||
import com.xxl.job.core.util.ShardingUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.Arrays;
|
||||
|
||||
|
||||
/**
|
||||
* xxl-job定时任务测试
|
||||
* @author: zyf
|
||||
* @date: 2022/04/21
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
public class DemoJobHandler {
|
||||
|
||||
|
||||
/**
|
||||
* 简单任务
|
||||
*
|
||||
* @param params
|
||||
* @return
|
||||
*/
|
||||
@XxlJob(value = "demoJob")
|
||||
public ReturnT<String> demoJobHandler(String params) {
|
||||
log.info("我是 jeecg-system 服务里的定时任务 demoJob,我执行了...............................");
|
||||
return ReturnT.SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* 2、分片广播任务
|
||||
*/
|
||||
@XxlJob("shardingJobHandler")
|
||||
public ReturnT<String> shardingJobHandler(String param) throws Exception {
|
||||
|
||||
// 分片参数
|
||||
ShardingUtil.ShardingVO shardingVO = ShardingUtil.getShardingVo();
|
||||
log.info("分片参数:当前分片序号 = {}, 总分片数 = {}", shardingVO.getIndex(), shardingVO.getTotal());
|
||||
|
||||
// 业务逻辑
|
||||
for (int i = 0; i < shardingVO.getTotal(); i++) {
|
||||
if (i == shardingVO.getIndex()) {
|
||||
log.info("第 {} 片, 命中分片开始处理", i);
|
||||
} else {
|
||||
log.info("第 {} 片, 忽略", i);
|
||||
}
|
||||
}
|
||||
|
||||
return ReturnT.SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 3、命令行任务
|
||||
*
|
||||
* 输入参数:ipconfig /all
|
||||
*/
|
||||
@XxlJob("commandJobHandler")
|
||||
public ReturnT<String> commandJobHandler(String param) throws Exception {
|
||||
String command = param;
|
||||
int exitValue = -1;
|
||||
|
||||
BufferedReader bufferedReader = null;
|
||||
try {
|
||||
// command process
|
||||
Process process = Runtime.getRuntime().exec(command);
|
||||
BufferedInputStream bufferedInputStream = new BufferedInputStream(process.getInputStream());
|
||||
bufferedReader = new BufferedReader(new InputStreamReader(bufferedInputStream));
|
||||
|
||||
// command log
|
||||
String line;
|
||||
while ((line = bufferedReader.readLine()) != null) {
|
||||
log.info(line);
|
||||
}
|
||||
|
||||
// command exit
|
||||
process.waitFor();
|
||||
exitValue = process.exitValue();
|
||||
} catch (Exception e) {
|
||||
log.info(e.getMessage(),e);
|
||||
} finally {
|
||||
if (bufferedReader != null) {
|
||||
bufferedReader.close();
|
||||
}
|
||||
}
|
||||
|
||||
if (exitValue == 0) {
|
||||
return IJobHandler.SUCCESS;
|
||||
} else {
|
||||
return new ReturnT<String>(IJobHandler.FAIL.getCode(), "command exit value(" + exitValue + ") is failed");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 4、跨平台Http任务
|
||||
*
|
||||
* 输入参数:
|
||||
* url: https://www.baidu.com
|
||||
* method: get
|
||||
* data: content
|
||||
*/
|
||||
@XxlJob("httpJobHandler")
|
||||
public ReturnT<String> httpJobHandler(String param) throws Exception {
|
||||
String[] methodArray=new String[]{"GET","POST"};
|
||||
int okState=200;
|
||||
// param parse
|
||||
if (param == null || param.trim().length() == 0) {
|
||||
log.info("param[" + param + "] invalid.");
|
||||
return ReturnT.FAIL;
|
||||
}
|
||||
String[] httpParams = param.split("\n");
|
||||
String url = null;
|
||||
String method = null;
|
||||
String data = null;
|
||||
for (String httpParam : httpParams) {
|
||||
if (httpParam.startsWith("url:")) {
|
||||
url = httpParam.substring(httpParam.indexOf("url:") + 4).trim();
|
||||
}
|
||||
if (httpParam.startsWith("method:")) {
|
||||
method = httpParam.substring(httpParam.indexOf("method:") + 7).trim().toUpperCase();
|
||||
}
|
||||
if (httpParam.startsWith("data:")) {
|
||||
data = httpParam.substring(httpParam.indexOf("data:") + 5).trim();
|
||||
}
|
||||
}
|
||||
|
||||
// param valid
|
||||
if (url == null || url.trim().length() == 0) {
|
||||
log.info("url[" + url + "] invalid.");
|
||||
return ReturnT.FAIL;
|
||||
}
|
||||
if (method == null || !Arrays.asList(methodArray).contains(method)) {
|
||||
log.info("method[" + method + "] invalid.");
|
||||
return ReturnT.FAIL;
|
||||
}
|
||||
|
||||
// request
|
||||
HttpURLConnection connection = null;
|
||||
BufferedReader bufferedReader = null;
|
||||
try {
|
||||
// connection
|
||||
URL realUrl = new URL(url);
|
||||
connection = (HttpURLConnection) realUrl.openConnection();
|
||||
|
||||
// connection setting
|
||||
connection.setRequestMethod(method);
|
||||
connection.setDoOutput(true);
|
||||
connection.setDoInput(true);
|
||||
connection.setUseCaches(false);
|
||||
connection.setReadTimeout(5 * 1000);
|
||||
connection.setConnectTimeout(3 * 1000);
|
||||
connection.setRequestProperty("connection", "Keep-Alive");
|
||||
connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
|
||||
connection.setRequestProperty("Accept-Charset", "application/json;charset=UTF-8");
|
||||
|
||||
// do connection
|
||||
connection.connect();
|
||||
|
||||
// data
|
||||
if (data != null && data.trim().length() > 0) {
|
||||
DataOutputStream dataOutputStream = new DataOutputStream(connection.getOutputStream());
|
||||
dataOutputStream.write(data.getBytes("UTF-8"));
|
||||
dataOutputStream.flush();
|
||||
dataOutputStream.close();
|
||||
}
|
||||
|
||||
// valid StatusCode
|
||||
int statusCode = connection.getResponseCode();
|
||||
if (statusCode != okState) {
|
||||
throw new RuntimeException("Http Request StatusCode(" + statusCode + ") Invalid.");
|
||||
}
|
||||
|
||||
// result
|
||||
bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
|
||||
StringBuilder result = new StringBuilder();
|
||||
String line;
|
||||
while ((line = bufferedReader.readLine()) != null) {
|
||||
result.append(line);
|
||||
}
|
||||
String responseMsg = result.toString();
|
||||
|
||||
log.info(responseMsg);
|
||||
return ReturnT.SUCCESS;
|
||||
} catch (Exception e) {
|
||||
log.info(e.getMessage(),e);
|
||||
return ReturnT.FAIL;
|
||||
} finally {
|
||||
try {
|
||||
if (bufferedReader != null) {
|
||||
bufferedReader.close();
|
||||
}
|
||||
if (connection != null) {
|
||||
connection.disconnect();
|
||||
}
|
||||
} catch (Exception e2) {
|
||||
log.info(e2.getMessage(),e2);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 5、生命周期任务示例:任务初始化与销毁时,支持自定义相关逻辑;
|
||||
*/
|
||||
@XxlJob(value = "demoJobHandler2", init = "init", destroy = "destroy")
|
||||
public ReturnT<String> demoJobHandler2(String param) throws Exception {
|
||||
log.info("XXL-JOB, Hello World.");
|
||||
return ReturnT.SUCCESS;
|
||||
}
|
||||
|
||||
public void init() {
|
||||
log.info("init");
|
||||
}
|
||||
|
||||
public void destroy() {
|
||||
log.info("destory");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -0,0 +1,41 @@
|
||||
|
||||
package org.jeecg.modules.test.xxljob;
|
||||
|
||||
import com.xxl.job.core.biz.model.ReturnT;
|
||||
import com.xxl.job.core.handler.annotation.XxlJob;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* xxl-job定时任务测试
|
||||
* @author: zyf
|
||||
* @date: 2022/04/21
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
public class XxclJobTest {
|
||||
|
||||
|
||||
/**
|
||||
* 简单任务
|
||||
*
|
||||
* @param params
|
||||
* @return
|
||||
*/
|
||||
|
||||
@XxlJob(value = "xxclJobTest")
|
||||
public ReturnT<String> demoJobHandler(String params) {
|
||||
log.info("我是 jeecg-system 服务里的定时任务 xxclJobTest , 我执行了...............................");
|
||||
return ReturnT.SUCCESS;
|
||||
}
|
||||
|
||||
public void init() {
|
||||
log.info("init");
|
||||
}
|
||||
|
||||
public void destroy() {
|
||||
log.info("destory");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -0,0 +1,22 @@
|
||||
<?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>jeecg-cloud-test</artifactId>
|
||||
<groupId>org.jeecgframework.boot</groupId>
|
||||
<version>3.4.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<description>消息队列测试模块</description>
|
||||
<artifactId>jeecg-cloud-test-rabbitmq</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<!--rabbitmq消息队列-->
|
||||
<dependency>
|
||||
<groupId>org.jeecgframework.boot</groupId>
|
||||
<artifactId>jeecg-boot-starter-rabbitmq</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@ -0,0 +1,28 @@
|
||||
package org.jeecg.modules.test.rabbitmq.constant;
|
||||
|
||||
/**
|
||||
* 微服务单元测试常量定义
|
||||
* @author: zyf
|
||||
* @date: 2022/04/21
|
||||
*/
|
||||
public interface CloudConstant {
|
||||
|
||||
|
||||
/**
|
||||
* MQ测试队列名字
|
||||
*/
|
||||
public final static String MQ_JEECG_PLACE_ORDER = "jeecg_place_order";
|
||||
public final static String MQ_JEECG_PLACE_ORDER_TIME = "jeecg_place_order_time";
|
||||
|
||||
/**
|
||||
* MQ测试消息总线
|
||||
*/
|
||||
public final static String MQ_DEMO_BUS_EVENT = "demoBusEvent";
|
||||
|
||||
/**
|
||||
* 分布式锁lock key
|
||||
*/
|
||||
public final static String REDISSON_DEMO_LOCK_KEY1 = "demoLockKey1";
|
||||
public final static String REDISSON_DEMO_LOCK_KEY2 = "demoLockKey2";
|
||||
|
||||
}
|
||||
@ -0,0 +1,62 @@
|
||||
package org.jeecg.modules.test.rabbitmq.controller;
|
||||
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.jeecg.boot.starter.rabbitmq.client.RabbitMqClient;
|
||||
import org.jeecg.common.api.vo.Result;
|
||||
import org.jeecg.common.base.BaseMap;
|
||||
import org.jeecg.modules.test.rabbitmq.constant.CloudConstant;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
|
||||
|
||||
/**
|
||||
* RabbitMqClient发送消息
|
||||
* @author: zyf
|
||||
* @date: 2022/04/21
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/sys/test")
|
||||
@Api(tags = "【微服务】MQ单元测试")
|
||||
public class JeecgMqTestController {
|
||||
|
||||
@Autowired
|
||||
private RabbitMqClient rabbitMqClient;
|
||||
|
||||
|
||||
/**
|
||||
* 测试方法:快速点击发送MQ消息
|
||||
* 观察三个接受者如何分配处理消息:HelloReceiver1、HelloReceiver2、HelloReceiver3,会均衡分配
|
||||
*
|
||||
* @param req
|
||||
* @return
|
||||
*/
|
||||
@GetMapping(value = "/rabbitmq")
|
||||
@ApiOperation(value = "测试rabbitmq", notes = "测试rabbitmq")
|
||||
public Result<?> rabbitMqClientTest(HttpServletRequest req) {
|
||||
//rabbitmq消息队列测试
|
||||
BaseMap map = new BaseMap();
|
||||
map.put("orderId", RandomUtil.randomNumbers(10));
|
||||
rabbitMqClient.sendMessage(CloudConstant.MQ_JEECG_PLACE_ORDER, map);
|
||||
rabbitMqClient.sendMessage(CloudConstant.MQ_JEECG_PLACE_ORDER_TIME, map,10);
|
||||
return Result.OK("MQ发送消息成功");
|
||||
}
|
||||
|
||||
@GetMapping(value = "/rabbitmq2")
|
||||
@ApiOperation(value = "rabbitmq消息总线测试", notes = "rabbitmq消息总线测试")
|
||||
public Result<?> rabbitmq2(HttpServletRequest req) {
|
||||
|
||||
//rabbitmq消息总线测试
|
||||
BaseMap params = new BaseMap();
|
||||
params.put("orderId", "123456");
|
||||
rabbitMqClient.publishEvent(CloudConstant.MQ_DEMO_BUS_EVENT, params);
|
||||
return Result.OK("MQ发送消息成功");
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,30 @@
|
||||
package org.jeecg.modules.test.rabbitmq.event;
|
||||
|
||||
import org.jeecg.boot.starter.rabbitmq.event.EventObj;
|
||||
import org.jeecg.boot.starter.rabbitmq.event.JeecgBusEventHandler;
|
||||
import org.jeecg.common.base.BaseMap;
|
||||
import org.jeecg.modules.test.rabbitmq.constant.CloudConstant;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* 消息处理器【发布订阅】
|
||||
* @author: zyf
|
||||
* @date: 2022/04/21
|
||||
*/
|
||||
@Slf4j
|
||||
@Component(CloudConstant.MQ_DEMO_BUS_EVENT)
|
||||
public class DemoBusEvent implements JeecgBusEventHandler {
|
||||
|
||||
|
||||
@Override
|
||||
public void onMessage(EventObj obj) {
|
||||
if (ObjectUtil.isNotEmpty(obj)) {
|
||||
BaseMap baseMap = obj.getBaseMap();
|
||||
String orderId = baseMap.get("orderId");
|
||||
log.info("业务处理----订单ID:" + orderId);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,62 @@
|
||||
package org.jeecg.modules.test.rabbitmq.listener;
|
||||
|
||||
import org.jeecg.boot.starter.rabbitmq.core.BaseRabbiMqHandler;
|
||||
import org.jeecg.boot.starter.rabbitmq.listenter.MqListener;
|
||||
import org.jeecg.common.annotation.RabbitComponent;
|
||||
import org.jeecg.common.base.BaseMap;
|
||||
import org.jeecg.modules.test.rabbitmq.constant.CloudConstant;
|
||||
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
|
||||
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
||||
import org.springframework.amqp.support.AmqpHeaders;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.messaging.handler.annotation.Header;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import com.rabbitmq.client.Channel;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* 定义接收者(可以定义N个接受者,消息会均匀的发送到N个接收者中)
|
||||
*
|
||||
* RabbitMq接受者1
|
||||
* (@RabbitListener声明类上,一个类只能监听一个队列)
|
||||
* @author: zyf
|
||||
* @date: 2022/04/21
|
||||
*/
|
||||
@Slf4j
|
||||
@RabbitListener(queues = CloudConstant.MQ_JEECG_PLACE_ORDER)
|
||||
@RabbitComponent(value = "helloReceiver1")
|
||||
public class HelloReceiver1 extends BaseRabbiMqHandler<BaseMap> {
|
||||
|
||||
@Autowired
|
||||
private RestTemplate restTemplate;
|
||||
|
||||
@RabbitHandler
|
||||
public void onMessage(BaseMap baseMap, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag) {
|
||||
super.onMessage(baseMap, deliveryTag, channel, new MqListener<BaseMap>() {
|
||||
@Override
|
||||
public void handler(BaseMap map, Channel channel) {
|
||||
//业务处理
|
||||
String orderId = map.get("orderId").toString();
|
||||
log.info("【我是处理人1】 MQ Receiver1,orderId : " + orderId);
|
||||
// jeecgTestClient.getMessage("JEECG");
|
||||
try{
|
||||
// HttpHeaders requestHeaders = new HttpHeaders();
|
||||
// requestHeaders.add("X-Access-Token", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2MzExOTcyOTEsInVzZXJuYW1lIjoiYWRtaW4ifQ.N8mJvwzb4G0i3vYF9A2Bmf5cDKb1LDnOp1RwtpYEu1E");
|
||||
// requestHeaders.add("content-type", MediaType.APPLICATION_JSON_UTF8.toString());
|
||||
// MultiValueMap<String, String> requestBody = new LinkedMultiValueMap<>();
|
||||
// requestBody.add("name", "test");
|
||||
// HttpEntity< MultiValueMap<String, String> > requestEntity = new HttpEntity(requestBody, requestHeaders);
|
||||
// //post
|
||||
// ResponseEntity<String> responseEntity = restTemplate.postForEntity("http://localhost:7002/test/getMessage", requestEntity, String.class);
|
||||
// System.out.println(" responseEntity :"+responseEntity.getBody());
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,40 @@
|
||||
package org.jeecg.modules.test.rabbitmq.listener;//package org.jeecg.modules.cloud.rabbitmq;
|
||||
|
||||
import com.rabbitmq.client.Channel;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jeecg.boot.starter.rabbitmq.core.BaseRabbiMqHandler;
|
||||
import org.jeecg.boot.starter.rabbitmq.listenter.MqListener;
|
||||
import org.jeecg.common.annotation.RabbitComponent;
|
||||
import org.jeecg.common.base.BaseMap;
|
||||
import org.jeecg.modules.test.rabbitmq.constant.CloudConstant;
|
||||
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
|
||||
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
||||
import org.springframework.amqp.support.AmqpHeaders;
|
||||
import org.springframework.messaging.handler.annotation.Header;
|
||||
|
||||
/**
|
||||
* 定义接收者(可以定义N个接受者,消息会均匀的发送到N个接收者中)
|
||||
*
|
||||
* RabbitMq接受者2
|
||||
* (@RabbitListener声明类上,一个类只能监听一个队列)
|
||||
* @author: zyf
|
||||
* @date: 2022/04/21
|
||||
*/
|
||||
@Slf4j
|
||||
@RabbitListener(queues = CloudConstant.MQ_JEECG_PLACE_ORDER)
|
||||
@RabbitComponent(value = "helloReceiver2")
|
||||
public class HelloReceiver2 extends BaseRabbiMqHandler<BaseMap> {
|
||||
|
||||
@RabbitHandler
|
||||
public void onMessage(BaseMap baseMap, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag) {
|
||||
super.onMessage(baseMap, deliveryTag, channel, new MqListener<BaseMap>() {
|
||||
@Override
|
||||
public void handler(BaseMap map, Channel channel) {
|
||||
//业务处理
|
||||
String orderId = map.get("orderId").toString();
|
||||
log.info("【我是处理人2】 MQ Receiver2,orderId : " + orderId);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,38 @@
|
||||
package org.jeecg.modules.test.rabbitmq.listener;//package org.jeecg.modules.cloud.rabbitmq;
|
||||
|
||||
import com.rabbitmq.client.Channel;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jeecg.boot.starter.rabbitmq.core.BaseRabbiMqHandler;
|
||||
import org.jeecg.boot.starter.rabbitmq.listenter.MqListener;
|
||||
import org.jeecg.common.annotation.RabbitComponent;
|
||||
import org.jeecg.common.base.BaseMap;
|
||||
import org.jeecg.modules.test.rabbitmq.constant.CloudConstant;
|
||||
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
||||
import org.springframework.amqp.support.AmqpHeaders;
|
||||
import org.springframework.messaging.handler.annotation.Header;
|
||||
|
||||
/**
|
||||
* 定义接收者(可以定义N个接受者,消息会均匀的发送到N个接收者中)
|
||||
*
|
||||
* RabbitMq接受者3【我是处理人3】
|
||||
* (@RabbitListener声明类方法上,一个类可以多监听多个队列)
|
||||
* @author: zyf
|
||||
* @date: 2022/04/21
|
||||
*/
|
||||
@Slf4j
|
||||
@RabbitComponent(value = "helloReceiver3")
|
||||
public class HelloReceiver3 extends BaseRabbiMqHandler<BaseMap> {
|
||||
|
||||
@RabbitListener(queues = CloudConstant.MQ_JEECG_PLACE_ORDER)
|
||||
public void onMessage(BaseMap baseMap, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag) {
|
||||
super.onMessage(baseMap, deliveryTag, channel, new MqListener<BaseMap>() {
|
||||
@Override
|
||||
public void handler(BaseMap map, Channel channel) {
|
||||
//业务处理
|
||||
String orderId = map.get("orderId").toString();
|
||||
log.info("【我是处理人3】MQ Receiver3,orderId : " + orderId);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,39 @@
|
||||
package org.jeecg.modules.test.rabbitmq.listener;
|
||||
|
||||
import org.jeecg.boot.starter.rabbitmq.core.BaseRabbiMqHandler;
|
||||
import org.jeecg.boot.starter.rabbitmq.listenter.MqListener;
|
||||
import org.jeecg.common.annotation.RabbitComponent;
|
||||
import org.jeecg.common.base.BaseMap;
|
||||
import org.jeecg.modules.test.rabbitmq.constant.CloudConstant;
|
||||
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
|
||||
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
||||
import org.springframework.amqp.support.AmqpHeaders;
|
||||
import org.springframework.messaging.handler.annotation.Header;
|
||||
|
||||
import com.rabbitmq.client.Channel;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* 定义接收者(可以定义N个接受者,消息会均匀的发送到N个接收者中)
|
||||
* @author: zyf
|
||||
* @date: 2022/04/21
|
||||
*/
|
||||
@Slf4j
|
||||
@RabbitListener(queues = CloudConstant.MQ_JEECG_PLACE_ORDER_TIME)
|
||||
@RabbitComponent(value = "helloTimeReceiver")
|
||||
public class HelloTimeReceiver extends BaseRabbiMqHandler<BaseMap> {
|
||||
|
||||
@RabbitHandler
|
||||
public void onMessage(BaseMap baseMap, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag) {
|
||||
super.onMessage(baseMap, deliveryTag, channel, new MqListener<BaseMap>() {
|
||||
@Override
|
||||
public void handler(BaseMap map, Channel channel) {
|
||||
//业务处理
|
||||
String orderId = map.get("orderId").toString();
|
||||
log.info("Time Receiver1,orderId : " + orderId);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,79 @@
|
||||
/*
|
||||
Navicat Premium Data Transfer
|
||||
|
||||
Source Server : localhost
|
||||
Source Server Type : MariaDB
|
||||
Source Server Version : 100316
|
||||
Source Host : localhost:3300
|
||||
Source Schema : seata
|
||||
|
||||
Target Server Type : MariaDB
|
||||
Target Server Version : 100316
|
||||
File Encoding : 65001
|
||||
|
||||
Date: 05/01/2022 20:25:07
|
||||
*/
|
||||
|
||||
SET NAMES utf8mb4;
|
||||
SET FOREIGN_KEY_CHECKS = 0;
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for branch_table
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `branch_table`;
|
||||
CREATE TABLE `branch_table` (
|
||||
`branch_id` bigint(20) NOT NULL,
|
||||
`xid` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
|
||||
`transaction_id` bigint(20) NULL DEFAULT NULL,
|
||||
`resource_group_id` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
|
||||
`resource_id` varchar(256) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
|
||||
`branch_type` varchar(8) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
|
||||
`status` tinyint(4) NULL DEFAULT NULL,
|
||||
`client_id` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
|
||||
`application_data` varchar(2000) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
|
||||
`gmt_create` datetime(6) NULL DEFAULT NULL,
|
||||
`gmt_modified` datetime(6) NULL DEFAULT NULL,
|
||||
PRIMARY KEY (`branch_id`) USING BTREE,
|
||||
INDEX `idx_xid`(`xid`) USING BTREE
|
||||
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for global_table
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `global_table`;
|
||||
CREATE TABLE `global_table` (
|
||||
`xid` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
|
||||
`transaction_id` bigint(20) NULL DEFAULT NULL,
|
||||
`status` tinyint(4) NOT NULL,
|
||||
`application_id` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
|
||||
`transaction_service_group` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
|
||||
`transaction_name` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
|
||||
`timeout` int(11) NULL DEFAULT NULL,
|
||||
`begin_time` bigint(20) NULL DEFAULT NULL,
|
||||
`application_data` varchar(2000) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
|
||||
`gmt_create` datetime(0) NULL DEFAULT NULL,
|
||||
`gmt_modified` datetime(0) NULL DEFAULT NULL,
|
||||
PRIMARY KEY (`xid`) USING BTREE,
|
||||
INDEX `idx_gmt_modified_status`(`gmt_modified`, `status`) USING BTREE,
|
||||
INDEX `idx_transaction_id`(`transaction_id`) USING BTREE
|
||||
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for lock_table
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `lock_table`;
|
||||
CREATE TABLE `lock_table` (
|
||||
`row_key` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
|
||||
`xid` varchar(96) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
|
||||
`transaction_id` bigint(20) NULL DEFAULT NULL,
|
||||
`branch_id` bigint(20) NOT NULL,
|
||||
`resource_id` varchar(256) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
|
||||
`table_name` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
|
||||
`pk` varchar(36) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
|
||||
`gmt_create` datetime(0) NULL DEFAULT NULL,
|
||||
`gmt_modified` datetime(0) NULL DEFAULT NULL,
|
||||
PRIMARY KEY (`row_key`) USING BTREE,
|
||||
INDEX `idx_branch_id`(`branch_id`) USING BTREE
|
||||
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
|
||||
|
||||
SET FOREIGN_KEY_CHECKS = 1;
|
||||
@ -0,0 +1,14 @@
|
||||
<?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>jeecg-cloud-test-seata</artifactId>
|
||||
<groupId>org.jeecgframework.boot</groupId>
|
||||
<version>3.4.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<description>分布式事务测试模块</description>
|
||||
<artifactId>jeecg-cloud-test-seata-account</artifactId>
|
||||
|
||||
</project>
|
||||
@ -0,0 +1,17 @@
|
||||
package org.jeecg;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
* 分布式事务-账户服务
|
||||
* @author zyf
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class SeataAccountApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SeataAccountApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,26 @@
|
||||
package org.jeecg.modules.test.seata.account.controller;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jeecg.modules.test.seata.account.service.SeataAccountService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* @author zyf
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/test/seata/account")
|
||||
public class SeataAccountController {
|
||||
|
||||
@Autowired
|
||||
private SeataAccountService accountService;
|
||||
|
||||
@PostMapping("/reduceBalance")
|
||||
public void reduceBalance(Long userId, BigDecimal amount) {
|
||||
accountService.reduceBalance(userId, amount);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,31 @@
|
||||
package org.jeecg.modules.test.seata.account.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @Description: 账户
|
||||
* @author: zyf
|
||||
* @date: 2022/01/24
|
||||
* @version: V1.0
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@TableName("account")
|
||||
public class SeataAccount {
|
||||
@TableId(type = IdType.AUTO)
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 余额
|
||||
*/
|
||||
private BigDecimal balance;
|
||||
|
||||
private Date lastUpdateTime;
|
||||
}
|
||||
@ -0,0 +1,17 @@
|
||||
package org.jeecg.modules.test.seata.account.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.jeecg.modules.test.seata.account.entity.SeataAccount;
|
||||
|
||||
|
||||
/**
|
||||
* @Description: TODO
|
||||
* @author: zyf
|
||||
* @date: 2022/01/24
|
||||
* @version: V1.0
|
||||
*/
|
||||
@Mapper
|
||||
public interface SeataAccountMapper extends BaseMapper<SeataAccount> {
|
||||
|
||||
}
|
||||
@ -0,0 +1,18 @@
|
||||
package org.jeecg.modules.test.seata.account.service;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* @Description: 账户接口
|
||||
* @author: zyf
|
||||
* @date: 2022/01/24
|
||||
* @version: V1.0
|
||||
*/
|
||||
public interface SeataAccountService {
|
||||
/**
|
||||
* 扣减金额
|
||||
* @param userId 用户 ID
|
||||
* @param amount 扣减金额
|
||||
*/
|
||||
void reduceBalance(Long userId, BigDecimal amount);
|
||||
}
|
||||
@ -0,0 +1,54 @@
|
||||
package org.jeecg.modules.test.seata.account.service.impl;
|
||||
|
||||
|
||||
import com.baomidou.dynamic.datasource.annotation.DS;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import org.jeecg.modules.test.seata.account.entity.SeataAccount;
|
||||
import org.jeecg.modules.test.seata.account.mapper.SeataAccountMapper;
|
||||
import org.jeecg.modules.test.seata.account.service.SeataAccountService;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* @Description: TODO
|
||||
* @author: zyf
|
||||
* @date: 2022/01/24
|
||||
* @version: V1.0
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class SeataAccountServiceImpl implements SeataAccountService {
|
||||
@Resource
|
||||
private SeataAccountMapper accountMapper;
|
||||
|
||||
/**
|
||||
* 事务传播特性设置为 REQUIRES_NEW 开启新的事务
|
||||
*/
|
||||
@DS("account")
|
||||
@Override
|
||||
@Transactional(propagation = Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
|
||||
public void reduceBalance(Long userId, BigDecimal amount) {
|
||||
log.info("=============ACCOUNT START=================");
|
||||
SeataAccount account = accountMapper.selectById(userId);
|
||||
Assert.notNull(account, "用户不存在");
|
||||
BigDecimal balance = account.getBalance();
|
||||
log.info("下单用户{}余额为 {},商品总价为{}", userId, balance, amount);
|
||||
|
||||
if (balance.compareTo(amount)==-1) {
|
||||
log.warn("用户 {} 余额不足,当前余额:{}", userId, balance);
|
||||
throw new RuntimeException("余额不足");
|
||||
}
|
||||
log.info("开始扣减用户 {} 余额", userId);
|
||||
BigDecimal currentBalance = account.getBalance().subtract(amount);
|
||||
account.setBalance(currentBalance);
|
||||
accountMapper.updateById(account);
|
||||
log.info("扣减用户 {} 余额成功,扣减后用户账户余额为{}", userId, currentBalance);
|
||||
log.info("=============ACCOUNT END=================");
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,26 @@
|
||||
server:
|
||||
port: 5002
|
||||
spring:
|
||||
application:
|
||||
name: seata-account
|
||||
datasource:
|
||||
dynamic:
|
||||
seata: true # 开启对 seata的支持
|
||||
seata-mode: AT #支持XA及AT模式,默认AT
|
||||
datasource:
|
||||
# 设置 账号数据源配置
|
||||
account:
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
url: jdbc:mysql://127.0.0.1:3306/jeecg_account?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useSSL=false
|
||||
username: root
|
||||
password: root
|
||||
schema: classpath:sql/schema-account.sql
|
||||
seata:
|
||||
enable-auto-data-source-proxy: false
|
||||
service:
|
||||
grouplist:
|
||||
default: 127.0.0.1:8091
|
||||
vgroup-mapping:
|
||||
springboot-seata-group: default
|
||||
# seata 事务组编号 用于TC集群名
|
||||
tx-service-group: springboot-seata-group
|
||||
@ -0,0 +1,37 @@
|
||||
SET NAMES utf8mb4;
|
||||
SET FOREIGN_KEY_CHECKS = 0;
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for account
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `account`;
|
||||
CREATE TABLE `account` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`balance` decimal(10, 2) NULL DEFAULT NULL,
|
||||
`last_update_time` timestamp NULL DEFAULT current_timestamp() ON UPDATE CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (`id`) USING BTREE
|
||||
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
|
||||
|
||||
-- ----------------------------
|
||||
-- Records of account
|
||||
-- ----------------------------
|
||||
INSERT INTO `account` VALUES (1, 50.00, '2022-03-16 17:02:53');
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for undo_log
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `undo_log`;
|
||||
CREATE TABLE `undo_log` (
|
||||
`id` bigint(20) NOT NULL AUTO_INCREMENT,
|
||||
`branch_id` bigint(20) NOT NULL,
|
||||
`xid` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
|
||||
`context` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
|
||||
`rollback_info` longblob NOT NULL,
|
||||
`log_status` int(11) NOT NULL,
|
||||
`log_created` datetime(0) NOT NULL,
|
||||
`log_modified` datetime(0) NOT NULL,
|
||||
PRIMARY KEY (`id`) USING BTREE,
|
||||
UNIQUE INDEX `ux_undo_log`(`xid`, `branch_id`) USING BTREE
|
||||
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
|
||||
|
||||
SET FOREIGN_KEY_CHECKS = 1;
|
||||
@ -0,0 +1,14 @@
|
||||
<?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>jeecg-cloud-test-seata</artifactId>
|
||||
<groupId>org.jeecgframework.boot</groupId>
|
||||
<version>3.4.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<description>分布式事务测试模块</description>
|
||||
<artifactId>jeecg-cloud-test-seata-order</artifactId>
|
||||
|
||||
</project>
|
||||
@ -0,0 +1,18 @@
|
||||
package org.jeecg;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
|
||||
/**
|
||||
* @author zyf
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@EnableFeignClients
|
||||
public class SeataOrderApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SeataOrderApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,60 @@
|
||||
package org.jeecg.modules.test.seata.order.controller;
|
||||
|
||||
/**
|
||||
* @Description: TODO
|
||||
* @author: zyf
|
||||
* @date: 2022/01/24
|
||||
* @version: V1.0
|
||||
*/
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
|
||||
import org.jeecg.modules.test.seata.order.dto.PlaceOrderRequest;
|
||||
import org.jeecg.modules.test.seata.order.service.SeataOrderService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/test/seata/order")
|
||||
@Api(tags = "seata测试")
|
||||
public class SeataOrderController {
|
||||
|
||||
@Autowired
|
||||
private SeataOrderService orderService;
|
||||
|
||||
/**
|
||||
* 自由下单
|
||||
*/
|
||||
@PostMapping("/placeOrder")
|
||||
@ApiOperation(value = "自由下单", notes = "自由下单")
|
||||
public String placeOrder(@Validated @RequestBody PlaceOrderRequest request) {
|
||||
orderService.placeOrder(request);
|
||||
return "下单成功";
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试商品库存不足-异常回滚
|
||||
*/
|
||||
@PostMapping("/test1")
|
||||
@ApiOperation(value = "测试商品库存不足", notes = "测试商品库存不足")
|
||||
public String test1() {
|
||||
//商品单价10元,库存20个,用户余额50元,模拟一次性购买22个。 期望异常回滚
|
||||
orderService.placeOrder(new PlaceOrderRequest(1L, 1L, 22));
|
||||
return "下单成功";
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试用户账户余额不足-异常回滚
|
||||
*/
|
||||
@PostMapping("/test2")
|
||||
@ApiOperation(value = "测试用户账户余额不足", notes = "测试用户账户余额不足")
|
||||
public String test2() {
|
||||
//商品单价10元,库存20个,用户余额50元,模拟一次性购买6个。 期望异常回滚
|
||||
orderService.placeOrder(new PlaceOrderRequest(1L, 1L, 6));
|
||||
return "下单成功";
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,28 @@
|
||||
package org.jeecg.modules.test.seata.order.dto;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
/**
|
||||
* @Description: 订单请求对象
|
||||
* @author: zyf
|
||||
* @date: 2022/01/24
|
||||
* @version: V1.0
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class PlaceOrderRequest {
|
||||
|
||||
@NotNull
|
||||
private Long userId;
|
||||
|
||||
@NotNull
|
||||
private Long productId;
|
||||
|
||||
@NotNull
|
||||
private Integer count;
|
||||
}
|
||||
@ -0,0 +1,21 @@
|
||||
package org.jeecg.modules.test.seata.order.dto;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* @Description: 余额请求对象
|
||||
* @author: zyf
|
||||
* @date: 2022/01/24
|
||||
* @version: V1.0
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class ReduceBalanceRequest {
|
||||
|
||||
private Long userId;
|
||||
private Integer price;
|
||||
}
|
||||
@ -0,0 +1,21 @@
|
||||
package org.jeecg.modules.test.seata.order.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
/**
|
||||
* @Description: 库存请求对象
|
||||
* @author: zyf
|
||||
* @date: 2022/01/24
|
||||
* @version: V1.0
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class ReduceStockRequest {
|
||||
|
||||
private Long productId;
|
||||
private Integer amount;
|
||||
}
|
||||
@ -0,0 +1,46 @@
|
||||
package org.jeecg.modules.test.seata.order.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import org.jeecg.modules.test.seata.order.enums.OrderStatus;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* @Description: 订单
|
||||
* @author: zyf
|
||||
* @date: 2022/01/24
|
||||
* @version: V1.0
|
||||
*/
|
||||
@Builder
|
||||
@Data
|
||||
@TableName("p_order")
|
||||
public class SeataOrder {
|
||||
|
||||
@TableId(type = IdType.AUTO)
|
||||
private Integer id;
|
||||
|
||||
/**
|
||||
* 用户ID
|
||||
*/
|
||||
private Long userId;
|
||||
/**
|
||||
* 商品ID
|
||||
*/
|
||||
private Long productId;
|
||||
/**
|
||||
* 订单状态
|
||||
*/
|
||||
private OrderStatus status;
|
||||
/**
|
||||
* 数量
|
||||
*/
|
||||
private Integer count;
|
||||
/**
|
||||
* 总金额
|
||||
*/
|
||||
private BigDecimal totalPrice;
|
||||
}
|
||||
@ -0,0 +1,22 @@
|
||||
package org.jeecg.modules.test.seata.order.enums;
|
||||
|
||||
/**
|
||||
* @Description: 订单状态
|
||||
* @author: zyf
|
||||
* @date: 2022/01/24
|
||||
* @version: V1.0
|
||||
*/
|
||||
public enum OrderStatus {
|
||||
/**
|
||||
* INIT
|
||||
*/
|
||||
INIT,
|
||||
/**
|
||||
* SUCCESS
|
||||
*/
|
||||
SUCCESS,
|
||||
/**
|
||||
* FAIL
|
||||
*/
|
||||
FAIL
|
||||
}
|
||||
@ -0,0 +1,23 @@
|
||||
package org.jeecg.modules.test.seata.order.feign;
|
||||
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* @author zyf
|
||||
*/
|
||||
@FeignClient(value ="seata-account")
|
||||
public interface AccountClient {
|
||||
|
||||
/**
|
||||
* 扣减余额
|
||||
* @param userId
|
||||
* @param amount
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/test/seata/account/reduceBalance")
|
||||
String reduceBalance(@RequestParam("userId") Long userId, @RequestParam("amount") BigDecimal amount);
|
||||
}
|
||||
@ -0,0 +1,25 @@
|
||||
package org.jeecg.modules.test.seata.order.feign;
|
||||
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* 分布式事务产品feign客户端
|
||||
* @author: zyf
|
||||
* @date: 2022/04/21
|
||||
*/
|
||||
@FeignClient(value ="seata-product")
|
||||
public interface ProductClient {
|
||||
/**
|
||||
* 扣减库存
|
||||
*
|
||||
* @param productId
|
||||
* @param count
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/test/seata/product/reduceStock")
|
||||
BigDecimal reduceStock(@RequestParam("productId") Long productId, @RequestParam("count") Integer count);
|
||||
}
|
||||
@ -0,0 +1,17 @@
|
||||
package org.jeecg.modules.test.seata.order.mapper;
|
||||
|
||||
/**
|
||||
* @Description: TODO
|
||||
* @author: zyf
|
||||
* @date: 2022/01/24
|
||||
* @version: V1.0
|
||||
*/
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.jeecg.modules.test.seata.order.entity.SeataOrder;
|
||||
|
||||
@Mapper
|
||||
public interface SeataOrderMapper extends BaseMapper<SeataOrder> {
|
||||
|
||||
}
|
||||
@ -0,0 +1,19 @@
|
||||
package org.jeecg.modules.test.seata.order.service;
|
||||
|
||||
|
||||
import org.jeecg.modules.test.seata.order.dto.PlaceOrderRequest;
|
||||
|
||||
/**
|
||||
* @Description: 订单接口
|
||||
* @author: zyf
|
||||
* @date: 2022/01/24
|
||||
* @version: V1.0
|
||||
*/
|
||||
public interface SeataOrderService {
|
||||
/**
|
||||
* 下单
|
||||
*
|
||||
* @param placeOrderRequest 订单请求参数
|
||||
*/
|
||||
void placeOrder(PlaceOrderRequest placeOrderRequest);
|
||||
}
|
||||
@ -0,0 +1,69 @@
|
||||
package org.jeecg.modules.test.seata.order.service.impl;
|
||||
import com.baomidou.dynamic.datasource.annotation.DS;
|
||||
|
||||
import io.seata.spring.annotation.GlobalTransactional;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jeecg.modules.test.seata.order.dto.PlaceOrderRequest;
|
||||
import org.jeecg.modules.test.seata.order.entity.SeataOrder;
|
||||
import org.jeecg.modules.test.seata.order.enums.OrderStatus;
|
||||
import org.jeecg.modules.test.seata.order.feign.AccountClient;
|
||||
import org.jeecg.modules.test.seata.order.feign.ProductClient;
|
||||
import org.jeecg.modules.test.seata.order.mapper.SeataOrderMapper;
|
||||
import org.jeecg.modules.test.seata.order.service.SeataOrderService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* @Description: 订单服务类
|
||||
* @author: zyf
|
||||
* @date: 2022/01/24
|
||||
* @version: V1.0
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class SeataOrderServiceImpl implements SeataOrderService {
|
||||
|
||||
@Resource
|
||||
private SeataOrderMapper orderMapper;
|
||||
@Resource
|
||||
private AccountClient accountClient;
|
||||
@Resource
|
||||
private ProductClient productClient;
|
||||
|
||||
@DS("order")
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@GlobalTransactional
|
||||
public void placeOrder(PlaceOrderRequest request) {
|
||||
log.info("=============ORDER START=================");
|
||||
Long userId = request.getUserId();
|
||||
Long productId = request.getProductId();
|
||||
Integer count = request.getCount();
|
||||
log.info("收到下单请求,用户:{}, 商品:{},数量:{}", userId, productId, count);
|
||||
|
||||
|
||||
SeataOrder order = SeataOrder.builder()
|
||||
.userId(userId)
|
||||
.productId(productId)
|
||||
.status(OrderStatus.INIT)
|
||||
.count(count)
|
||||
.build();
|
||||
|
||||
orderMapper.insert(order);
|
||||
log.info("订单一阶段生成,等待扣库存付款中");
|
||||
// 扣减库存并计算总价
|
||||
BigDecimal amount = productClient.reduceStock(productId, count);
|
||||
// 扣减余额
|
||||
accountClient.reduceBalance(userId, amount);
|
||||
|
||||
order.setStatus(OrderStatus.SUCCESS);
|
||||
order.setTotalPrice(amount);
|
||||
orderMapper.updateById(order);
|
||||
log.info("订单已成功下单");
|
||||
log.info("=============ORDER END=================");
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,26 @@
|
||||
server:
|
||||
port: 5001
|
||||
spring:
|
||||
application:
|
||||
name: seata-order
|
||||
datasource:
|
||||
dynamic:
|
||||
seata: true # 开启对 seata的支持
|
||||
seata-mode: AT #支持XA及AT模式,默认AT
|
||||
datasource:
|
||||
# 设置 账号数据源配置
|
||||
order:
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
url: jdbc:mysql://127.0.0.1:3306/jeecg_order?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useSSL=false
|
||||
username: root
|
||||
password: root
|
||||
schema: classpath:sql/schema-order.sql
|
||||
seata:
|
||||
enable-auto-data-source-proxy: false
|
||||
service:
|
||||
grouplist:
|
||||
default: 127.0.0.1:8091
|
||||
vgroup-mapping:
|
||||
springboot-seata-group: default
|
||||
# seata 事务组编号 用于TC集群名
|
||||
tx-service-group: springboot-seata-group
|
||||
@ -0,0 +1,37 @@
|
||||
SET NAMES utf8mb4;
|
||||
SET FOREIGN_KEY_CHECKS = 0;
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for p_order
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `p_order`;
|
||||
CREATE TABLE `p_order` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`user_id` int(11) NULL DEFAULT NULL,
|
||||
`product_id` int(11) NULL DEFAULT NULL,
|
||||
`count` int(11) NULL DEFAULT NULL,
|
||||
`total_price` decimal(10, 2) NULL DEFAULT NULL,
|
||||
`status` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
|
||||
`add_time` timestamp NULL DEFAULT current_timestamp(),
|
||||
`last_update_time` timestamp NULL DEFAULT current_timestamp() ON UPDATE CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (`id`) USING BTREE
|
||||
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for undo_log
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `undo_log`;
|
||||
CREATE TABLE `undo_log` (
|
||||
`id` bigint(20) NOT NULL AUTO_INCREMENT,
|
||||
`branch_id` bigint(20) NOT NULL,
|
||||
`xid` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
|
||||
`context` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
|
||||
`rollback_info` longblob NOT NULL,
|
||||
`log_status` int(11) NOT NULL,
|
||||
`log_created` datetime(0) NOT NULL,
|
||||
`log_modified` datetime(0) NOT NULL,
|
||||
PRIMARY KEY (`id`) USING BTREE,
|
||||
UNIQUE INDEX `ux_undo_log`(`xid`, `branch_id`) USING BTREE
|
||||
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
|
||||
|
||||
SET FOREIGN_KEY_CHECKS = 1;
|
||||
@ -0,0 +1,14 @@
|
||||
<?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>jeecg-cloud-test-seata</artifactId>
|
||||
<groupId>org.jeecgframework.boot</groupId>
|
||||
<version>3.4.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<description>分布式事务测试模块</description>
|
||||
<artifactId>jeecg-cloud-test-seata-product</artifactId>
|
||||
|
||||
</project>
|
||||
@ -0,0 +1,16 @@
|
||||
package org.jeecg;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
* @author zyf
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class SeataProductApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SeataProductApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,25 @@
|
||||
package org.jeecg.modules.test.seata.product.controller;
|
||||
|
||||
import org.jeecg.modules.test.seata.product.service.SeataProductService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* @author zyf
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/test/seata/product")
|
||||
public class SeataProductController {
|
||||
|
||||
@Autowired
|
||||
private SeataProductService seataProductService;
|
||||
|
||||
@PostMapping("/reduceStock")
|
||||
public BigDecimal reduceStock(Long productId, Integer count) {
|
||||
return seataProductService.reduceStock(productId, count);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,34 @@
|
||||
package org.jeecg.modules.test.seata.product.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Date;
|
||||
/**
|
||||
* @Description: 产品
|
||||
* @author: zyf
|
||||
* @date: 2022/01/24
|
||||
* @version: V1.0
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@TableName("product")
|
||||
public class SeataProduct {
|
||||
|
||||
@TableId(type = IdType.AUTO)
|
||||
private Integer id;
|
||||
/**
|
||||
* 价格
|
||||
*/
|
||||
private BigDecimal price;
|
||||
/**
|
||||
* 库存
|
||||
*/
|
||||
private Integer stock;
|
||||
|
||||
private Date lastUpdateTime;
|
||||
}
|
||||
@ -0,0 +1,16 @@
|
||||
package org.jeecg.modules.test.seata.product.mapper;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.jeecg.modules.test.seata.product.entity.SeataProduct;
|
||||
|
||||
|
||||
/**
|
||||
* @Description: TODO
|
||||
* @author: zyf
|
||||
* @date: 2022/01/24
|
||||
* @version: V1.0
|
||||
*/
|
||||
@Mapper
|
||||
public interface SeataProductMapper extends BaseMapper<SeataProduct> {
|
||||
|
||||
}
|
||||
@ -0,0 +1,20 @@
|
||||
package org.jeecg.modules.test.seata.product.service;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* @Description: 产品接口
|
||||
* @author: zyf
|
||||
* @date: 2022/01/24
|
||||
* @version: V1.0
|
||||
*/
|
||||
public interface SeataProductService {
|
||||
/**
|
||||
* 扣减库存
|
||||
*
|
||||
* @param productId 商品 ID
|
||||
* @param count 扣减数量
|
||||
* @return 商品总价
|
||||
*/
|
||||
BigDecimal reduceStock(Long productId, Integer count);
|
||||
}
|
||||
@ -0,0 +1,59 @@
|
||||
package org.jeecg.modules.test.seata.product.service.impl;
|
||||
|
||||
import com.baomidou.dynamic.datasource.annotation.DS;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
|
||||
import org.jeecg.modules.test.seata.product.entity.SeataProduct;
|
||||
import org.jeecg.modules.test.seata.product.mapper.SeataProductMapper;
|
||||
import org.jeecg.modules.test.seata.product.service.SeataProductService;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* @Description: 产品服务类
|
||||
* @author: zyf
|
||||
* @date: 2022/01/24
|
||||
* @version: V1.0
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class SeataProductServiceImpl implements SeataProductService {
|
||||
|
||||
@Resource
|
||||
private SeataProductMapper productMapper;
|
||||
|
||||
/**
|
||||
* 事务传播特性设置为 REQUIRES_NEW 开启新的事务
|
||||
*/
|
||||
@DS("product")
|
||||
@Transactional(propagation = Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
|
||||
@Override
|
||||
public BigDecimal reduceStock(Long productId, Integer count) {
|
||||
log.info("=============PRODUCT START=================");
|
||||
// 检查库存
|
||||
SeataProduct product = productMapper.selectById(productId);
|
||||
Assert.notNull(product, "商品不存在");
|
||||
Integer stock = product.getStock();
|
||||
log.info("商品编号为 {} 的库存为{},订单商品数量为{}", productId, stock, count);
|
||||
|
||||
if (stock < count) {
|
||||
log.warn("商品编号为{} 库存不足,当前库存:{}", productId, stock);
|
||||
throw new RuntimeException("库存不足");
|
||||
}
|
||||
log.info("开始扣减商品编号为 {} 库存,单价商品价格为{}", productId, product.getPrice());
|
||||
// 扣减库存
|
||||
int currentStock = stock - count;
|
||||
product.setStock(currentStock);
|
||||
productMapper.updateById(product);
|
||||
BigDecimal totalPrice = product.getPrice().multiply(new BigDecimal(count));
|
||||
log.info("扣减商品编号为 {} 库存成功,扣减后库存为{}, {} 件商品总价为 {} ", productId, currentStock, count, totalPrice);
|
||||
log.info("=============PRODUCT END=================");
|
||||
return totalPrice;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,26 @@
|
||||
server:
|
||||
port: 5003
|
||||
spring:
|
||||
application:
|
||||
name: seata-product
|
||||
datasource:
|
||||
dynamic:
|
||||
seata: true # 开启对 seata的支持
|
||||
seata-mode: AT #支持XA及AT模式,默认AT
|
||||
datasource:
|
||||
# 设置 账号数据源配置
|
||||
product:
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
url: jdbc:mysql://127.0.0.1:3306/jeecg_product?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useSSL=false
|
||||
username: root
|
||||
password: root
|
||||
schema: classpath:sql/schema-product.sql
|
||||
seata:
|
||||
enable-auto-data-source-proxy: false
|
||||
service:
|
||||
grouplist:
|
||||
default: 127.0.0.1:8091
|
||||
vgroup-mapping:
|
||||
springboot-seata-group: default
|
||||
# seata 事务组编号 用于TC集群名
|
||||
tx-service-group: springboot-seata-group
|
||||
@ -0,0 +1,38 @@
|
||||
SET NAMES utf8mb4;
|
||||
SET FOREIGN_KEY_CHECKS = 0;
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for product
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `product`;
|
||||
CREATE TABLE `product` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`price` decimal(10, 2) NULL DEFAULT NULL,
|
||||
`stock` int(11) NULL DEFAULT NULL,
|
||||
`last_update_time` timestamp NULL DEFAULT current_timestamp() ON UPDATE CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (`id`) USING BTREE
|
||||
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
|
||||
|
||||
-- ----------------------------
|
||||
-- Records of product
|
||||
-- ----------------------------
|
||||
INSERT INTO `product` VALUES (1, 10.00, 20, '2022-01-13 09:52:50');
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for undo_log
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `undo_log`;
|
||||
CREATE TABLE `undo_log` (
|
||||
`id` bigint(20) NOT NULL AUTO_INCREMENT,
|
||||
`branch_id` bigint(20) NOT NULL,
|
||||
`xid` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
|
||||
`context` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
|
||||
`rollback_info` longblob NOT NULL,
|
||||
`log_status` int(11) NOT NULL,
|
||||
`log_created` datetime(0) NOT NULL,
|
||||
`log_modified` datetime(0) NOT NULL,
|
||||
PRIMARY KEY (`id`) USING BTREE,
|
||||
UNIQUE INDEX `ux_undo_log`(`xid`, `branch_id`) USING BTREE
|
||||
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
|
||||
|
||||
SET FOREIGN_KEY_CHECKS = 1;
|
||||
@ -0,0 +1,30 @@
|
||||
<?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>jeecg-cloud-test</artifactId>
|
||||
<groupId>org.jeecgframework.boot</groupId>
|
||||
<version>3.4.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>jeecg-cloud-test-seata</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<modules>
|
||||
<module>jeecg-cloud-test-seata-account</module>
|
||||
<module>jeecg-cloud-test-seata-product</module>
|
||||
<module>jeecg-cloud-test-seata-order</module>
|
||||
</modules>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.jeecgframework.boot</groupId>
|
||||
<artifactId>jeecg-boot-starter-cloud</artifactId>
|
||||
<version>${jeecgboot.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jeecgframework.boot</groupId>
|
||||
<artifactId>jeecg-boot-starter-seata</artifactId>
|
||||
<version>${jeecgboot.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@ -0,0 +1,47 @@
|
||||
CREATE TABLE `sys_log0` (
|
||||
`id` varchar(32) NOT NULL,
|
||||
`log_type` int(2) DEFAULT NULL COMMENT '日志类型(1登录日志,2操作日志)',
|
||||
`log_content` varchar(1000) DEFAULT NULL COMMENT '日志内容',
|
||||
`operate_type` int(2) DEFAULT NULL COMMENT '操作类型',
|
||||
`userid` varchar(32) DEFAULT NULL COMMENT '操作用户账号',
|
||||
`username` varchar(100) DEFAULT NULL COMMENT '操作用户名称',
|
||||
`ip` varchar(100) DEFAULT NULL COMMENT 'IP',
|
||||
`method` varchar(500) DEFAULT NULL COMMENT '请求java方法',
|
||||
`request_url` varchar(255) DEFAULT NULL COMMENT '请求路径',
|
||||
`request_param` longtext DEFAULT NULL COMMENT '请求参数',
|
||||
`request_type` varchar(10) DEFAULT NULL COMMENT '请求类型',
|
||||
`cost_time` bigint(20) DEFAULT NULL COMMENT '耗时',
|
||||
`create_by` varchar(32) DEFAULT NULL COMMENT '创建人',
|
||||
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
|
||||
`update_by` varchar(32) DEFAULT NULL COMMENT '更新人',
|
||||
`update_time` datetime DEFAULT NULL COMMENT '更新时间',
|
||||
PRIMARY KEY (`id`) USING BTREE,
|
||||
KEY `index_table_userid` (`userid`) USING BTREE,
|
||||
KEY `index_logt_ype` (`log_type`) USING BTREE,
|
||||
KEY `index_operate_type` (`operate_type`) USING BTREE,
|
||||
KEY `index_createtime` (`create_time`) USING BTREE
|
||||
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='系统日志表';
|
||||
|
||||
CREATE TABLE `sys_log1` (
|
||||
`id` varchar(32) NOT NULL,
|
||||
`log_type` int(2) DEFAULT NULL COMMENT '日志类型(1登录日志,2操作日志)',
|
||||
`log_content` varchar(1000) DEFAULT NULL COMMENT '日志内容',
|
||||
`operate_type` int(2) DEFAULT NULL COMMENT '操作类型',
|
||||
`userid` varchar(32) DEFAULT NULL COMMENT '操作用户账号',
|
||||
`username` varchar(100) DEFAULT NULL COMMENT '操作用户名称',
|
||||
`ip` varchar(100) DEFAULT NULL COMMENT 'IP',
|
||||
`method` varchar(500) DEFAULT NULL COMMENT '请求java方法',
|
||||
`request_url` varchar(255) DEFAULT NULL COMMENT '请求路径',
|
||||
`request_param` longtext DEFAULT NULL COMMENT '请求参数',
|
||||
`request_type` varchar(10) DEFAULT NULL COMMENT '请求类型',
|
||||
`cost_time` bigint(20) DEFAULT NULL COMMENT '耗时',
|
||||
`create_by` varchar(32) DEFAULT NULL COMMENT '创建人',
|
||||
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
|
||||
`update_by` varchar(32) DEFAULT NULL COMMENT '更新人',
|
||||
`update_time` datetime DEFAULT NULL COMMENT '更新时间',
|
||||
PRIMARY KEY (`id`) USING BTREE,
|
||||
KEY `index_table_userid` (`userid`) USING BTREE,
|
||||
KEY `index_logt_ype` (`log_type`) USING BTREE,
|
||||
KEY `index_operate_type` (`operate_type`) USING BTREE,
|
||||
KEY `index_createtime` (`create_time`) USING BTREE
|
||||
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='系统日志表';
|
||||
@ -0,0 +1,21 @@
|
||||
<?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>jeecg-cloud-test</artifactId>
|
||||
<groupId>org.jeecgframework.boot</groupId>
|
||||
<version>3.4.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>jeecg-cloud-test-shardingsphere</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.jeecgframework.boot</groupId>
|
||||
<artifactId>jeecg-boot-starter-shardingsphere</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@ -0,0 +1,86 @@
|
||||
package org.jeecg.modules.test.sharding.algorithm;
|
||||
|
||||
|
||||
import org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue;
|
||||
import org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue;
|
||||
import org.apache.shardingsphere.sharding.api.sharding.standard.StandardShardingAlgorithm;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* 用于处理使用单一键
|
||||
* 根据分片字段的值和sharding-count进行取模运算
|
||||
* SQL 语句中有>,>=, <=,<,=,IN 和 BETWEEN AND 操作符,都可以应用此分片策略。
|
||||
*
|
||||
* @author zyf
|
||||
*/
|
||||
public class StandardModTableShardAlgorithm implements StandardShardingAlgorithm<Integer> {
|
||||
private Properties props = new Properties();
|
||||
|
||||
|
||||
/**
|
||||
* 用于处理=和IN的分片
|
||||
*
|
||||
* @param collection 目标分片的集合(表名)
|
||||
* @param preciseShardingValue 逻辑表相关信息
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public String doSharding(Collection<String> collection, PreciseShardingValue<Integer> preciseShardingValue) {
|
||||
|
||||
for (String name : collection) {
|
||||
Integer value = preciseShardingValue.getValue();
|
||||
//根据值进行取模,得到一个目标值
|
||||
if (name.indexOf(value % 2+"") > -1) {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/**
|
||||
* 用于处理BETWEEN AND分片,如果不配置RangeShardingAlgorithm,SQL中的BETWEEN AND将按照全库路由处理
|
||||
*
|
||||
* @param collection
|
||||
* @param rangeShardingValue
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Collection<String> doSharding(Collection<String> collection, RangeShardingValue<Integer> rangeShardingValue) {
|
||||
|
||||
return collection;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化对象的时候调用的方法
|
||||
*/
|
||||
@Override
|
||||
public void init() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 对应分片算法(sharding-algorithms)的类型
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public String getType() {
|
||||
return "STANDARD_MOD";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Properties getProps() {
|
||||
return this.props;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取分片相关属性
|
||||
*
|
||||
* @param properties
|
||||
*/
|
||||
@Override
|
||||
public void setProps(Properties properties) {
|
||||
this.props = properties;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,89 @@
|
||||
package org.jeecg.modules.test.sharding.controller;
|
||||
|
||||
import org.jeecg.common.api.vo.Result;
|
||||
import org.jeecg.common.aspect.annotation.AutoLog;
|
||||
import org.jeecg.common.system.base.controller.JeecgController;
|
||||
import org.jeecg.modules.test.sharding.entity.ShardingSysLog;
|
||||
import org.jeecg.modules.test.sharding.service.IShardingSysLogService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* @Description: 分库分表测试
|
||||
* @author: zyf
|
||||
* @date: 2022/01/24
|
||||
* @version: V1.0
|
||||
*/
|
||||
@Slf4j
|
||||
@Api(tags = "分库分表测试")
|
||||
@RestController
|
||||
@RequestMapping("/sharding")
|
||||
public class JeecgShardingDemoController extends JeecgController<ShardingSysLog, IShardingSysLogService> {
|
||||
@Autowired
|
||||
private IShardingSysLogService shardingSysLogService;
|
||||
|
||||
/**
|
||||
* 单库分表 —— 添加
|
||||
* @return
|
||||
*/
|
||||
@PostMapping(value = "/test1")
|
||||
@ApiOperation(value = "单库分表插入", notes = "单库分表")
|
||||
public Result<?> add() {
|
||||
log.info("---------------------------------单库分表插入--------------------------------");
|
||||
int size = 10;
|
||||
for (int i = 0; i < size; i++) {
|
||||
ShardingSysLog shardingSysLog = new ShardingSysLog();
|
||||
shardingSysLog.setLogContent("jeecg");
|
||||
shardingSysLog.setLogType(i);
|
||||
shardingSysLog.setOperateType(i);
|
||||
shardingSysLogService.save(shardingSysLog);
|
||||
}
|
||||
return Result.OK("单库分表插入10条数据完成!");
|
||||
}
|
||||
|
||||
/**
|
||||
* 单库分表 —— 查询
|
||||
* @return
|
||||
*/
|
||||
@PostMapping(value = "/list1")
|
||||
@ApiOperation(value = "单库分表查询", notes = "单库分表")
|
||||
public Result<?> list() {
|
||||
return Result.OK(shardingSysLogService.list());
|
||||
}
|
||||
|
||||
/**
|
||||
* 分库分表 - 插入
|
||||
* @return
|
||||
*/
|
||||
@PostMapping(value = "/test2")
|
||||
@ApiOperation(value = "分库分表插入", notes = "分库分表")
|
||||
public Result<?> test2() {
|
||||
int start=20;
|
||||
int size=50;
|
||||
for (int i = start; i <= size; i++) {
|
||||
ShardingSysLog shardingSysLog = new ShardingSysLog();
|
||||
shardingSysLog.setLogContent("分库分表测试");
|
||||
shardingSysLog.setLogType(0);
|
||||
shardingSysLog.setOperateType(i);
|
||||
shardingSysLogService.save(shardingSysLog);
|
||||
}
|
||||
return Result.OK("分库分表插入10条数据完成!");
|
||||
}
|
||||
|
||||
/**
|
||||
* 分库分表 - 查询
|
||||
* @return
|
||||
*/
|
||||
@PostMapping(value = "/list2")
|
||||
@ApiOperation(value = "分库分表查询", notes = "分库分表")
|
||||
public Result<?> list2() {
|
||||
return Result.OK(shardingSysLogService.list());
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,109 @@
|
||||
package org.jeecg.modules.test.sharding.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.experimental.Accessors;
|
||||
import org.jeecg.common.aspect.annotation.Dict;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 系统日志表
|
||||
* @author: zyf
|
||||
* @date: 2022/04/21
|
||||
*/
|
||||
@Data
|
||||
@TableName("sys_log")
|
||||
public class ShardingSysLog implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* id
|
||||
*/
|
||||
@TableId(type = IdType.ASSIGN_ID)
|
||||
private String id;
|
||||
|
||||
/**
|
||||
* 创建人
|
||||
*/
|
||||
private String createBy;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
private Date createTime;
|
||||
|
||||
/**
|
||||
* 更新人
|
||||
*/
|
||||
private String updateBy;
|
||||
|
||||
/**
|
||||
* 更新时间
|
||||
*/
|
||||
private Date updateTime;
|
||||
|
||||
/**
|
||||
* 耗时
|
||||
*/
|
||||
private Long costTime;
|
||||
|
||||
/**
|
||||
* IP
|
||||
*/
|
||||
private String ip;
|
||||
|
||||
/**
|
||||
* 请求参数
|
||||
*/
|
||||
private String requestParam;
|
||||
|
||||
/**
|
||||
* 请求类型
|
||||
*/
|
||||
private String requestType;
|
||||
|
||||
/**
|
||||
* 请求路径
|
||||
*/
|
||||
private String requestUrl;
|
||||
/**
|
||||
* 请求方法
|
||||
*/
|
||||
private String method;
|
||||
|
||||
/**
|
||||
* 操作人用户名称
|
||||
*/
|
||||
private String username;
|
||||
/**
|
||||
* 操作人用户账户
|
||||
*/
|
||||
private String userid;
|
||||
/**
|
||||
* 操作详细日志
|
||||
*/
|
||||
private String logContent;
|
||||
|
||||
/**
|
||||
* 日志类型(1登录日志,2操作日志)
|
||||
*/
|
||||
@Dict(dicCode = "log_type")
|
||||
private Integer logType;
|
||||
|
||||
/**
|
||||
* 操作类型(1查询,2添加,3修改,4删除,5导入,6导出)
|
||||
*/
|
||||
@Dict(dicCode = "operate_type")
|
||||
private Integer operateType;
|
||||
|
||||
}
|
||||
@ -0,0 +1,15 @@
|
||||
package org.jeecg.modules.test.sharding.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.jeecg.modules.test.sharding.entity.ShardingSysLog;
|
||||
|
||||
|
||||
/**
|
||||
* @Description: 系统日志表 Mapper 接口
|
||||
* @author: zyf
|
||||
* @date: 2022/01/24
|
||||
* @version: V1.0
|
||||
*/
|
||||
public interface ShardingSysLogMapper extends BaseMapper<ShardingSysLog> {
|
||||
|
||||
}
|
||||
@ -0,0 +1,5 @@
|
||||
<?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="org.jeecg.modules.test.sharding.mapper.ShardingSysLogMapper">
|
||||
|
||||
</mapper>
|
||||
@ -0,0 +1,14 @@
|
||||
package org.jeecg.modules.test.sharding.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import org.jeecg.modules.test.sharding.entity.ShardingSysLog;
|
||||
|
||||
/**
|
||||
* @Description: 系统日志表 服务类
|
||||
* @author: zyf
|
||||
* @date: 2022/01/24
|
||||
* @version: V1.0
|
||||
*/
|
||||
public interface IShardingSysLogService extends IService<ShardingSysLog> {
|
||||
|
||||
}
|
||||
@ -0,0 +1,19 @@
|
||||
package org.jeecg.modules.test.sharding.service.impl;
|
||||
|
||||
import com.baomidou.dynamic.datasource.annotation.DS;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.jeecg.modules.test.sharding.entity.ShardingSysLog;
|
||||
import org.jeecg.modules.test.sharding.mapper.ShardingSysLogMapper;
|
||||
import org.jeecg.modules.test.sharding.service.IShardingSysLogService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* 系统日志表 服务实现类
|
||||
* @author: zyf
|
||||
* @date: 2022/04/21
|
||||
*/
|
||||
@Service
|
||||
@DS("sharding")
|
||||
public class ShardingSysLogServiceImpl extends ServiceImpl<ShardingSysLogMapper, ShardingSysLog> implements IShardingSysLogService {
|
||||
|
||||
}
|
||||
@ -0,0 +1,72 @@
|
||||
# 双库分表配置
|
||||
spring:
|
||||
shardingsphere:
|
||||
props:
|
||||
sql-show: true
|
||||
datasource:
|
||||
ds0:
|
||||
driverClassName: com.mysql.cj.jdbc.Driver
|
||||
url: jdbc:mysql://jeecg-boot-mysql:3306/jeecg-boot?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
|
||||
type: com.alibaba.druid.pool.DruidDataSource
|
||||
username: root
|
||||
password: root
|
||||
ds1:
|
||||
driverClassName: com.mysql.cj.jdbc.Driver
|
||||
url: jdbc:mysql://jeecg-boot-mysql:3306/jeecg-boot2?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
|
||||
type: com.alibaba.druid.pool.DruidDataSource
|
||||
username: root
|
||||
password: root
|
||||
names: ds0,ds1
|
||||
# 规则配置
|
||||
rules:
|
||||
replica-query:
|
||||
# 负载均衡算法
|
||||
load-balancers:
|
||||
round-robin:
|
||||
type: ROUND_ROBIN
|
||||
props:
|
||||
default: 0
|
||||
data-sources:
|
||||
prds:
|
||||
primary-data-source-name: ds0
|
||||
replica-data-source-names: ds1
|
||||
load-balancer-name: round_robin
|
||||
sharding:
|
||||
# 配置绑定表,每一行为一组,绑定表会提高查询效率
|
||||
binding-tables:
|
||||
- sys_log
|
||||
# 分布式序列算法配置
|
||||
key-generators:
|
||||
snowflake:
|
||||
type: SNOWFLAKE
|
||||
props:
|
||||
worker-id: 123
|
||||
# 分片算法配置
|
||||
sharding-algorithms:
|
||||
table-classbased:
|
||||
props:
|
||||
strategy: standard
|
||||
algorithmClassName: org.jeecg.modules.test.sharding.algorithm.StandardModTableShardAlgorithm
|
||||
type: CLASS_BASED
|
||||
# 通过operate_type取模的方式确定数据落在哪个库
|
||||
database-inline:
|
||||
type: INLINE
|
||||
props:
|
||||
algorithm-expression: ds$->{operate_type % 2}
|
||||
tables:
|
||||
# 逻辑表名称
|
||||
sys_log:
|
||||
#配置具体表的数据节点
|
||||
actual-data-nodes: ds$->{0..1}.sys_log$->{0..1}
|
||||
# 分库策略
|
||||
database-strategy:
|
||||
standard:
|
||||
sharding-column: operate_type
|
||||
sharding-algorithm-name: database-inline
|
||||
# 分表策略
|
||||
table-strategy:
|
||||
standard:
|
||||
# 分片算法名称
|
||||
sharding-algorithm-name: table-classbased
|
||||
# 分片列名称
|
||||
sharding-column: log_type
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user